source: trunk/minix/servers/pm/exec.c@ 22

Last change on this file since 22 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 22.4 KB
Line 
1/* This file handles the EXEC system call. It performs the work as follows:
2 * - see if the permissions allow the file to be executed
3 * - read the header and extract the sizes
4 * - fetch the initial args and environment from the user space
5 * - allocate the memory for the new process
6 * - copy the initial stack from PM to the process
7 * - read in the text and data segments and copy to the process
8 * - take care of setuid and setgid bits
9 * - fix up 'mproc' table
10 * - tell kernel about EXEC
11 * - save offset to initial argc (for ps)
12 *
13 * The entry points into this file are:
14 * do_exec: perform the EXEC system call
15 * rw_seg: read or write a segment from or to a file
16 * find_share: find a process whose text segment can be shared
17 */
18
19#include "pm.h"
20#include <sys/stat.h>
21#include <minix/callnr.h>
22#include <minix/endpoint.h>
23#include <minix/com.h>
24#include <a.out.h>
25#include <signal.h>
26#include <string.h>
27#include "mproc.h"
28#include "param.h"
29
30FORWARD _PROTOTYPE( int new_mem, (struct mproc *sh_mp, vir_bytes text_bytes,
31 vir_bytes data_bytes, vir_bytes bss_bytes,
32 vir_bytes stk_bytes, phys_bytes tot_bytes) );
33FORWARD _PROTOTYPE( void patch_ptr, (char stack[ARG_MAX], vir_bytes base) );
34FORWARD _PROTOTYPE( int insert_arg, (char stack[ARG_MAX],
35 vir_bytes *stk_bytes, char *arg, int replace) );
36FORWARD _PROTOTYPE( char *patch_stack, (int fd, char stack[ARG_MAX],
37 vir_bytes *stk_bytes, char *script) );
38FORWARD _PROTOTYPE( int read_header, (int fd, int *ft, vir_bytes *text_bytes,
39 vir_bytes *data_bytes, vir_bytes *bss_bytes,
40 phys_bytes *tot_bytes, long *sym_bytes, vir_clicks sc,
41 vir_bytes *pc) );
42
43#define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
44#define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
45
46/*===========================================================================*
47 * do_exec *
48 *===========================================================================*/
49PUBLIC int do_exec()
50{
51/* Perform the execve(name, argv, envp) call. The user library builds a
52 * complete stack image, including pointers, args, environ, etc. The stack
53 * is copied to a buffer inside PM, and then to the new core image.
54 */
55 register struct mproc *rmp;
56 struct mproc *sh_mp;
57 int m, r, r2, fd, ft, sn;
58 static char mbuf[ARG_MAX]; /* buffer for stack and zeroes */
59 static char name_buf[PATH_MAX]; /* the name of the file to exec */
60 char *new_sp, *name, *basename;
61 vir_bytes src, dst, text_bytes, data_bytes, bss_bytes, stk_bytes, vsp;
62 phys_bytes tot_bytes; /* total space for program, including gap */
63 long sym_bytes;
64 vir_clicks sc;
65 struct stat s_buf[2], *s_p;
66 vir_bytes pc;
67
68 /* Do some validity checks. */
69 rmp = mp;
70 stk_bytes = (vir_bytes) m_in.stack_bytes;
71 if (stk_bytes > ARG_MAX) return(ENOMEM); /* stack too big */
72 if (m_in.exec_len <= 0 || m_in.exec_len > PATH_MAX) return(EINVAL);
73
74 /* Get the exec file name and see if the file is executable. */
75 src = (vir_bytes) m_in.exec_name;
76 dst = (vir_bytes) name_buf;
77 r = sys_datacopy(who_e, (vir_bytes) src,
78 PM_PROC_NR, (vir_bytes) dst, (phys_bytes) m_in.exec_len);
79 if (r != OK) return(r); /* file name not in user data segment */
80
81 /* Fetch the stack from the user before destroying the old core image. */
82 src = (vir_bytes) m_in.stack_ptr;
83 dst = (vir_bytes) mbuf;
84 r = sys_datacopy(who_e, (vir_bytes) src,
85 PM_PROC_NR, (vir_bytes) dst, (phys_bytes)stk_bytes);
86 /* can't fetch stack (e.g. bad virtual addr) */
87 if (r != OK) return(EACCES);
88
89 r = 0; /* r = 0 (first attempt), or 1 (interpreted script) */
90 name = name_buf; /* name of file to exec. */
91 do {
92 s_p = &s_buf[r];
93 tell_fs(CHDIR, who_e, FALSE, 0); /* switch to the user's FS environ */
94 fd = allowed(name, s_p, X_BIT); /* is file executable? */
95 if (fd < 0) return(fd); /* file was not executable */
96
97 /* Read the file header and extract the segment sizes. */
98 sc = (stk_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
99
100 m = read_header(fd, &ft, &text_bytes, &data_bytes, &bss_bytes,
101 &tot_bytes, &sym_bytes, sc, &pc);
102 if (m != ESCRIPT || ++r > 1) break;
103 } while ((name = patch_stack(fd, mbuf, &stk_bytes, name_buf)) != NULL);
104
105 if (m < 0) {
106 close(fd); /* something wrong with header */
107 return(stk_bytes > ARG_MAX ? ENOMEM : ENOEXEC);
108 }
109
110 /* Can the process' text be shared with that of one already running? */
111 sh_mp = find_share(rmp, s_p->st_ino, s_p->st_dev, s_p->st_ctime);
112
113 /* Allocate new memory and release old memory. Fix map and tell kernel. */
114 r = new_mem(sh_mp, text_bytes, data_bytes, bss_bytes, stk_bytes, tot_bytes);
115 if (r != OK) {
116 close(fd); /* insufficient core or program too big */
117 return(r);
118 }
119
120 /* Save file identification to allow it to be shared. */
121 rmp->mp_ino = s_p->st_ino;
122 rmp->mp_dev = s_p->st_dev;
123 rmp->mp_ctime = s_p->st_ctime;
124
125 /* Patch up stack and copy it from PM to new core image. */
126 vsp = (vir_bytes) rmp->mp_seg[S].mem_vir << CLICK_SHIFT;
127 vsp += (vir_bytes) rmp->mp_seg[S].mem_len << CLICK_SHIFT;
128 vsp -= stk_bytes;
129 patch_ptr(mbuf, vsp);
130 src = (vir_bytes) mbuf;
131 r = sys_datacopy(PM_PROC_NR, (vir_bytes) src,
132 who_e, (vir_bytes) vsp, (phys_bytes)stk_bytes);
133 if (r != OK) panic(__FILE__,"do_exec stack copy err on", who_e);
134
135 /* Read in text and data segments. */
136 if (sh_mp != NULL) {
137 lseek(fd, (off_t) text_bytes, SEEK_CUR); /* shared: skip text */
138 } else {
139 rw_seg(0, fd, who_e, T, text_bytes);
140 }
141 rw_seg(0, fd, who_e, D, data_bytes);
142
143 close(fd); /* don't need exec file any more */
144
145 /* Take care of setuid/setgid bits. */
146 if ((rmp->mp_flags & TRACED) == 0) { /* suppress if tracing */
147 if (s_buf[0].st_mode & I_SET_UID_BIT) {
148 rmp->mp_effuid = s_buf[0].st_uid;
149 tell_fs(SETUID, who_e, (int)rmp->mp_realuid, (int)rmp->mp_effuid);
150 }
151 if (s_buf[0].st_mode & I_SET_GID_BIT) {
152 rmp->mp_effgid = s_buf[0].st_gid;
153 tell_fs(SETGID,who_e, (int)rmp->mp_realgid, (int)rmp->mp_effgid);
154 }
155 }
156
157 /* Save offset to initial argc (for ps) */
158 rmp->mp_procargs = vsp;
159
160 /* Fix 'mproc' fields, tell kernel that exec is done, reset caught sigs. */
161 for (sn = 1; sn <= _NSIG; sn++) {
162 if (sigismember(&rmp->mp_catch, sn)) {
163 sigdelset(&rmp->mp_catch, sn);
164 rmp->mp_sigact[sn].sa_handler = SIG_DFL;
165 sigemptyset(&rmp->mp_sigact[sn].sa_mask);
166 }
167 }
168
169 rmp->mp_flags &= ~SEPARATE; /* turn off SEPARATE bit */
170 rmp->mp_flags |= ft; /* turn it on for separate I & D files */
171 new_sp = (char *) vsp;
172
173 tell_fs(EXEC, who_e, 0, 0); /* allow FS to handle FD_CLOEXEC files */
174
175 /* System will save command line for debugging, ps(1) output, etc. */
176 basename = strrchr(name, '/');
177 if (basename == NULL) basename = name; else basename++;
178 strncpy(rmp->mp_name, basename, PROC_NAME_LEN-1);
179 rmp->mp_name[PROC_NAME_LEN] = '\0';
180 if((r2=sys_exec(who_e, new_sp, basename, pc)) != OK) {
181 panic(__FILE__,"sys_exec failed", r2);
182 }
183
184 /* Cause a signal if this process is traced. */
185 if (rmp->mp_flags & TRACED) check_sig(rmp->mp_pid, SIGTRAP);
186
187 return(SUSPEND); /* no reply, new program just runs */
188}
189
190/*===========================================================================*
191 * read_header *
192 *===========================================================================*/
193PRIVATE int read_header(fd, ft, text_bytes, data_bytes, bss_bytes,
194 tot_bytes, sym_bytes, sc, pc)
195int fd; /* file descriptor for reading exec file */
196int *ft; /* place to return ft number */
197vir_bytes *text_bytes; /* place to return text size */
198vir_bytes *data_bytes; /* place to return initialized data size */
199vir_bytes *bss_bytes; /* place to return bss size */
200phys_bytes *tot_bytes; /* place to return total size */
201long *sym_bytes; /* place to return symbol table size */
202vir_clicks sc; /* stack size in clicks */
203vir_bytes *pc; /* program entry point (initial PC) */
204{
205/* Read the header and extract the text, data, bss and total sizes from it. */
206
207 int m, ct;
208 vir_clicks tc, dc, s_vir, dvir;
209 phys_clicks totc;
210 struct exec hdr; /* a.out header is read in here */
211
212 /* Read the header and check the magic number. The standard MINIX header
213 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
214 * Then come 4 more longs that are not used here.
215 * Byte 0: magic number 0x01
216 * Byte 1: magic number 0x03
217 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
218 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
219 * Motorola = 0x0B, Sun SPARC = 0x17
220 * Byte 4: Header length = 0x20
221 * Bytes 5-7 are not used.
222 *
223 * Now come the 6 longs
224 * Bytes 8-11: size of text segments in bytes
225 * Bytes 12-15: size of initialized data segment in bytes
226 * Bytes 16-19: size of bss in bytes
227 * Bytes 20-23: program entry point
228 * Bytes 24-27: total memory allocated to program (text, data + stack)
229 * Bytes 28-31: size of symbol table in bytes
230 * The longs are represented in a machine dependent order,
231 * little-endian on the 8088, big-endian on the 68000.
232 * The header is followed directly by the text and data segments, and the
233 * symbol table (if any). The sizes are given in the header. Only the
234 * text and data segments are copied into memory by exec. The header is
235 * used here only. The symbol table is for the benefit of a debugger and
236 * is ignored here.
237 */
238
239 if ((m= read(fd, &hdr, A_MINHDR)) < 2) return(ENOEXEC);
240
241 /* Interpreted script? */
242 if (((char *) &hdr)[0] == '#' && ((char *) &hdr)[1] == '!') return(ESCRIPT);
243
244 if (m != A_MINHDR) return(ENOEXEC);
245
246 /* Check magic number, cpu type, and flags. */
247 if (BADMAG(hdr)) return(ENOEXEC);
248#if (CHIP == INTEL && _WORD_SIZE == 2)
249 if (hdr.a_cpu != A_I8086) return(ENOEXEC);
250#endif
251#if (CHIP == INTEL && _WORD_SIZE == 4)
252 if (hdr.a_cpu != A_I80386) return(ENOEXEC);
253#endif
254 if ((hdr.a_flags & ~(A_NSYM | A_EXEC | A_SEP)) != 0) return(ENOEXEC);
255
256 *ft = ( (hdr.a_flags & A_SEP) ? SEPARATE : 0); /* separate I & D or not */
257
258 /* Get text and data sizes. */
259 *text_bytes = (vir_bytes) hdr.a_text; /* text size in bytes */
260 *data_bytes = (vir_bytes) hdr.a_data; /* data size in bytes */
261 *bss_bytes = (vir_bytes) hdr.a_bss; /* bss size in bytes */
262 *tot_bytes = hdr.a_total; /* total bytes to allocate for prog */
263 *sym_bytes = hdr.a_syms; /* symbol table size in bytes */
264 if (*tot_bytes == 0) return(ENOEXEC);
265
266 if (*ft != SEPARATE) {
267 /* If I & D space is not separated, it is all considered data. Text=0*/
268 *data_bytes += *text_bytes;
269 *text_bytes = 0;
270 }
271 *pc = hdr.a_entry; /* initial address to start execution */
272
273 /* Check to see if segment sizes are feasible. */
274 tc = ((unsigned long) *text_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
275 dc = (*data_bytes + *bss_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
276 totc = (*tot_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
277 if (dc >= totc) return(ENOEXEC); /* stack must be at least 1 click */
278 dvir = (*ft == SEPARATE ? 0 : tc);
279 s_vir = dvir + (totc - sc);
280#if (CHIP == INTEL && _WORD_SIZE == 2)
281 m = size_ok(*ft, tc, dc, sc, dvir, s_vir);
282#else
283 m = (dvir + dc > s_vir) ? ENOMEM : OK;
284#endif
285 ct = hdr.a_hdrlen & BYTE; /* header length */
286 if (ct > A_MINHDR) lseek(fd, (off_t) ct, SEEK_SET); /* skip unused hdr */
287 return(m);
288}
289
290/*===========================================================================*
291 * new_mem *
292 *===========================================================================*/
293PRIVATE int new_mem(sh_mp, text_bytes, data_bytes,
294 bss_bytes,stk_bytes,tot_bytes)
295struct mproc *sh_mp; /* text can be shared with this process */
296vir_bytes text_bytes; /* text segment size in bytes */
297vir_bytes data_bytes; /* size of initialized data in bytes */
298vir_bytes bss_bytes; /* size of bss in bytes */
299vir_bytes stk_bytes; /* size of initial stack segment in bytes */
300phys_bytes tot_bytes; /* total memory to allocate, including gap */
301{
302/* Allocate new memory and release the old memory. Change the map and report
303 * the new map to the kernel. Zero the new core image's bss, gap and stack.
304 */
305
306 register struct mproc *rmp = mp;
307 vir_clicks text_clicks, data_clicks, gap_clicks, stack_clicks, tot_clicks;
308 phys_clicks new_base;
309 phys_bytes bytes, base, bss_offset;
310 int s, r2;
311
312 /* No need to allocate text if it can be shared. */
313 if (sh_mp != NULL) text_bytes = 0;
314
315 /* Allow the old data to be swapped out to make room. (Which is really a
316 * waste of time, because we are going to throw it away anyway.)
317 */
318 rmp->mp_flags |= WAITING;
319
320 /* Acquire the new memory. Each of the 4 parts: text, (data+bss), gap,
321 * and stack occupies an integral number of clicks, starting at click
322 * boundary. The data and bss parts are run together with no space.
323 */
324 text_clicks = ((unsigned long) text_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
325 data_clicks = (data_bytes + bss_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
326 stack_clicks = (stk_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
327 tot_clicks = (tot_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
328 gap_clicks = tot_clicks - data_clicks - stack_clicks;
329 if ( (int) gap_clicks < 0) return(ENOMEM);
330
331 /* Try to allocate memory for the new process. */
332 new_base = alloc_mem(text_clicks + tot_clicks);
333 if (new_base == NO_MEM) return(ENOMEM);
334
335 /* We've got memory for the new core image. Release the old one. */
336 rmp = mp;
337
338 if (find_share(rmp, rmp->mp_ino, rmp->mp_dev, rmp->mp_ctime) == NULL) {
339 /* No other process shares the text segment, so free it. */
340 free_mem(rmp->mp_seg[T].mem_phys, rmp->mp_seg[T].mem_len);
341 }
342 /* Free the data and stack segments. */
343 free_mem(rmp->mp_seg[D].mem_phys,
344 rmp->mp_seg[S].mem_vir + rmp->mp_seg[S].mem_len - rmp->mp_seg[D].mem_vir);
345
346 /* We have now passed the point of no return. The old core image has been
347 * forever lost, memory for a new core image has been allocated. Set up
348 * and report new map.
349 */
350 if (sh_mp != NULL) {
351 /* Share the text segment. */
352 rmp->mp_seg[T] = sh_mp->mp_seg[T];
353 } else {
354 rmp->mp_seg[T].mem_phys = new_base;
355 rmp->mp_seg[T].mem_vir = 0;
356 rmp->mp_seg[T].mem_len = text_clicks;
357 }
358 rmp->mp_seg[D].mem_phys = new_base + text_clicks;
359 rmp->mp_seg[D].mem_vir = 0;
360 rmp->mp_seg[D].mem_len = data_clicks;
361 rmp->mp_seg[S].mem_phys = rmp->mp_seg[D].mem_phys + data_clicks + gap_clicks;
362 rmp->mp_seg[S].mem_vir = rmp->mp_seg[D].mem_vir + data_clicks + gap_clicks;
363 rmp->mp_seg[S].mem_len = stack_clicks;
364
365#if (CHIP == M68000)
366 rmp->mp_seg[T].mem_vir = 0;
367 rmp->mp_seg[D].mem_vir = rmp->mp_seg[T].mem_len;
368 rmp->mp_seg[S].mem_vir = rmp->mp_seg[D].mem_vir
369 + rmp->mp_seg[D].mem_len + gap_clicks;
370#endif
371
372 if((r2=sys_newmap(who_e, rmp->mp_seg)) != OK) {
373 /* report new map to the kernel */
374 panic(__FILE__,"sys_newmap failed", r2);
375 }
376
377 /* The old memory may have been swapped out, but the new memory is real. */
378 rmp->mp_flags &= ~(WAITING|ONSWAP|SWAPIN);
379
380 /* Zero the bss, gap, and stack segment. */
381 bytes = (phys_bytes)(data_clicks + gap_clicks + stack_clicks) << CLICK_SHIFT;
382 base = (phys_bytes) rmp->mp_seg[D].mem_phys << CLICK_SHIFT;
383 bss_offset = (data_bytes >> CLICK_SHIFT) << CLICK_SHIFT;
384 base += bss_offset;
385 bytes -= bss_offset;
386
387 if ((s=sys_memset(0, base, bytes)) != OK) {
388 panic(__FILE__,"new_mem can't zero", s);
389 }
390
391 return(OK);
392}
393
394/*===========================================================================*
395 * patch_ptr *
396 *===========================================================================*/
397PRIVATE void patch_ptr(stack, base)
398char stack[ARG_MAX]; /* pointer to stack image within PM */
399vir_bytes base; /* virtual address of stack base inside user */
400{
401/* When doing an exec(name, argv, envp) call, the user builds up a stack
402 * image with arg and env pointers relative to the start of the stack. Now
403 * these pointers must be relocated, since the stack is not positioned at
404 * address 0 in the user's address space.
405 */
406
407 char **ap, flag;
408 vir_bytes v;
409
410 flag = 0; /* counts number of 0-pointers seen */
411 ap = (char **) stack; /* points initially to 'nargs' */
412 ap++; /* now points to argv[0] */
413 while (flag < 2) {
414 if (ap >= (char **) &stack[ARG_MAX]) return; /* too bad */
415 if (*ap != NULL) {
416 v = (vir_bytes) *ap; /* v is relative pointer */
417 v += base; /* relocate it */
418 *ap = (char *) v; /* put it back */
419 } else {
420 flag++;
421 }
422 ap++;
423 }
424}
425
426/*===========================================================================*
427 * insert_arg *
428 *===========================================================================*/
429PRIVATE int insert_arg(stack, stk_bytes, arg, replace)
430char stack[ARG_MAX]; /* pointer to stack image within PM */
431vir_bytes *stk_bytes; /* size of initial stack */
432char *arg; /* argument to prepend/replace as new argv[0] */
433int replace;
434{
435/* Patch the stack so that arg will become argv[0]. Be careful, the stack may
436 * be filled with garbage, although it normally looks like this:
437 * nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL
438 * followed by the strings "pointed" to by the argv[i] and the envp[i]. The
439 * pointers are really offsets from the start of stack.
440 * Return true iff the operation succeeded.
441 */
442 int offset, a0, a1, old_bytes = *stk_bytes;
443
444 /* Prepending arg adds at least one string and a zero byte. */
445 offset = strlen(arg) + 1;
446
447 a0 = (int) ((char **) stack)[1]; /* argv[0] */
448 if (a0 < 4 * PTRSIZE || a0 >= old_bytes) return(FALSE);
449
450 a1 = a0; /* a1 will point to the strings to be moved */
451 if (replace) {
452 /* Move a1 to the end of argv[0][] (argv[1] if nargs > 1). */
453 do {
454 if (a1 == old_bytes) return(FALSE);
455 --offset;
456 } while (stack[a1++] != 0);
457 } else {
458 offset += PTRSIZE; /* new argv[0] needs new pointer in argv[] */
459 a0 += PTRSIZE; /* location of new argv[0][]. */
460 }
461
462 /* stack will grow by offset bytes (or shrink by -offset bytes) */
463 if ((*stk_bytes += offset) > ARG_MAX) return(FALSE);
464
465 /* Reposition the strings by offset bytes */
466 memmove(stack + a1 + offset, stack + a1, old_bytes - a1);
467
468 strcpy(stack + a0, arg); /* Put arg in the new space. */
469
470 if (!replace) {
471 /* Make space for a new argv[0]. */
472 memmove(stack + 2 * PTRSIZE, stack + 1 * PTRSIZE, a0 - 2 * PTRSIZE);
473
474 ((char **) stack)[0]++; /* nargs++; */
475 }
476 /* Now patch up argv[] and envp[] by offset. */
477 patch_ptr(stack, (vir_bytes) offset);
478 ((char **) stack)[1] = (char *) a0; /* set argv[0] correctly */
479 return(TRUE);
480}
481
482/*===========================================================================*
483 * patch_stack *
484 *===========================================================================*/
485PRIVATE char *patch_stack(fd, stack, stk_bytes, script)
486int fd; /* file descriptor to open script file */
487char stack[ARG_MAX]; /* pointer to stack image within PM */
488vir_bytes *stk_bytes; /* size of initial stack */
489char *script; /* name of script to interpret */
490{
491/* Patch the argument vector to include the path name of the script to be
492 * interpreted, and all strings on the #! line. Returns the path name of
493 * the interpreter.
494 */
495 char *sp, *interp = NULL;
496 int n;
497 enum { INSERT=FALSE, REPLACE=TRUE };
498
499 /* Make script[] the new argv[0]. */
500 if (!insert_arg(stack, stk_bytes, script, REPLACE)) return(NULL);
501
502 if (lseek(fd, 2L, 0) == -1 /* just behind the #! */
503 || (n= read(fd, script, PATH_MAX)) < 0 /* read line one */
504 || (sp= memchr(script, '\n', n)) == NULL) /* must be a proper line */
505 return(NULL);
506
507 /* Move sp backwards through script[], prepending each string to stack. */
508 for (;;) {
509 /* skip spaces behind argument. */
510 while (sp > script && (*--sp == ' ' || *sp == '\t')) {}
511 if (sp == script) break;
512
513 sp[1] = 0;
514 /* Move to the start of the argument. */
515 while (sp > script && sp[-1] != ' ' && sp[-1] != '\t') --sp;
516
517 interp = sp;
518 if (!insert_arg(stack, stk_bytes, sp, INSERT)) return(NULL);
519 }
520
521 /* Round *stk_bytes up to the size of a pointer for alignment contraints. */
522 *stk_bytes= ((*stk_bytes + PTRSIZE - 1) / PTRSIZE) * PTRSIZE;
523
524 close(fd);
525 return(interp);
526}
527
528/*===========================================================================*
529 * rw_seg *
530 *===========================================================================*/
531PUBLIC void rw_seg(rw, fd, proc_e, seg, seg_bytes0)
532int rw; /* 0 = read, 1 = write */
533int fd; /* file descriptor to read from / write to */
534int proc_e; /* process number (endpoint) */
535int seg; /* T, D, or S */
536phys_bytes seg_bytes0; /* how much is to be transferred? */
537{
538/* Transfer text or data from/to a file and copy to/from a process segment.
539 * This procedure is a little bit tricky. The logical way to transfer a
540 * segment would be block by block and copying each block to/from the user
541 * space one at a time. This is too slow, so we do something dirty here,
542 * namely send the user space and virtual address to the file system in the
543 * upper 10 bits of the file descriptor, and pass it the user virtual address
544 * instead of a PM address. The file system extracts these parameters when
545 * gets a read or write call from the process manager, which is the only
546 * process that is permitted to use this trick. The file system then copies
547 * the whole segment directly to/from user space, bypassing PM completely.
548 *
549 * The byte count on read is usually smaller than the segment count, because
550 * a segment is padded out to a click multiple, and the data segment is only
551 * partially initialized.
552 */
553
554 int bytes, r, proc_n;
555 char *ubuf_ptr;
556 struct mem_map *sp;
557 phys_bytes seg_bytes = seg_bytes0;
558
559 if(pm_isokendpt(proc_e, &proc_n) != OK || proc_n < 0)
560 return;
561
562 sp = &mproc[proc_n].mp_seg[seg];
563
564 ubuf_ptr = (char *) ((vir_bytes) sp->mem_vir << CLICK_SHIFT);
565
566 while (seg_bytes != 0) {
567#define PM_CHUNK_SIZE 8192
568 bytes = MIN((INT_MAX / PM_CHUNK_SIZE) * PM_CHUNK_SIZE, seg_bytes);
569 if(!rw) {
570 r = _read_pm(fd, ubuf_ptr, bytes, seg, proc_e);
571 } else {
572 r = _write_pm(fd, ubuf_ptr, bytes, seg, proc_e);
573 }
574 if (r != bytes) break;
575 ubuf_ptr += bytes;
576 seg_bytes -= bytes;
577 }
578}
579
580/*===========================================================================*
581 * find_share *
582 *===========================================================================*/
583PUBLIC struct mproc *find_share(mp_ign, ino, dev, ctime)
584struct mproc *mp_ign; /* process that should not be looked at */
585ino_t ino; /* parameters that uniquely identify a file */
586dev_t dev;
587time_t ctime;
588{
589/* Look for a process that is the file <ino, dev, ctime> in execution. Don't
590 * accidentally "find" mp_ign, because it is the process on whose behalf this
591 * call is made.
592 */
593 struct mproc *sh_mp;
594 for (sh_mp = &mproc[0]; sh_mp < &mproc[NR_PROCS]; sh_mp++) {
595
596 if (!(sh_mp->mp_flags & SEPARATE)) continue;
597 if (sh_mp == mp_ign) continue;
598 if (sh_mp->mp_ino != ino) continue;
599 if (sh_mp->mp_dev != dev) continue;
600 if (sh_mp->mp_ctime != ctime) continue;
601 return sh_mp;
602 }
603 return(NULL);
604}
Note: See TracBrowser for help on using the repository browser.