Minor change to path lookup that fixes the bug that creating a file
[minix3.git] / servers / rs / exec.c
blob9f02d96ec37c45a76276a7e72e568f0de0cab498
1 #include "inc.h"
2 #include <a.out.h>
4 #define BLOCK_SIZE 1024
6 static void do_exec(int proc_e, char *exec, size_t exec_len, char *progname,
7 char *frame, int frame_len);
8 FORWARD _PROTOTYPE( int read_header, (char *exec, size_t exec_len, int *sep_id,
9 vir_bytes *text_bytes, vir_bytes *data_bytes,
10 vir_bytes *bss_bytes, phys_bytes *tot_bytes, vir_bytes *pc,
11 int *hdrlenp) );
12 FORWARD _PROTOTYPE( int exec_newmem, (int proc_e, vir_bytes text_bytes,
13 vir_bytes data_bytes, vir_bytes bss_bytes, vir_bytes tot_bytes,
14 vir_bytes frame_len, int sep_id,
15 Dev_t st_dev, ino_t st_ino, time_t st_ctime, char *progname,
16 int new_uid, int new_gid,
17 vir_bytes *stack_topp, int *load_textp, int *allow_setuidp) );
18 FORWARD _PROTOTYPE( int exec_restart, (int proc_e, int result) );
19 FORWARD _PROTOTYPE( void patch_ptr, (char stack[ARG_MAX],
20 vir_bytes base) );
21 FORWARD _PROTOTYPE( int read_seg, (char *exec, size_t exec_len, off_t off,
22 int proc_e, int seg, phys_bytes seg_bytes) );
24 static int self_e= NONE;
26 int dev_execve(int proc_e, char *exec, size_t exec_len, char **argv,
27 char **Xenvp)
29 char * const *ap;
30 char * const *ep;
31 char *frame;
32 char **vp;
33 char *sp, *progname;
34 size_t argc;
35 size_t frame_size;
36 size_t string_off;
37 size_t n;
38 int ov;
39 message m;
41 /* Assumptions: size_t and char *, it's all the same thing. */
43 /* Create a stack image that only needs to be patched up slightly
44 * by the kernel to be used for the process to be executed.
47 ov= 0; /* No overflow yet. */
48 frame_size= 0; /* Size of the new initial stack. */
49 string_off= 0; /* Offset to start of the strings. */
50 argc= 0; /* Argument count. */
52 for (ap= argv; *ap != NULL; ap++) {
53 n = sizeof(*ap) + strlen(*ap) + 1;
54 frame_size+= n;
55 if (frame_size < n) ov= 1;
56 string_off+= sizeof(*ap);
57 argc++;
60 #if 0
61 printf("here: %s, %d\n", __FILE__, __LINE__);
62 for (ep= envp; *ep != NULL; ep++) {
63 n = sizeof(*ep) + strlen(*ep) + 1;
64 frame_size+= n;
65 if (frame_size < n) ov= 1;
66 string_off+= sizeof(*ap);
68 #endif
70 /* Add an argument count and two terminating nulls. */
71 frame_size+= sizeof(argc) + sizeof(*ap) + sizeof(*ep);
72 string_off+= sizeof(argc) + sizeof(*ap) + sizeof(*ep);
74 /* Align. */
75 frame_size= (frame_size + sizeof(char *) - 1) & ~(sizeof(char *) - 1);
77 /* The party is off if there is an overflow. */
78 if (ov || frame_size < 3 * sizeof(char *)) {
79 errno= E2BIG;
80 return -1;
83 /* Allocate space for the stack frame. */
84 if ((frame = (char *) sbrk(frame_size)) == (char *) -1) {
85 errno = E2BIG;
86 return -1;
89 /* Set arg count, init pointers to vector and string tables. */
90 * (size_t *) frame = argc;
91 vp = (char **) (frame + sizeof(argc));
92 sp = frame + string_off;
94 /* Load the argument vector and strings. */
95 for (ap= argv; *ap != NULL; ap++) {
96 *vp++= (char *) (sp - frame);
97 n= strlen(*ap) + 1;
98 memcpy(sp, *ap, n);
99 sp+= n;
101 *vp++= NULL;
103 #if 0
104 /* Load the environment vector and strings. */
105 for (ep= envp; *ep != NULL; ep++) {
106 *vp++= (char *) (sp - frame);
107 n= strlen(*ep) + 1;
108 memcpy(sp, *ep, n);
109 sp+= n;
111 #endif
112 *vp++= NULL;
114 /* Padding. */
115 while (sp < frame + frame_size) *sp++= 0;
117 (progname=strrchr(argv[0], '/')) ? progname++ : (progname=argv[0]);
118 do_exec(proc_e, exec, exec_len, progname, frame, frame_size);
120 /* Failure, return the memory used for the frame and exit. */
121 (void) sbrk(-frame_size);
122 return -1;
125 static void do_exec(int proc_e, char *exec, size_t exec_len, char *progname,
126 char *frame, int frame_len)
128 int r;
129 int hdrlen, sep_id, load_text, allow_setuid;
130 int need_restart, error;
131 vir_bytes stack_top, vsp;
132 vir_bytes text_bytes, data_bytes, bss_bytes, pc;
133 phys_bytes tot_bytes;
134 off_t off;
135 uid_t new_uid;
136 gid_t new_gid;
138 need_restart= 0;
139 error= 0;
141 self_e = getnprocnr(getpid());
143 /* Read the file header and extract the segment sizes. */
144 r = read_header(exec, exec_len, &sep_id,
145 &text_bytes, &data_bytes, &bss_bytes,
146 &tot_bytes, &pc, &hdrlen);
147 if (r != OK)
149 printf("do_exec: read_header failed\n");
150 goto fail;
152 need_restart= 1;
154 new_uid= getuid();
155 new_gid= getgid();
156 /* XXX what should we use to identify the executable? */
157 r= exec_newmem(proc_e, text_bytes, data_bytes, bss_bytes, tot_bytes,
158 frame_len, sep_id, 0 /*dev*/, proc_e /*inum*/, 0 /*ctime*/,
159 progname, new_uid, new_gid, &stack_top, &load_text,
160 &allow_setuid);
161 if (r != OK)
163 printf("do_exec: exec_newmap failed: %d\n", r);
164 error= r;
165 goto fail;
168 /* Patch up stack and copy it from FS to new core image. */
169 vsp = stack_top;
170 vsp -= frame_len;
171 patch_ptr(frame, vsp);
172 r = sys_datacopy(SELF, (vir_bytes) frame,
173 proc_e, (vir_bytes) vsp, (phys_bytes)frame_len);
174 if (r != OK) panic(__FILE__,"pm_exec stack copy err on", proc_e);
176 off = hdrlen;
178 /* Read in text and data segments. */
179 if (load_text) {
180 r= read_seg(exec, exec_len, off, proc_e, T, text_bytes);
181 if (r != OK)
183 printf("do_exec: read_seg failed: %d\n", r);
184 error= r;
185 goto fail;
188 else
189 printf("do_exec: not loading text segment\n");
191 off += text_bytes;
192 r= read_seg(exec, exec_len, off, proc_e, D, data_bytes);
193 if (r != OK)
195 printf("do_exec: read_seg failed: %d\n", r);
196 error= r;
197 goto fail;
200 exec_restart(proc_e, OK);
202 return;
204 fail:
205 printf("do_exec(fail): error = %d\n", error);
206 if (need_restart)
207 exec_restart(proc_e, error);
210 /*===========================================================================*
211 * exec_newmem *
212 *===========================================================================*/
213 PRIVATE int exec_newmem(proc_e, text_bytes, data_bytes, bss_bytes, tot_bytes,
214 frame_len, sep_id, st_dev, st_ino, st_ctime, progname,
215 new_uid, new_gid, stack_topp, load_textp, allow_setuidp)
216 int proc_e;
217 vir_bytes text_bytes;
218 vir_bytes data_bytes;
219 vir_bytes bss_bytes;
220 vir_bytes tot_bytes;
221 vir_bytes frame_len;
222 int sep_id;
223 dev_t st_dev;
224 ino_t st_ino;
225 time_t st_ctime;
226 int new_uid;
227 int new_gid;
228 char *progname;
229 vir_bytes *stack_topp;
230 int *load_textp;
231 int *allow_setuidp;
233 int r;
234 struct exec_newmem e;
235 message m;
237 e.text_bytes= text_bytes;
238 e.data_bytes= data_bytes;
239 e.bss_bytes= bss_bytes;
240 e.tot_bytes= tot_bytes;
241 e.args_bytes= frame_len;
242 e.sep_id= sep_id;
243 e.st_dev= st_dev;
244 e.st_ino= st_ino;
245 e.st_ctime= st_ctime;
246 e.new_uid= new_uid;
247 e.new_gid= new_gid;
248 strncpy(e.progname, progname, sizeof(e.progname)-1);
249 e.progname[sizeof(e.progname)-1]= '\0';
251 m.m_type= EXEC_NEWMEM;
252 m.EXC_NM_PROC= proc_e;
253 m.EXC_NM_PTR= (char *)&e;
254 r= sendrec(PM_PROC_NR, &m);
255 if (r != OK)
256 return r;
257 #if 0
258 printf("exec_newmem: r = %d, m_type = %d\n", r, m.m_type);
259 #endif
260 *stack_topp= m.m1_i1;
261 *load_textp= !!(m.m1_i2 & EXC_NM_RF_LOAD_TEXT);
262 *allow_setuidp= !!(m.m1_i2 & EXC_NM_RF_ALLOW_SETUID);
263 #if 0
264 printf("exec_newmem: stack_top = 0x%x\n", *stack_topp);
265 printf("exec_newmem: load_text = %d\n", *load_textp);
266 #endif
267 return m.m_type;
271 /*===========================================================================*
272 * exec_restart *
273 *===========================================================================*/
274 PRIVATE int exec_restart(proc_e, result)
275 int proc_e;
276 int result;
278 int r;
279 message m;
281 m.m_type= EXEC_RESTART;
282 m.EXC_RS_PROC= proc_e;
283 m.EXC_RS_RESULT= result;
284 r= sendrec(PM_PROC_NR, &m);
285 if (r != OK)
286 return r;
287 return m.m_type;
291 /*===========================================================================*
292 * read_header *
293 *===========================================================================*/
294 PRIVATE int read_header(exec, exec_len, sep_id, text_bytes, data_bytes,
295 bss_bytes, tot_bytes, pc, hdrlenp)
296 char *exec; /* executable image */
297 size_t exec_len; /* size of the image */
298 int *sep_id; /* true iff sep I&D */
299 vir_bytes *text_bytes; /* place to return text size */
300 vir_bytes *data_bytes; /* place to return initialized data size */
301 vir_bytes *bss_bytes; /* place to return bss size */
302 phys_bytes *tot_bytes; /* place to return total size */
303 vir_bytes *pc; /* program entry point (initial PC) */
304 int *hdrlenp;
306 /* Read the header and extract the text, data, bss and total sizes from it. */
307 off_t pos;
308 block_t b;
309 struct exec hdr; /* a.out header is read in here */
311 /* Read the header and check the magic number. The standard MINIX header
312 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
313 * Then come 4 more longs that are not used here.
314 * Byte 0: magic number 0x01
315 * Byte 1: magic number 0x03
316 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
317 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
318 * Motorola = 0x0B, Sun SPARC = 0x17
319 * Byte 4: Header length = 0x20
320 * Bytes 5-7 are not used.
322 * Now come the 6 longs
323 * Bytes 8-11: size of text segments in bytes
324 * Bytes 12-15: size of initialized data segment in bytes
325 * Bytes 16-19: size of bss in bytes
326 * Bytes 20-23: program entry point
327 * Bytes 24-27: total memory allocated to program (text, data + stack)
328 * Bytes 28-31: size of symbol table in bytes
329 * The longs are represented in a machine dependent order,
330 * little-endian on the 8088, big-endian on the 68000.
331 * The header is followed directly by the text and data segments, and the
332 * symbol table (if any). The sizes are given in the header. Only the
333 * text and data segments are copied into memory by exec. The header is
334 * used here only. The symbol table is for the benefit of a debugger and
335 * is ignored here.
337 int r;
339 pos= 0; /* Read from the start of the file */
341 if (exec_len < sizeof(hdr)) return(ENOEXEC);
343 memcpy(&hdr, exec, sizeof(hdr));
345 /* Check magic number, cpu type, and flags. */
346 if (BADMAG(hdr)) return(ENOEXEC);
347 #if (CHIP == INTEL && _WORD_SIZE == 2)
348 if (hdr.a_cpu != A_I8086) return(ENOEXEC);
349 #endif
350 #if (CHIP == INTEL && _WORD_SIZE == 4)
351 if (hdr.a_cpu != A_I80386) return(ENOEXEC);
352 #endif
353 if ((hdr.a_flags & ~(A_NSYM | A_EXEC | A_SEP)) != 0) return(ENOEXEC);
355 *sep_id = !!(hdr.a_flags & A_SEP); /* separate I & D or not */
357 /* Get text and data sizes. */
358 *text_bytes = (vir_bytes) hdr.a_text; /* text size in bytes */
359 *data_bytes = (vir_bytes) hdr.a_data; /* data size in bytes */
360 *bss_bytes = (vir_bytes) hdr.a_bss; /* bss size in bytes */
361 *tot_bytes = hdr.a_total; /* total bytes to allocate for prog */
362 if (*tot_bytes == 0) return(ENOEXEC);
364 if (!*sep_id) {
365 /* If I & D space is not separated, it is all considered data. Text=0*/
366 *data_bytes += *text_bytes;
367 *text_bytes = 0;
369 *pc = hdr.a_entry; /* initial address to start execution */
370 *hdrlenp = hdr.a_hdrlen & BYTE; /* header length */
372 return(OK);
375 /*===========================================================================*
376 * patch_ptr *
377 *===========================================================================*/
378 PRIVATE void patch_ptr(stack, base)
379 char stack[ARG_MAX]; /* pointer to stack image within PM */
380 vir_bytes base; /* virtual address of stack base inside user */
382 /* When doing an exec(name, argv, envp) call, the user builds up a stack
383 * image with arg and env pointers relative to the start of the stack. Now
384 * these pointers must be relocated, since the stack is not positioned at
385 * address 0 in the user's address space.
388 char **ap, flag;
389 vir_bytes v;
391 flag = 0; /* counts number of 0-pointers seen */
392 ap = (char **) stack; /* points initially to 'nargs' */
393 ap++; /* now points to argv[0] */
394 while (flag < 2) {
395 if (ap >= (char **) &stack[ARG_MAX]) return; /* too bad */
396 if (*ap != NULL) {
397 v = (vir_bytes) *ap; /* v is relative pointer */
398 v += base; /* relocate it */
399 *ap = (char *) v; /* put it back */
400 } else {
401 flag++;
403 ap++;
407 /*===========================================================================*
408 * read_seg *
409 *===========================================================================*/
410 PRIVATE int read_seg(exec, exec_len, off, proc_e, seg, seg_bytes)
411 char *exec; /* executable image */
412 size_t exec_len; /* size of the image */
413 off_t off; /* offset in file */
414 int proc_e; /* process number (endpoint) */
415 int seg; /* T, D, or S */
416 phys_bytes seg_bytes; /* how much is to be transferred? */
419 * The byte count on read is usually smaller than the segment count, because
420 * a segment is padded out to a click multiple, and the data segment is only
421 * partially initialized.
424 int r;
425 off_t n, o, b_off, seg_off;
427 if (off+seg_bytes > exec_len) return ENOEXEC;
428 r= sys_vircopy(SELF, D, (vir_bytes)exec+off, proc_e, seg, 0, seg_bytes);
429 return r;