RS_LOOKUP feature for libc functions that want to access servers.
[minix.git] / servers / rs / exec.c
blobd7ce232569e2adb48796f81a238cab97a0c5a05a
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) {
175 printf("RS: stack_top is 0x%lx; tried to copy to 0x%lx in %d\n",
176 stack_top, vsp);
177 panic("RS", "do_exec: stack copy err on", proc_e);
180 off = hdrlen;
182 /* Read in text and data segments. */
183 if (load_text) {
184 r= read_seg(exec, exec_len, off, proc_e, T, text_bytes);
185 if (r != OK)
187 printf("do_exec: read_seg failed: %d\n", r);
188 error= r;
189 goto fail;
192 else
193 printf("do_exec: not loading text segment\n");
195 off += text_bytes;
196 r= read_seg(exec, exec_len, off, proc_e, D, data_bytes);
197 if (r != OK)
199 printf("do_exec: read_seg failed: %d\n", r);
200 error= r;
201 goto fail;
204 exec_restart(proc_e, OK);
206 return;
208 fail:
209 printf("do_exec(fail): error = %d\n", error);
210 if (need_restart)
211 exec_restart(proc_e, error);
214 /*===========================================================================*
215 * exec_newmem *
216 *===========================================================================*/
217 PRIVATE int exec_newmem(proc_e, text_bytes, data_bytes, bss_bytes, tot_bytes,
218 frame_len, sep_id, st_dev, st_ino, st_ctime, progname,
219 new_uid, new_gid, stack_topp, load_textp, allow_setuidp)
220 int proc_e;
221 vir_bytes text_bytes;
222 vir_bytes data_bytes;
223 vir_bytes bss_bytes;
224 vir_bytes tot_bytes;
225 vir_bytes frame_len;
226 int sep_id;
227 dev_t st_dev;
228 ino_t st_ino;
229 time_t st_ctime;
230 int new_uid;
231 int new_gid;
232 char *progname;
233 vir_bytes *stack_topp;
234 int *load_textp;
235 int *allow_setuidp;
237 int r;
238 struct exec_newmem e;
239 message m;
241 e.text_bytes= text_bytes;
242 e.data_bytes= data_bytes;
243 e.bss_bytes= bss_bytes;
244 e.tot_bytes= tot_bytes;
245 e.args_bytes= frame_len;
246 e.sep_id= sep_id;
247 e.st_dev= st_dev;
248 e.st_ino= st_ino;
249 e.st_ctime= st_ctime;
250 e.new_uid= new_uid;
251 e.new_gid= new_gid;
252 strncpy(e.progname, progname, sizeof(e.progname)-1);
253 e.progname[sizeof(e.progname)-1]= '\0';
255 m.m_type= EXEC_NEWMEM;
256 m.EXC_NM_PROC= proc_e;
257 m.EXC_NM_PTR= (char *)&e;
258 r= sendrec(PM_PROC_NR, &m);
259 if (r != OK)
260 return r;
261 #if 0
262 printf("exec_newmem: r = %d, m_type = %d\n", r, m.m_type);
263 #endif
264 *stack_topp= m.m1_i1;
265 *load_textp= !!(m.m1_i2 & EXC_NM_RF_LOAD_TEXT);
266 *allow_setuidp= !!(m.m1_i2 & EXC_NM_RF_ALLOW_SETUID);
267 #if 0
268 printf("RS: exec_newmem: stack_top = 0x%x\n", *stack_topp);
269 printf("RS: exec_newmem: load_text = %d\n", *load_textp);
270 #endif
271 return m.m_type;
275 /*===========================================================================*
276 * exec_restart *
277 *===========================================================================*/
278 PRIVATE int exec_restart(proc_e, result)
279 int proc_e;
280 int result;
282 int r;
283 message m;
285 m.m_type= EXEC_RESTART;
286 m.EXC_RS_PROC= proc_e;
287 m.EXC_RS_RESULT= result;
288 r= sendrec(PM_PROC_NR, &m);
289 if (r != OK)
290 return r;
291 return m.m_type;
295 /*===========================================================================*
296 * read_header *
297 *===========================================================================*/
298 PRIVATE int read_header(exec, exec_len, sep_id, text_bytes, data_bytes,
299 bss_bytes, tot_bytes, pc, hdrlenp)
300 char *exec; /* executable image */
301 size_t exec_len; /* size of the image */
302 int *sep_id; /* true iff sep I&D */
303 vir_bytes *text_bytes; /* place to return text size */
304 vir_bytes *data_bytes; /* place to return initialized data size */
305 vir_bytes *bss_bytes; /* place to return bss size */
306 phys_bytes *tot_bytes; /* place to return total size */
307 vir_bytes *pc; /* program entry point (initial PC) */
308 int *hdrlenp;
310 /* Read the header and extract the text, data, bss and total sizes from it. */
311 off_t pos;
312 block_t b;
313 struct exec hdr; /* a.out header is read in here */
315 /* Read the header and check the magic number. The standard MINIX header
316 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
317 * Then come 4 more longs that are not used here.
318 * Byte 0: magic number 0x01
319 * Byte 1: magic number 0x03
320 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
321 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
322 * Motorola = 0x0B, Sun SPARC = 0x17
323 * Byte 4: Header length = 0x20
324 * Bytes 5-7 are not used.
326 * Now come the 6 longs
327 * Bytes 8-11: size of text segments in bytes
328 * Bytes 12-15: size of initialized data segment in bytes
329 * Bytes 16-19: size of bss in bytes
330 * Bytes 20-23: program entry point
331 * Bytes 24-27: total memory allocated to program (text, data + stack)
332 * Bytes 28-31: size of symbol table in bytes
333 * The longs are represented in a machine dependent order,
334 * little-endian on the 8088, big-endian on the 68000.
335 * The header is followed directly by the text and data segments, and the
336 * symbol table (if any). The sizes are given in the header. Only the
337 * text and data segments are copied into memory by exec. The header is
338 * used here only. The symbol table is for the benefit of a debugger and
339 * is ignored here.
341 int r;
343 pos= 0; /* Read from the start of the file */
345 if (exec_len < sizeof(hdr)) return(ENOEXEC);
347 memcpy(&hdr, exec, sizeof(hdr));
349 /* Check magic number, cpu type, and flags. */
350 if (BADMAG(hdr)) return(ENOEXEC);
351 #if (CHIP == INTEL && _WORD_SIZE == 2)
352 if (hdr.a_cpu != A_I8086) return(ENOEXEC);
353 #endif
354 #if (CHIP == INTEL && _WORD_SIZE == 4)
355 if (hdr.a_cpu != A_I80386) return(ENOEXEC);
356 #endif
357 if ((hdr.a_flags & ~(A_NSYM | A_EXEC | A_SEP)) != 0) return(ENOEXEC);
359 *sep_id = !!(hdr.a_flags & A_SEP); /* separate I & D or not */
361 /* Get text and data sizes. */
362 *text_bytes = (vir_bytes) hdr.a_text; /* text size in bytes */
363 *data_bytes = (vir_bytes) hdr.a_data; /* data size in bytes */
364 *bss_bytes = (vir_bytes) hdr.a_bss; /* bss size in bytes */
365 *tot_bytes = hdr.a_total; /* total bytes to allocate for prog */
366 if (*tot_bytes == 0) return(ENOEXEC);
368 if (!*sep_id) {
369 /* If I & D space is not separated, it is all considered data. Text=0*/
370 *data_bytes += *text_bytes;
371 *text_bytes = 0;
373 *pc = hdr.a_entry; /* initial address to start execution */
374 *hdrlenp = hdr.a_hdrlen & BYTE; /* header length */
376 return(OK);
379 /*===========================================================================*
380 * patch_ptr *
381 *===========================================================================*/
382 PRIVATE void patch_ptr(stack, base)
383 char stack[ARG_MAX]; /* pointer to stack image within PM */
384 vir_bytes base; /* virtual address of stack base inside user */
386 /* When doing an exec(name, argv, envp) call, the user builds up a stack
387 * image with arg and env pointers relative to the start of the stack. Now
388 * these pointers must be relocated, since the stack is not positioned at
389 * address 0 in the user's address space.
392 char **ap, flag;
393 vir_bytes v;
395 flag = 0; /* counts number of 0-pointers seen */
396 ap = (char **) stack; /* points initially to 'nargs' */
397 ap++; /* now points to argv[0] */
398 while (flag < 2) {
399 if (ap >= (char **) &stack[ARG_MAX]) return; /* too bad */
400 if (*ap != NULL) {
401 v = (vir_bytes) *ap; /* v is relative pointer */
402 v += base; /* relocate it */
403 *ap = (char *) v; /* put it back */
404 } else {
405 flag++;
407 ap++;
411 /*===========================================================================*
412 * read_seg *
413 *===========================================================================*/
414 PRIVATE int read_seg(exec, exec_len, off, proc_e, seg, seg_bytes)
415 char *exec; /* executable image */
416 size_t exec_len; /* size of the image */
417 off_t off; /* offset in file */
418 int proc_e; /* process number (endpoint) */
419 int seg; /* T, D, or S */
420 phys_bytes seg_bytes; /* how much is to be transferred? */
423 * The byte count on read is usually smaller than the segment count, because
424 * a segment is padded out to a click multiple, and the data segment is only
425 * partially initialized.
428 int r;
429 off_t n, o, b_off, seg_off;
431 if (off+seg_bytes > exec_len) return ENOEXEC;
432 r= sys_vircopy(SELF, D, (vir_bytes)exec+off, proc_e, seg, 0, seg_bytes);
433 return r;