summaryrefslogtreecommitdiff
path: root/utils/sb2
blob: 6bf9defafd82ab0189873dfdb7a46fd6f06ce7a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
# Copyright (C) 2006,2007 Lauri Leukkunen <lle@rahina.org>
# Licensed under GPL version 2

function version()
{
	cat $SBOX_DIR/share/scratchbox2/version
	exit 0
}

function usage()
{
	cat <<EOF
sb2 - crosscompiling environment
Usage:
    sb2 [OPTION]... [COMMAND] [PARAMETERS]

If no COMMAND is given, a bash shell in scratchbox2 environment is started.

Options:
    -v           display version
    -L level     enable logging (levels=one of error,warning,notice,info,debug,noise,noise2)
    -d           debug mode: log all redirections (logging level=debug)
    -h           print this help
    -t TARGET    target to use, use sb2-config -d TARGET to set a default
    -e           emulation mode
    -m MODE      use mapping mode MODE
    -M file      read mapping rules from "file"
    -s DIRECTORY load mapping scripts from alternative location
    -Q BUGLIST   emulate bugs of the old scratchbox 1 (BUGLIST consists of
                 letters: 'x' enables exec permission checking bug emulation)
    -p           Preserve session directories (including the directory
                 which is used for /tmp by most mapping modes)

Examples:
    sb2 ./configure
    sb2 make
    sb2 -e make install
    sb2 -m emulate make install
EOF
	exit 2
}

function exit_error()
{
		echo "$@"
		exit 1
}

function sanity_check()
{
	if [ `id -u` = 0 ] ; then
		exit_error "Do not use sbox2 as root!"
	fi
	# check that most important host and target files exist
}

# Collect & write all mapping- and policy rules to $SBOX_SESSION_DIR/rules.lua
#
# Parameters:
#  - list of rule files (if specified by the -M option)
#  - uses rules from $SBOX_LUA_SCRIPTS/pathmaps/$SBOX_MAPMODE if -M was not used
function write_rules_to_session_dir()
{
	input_files="$@"

	cat >$SBOX_SESSION_DIR/rules.lua <<END
-- Rules for session $SBOX_SESSION_DIR
-- Automatically generated file, do not edit.
--
END

	if [ -z "$input_files" ]
	then
		if [ -z "$SBOX_LUA_SCRIPTS" ]
		then
			SBOX_LUA_SCRIPTS="$SBOX_DIR/share/scratchbox2/lua_scripts"
		fi
		input_files=`ls $SBOX_LUA_SCRIPTS/pathmaps/$SBOX_MAPMODE/*.lua`
	fi
	
	for f in $input_files
	do
		if [ -r $f ]
		then
			echo "-- [ $f ]" >>$SBOX_SESSION_DIR/rules.lua
			cat $f >>$SBOX_SESSION_DIR/rules.lua
		else
			echo "-- [ Failed to read $f ]" >>$SBOX_SESSION_DIR/rules.lua
		fi
	done
}

function sboxify_environment()
{
	if [ -r ~/.scratchbox2/config ]; then
		. ~/.scratchbox2/config
	fi

	if [ -z "$SBOX_TARGET" ]; then
		SBOX_TARGET=$DEFAULT_TARGET
	fi

	if [ -z "$SBOX_TARGET" ]; then
		echo "No target specified and none set as default, aborting."
		exit 2
	fi

	if [ ! -e ~/.scratchbox2/$SBOX_TARGET/sb2.config ]; then
		echo "Invalid target specified, aborting."
		exit 2
	fi
	export SBOX_TARGET

	LD_LIBRARY_PATH=$SBOX_DIR/lib/libsb2:$SBOX_DIR/lib64/libsb2:$SBOX_DIR/lib32/libsb2:/emul/lib64/libsb2:/emul/lib32/libsb2
	SBOX_LIBSB2="libsb2.so.1"
	
	. ~/.scratchbox2/$SBOX_TARGET/sb2.config

	# SBOX_MAPMODE has been set, read mode-specific settings
	if [ -f $SBOX_DIR/share/scratchbox2/modeconf/sb2rc.$SBOX_MAPMODE ]
	then
		. $SBOX_DIR/share/scratchbox2/modeconf/sb2rc.$SBOX_MAPMODE starting
	fi

	if [ "$SBOX_CONFIG_VERSION" != "7" ]; then
		echo "Please run sb2-init for your target:"
		echo "name: $SBOX_TARGET"
		echo "dir:  $SBOX_TARGET_ROOT"
		echo "gcc:  $SBOX_CROSS_GCC_DIR/${SBOX_CROSS_GCC_SUBST_PREFIX}gcc"
		echo "to update its sb2.config to work with current version of sb2"
		echo "for example: "
		echo sb2-init $SBOX_TARGET $SBOX_CROSS_GCC_DIR/${SBOX_CROSS_GCC_SUBST_PREFIX}gcc""
		exit 1
	fi

	if [ -n "$SBOX_DIR" ]; then
		if [ -z "$SBOX_LUA_SCRIPTS" ]; then
			SBOX_LUA_SCRIPTS="$SBOX_DIR/share/scratchbox2/lua_scripts"
		fi
	else
		exit_error "Incorrect target config in ~/.scratchbox2/$SBOX_TARGET/sb2.config"
	fi

	write_rules_to_session_dir $SBOX_RULEFILES

	export LD_LIBRARY_PATH SBOX_LIBSB2 SBOX_TARGET_COMPILER
	export SBOX_TARGET_TOOLCHAIN_DIR=$(dirname "$SBOX_CROSS_GCC_DIR")

	if [ -z "$SBOX_MODE_PATH" ]
	then
		export PATH=$HOME/.scratchbox2/$SBOX_TARGET/bin:$SBOX_DIR/share/scratchbox2/scripts:$SBOX_DIR/bin:$PATH:/sbin:/usr/sbin:$SBOX_TARGET_ROOT/bin:$SBOX_TARGET_ROOT/usr/bin:$SBOX_TARGET_ROOT/usr/local/bin
	else
		export PATH=$SBOX_MODE_PATH
	fi

	# LD_PRELOAD will be set to SBOX_LIBSB2 by sb2-monitor.

	export PS1="[SB2 $SBOX_MAPMODE $SBOX_TARGET] \u@\h \W \$ "
}


function initialize_sb_logging()
{
	cmd_param=$1
	args_param=$2
	if [ "$SBOX_MAPPING_LOGLEVEL" != "" ]; then
		tstamp=`SBOX_DISABLE_MAPPING=1 /bin/date +%Y%m%d-%H%M`
		if [ ! -d $HOME/sb2_logs ]
		then
			SBOX_DISABLE_MAPPING=1 mkdir $HOME/sb2_logs 
			echo "Created directory $HOME/sb2_logs for log files"
		fi
		MAPPING_LOGFILE=$HOME/sb2_logs/$cmd_param.$tstamp.$$.log
		export SBOX_MAPPING_LOGFILE=$MAPPING_LOGFILE

		if [ "$SBOX_MAPPING_DEBUG" == "1" ]; then
			# log command:
			echo "##Log from $cmd_param $args_param" >$MAPPING_LOGFILE

			# log initial environment if logging is enabled
			env | sed -e 's/^/#/' >>$MAPPING_LOGFILE
			echo "#SBOX_TARGET_ROOT=$SBOX_TARGET_ROOT" >>$MAPPING_LOGFILE
			echo "#SBOX_TOOLS_ROOT=$SBOX_TOOLS_ROOT" >>$MAPPING_LOGFILE
			echo "#SBOX_MAPMODE=$SBOX_MAPMODE" >>$MAPPING_LOGFILE

			echo "Running scratchbox with these settings:"
			echo "SBOX_LIBSB2 = $SBOX_LIBSB2"
			echo "SBOX_LUA_SCRIPTS = $SBOX_LUA_SCRIPTS"
			echo "SBOX_TOOLS_ROOT = $SBOX_TOOLS_ROOT"
			echo "SBOX_REDIR_LD_SO = $SBOX_REDIR_LD_SO"
			echo "SBOX_REDIR_LD_LIBRARY_PATH = $SBOX_REDIR_LD_LIBRARY_PATH"
			echo "SBOX_TARGET_ROOT = $SBOX_TARGET_ROOT"
			echo "SBOX_MAPPING_LOGFILE = $SBOX_MAPPING_LOGFILE"
			echo "SBOX_MAPPING_LOGLEVEL = $SBOX_MAPPING_LOGLEVEL"
			echo
		fi
	fi
}

#
# Write variables to $SBOX_SESSION_DIR/sb2-session.conf
# NOTE: this file will be read by lua interpreter and the shell,
# so only simple string assignments are allowed! 
# (even comments are different in Lua and shell scripts..)
function create_common_sb2_conf_file_for_session()
{
	sbox_user_home_dir=$HOME
	if [ -z "$HOME" ]
	then
		sbox_user_home_dir="/home/unknown_user"
	fi

	cat <<END >>$SBOX_SESSION_DIR/sb2-session.conf
comment_1=" Common configuration file for Lua and Shell scripts."
comment_2=" Automatically generated file, do not edit."

sbox_target="$SBOX_TARGET"

sbox_dir="$SBOX_DIR"
sbox_workdir="$SBOX_WORKDIR"
sbox_user_home_dir="$sbox_user_home_dir"
sbox_target_toolchain_dir="$SBOX_TARGET_TOOLCHAIN_DIR"

sbox_mapmode="$SBOX_MAPMODE"
sbox_target_root="$SBOX_TARGET_ROOT"
sbox_tools_root="$SBOX_TOOLS_ROOT"

sbox_cputransparency_method="$SBOX_CPUTRANSPARENCY_METHOD"
END
}

function write_ld_library_path_replacement_to_exec_config()
{
	rootdir=$1
	varname=$2

	# Build replacement for LD_LIBRARY_PATH:

	# First, make sure that libsb2 is searched
	liblocations="$SBOX_DIR/lib/libsb2 /usr/lib/libsb2"

	# Include the directories listed in ld.so.conf
	if [ -f $rootdir/etc/ld.so.conf ]
	then
		lloc2=`cat $rootdir/etc/ld.so.conf \
			$rootdir/etc/ld.so.conf.d/* |
			egrep '^/'`
		liblocations="$liblocations $lloc2"
	fi

	# Next, force libfakeroot to be searced:
	liblocations="$liblocations /usr/lib/libfakeroot \
		/usr/lib64/libfakeroot /usr/lib32/libfakeroot"

	# Find all directories that are used in the ld.so cache file:
	if [ -x $rootdir/sbin/ldconfig ]
	then
		# print contents of the cache, take destination
		# names (the part after "=>"), drop file names (leave
		# directory names, and remove duplicates:
		dirs_in_cache=`$rootdir/sbin/ldconfig -p \
			-C $rootdir/etc/ld.so.cache | 
			fgrep '=>' | 
			sed -e 's/^.*=> //' -e 's:/[^/]*$::' | 
			sort | uniq`
		liblocations="$liblocations $dirs_in_cache"
	fi

	# Last, the default locations:
	# (these may be already included by the previous step)
	liblocations="$liblocations /lib /usr/lib"

	# Create the variable.
	echo "$varname =" >>$SBOX_SESSION_DIR/exec_config.lua
	colon=""
	for l in $liblocations
	do
		if [ -d $rootdir$l ]
		then
			echo " \"$colon$rootdir$l\" .." >>$SBOX_SESSION_DIR/exec_config.lua
			colon=":"
		fi
	done
	# Finalize the library search path.
	echo " \"\"" >>$SBOX_SESSION_DIR/exec_config.lua
}

function write_libsb2_and_ld_so_state_to_exec_config()
{
	rootdir=$1
	sb2_installed_varname=$2
	sbox_dir_varname=$3
	ld_so_path_varname=$4

	# Check if ld.so can be used from $rootdir by 
	# checking that libsb2 has been installed to tools

	if [ -d $rootdir/$SBOX_DIR/lib/libsb2 ]
	then
		sbox_dir_2=$rootdir/$SBOX_DIR
	elif [ -d $rootdir/usr/lib/libsb2 ]
	then
		sbox_dir_2=$rootdir/usr
	else
		sbox_dir_2=""
	fi

	if [ -f $sbox_dir_2/lib/libsb2/libsb2.so.1 ]
	then
		echo "$sb2_installed_varname = true" >>$SBOX_SESSION_DIR/exec_config.lua
	else
		echo "$sb2_installed_varname = false" >>$SBOX_SESSION_DIR/exec_config.lua
	fi

	echo "$sbox_dir_varname = \"$sbox_dir_2\"" >>$SBOX_SESSION_DIR/exec_config.lua

	# check the dynamic linker
	if [ -f $rootdir/lib/ld-linux.so.2 ]
	then
		echo "$ld_so_path_varname = \"$rootdir/lib/ld-linux.so.2\"" >>$SBOX_SESSION_DIR/exec_config.lua
	else
		echo "$ld_so_path_varname = nil" >>$SBOX_SESSION_DIR/exec_config.lua
	fi
}

# Write configuration file $SBOX_SESSION_DIR/exec_config.lua
function create_exec_config_file()
{
	cat >$SBOX_SESSION_DIR/exec_config.lua <<END
-- exec settings. Automatically generated file, do not edit.
END

	# 1. Exec settings for tools
	if [ "$SBOX_TOOLS_ROOT" != "" ]; then
		write_ld_library_path_replacement_to_exec_config \
			$SBOX_TOOLS_ROOT conf_tools_ld_so_library_path 

		write_libsb2_and_ld_so_state_to_exec_config \
			$SBOX_TOOLS_ROOT \
			conf_tools_sb2_installed conf_tools_sbox_dir \
			conf_tools_ld_so
	else
		# SBOX_TOOLS_ROOT was empty, tools will be used from
		# host environment.
		cat <<END >>$SBOX_SESSION_DIR/exec_config.lua
conf_tools_ld_so_library_path = nil
conf_tools_sb2_installed = false
conf_tools_sbox_dir = ""
conf_tools_ld_so = nil
END
	fi

	# 2. Exec settings for rootstrap
	if [ "$SBOX_CPUTRANSPARENCY_METHOD" == "" ]; then
		# CPU transparency method has not been set:
		# host CPU == target CPU

		write_ld_library_path_replacement_to_exec_config \
			$SBOX_TARGET_ROOT conf_target_ld_so_library_path 

		write_libsb2_and_ld_so_state_to_exec_config \
			$SBOX_TARGET_ROOT \
			conf_target_sb2_installed conf_target_sbox_dir \
			conf_target_ld_so
	else
		# SBOX_TOOLS_ROOT was empty, tools will be used from
		# host environment.
		cat <<END >>$SBOX_SESSION_DIR/exec_config.lua
conf_target_ld_so_library_path = nil
conf_target_sb2_installed = false
conf_target_sbox_dir = ""
conf_target_ld_so = nil
END
	fi
}

my_path=$_
if [ $(basename $my_path) != $(basename $0) ]; then
	my_path=$0
	if [ $(basename $my_path) = $my_path ]; then
		my_path=$(which $my_path)
	fi
fi

export SBOX_DIR=$(readlink -f $(dirname $(readlink -f $my_path))/..)
export SBOX_WORKDIR=$(readlink -f $PWD)

while getopts vdht:em:s:L:Q:pM: foo
do
	case $foo in
	(v) version ;;
	(d) export SBOX_MAPPING_DEBUG=1
	    export SBOX_MAPPING_LOGLEVEL=debug ;;
	(L) export SBOX_MAPPING_DEBUG=1
	    export SBOX_MAPPING_LOGLEVEL=$OPTARG ;;
	(Q) export SBOX_EMULATE_SB1_BUGS=$OPTARG ;;
	(h) usage ;;
	(t) export SBOX_TARGET=$OPTARG ;;
	(e) export SBOX_MAPMODE=emulate ;;
	(m) export SBOX_MAPMODE=$OPTARG ;;
	(M) SBOX_RULEFILES="$SBOX_RULEFILES $OPTARG" ;;
	(s) SBOX_LUA_SCRIPTS=$OPTARG;;
	(p) SBOX_SESSION_NO_CLEANUP="y";;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

if [ "$SBOX_MAPPING_DEBUG" == "1" ]; then
	# check that loglevel is valid
	case $SBOX_MAPPING_LOGLEVEL in
	(error|warning|notice|info|debug|noise|noise2)	;; # OK
	(*) usage ;;
	esac
else
	# default logging level
	export SBOX_MAPPING_LOGLEVEL=warning
fi

# read commands to execute from stdin - not yet implemented
if [ "$1" = "-" ] ; then
	STDIN=true
fi

# Create session directories
date_and_time_now=`date +%Y%m%d-%H%M%S`
SBOX_SESSION_DIR=`mktemp -d /tmp/sb2-$USER-$date_and_time_now.XXXXXX`

# SBOX_SESSION_DIR needs to be passed in environment variable, always.
export SBOX_SESSION_DIR

mkdir -p $SBOX_SESSION_DIR
mkdir -p $SBOX_SESSION_DIR/tmp
if [ -n "$SBOX_SESSION_NO_CLEANUP" ]
then
	touch $SBOX_SESSION_DIR/.preserve-session
fi

sboxify_environment

ln -s $SBOX_LUA_SCRIPTS $SBOX_SESSION_DIR/lua_scripts

create_exec_config_file

create_common_sb2_conf_file_for_session

# set the initial binary name for the mapping engine
export __SB2_BINARYNAME="bash"

if [ -n "$SBOX_TOOLS_ROOT" ]; then
	SHELL=$SBOX_TOOLS_ROOT/bin/bash
else
	SHELL=/bin/bash
fi

# ------------ Temporary cleanup:
# Unset variables which used to be passed in environment,
# but have been moved to sb2-session.conf.
# Once all variables have been moved, "export" statements can be removed
# from sb2-init...
unset SBOX_MAPMODE
unset SBOX_TARGET_ROOT
unset SBOX_TOOLS_ROOT
# ------------

if [ $# -gt 0 -o "$STDIN" = true ] ; then
	binary="$1"
	shift 1
	args="$@"
	initialize_sb_logging $(echo $binary | sed 's/\//_/g') "$args"
	exec sb2-monitor -x $SBOX_DIR/share/scratchbox2/scripts/sb2-exitreport \
		-- $SHELL -c "$binary $args"
else
	initialize_sb_logging sb2
	exec sb2-monitor -x $SBOX_DIR/share/scratchbox2/scripts/sb2-exitreport \
		-- $SHELL --noprofile --norc
fi