[comp.os.minix] [source] #! in MM -- take 2

klamer@mi.eltn.utwente.nl (Klamer Schutte) (05/17/91)

I already posted this yesterday, but it seems it didn't make it.
Sorry if anybody receives thsi message twice -- Klamer

Here is the second version of my #!interpreter patch for mm/exec.c.
This version has all known bugs fixed. My logfile says:

 $Log: exec.c,v $
 Revision 1.3  1991/05/14  21:38:25  klamer
 accept \t as well as ' '.
 replace the old argv[0] with the filename.
 made references to cbuf[] rather than to buf[].

 Revision 1.2  1991/05/07  13:02:46  klamer
 added #! processing to the kernel

 revision 1.1 : minix-st version 1.5.10.3

One feature (bug ???) remains: i keep alignment from the data argv[] and
envp[] point to intact. There (migh ???) be a tradition of having this
data in the form of strings with only 1 \0 in between.
Where is the manual page for execve(2) ???? Or does POSIX(*) say anything
about this?

Klamer (.signature at end)

(*) As anybody knows, POSIX is todays oracle. ;-)

---------------- cut here ----------------
table
 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
begin 644 exec.c.cdif.Z
M'YV-*@*"B.$B1H(8.7+$> &C!D,; VGHF'%#1PT:"EIH!%$&3YDQ+L8<3+BPz
MX8L8-$#(B#$QA\49"@+*G$E314R!,62PD#$#Q,R,&W.RH-%3HT8%()*J2 J"y
M"IHR(-ZX89.'HQLZ<JK">9/F*H@T<T"0>?.EXT<72)6^2*M@!8@7-MWZ!$$Bx
M"1D='#V"',/"SD 7/1$J9.@0I4J6,W#HD%$#Q!HV8=J4D0.B"!XX=-LJU3R7w
M!),W9_":W=LWL]RE4LK8 9M&ZM^> TD2/IGR\$3%C),^CCR9\](P8\:4@4,'v
M!)?B8<+>*<.&#8CD($Y$1WL:A)SAD(6#H/,T*ALRS^6<L;,%1A<0=])PW][=u
M3!HV9=SPIJX41)LP9*!>-S,YOO"P=+P!PAABU&'&%N?)$09WD[$7AAO;"5C@t
M@5W0YY-O(*2VVARM04B0#$D)5I)#,-P0X@PZP""##C1 Y!ADDLF!(7[Y@3="s
M""# (<<;_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THSCC(G'N Xw
MWK\[_K_QVS&(KL="ON=G;.2/OFP7A3%,7M- ^.2O*^54CF]6GNEXE$=U'%D<v
M#>>C U!B7AQDSM]2L+@!]CS#QN;-C#W: [WX!R\R( ,:7BX=SA2MTTC%:W4[u
MYQ8])UQ8''3)173/_EQ(ETQ(IJ:5NZ ^H>=H6W30WL72/G3,5>U'EP32)4 6t
MD^TH<.6]Y>S.%>W(1>[@;NWHGG3\=6ID\$L^0#?KWNH9=L=>'N C:!\:51QVs
MNB /HVG+Y"0+#.1BH3_*3M]T4^:W_HII19-XT3I?GEM?(/%T0P82[Q9A>%&_r
ME"_Z)4!,Y[Q?\ ;,L\>2ZV?[KD8 I)SZ0_,?+SS_).>"."_^<N<_-%+LONSAq
M]>[U+N]"IUSE'N_0=>_8KNS;WHYNH0+>SG-,#W3SKO1'W_3I[D#KKNRJ;O5&p
M)^Y93^U,?^VQ7#?[KES^;O( O]$=3? (UP8'7S59P]T,7QP./\"=ZN(B+SP3o
MKV85;^N(G/&)S/$GE//5%/& 3_)?C_)CT/'+%Y[[V/(OWUTQKYPO*?,WGS&*n
"_T\5m
 l
end

-- 
Klamer Schutte
Faculty of electrical engineering -- University of Twente, The Netherlands
klamer@mi.eltn.utwente.nl	{backbone}!mcsun!mi.eltn.utwente.nl!klamer

kjb@cs.vu.nl (Kees J. Bot) (05/23/91)

I'm posting my comments to Klamer's second try at an #! implementation in
MM to remind you about my implementation of #! that I posted on May 13.
So far, I have only received comments from Klamer on my version telling
me that it is slower than his, because it makes two more calls to FS.
Apart from being a little bit slower, using my version is still the easiest
way to fix the bugs in Klamer's version.

klamer@mi.eltn.utwente.nl (Klamer Schutte) writes:
>Here is the second version of my #!interpreter patch for mm/exec.c.
>This version has all known bugs fixed.

Except for not doing setuid and this other "feature".

>One feature (bug ???) remains: i keep alignment from the data argv[] and
>envp[] point to intact. There (migh ???) be a tradition of having this
>data in the form of strings with only 1 \0 in between.
>Where is the manual page for execve(2) ???? Or does POSIX(*) say anything
>about this?

I know of three places to look for the proper format of the initial stack:
- The old V7 manuals under exec(2), written when users were not considered
  too stupid to know such things.
- The source code of execve(2).
- The source code of ps(1).
The ps(1) source contains this interesting comment:
/*
 * Get_args inspects /dev/mem, using bufp, and tries to locate the initial
 * stack frame pointer, i.e. the place where the stack started at exec time.
 * It is assumed that the end of the stack frame looks as follows:
 *      argc    <-- initial stack frame starts here
 *      argv[0]
 *      ...
 *      NULL    (*)
 *      envp[0]
 *      ...
 *      NULL    (**)
 *      argv[0][0] ... '\0'
 *      ...
 *      argv[argc - 1][0] ... '\0'
 *      envp[0][0] ... '\0'
 *      ...
 *      [trailing '\0']
 * Where the total space occupied by this original stack frame <= ARG_MAX.
 * Get_args reads in the last ARG_MAX bytes of the process' data, and
 * searches back for two NULL ptrs (hopefully the (*) & (**) above).
 * If it finds such a portion, it continues backwards, counting ptrs until:
 * a) either a word is found that has as its value the count (supposedly argc),
 * b) another NULL word is found, in which case the algorithm is reiterated, or
 * c) we wind up before the start of the buffer and fail.
 * Upon success, get_args returns a pointer to the conactenated arg list.
 * Warning: this routine is inherently unreliable and probably doesn't work if
 * ptrs and ints have different sizes.
 */

I decided to go over Klamer's patch with a fine comb this time.  (I wish
someone would do that with my patch, with a mental -pedantic flag on.)

- ALIGN align to a multiple of 2, execve to a multiple of sizeof(char *).
- The interpreter is found relative to '/'.  (Move the first
  tell_fs(CHDIR, ...) inside the do loop.)
- Setuid bits on the script are still ignored.  (Wouldn't it be nice to
  allow people to explore the security risks of a setuid script?)
- Change 'know' to 'now' in patch_stack.  (-pedantic)
- The old argv[0][] is not removed from the initial stack.
- The ALIGN(len) is still at the wrong place.  Try moving only the strings
  by disp bytes, then move the pointers by argc*sizeof(char *) bytes.  Do
  an ALIGN(disp) just before the return.
- If stk_bytes is close to ARG_MAX then the last few environment variables
  may be truncated.
- Read_header returns 0 when there is nothing behind #!.
- The size_ok function may return something other than -100.
--
	                        Kees J. Bot  (kjb@cs.vu.nl)
	              Systems Programmer, Vrije Universiteit Amsterdam