cae36a52 |
1 | #!/bin/rc |
2 | |
3 | arg0=$0 |
4 | DISPLAY=:0 |
5 | HOME=/tmp |
6 | PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/usr/games |
7 | |
8 | x='' |
9 | e=/bin/linuxemu |
10 | r=/sys/lib/linux |
11 | |
12 | fn eprint { |
13 | echo $arg0: $* >[1=2] |
14 | } |
15 | |
16 | fn usage { |
17 | echo usage: $arg0 [-h] [-d...] [-u uid] [-g gid] [-startx] [-display :n] [-e emubin] [-r linuxroot] command [args ...] >[1=2] |
18 | exit usage |
19 | } |
20 | |
21 | # extract options |
22 | o=() |
23 | while(~ $1 -*){ |
24 | switch($1){ |
25 | case -h |
26 | usage |
27 | case -r |
28 | shift |
29 | r=$1 |
30 | case -e |
31 | shift |
32 | e=$1 |
33 | case -startx |
34 | x=1 |
35 | case -display |
36 | shift |
37 | DISPLAY=$1 |
38 | case -[ug] |
39 | o=($o $1 $2) |
40 | shift |
41 | case -* |
42 | o=($o $1) |
43 | } |
44 | shift |
45 | } |
46 | |
47 | switch($#*){ |
48 | case 0 |
49 | usage |
50 | } |
51 | |
52 | if(! ~ $x ''){ |
53 | # find free local display |
54 | d=(`{{seq 0 32; {echo /srv/UD.X* | sed 's!/srv/UD\.X!!g; s!\ !\ |
55 | !g; s!\*!!g;'}} | sort | uniq -c | awk '/^\ *1\ /{print $2}'}) |
56 | d=$d(1) |
57 | X11/equis -ac :$d & |
58 | k=/proc/$apid/notepg |
59 | $arg0 -e $e -r $r -display :$d $o $* |
60 | {echo kill >$k} >/dev/null >[2=1] |
61 | exit |
62 | } |
63 | |
64 | # rewrite the path so it would accessible after binding $r to / |
65 | fn ninepath { |
66 | if(~ $1 /* && test -e $1 && ! test -e $r/$1){ |
67 | echo /9$1 |
68 | } |
69 | if not { |
70 | echo $1 |
71 | } |
72 | } |
73 | |
74 | w=`{pwd} |
75 | r=`{cleanname -d $w $r} |
76 | if(! test -d $r){ |
77 | eprint bad rootpath: $r |
78 | exit rootpath |
79 | } |
80 | e=`{cleanname -d $w $e} |
81 | if(! test -x $e){ |
82 | eprint bad emubin: $e |
83 | exit emubin |
84 | } |
85 | e=`{ninepath $e} |
86 | p=`{ninepath $w} |
87 | a=($e $o) |
88 | while(! ~ $#* 0){ |
89 | x=`{ninepath $1} |
90 | a=($a $"x) |
91 | shift |
92 | } |
93 | |
94 | # bind the required plan9 stuff |
95 | rfork n |
96 | mntgen $r |
97 | bind -a '#P' /dev |
98 | for(d in /9 /dev /proc /net /env /srv /n /mnt /tmp){ |
99 | t=$r^$d |
100 | switch($d){ |
101 | case /tmp /env /srv |
102 | bind -c $d $t |
103 | case /9 |
104 | bind / $t |
105 | case * |
106 | bind $d $t |
107 | } |
108 | } |
109 | |
110 | # change root and run the emulator |
111 | builtin cd / |
112 | bind $r / |
113 | builtin cd $p |
114 | exec $a |