source: branches/minix3-book/servers/pm/exec.c@ 26

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

Importazione sorgenti libro

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