Fixed extern declaration from pointer to array
[minix.git] / servers / rs / exec.c
blob758f64842e3b175f460eb8d12ad5b98e92303a64
1 #include "inc.h"
2 #include <a.out.h>
4 #define BLOCK_SIZE 1024
6 static int 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 int dev_execve(int proc_e, char *exec, size_t exec_len, char **argv,
25 char **Xenvp)
27 char * const *ap;
28 char * const *ep;
29 char *frame;
30 char **vp;
31 char *sp, *progname;
32 size_t argc;
33 size_t frame_size;
34 size_t string_off;
35 size_t n;
36 int ov;
37 message m;
38 int r;
40 /* Assumptions: size_t and char *, it's all the same thing. */
42 /* Create a stack image that only needs to be patched up slightly
43 * by the kernel to be used for the process to be executed.
46 ov= 0; /* No overflow yet. */
47 frame_size= 0; /* Size of the new initial stack. */
48 string_off= 0; /* Offset to start of the strings. */
49 argc= 0; /* Argument count. */
51 for (ap= argv; *ap != NULL; ap++) {
52 n = sizeof(*ap) + strlen(*ap) + 1;
53 frame_size+= n;
54 if (frame_size < n) ov= 1;
55 string_off+= sizeof(*ap);
56 argc++;
59 #if 0
60 printf("here: %s, %d\n", __FILE__, __LINE__);
61 for (ep= envp; *ep != NULL; ep++) {
62 n = sizeof(*ep) + strlen(*ep) + 1;
63 frame_size+= n;
64 if (frame_size < n) ov= 1;
65 string_off+= sizeof(*ap);
67 #endif
69 /* Add an argument count and two terminating nulls. */
70 frame_size+= sizeof(argc) + sizeof(*ap) + sizeof(*ep);
71 string_off+= sizeof(argc) + sizeof(*ap) + sizeof(*ep);
73 /* Align. */
74 frame_size= (frame_size + sizeof(char *) - 1) & ~(sizeof(char *) - 1);
76 /* The party is off if there is an overflow. */
77 if (ov || frame_size < 3 * sizeof(char *)) {
78 errno= E2BIG;
79 return -1;
82 /* Allocate space for the stack frame. */
83 if ((frame = (char *) sbrk(frame_size)) == (char *) -1) {
84 errno = E2BIG;
85 return -1;
88 /* Set arg count, init pointers to vector and string tables. */
89 * (size_t *) frame = argc;
90 vp = (char **) (frame + sizeof(argc));
91 sp = frame + string_off;
93 /* Load the argument vector and strings. */
94 for (ap= argv; *ap != NULL; ap++) {
95 *vp++= (char *) (sp - frame);
96 n= strlen(*ap) + 1;
97 memcpy(sp, *ap, n);
98 sp+= n;
100 *vp++= NULL;
102 #if 0
103 /* Load the environment vector and strings. */
104 for (ep= envp; *ep != NULL; ep++) {
105 *vp++= (char *) (sp - frame);
106 n= strlen(*ep) + 1;
107 memcpy(sp, *ep, n);
108 sp+= n;
110 #endif
111 *vp++= NULL;
113 /* Padding. */
114 while (sp < frame + frame_size) *sp++= 0;
116 (progname=strrchr(argv[0], '/')) ? progname++ : (progname=argv[0]);
117 r = do_exec(proc_e, exec, exec_len, progname, frame, frame_size);
119 /* Return the memory used for the frame and exit. */
120 (void) sbrk(-frame_size);
121 return r;
124 static int do_exec(int proc_e, char *exec, size_t exec_len, char *progname,
125 char *frame, int frame_len)
127 int r;
128 int hdrlen, sep_id, load_text, allow_setuid;
129 int need_restart, error;
130 vir_bytes stack_top, vsp;
131 vir_bytes text_bytes, data_bytes, bss_bytes, pc;
132 phys_bytes tot_bytes;
133 off_t off;
134 uid_t new_uid;
135 gid_t new_gid;
137 need_restart= 0;
138 error= 0;
140 /* Read the file header and extract the segment sizes. */
141 r = read_header(exec, exec_len, &sep_id,
142 &text_bytes, &data_bytes, &bss_bytes,
143 &tot_bytes, &pc, &hdrlen);
144 if (r != OK)
146 printf("do_exec: read_header failed\n");
147 goto fail;
149 need_restart= 1;
151 new_uid= getuid();
152 new_gid= getgid();
153 /* XXX what should we use to identify the executable? */
154 r= exec_newmem(proc_e, text_bytes, data_bytes, bss_bytes, tot_bytes,
155 frame_len, sep_id, 0 /*dev*/, proc_e /*inum*/, 0 /*ctime*/,
156 progname, new_uid, new_gid, &stack_top, &load_text,
157 &allow_setuid);
158 if (r != OK)
160 printf("do_exec: exec_newmap failed: %d\n", r);
161 error= r;
162 goto fail;
165 /* Patch up stack and copy it from RS to new core image. */
166 vsp = stack_top;
167 vsp -= frame_len;
168 patch_ptr(frame, vsp);
169 r = sys_datacopy(SELF, (vir_bytes) frame,
170 proc_e, (vir_bytes) vsp, (phys_bytes)frame_len);
171 if (r != OK) {
172 printf("RS: stack_top is 0x%lx; tried to copy to 0x%lx in %d\n",
173 stack_top, vsp);
174 printf("do_exec: copying out new stack failed: %d\n", r);
175 error= r;
176 goto fail;
179 off = hdrlen;
181 /* Read in text and data segments. */
182 if (load_text) {
183 r= read_seg(exec, exec_len, off, proc_e, T, text_bytes);
184 if (r != OK)
186 printf("do_exec: read_seg failed: %d\n", r);
187 error= r;
188 goto fail;
191 else
192 printf("do_exec: not loading text segment\n");
194 off += text_bytes;
195 r= read_seg(exec, exec_len, off, proc_e, D, data_bytes);
196 if (r != OK)
198 printf("do_exec: read_seg failed: %d\n", r);
199 error= r;
200 goto fail;
203 return exec_restart(proc_e, OK);
205 fail:
206 printf("do_exec(fail): error = %d\n", error);
207 if (need_restart)
208 exec_restart(proc_e, error);
210 return error;
213 /*===========================================================================*
214 * exec_newmem *
215 *===========================================================================*/
216 PRIVATE int exec_newmem(proc_e, text_bytes, data_bytes, bss_bytes, tot_bytes,
217 frame_len, sep_id, st_dev, st_ino, st_ctime, progname,
218 new_uid, new_gid, stack_topp, load_textp, allow_setuidp)
219 int proc_e;
220 vir_bytes text_bytes;
221 vir_bytes data_bytes;
222 vir_bytes bss_bytes;
223 vir_bytes tot_bytes;
224 vir_bytes frame_len;
225 int sep_id;
226 dev_t st_dev;
227 ino_t st_ino;
228 time_t st_ctime;
229 int new_uid;
230 int new_gid;
231 char *progname;
232 vir_bytes *stack_topp;
233 int *load_textp;
234 int *allow_setuidp;
236 int r;
237 struct exec_newmem e;
238 message m;
240 e.text_bytes= text_bytes;
241 e.data_bytes= data_bytes;
242 e.bss_bytes= bss_bytes;
243 e.tot_bytes= tot_bytes;
244 e.args_bytes= frame_len;
245 e.sep_id= sep_id;
246 e.st_dev= st_dev;
247 e.st_ino= st_ino;
248 e.st_ctime= st_ctime;
249 e.new_uid= new_uid;
250 e.new_gid= new_gid;
251 strncpy(e.progname, progname, sizeof(e.progname)-1);
252 e.progname[sizeof(e.progname)-1]= '\0';
254 m.m_type= EXEC_NEWMEM;
255 m.EXC_NM_PROC= proc_e;
256 m.EXC_NM_PTR= (char *)&e;
257 r= sendrec(PM_PROC_NR, &m);
258 if (r != OK)
259 return r;
260 #if 0
261 printf("exec_newmem: r = %d, m_type = %d\n", r, m.m_type);
262 #endif
263 *stack_topp= m.m1_i1;
264 *load_textp= !!(m.m1_i2 & EXC_NM_RF_LOAD_TEXT);
265 *allow_setuidp= !!(m.m1_i2 & EXC_NM_RF_ALLOW_SETUID);
266 #if 0
267 printf("RS: exec_newmem: stack_top = 0x%x\n", *stack_topp);
268 printf("RS: exec_newmem: load_text = %d\n", *load_textp);
269 #endif
270 return m.m_type;
274 /*===========================================================================*
275 * exec_restart *
276 *===========================================================================*/
277 PRIVATE int exec_restart(proc_e, result)
278 int proc_e;
279 int result;
281 int r;
282 message m;
284 m.m_type= EXEC_RESTART;
285 m.EXC_RS_PROC= proc_e;
286 m.EXC_RS_RESULT= result;
287 r= sendrec(PM_PROC_NR, &m);
288 if (r != OK)
289 return r;
290 return m.m_type;
294 /*===========================================================================*
295 * read_header *
296 *===========================================================================*/
297 PRIVATE int read_header(exec, exec_len, sep_id, text_bytes, data_bytes,
298 bss_bytes, tot_bytes, pc, hdrlenp)
299 char *exec; /* executable image */
300 size_t exec_len; /* size of the image */
301 int *sep_id; /* true iff sep I&D */
302 vir_bytes *text_bytes; /* place to return text size */
303 vir_bytes *data_bytes; /* place to return initialized data size */
304 vir_bytes *bss_bytes; /* place to return bss size */
305 phys_bytes *tot_bytes; /* place to return total size */
306 vir_bytes *pc; /* program entry point (initial PC) */
307 int *hdrlenp;
309 /* Read the header and extract the text, data, bss and total sizes from it. */
310 off_t pos;
311 block_t b;
312 struct exec hdr; /* a.out header is read in here */
314 /* Read the header and check the magic number. The standard MINIX header
315 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
316 * Then come 4 more longs that are not used here.
317 * Byte 0: magic number 0x01
318 * Byte 1: magic number 0x03
319 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
320 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
321 * Motorola = 0x0B, Sun SPARC = 0x17
322 * Byte 4: Header length = 0x20
323 * Bytes 5-7 are not used.
325 * Now come the 6 longs
326 * Bytes 8-11: size of text segments in bytes
327 * Bytes 12-15: size of initialized data segment in bytes
328 * Bytes 16-19: size of bss in bytes
329 * Bytes 20-23: program entry point
330 * Bytes 24-27: total memory allocated to program (text, data + stack)
331 * Bytes 28-31: size of symbol table in bytes
332 * The longs are represented in a machine dependent order,
333 * little-endian on the 8088, big-endian on the 68000.
334 * The header is followed directly by the text and data segments, and the
335 * symbol table (if any). The sizes are given in the header. Only the
336 * text and data segments are copied into memory by exec. The header is
337 * used here only. The symbol table is for the benefit of a debugger and
338 * is ignored here.
340 int r;
342 pos= 0; /* Read from the start of the file */
344 if (exec_len < sizeof(hdr)) return(ENOEXEC);
346 memcpy(&hdr, exec, sizeof(hdr));
348 /* Check magic number, cpu type, and flags. */
349 if (BADMAG(hdr)) return(ENOEXEC);
350 #if (CHIP == INTEL && _WORD_SIZE == 2)
351 if (hdr.a_cpu != A_I8086) return(ENOEXEC);
352 #endif
353 #if (CHIP == INTEL && _WORD_SIZE == 4)
354 if (hdr.a_cpu != A_I80386) return(ENOEXEC);
355 #endif
356 if ((hdr.a_flags & ~(A_NSYM | A_EXEC | A_SEP)) != 0) return(ENOEXEC);
358 *sep_id = !!(hdr.a_flags & A_SEP); /* separate I & D or not */
360 /* Get text and data sizes. */
361 *text_bytes = (vir_bytes) hdr.a_text; /* text size in bytes */
362 *data_bytes = (vir_bytes) hdr.a_data; /* data size in bytes */
363 *bss_bytes = (vir_bytes) hdr.a_bss; /* bss size in bytes */
364 *tot_bytes = hdr.a_total; /* total bytes to allocate for prog */
365 if (*tot_bytes == 0) return(ENOEXEC);
367 if (!*sep_id) {
368 /* If I & D space is not separated, it is all considered data. Text=0*/
369 *data_bytes += *text_bytes;
370 *text_bytes = 0;
372 *pc = hdr.a_entry; /* initial address to start execution */
373 *hdrlenp = hdr.a_hdrlen & BYTE; /* header length */
375 return(OK);
378 /*===========================================================================*
379 * patch_ptr *
380 *===========================================================================*/
381 PRIVATE void patch_ptr(stack, base)
382 char stack[ARG_MAX]; /* pointer to stack image within PM */
383 vir_bytes base; /* virtual address of stack base inside user */
385 /* When doing an exec(name, argv, envp) call, the user builds up a stack
386 * image with arg and env pointers relative to the start of the stack. Now
387 * these pointers must be relocated, since the stack is not positioned at
388 * address 0 in the user's address space.
391 char **ap, flag;
392 vir_bytes v;
394 flag = 0; /* counts number of 0-pointers seen */
395 ap = (char **) stack; /* points initially to 'nargs' */
396 ap++; /* now points to argv[0] */
397 while (flag < 2) {
398 if (ap >= (char **) &stack[ARG_MAX]) return; /* too bad */
399 if (*ap != NULL) {
400 v = (vir_bytes) *ap; /* v is relative pointer */
401 v += base; /* relocate it */
402 *ap = (char *) v; /* put it back */
403 } else {
404 flag++;
406 ap++;
410 /*===========================================================================*
411 * read_seg *
412 *===========================================================================*/
413 PRIVATE int read_seg(exec, exec_len, off, proc_e, seg, seg_bytes)
414 char *exec; /* executable image */
415 size_t exec_len; /* size of the image */
416 off_t off; /* offset in file */
417 int proc_e; /* process number (endpoint) */
418 int seg; /* T, D, or S */
419 phys_bytes seg_bytes; /* how much is to be transferred? */
422 * The byte count on read is usually smaller than the segment count, because
423 * a segment is padded out to a click multiple, and the data segment is only
424 * partially initialized.
427 int r;
428 off_t n, o, b_off, seg_off;
430 if (off+seg_bytes > exec_len) return ENOEXEC;
431 r= sys_vircopy(SELF, D, (vir_bytes)exec+off, proc_e, seg, 0, seg_bytes);
432 return r;