xref: /minix3/minix/servers/vfs/misc.c (revision 424cad2cd6b21072ac918889f0901f3920356e25)
1 /* This file contains a collection of miscellaneous procedures.  Some of them
2  * perform simple system calls.  Some others do a little part of system calls
3  * that are mostly performed by the Memory Manager.
4  *
5  * The entry points into this file are
6  *   do_fcntl:	  perform the FCNTL system call
7  *   do_sync:	  perform the SYNC system call
8  *   do_fsync:	  perform the FSYNC system call
9  *   pm_setsid:	  perform VFS's side of setsid system call
10  *   pm_reboot:	  sync disks and prepare for shutdown
11  *   pm_fork:	  adjust the tables after PM has performed a FORK system call
12  *   do_exec:	  handle files with FD_CLOEXEC on after PM has done an EXEC
13  *   do_exit:	  a process has exited; note that in the tables
14  *   do_set:	  set uid or gid for some process
15  *   do_revive:	  revive a process that was waiting for something (e.g. TTY)
16  *   do_svrctl:	  file system control
17  *   do_getsysinfo:	request copy of FS data structure
18  *   pm_dumpcore: create a core dump
19  */
20 
21 #include "fs.h"
22 #include <fcntl.h>
23 #include <assert.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <minix/callnr.h>
27 #include <minix/safecopies.h>
28 #include <minix/endpoint.h>
29 #include <minix/com.h>
30 #include <minix/sysinfo.h>
31 #include <minix/u64.h>
32 #include <sys/ptrace.h>
33 #include <sys/svrctl.h>
34 #include <sys/resource.h>
35 #include "file.h"
36 #include "scratchpad.h"
37 #include <minix/vfsif.h>
38 #include "vnode.h"
39 #include "vmnt.h"
40 
41 #define CORE_NAME	"core"
42 #define CORE_MODE	0777	/* mode to use on core image files */
43 
44 #if ENABLE_SYSCALL_STATS
45 unsigned long calls_stats[NR_VFS_CALLS];
46 #endif
47 
48 static void free_proc(int flags);
49 
50 /*===========================================================================*
51  *				do_getsysinfo				     *
52  *===========================================================================*/
53 int do_getsysinfo(void)
54 {
55   vir_bytes src_addr, dst_addr;
56   size_t len, buf_size;
57   int what;
58 
59   what = job_m_in.m_lsys_getsysinfo.what;
60   dst_addr = job_m_in.m_lsys_getsysinfo.where;
61   buf_size = job_m_in.m_lsys_getsysinfo.size;
62 
63   /* Only su may call do_getsysinfo. This call may leak information (and is not
64    * stable enough to be part of the API/ABI). In the future, requests from
65    * non-system processes should be denied.
66    */
67 
68   if (!super_user) return(EPERM);
69 
70   switch(what) {
71     case SI_PROC_TAB:
72 	src_addr = (vir_bytes) fproc;
73 	len = sizeof(struct fproc) * NR_PROCS;
74 	break;
75     case SI_DMAP_TAB:
76 	src_addr = (vir_bytes) dmap;
77 	len = sizeof(struct dmap) * NR_DEVICES;
78 	break;
79 #if ENABLE_SYSCALL_STATS
80     case SI_CALL_STATS:
81 	src_addr = (vir_bytes) calls_stats;
82 	len = sizeof(calls_stats);
83 	break;
84 #endif
85     default:
86 	return(EINVAL);
87   }
88 
89   if (len != buf_size)
90 	return(EINVAL);
91 
92   return sys_datacopy_wrapper(SELF, src_addr, who_e, dst_addr, len);
93 }
94 
95 /*===========================================================================*
96  *				do_fcntl				     *
97  *===========================================================================*/
98 int do_fcntl(void)
99 {
100 /* Perform the fcntl(fd, cmd, ...) system call. */
101 
102   register struct filp *f;
103   int new_fd, fl, r = OK, fcntl_req, fcntl_argx;
104   tll_access_t locktype;
105 
106   scratch(fp).file.fd_nr = job_m_in.m_lc_vfs_fcntl.fd;
107   scratch(fp).io.io_buffer = job_m_in.m_lc_vfs_fcntl.arg_ptr;
108   scratch(fp).io.io_nbytes = job_m_in.m_lc_vfs_fcntl.cmd;
109   fcntl_req = job_m_in.m_lc_vfs_fcntl.cmd;
110   fcntl_argx = job_m_in.m_lc_vfs_fcntl.arg_int;
111 
112   /* Is the file descriptor valid? */
113   locktype = (fcntl_req == F_FREESP) ? VNODE_WRITE : VNODE_READ;
114   if ((f = get_filp(scratch(fp).file.fd_nr, locktype)) == NULL)
115 	return(err_code);
116 
117   switch (fcntl_req) {
118     case F_DUPFD:
119     case F_DUPFD_CLOEXEC:
120 	/* This replaces the old dup() system call. */
121 	if (fcntl_argx < 0 || fcntl_argx >= OPEN_MAX) r = EINVAL;
122 	else if ((r = get_fd(fp, fcntl_argx, 0, &new_fd, NULL)) == OK) {
123 		f->filp_count++;
124 		fp->fp_filp[new_fd] = f;
125 		assert(!FD_ISSET(new_fd, &fp->fp_cloexec_set));
126 		if (fcntl_req == F_DUPFD_CLOEXEC)
127 			FD_SET(new_fd, &fp->fp_cloexec_set);
128 		r = new_fd;
129 	}
130 	break;
131 
132     case F_GETFD:
133 	/* Get close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
134 	r = 0;
135 	if (FD_ISSET(scratch(fp).file.fd_nr, &fp->fp_cloexec_set))
136 		r = FD_CLOEXEC;
137 	break;
138 
139     case F_SETFD:
140 	/* Set close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
141 	if (fcntl_argx & FD_CLOEXEC)
142 		FD_SET(scratch(fp).file.fd_nr, &fp->fp_cloexec_set);
143 	else
144 		FD_CLR(scratch(fp).file.fd_nr, &fp->fp_cloexec_set);
145 	break;
146 
147     case F_GETFL:
148 	/* Get file status flags (O_NONBLOCK and O_APPEND). */
149 	fl = f->filp_flags & (O_NONBLOCK | O_APPEND | O_ACCMODE);
150 	r = fl;
151 	break;
152 
153     case F_SETFL:
154 	/* Set file status flags (O_NONBLOCK and O_APPEND). */
155 	fl = O_NONBLOCK | O_APPEND;
156 	f->filp_flags = (f->filp_flags & ~fl) | (fcntl_argx & fl);
157 	break;
158 
159     case F_GETLK:
160     case F_SETLK:
161     case F_SETLKW:
162 	/* Set or clear a file lock. */
163 	r = lock_op(f, fcntl_req);
164 	break;
165 
166     case F_FREESP:
167      {
168 	/* Free a section of a file */
169 	off_t start, end, offset;
170 	struct flock flock_arg;
171 
172 	/* Check if it's a regular file. */
173 	if (!S_ISREG(f->filp_vno->v_mode)) r = EINVAL;
174 	else if (!(f->filp_mode & W_BIT)) r = EBADF;
175 	else {
176 		/* Copy flock data from userspace. */
177 		r = sys_datacopy_wrapper(who_e, scratch(fp).io.io_buffer,
178 			SELF, (vir_bytes) &flock_arg, sizeof(flock_arg));
179 	}
180 
181 	if (r != OK) break;
182 
183 	/* Convert starting offset to signed. */
184 	offset = (off_t) flock_arg.l_start;
185 
186 	/* Figure out starting position base. */
187 	switch(flock_arg.l_whence) {
188 	  case SEEK_SET: start = 0; break;
189 	  case SEEK_CUR: start = f->filp_pos; break;
190 	  case SEEK_END: start = f->filp_vno->v_size; break;
191 	  default: r = EINVAL;
192 	}
193 	if (r != OK) break;
194 
195 	/* Check for overflow or underflow. */
196 	if (offset > 0 && start + offset < start) r = EINVAL;
197 	else if (offset < 0 && start + offset > start) r = EINVAL;
198 	else {
199 		start += offset;
200 		if (start < 0) r = EINVAL;
201 	}
202 	if (r != OK) break;
203 
204 	if (flock_arg.l_len != 0) {
205 		if (start >= f->filp_vno->v_size) r = EINVAL;
206 		else if ((end = start + flock_arg.l_len) <= start) r = EINVAL;
207 		else if (end > f->filp_vno->v_size) end = f->filp_vno->v_size;
208 	} else {
209                 end = 0;
210 	}
211 	if (r != OK) break;
212 
213 	r = req_ftrunc(f->filp_vno->v_fs_e, f->filp_vno->v_inode_nr,start,end);
214 
215 	if (r == OK && flock_arg.l_len == 0)
216 		f->filp_vno->v_size = start;
217 
218 	break;
219      }
220     case F_GETNOSIGPIPE:
221 	r = !!(f->filp_flags & O_NOSIGPIPE);
222 	break;
223     case F_SETNOSIGPIPE:
224 	if (fcntl_argx)
225 		f->filp_flags |= O_NOSIGPIPE;
226 	else
227 		f->filp_flags &= ~O_NOSIGPIPE;
228 	break;
229     case F_FLUSH_FS_CACHE:
230     {
231 	struct vnode *vn = f->filp_vno;
232 	mode_t mode = f->filp_vno->v_mode;
233 	if (!super_user) {
234 		r = EPERM;
235 	} else if (S_ISBLK(mode)) {
236 		/* Block device; flush corresponding device blocks. */
237 		r = req_flush(vn->v_bfs_e, vn->v_sdev);
238 	} else if (S_ISREG(mode) || S_ISDIR(mode)) {
239 		/* Directory or regular file; flush hosting FS blocks. */
240 		r = req_flush(vn->v_fs_e, vn->v_dev);
241 	} else {
242 		/* Remaining cases.. Meaning unclear. */
243 		r = ENODEV;
244 	}
245 	break;
246     }
247     default:
248 	r = EINVAL;
249   }
250 
251   unlock_filp(f);
252   return(r);
253 }
254 
255 /*===========================================================================*
256  *				do_sync					     *
257  *===========================================================================*/
258 int do_sync(void)
259 {
260   struct vmnt *vmp;
261   int r = OK;
262 
263   for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
264 	if ((r = lock_vmnt(vmp, VMNT_READ)) != OK)
265 		break;
266 	if (vmp->m_dev != NO_DEV && vmp->m_fs_e != NONE &&
267 		 vmp->m_root_node != NULL) {
268 		req_sync(vmp->m_fs_e);
269 	}
270 	unlock_vmnt(vmp);
271   }
272 
273   return(r);
274 }
275 
276 /*===========================================================================*
277  *				do_fsync				     *
278  *===========================================================================*/
279 int do_fsync(void)
280 {
281 /* Perform the fsync() system call. */
282   struct filp *rfilp;
283   struct vmnt *vmp;
284   dev_t dev;
285   int r = OK;
286 
287   scratch(fp).file.fd_nr = job_m_in.m_lc_vfs_fsync.fd;
288 
289   if ((rfilp = get_filp(scratch(fp).file.fd_nr, VNODE_READ)) == NULL)
290 	return(err_code);
291 
292   dev = rfilp->filp_vno->v_dev;
293   unlock_filp(rfilp);
294 
295   for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
296 	if (vmp->m_dev != dev) continue;
297 	if ((r = lock_vmnt(vmp, VMNT_READ)) != OK)
298 		break;
299 	if (vmp->m_dev != NO_DEV && vmp->m_dev == dev &&
300 		vmp->m_fs_e != NONE && vmp->m_root_node != NULL) {
301 
302 		req_sync(vmp->m_fs_e);
303 	}
304 	unlock_vmnt(vmp);
305   }
306 
307   return(r);
308 }
309 
310 int dupvm(struct fproc *rfp, int pfd, int *vmfd, struct filp **newfilp)
311 {
312 	int result, procfd;
313 	struct filp *f = NULL;
314 	struct fproc *vmf = fproc_addr(VM_PROC_NR);
315 
316 	*newfilp = NULL;
317 
318 	if ((f = get_filp2(rfp, pfd, VNODE_READ)) == NULL) {
319 		printf("VFS dupvm: get_filp2 failed\n");
320 		return EBADF;
321 	}
322 
323 	if(!(f->filp_vno->v_vmnt->m_fs_flags & RES_HASPEEK)) {
324 		unlock_filp(f);
325 #if 0	/* Noisy diagnostic for mmap() by ld.so */
326 		printf("VFS dupvm: no peek available\n");
327 #endif
328 		return EINVAL;
329 	}
330 
331 	assert(f->filp_vno);
332 	assert(f->filp_vno->v_vmnt);
333 
334 	if (!S_ISREG(f->filp_vno->v_mode) && !S_ISBLK(f->filp_vno->v_mode)) {
335 		printf("VFS: mmap regular/blockdev only; dev 0x%llx ino %llu has mode 0%o\n",
336 			f->filp_vno->v_dev, f->filp_vno->v_inode_nr, f->filp_vno->v_mode);
337 		unlock_filp(f);
338 		return EINVAL;
339 	}
340 
341 	/* get free FD in VM */
342 	if((result=get_fd(vmf, 0, 0, &procfd, NULL)) != OK) {
343 		unlock_filp(f);
344 		printf("VFS dupvm: getfd failed\n");
345 		return result;
346 	}
347 
348 	*vmfd = procfd;
349 
350 	f->filp_count++;
351 	assert(f->filp_count > 0);
352 	vmf->fp_filp[procfd] = f;
353 
354 	*newfilp = f;
355 
356 	return OK;
357 }
358 
359 /*===========================================================================*
360  *				do_vm_call				     *
361  *===========================================================================*/
362 int do_vm_call(void)
363 {
364 /* A call that VM does to VFS.
365  * We must reply with the fixed type VM_VFS_REPLY (and put our result info
366  * in the rest of the message) so VM can tell the difference between a
367  * request from VFS and a reply to this call.
368  */
369 	int req = job_m_in.VFS_VMCALL_REQ;
370 	int req_fd = job_m_in.VFS_VMCALL_FD;
371 	u32_t req_id = job_m_in.VFS_VMCALL_REQID;
372 	endpoint_t ep = job_m_in.VFS_VMCALL_ENDPOINT;
373 	u64_t offset = job_m_in.VFS_VMCALL_OFFSET;
374 	u32_t length = job_m_in.VFS_VMCALL_LENGTH;
375 	int result = OK;
376 	int slot;
377 	struct fproc *rfp, *vmf;
378 	struct filp *f = NULL;
379 	int r;
380 
381 	if(job_m_in.m_source != VM_PROC_NR)
382 		return ENOSYS;
383 
384 	if(isokendpt(ep, &slot) != OK) rfp = NULL;
385 	else rfp = &fproc[slot];
386 
387 	vmf = fproc_addr(VM_PROC_NR);
388 	assert(fp == vmf);
389 	assert(rfp != vmf);
390 
391 	switch(req) {
392 		case VMVFSREQ_FDLOOKUP:
393 		{
394 			int procfd;
395 
396 			/* Lookup fd in referenced process. */
397 
398 			if(!rfp) {
399 				printf("VFS: why isn't ep %d here?!\n", ep);
400 				result = ESRCH;
401 				goto reqdone;
402 			}
403 
404 			if((result = dupvm(rfp, req_fd, &procfd, &f)) != OK) {
405 #if 0   /* Noisy diagnostic for mmap() by ld.so */
406 				printf("vfs: dupvm failed\n");
407 #endif
408 				goto reqdone;
409 			}
410 
411 			if(S_ISBLK(f->filp_vno->v_mode)) {
412 				assert(f->filp_vno->v_sdev != NO_DEV);
413 				job_m_out.VMV_DEV = f->filp_vno->v_sdev;
414 				job_m_out.VMV_INO = VMC_NO_INODE;
415 				job_m_out.VMV_SIZE_PAGES = LONG_MAX;
416 			} else {
417 				job_m_out.VMV_DEV = f->filp_vno->v_dev;
418 				job_m_out.VMV_INO = f->filp_vno->v_inode_nr;
419 				job_m_out.VMV_SIZE_PAGES =
420 					roundup(f->filp_vno->v_size,
421 						PAGE_SIZE)/PAGE_SIZE;
422 			}
423 
424 			job_m_out.VMV_FD = procfd;
425 
426 			result = OK;
427 
428 			break;
429 		}
430 		case VMVFSREQ_FDCLOSE:
431 		{
432 			result = close_fd(fp, req_fd);
433 			if(result != OK) {
434 				printf("VFS: VM fd close for fd %d, %d (%d)\n",
435 					req_fd, fp->fp_endpoint, result);
436 			}
437 			break;
438 		}
439 		case VMVFSREQ_FDIO:
440 		{
441 			result = actual_lseek(fp, req_fd, SEEK_SET, offset,
442 				NULL);
443 
444 			if(result == OK) {
445 				result = actual_read_write_peek(fp, PEEKING,
446 					req_fd, /* vir_bytes */ 0, length);
447 			}
448 
449 			break;
450 		}
451 		default:
452 			panic("VFS: bad request code from VM\n");
453 			break;
454 	}
455 
456 reqdone:
457 	if(f)
458 		unlock_filp(f);
459 
460 	/* fp is VM still. */
461 	assert(fp == vmf);
462 	job_m_out.VMV_ENDPOINT = ep;
463 	job_m_out.VMV_RESULT = result;
464 	job_m_out.VMV_REQID = req_id;
465 
466 	/* Reply asynchronously as VM may not be able to receive
467 	 * an ipc_sendnb() message.
468 	 */
469 	job_m_out.m_type = VM_VFS_REPLY;
470 	r = asynsend3(VM_PROC_NR, &job_m_out, 0);
471 	if(r != OK) printf("VFS: couldn't asynsend3() to VM\n");
472 
473 	/* VFS does not reply any further */
474 	return SUSPEND;
475 }
476 
477 /*===========================================================================*
478  *				pm_reboot				     *
479  *===========================================================================*/
480 void pm_reboot()
481 {
482 /* Perform the VFS side of the reboot call. This call is performed from the PM
483  * process context.
484  */
485   message m_out;
486   int i, r;
487   struct fproc *rfp, *pmfp;
488 
489   pmfp = fp;
490 
491   do_sync();
492 
493   /* Do exit processing for all leftover processes and servers, but don't
494    * actually exit them (if they were really gone, PM will tell us about it).
495    * Skip processes that handle parts of the file system; we first need to give
496    * them the chance to unmount (which should be possible as all normal
497    * processes have no open files anymore).
498    */
499   /* This is the only place where we allow special modification of "fp". The
500    * reboot procedure should really be implemented as a PM message broadcasted
501    * to all processes, so that each process will be shut down cleanly by a
502    * thread operating on its behalf. Doing everything here is simpler, but it
503    * requires an exception to the strict model of having "fp" be the process
504    * that owns the current worker thread.
505    */
506   for (i = 0; i < NR_PROCS; i++) {
507 	rfp = &fproc[i];
508 
509 	/* Don't just free the proc right away, but let it finish what it was
510 	 * doing first */
511 	if (rfp != fp) lock_proc(rfp);
512 	if (rfp->fp_endpoint != NONE && find_vmnt(rfp->fp_endpoint) == NULL) {
513 		worker_set_proc(rfp);	/* temporarily fake process context */
514 		free_proc(0);
515 		worker_set_proc(pmfp);	/* restore original process context */
516 	}
517 	if (rfp != fp) unlock_proc(rfp);
518   }
519 
520   do_sync();
521   unmount_all(0 /* Don't force */);
522 
523   /* Try to exit all processes again including File Servers */
524   for (i = 0; i < NR_PROCS; i++) {
525 	rfp = &fproc[i];
526 
527 	/* Don't just free the proc right away, but let it finish what it was
528 	 * doing first */
529 	if (rfp != fp) lock_proc(rfp);
530 	if (rfp->fp_endpoint != NONE) {
531 		worker_set_proc(rfp);	/* temporarily fake process context */
532 		free_proc(0);
533 		worker_set_proc(pmfp);	/* restore original process context */
534 	}
535 	if (rfp != fp) unlock_proc(rfp);
536   }
537 
538   do_sync();
539   unmount_all(1 /* Force */);
540 
541   /* Reply to PM for synchronization */
542   memset(&m_out, 0, sizeof(m_out));
543 
544   m_out.m_type = VFS_PM_REBOOT_REPLY;
545 
546   if ((r = ipc_send(PM_PROC_NR, &m_out)) != OK)
547 	panic("pm_reboot: ipc_send failed: %d", r);
548 }
549 
550 /*===========================================================================*
551  *				pm_fork					     *
552  *===========================================================================*/
553 void pm_fork(endpoint_t pproc, endpoint_t cproc, pid_t cpid)
554 {
555 /* Perform those aspects of the fork() system call that relate to files.
556  * In particular, let the child inherit its parent's file descriptors.
557  * The parent and child parameters tell who forked off whom. The file
558  * system uses the same slot numbers as the kernel.  Only PM makes this call.
559  */
560 
561   struct fproc *cp, *pp;
562   int i, parentno, childno;
563   mutex_t c_fp_lock;
564 
565   /* Check up-to-dateness of fproc. */
566   okendpt(pproc, &parentno);
567 
568   /* PM gives child endpoint, which implies process slot information.
569    * Don't call isokendpt, because that will verify if the endpoint
570    * number is correct in fproc, which it won't be.
571    */
572   childno = _ENDPOINT_P(cproc);
573   if (childno < 0 || childno >= NR_PROCS)
574 	panic("VFS: bogus child for forking: %d", cproc);
575   if (fproc[childno].fp_pid != PID_FREE)
576 	panic("VFS: forking on top of in-use child: %d", childno);
577 
578   /* Copy the parent's fproc struct to the child. */
579   /* However, the mutex variables belong to a slot and must stay the same. */
580   c_fp_lock = fproc[childno].fp_lock;
581   fproc[childno] = fproc[parentno];
582   fproc[childno].fp_lock = c_fp_lock;
583 
584   /* Increase the counters in the 'filp' table. */
585   cp = &fproc[childno];
586   pp = &fproc[parentno];
587 
588   for (i = 0; i < OPEN_MAX; i++)
589 	if (cp->fp_filp[i] != NULL) cp->fp_filp[i]->filp_count++;
590 
591   /* Fill in new process and endpoint id. */
592   cp->fp_pid = cpid;
593   cp->fp_endpoint = cproc;
594 
595   /* A forking process never has an outstanding grant, as it isn't blocking on
596    * I/O. */
597   if (GRANT_VALID(pp->fp_grant)) {
598 	panic("VFS: fork: pp (endpoint %d) has grant %d\n", pp->fp_endpoint,
599 	       pp->fp_grant);
600   }
601   if (GRANT_VALID(cp->fp_grant)) {
602 	panic("VFS: fork: cp (endpoint %d) has grant %d\n", cp->fp_endpoint,
603 	       cp->fp_grant);
604   }
605 
606   /* A child is not a process leader, not being revived, etc. */
607   cp->fp_flags = FP_NOFLAGS;
608 
609   /* Record the fact that both root and working dir have another user. */
610   if (cp->fp_rd) dup_vnode(cp->fp_rd);
611   if (cp->fp_wd) dup_vnode(cp->fp_wd);
612 }
613 
614 /*===========================================================================*
615  *				free_proc				     *
616  *===========================================================================*/
617 static void free_proc(int flags)
618 {
619   int i;
620   register struct fproc *rfp;
621   register struct filp *rfilp;
622   register struct vnode *vp;
623   dev_t dev;
624 
625   if (fp->fp_endpoint == NONE)
626 	panic("free_proc: already free");
627 
628   if (fp_is_blocked(fp))
629 	unpause();
630 
631   /* Loop on file descriptors, closing any that are open. */
632   for (i = 0; i < OPEN_MAX; i++) {
633 	(void) close_fd(fp, i);
634   }
635 
636   /* Release root and working directories. */
637   if (fp->fp_rd) { put_vnode(fp->fp_rd); fp->fp_rd = NULL; }
638   if (fp->fp_wd) { put_vnode(fp->fp_wd); fp->fp_wd = NULL; }
639 
640   /* The rest of these actions is only done when processes actually exit. */
641   if (!(flags & FP_EXITING)) return;
642 
643   fp->fp_flags |= FP_EXITING;
644 
645   /* Check if any process is SUSPENDed on this driver.
646    * If a driver exits, unmap its entries in the dmap table.
647    * (unmapping has to be done after the first step, because the
648    * dmap table is used in the first step.)
649    */
650   unsuspend_by_endpt(fp->fp_endpoint);
651   dmap_unmap_by_endpt(fp->fp_endpoint);
652 
653   worker_stop_by_endpt(fp->fp_endpoint); /* Unblock waiting threads */
654   vmnt_unmap_by_endpt(fp->fp_endpoint); /* Invalidate open files if this
655 					     * was an active FS */
656 
657   /* If a session leader exits and it has a controlling tty, then revoke
658    * access to its controlling tty from all other processes using it.
659    */
660   if ((fp->fp_flags & FP_SESLDR) && fp->fp_tty != 0) {
661       dev = fp->fp_tty;
662       for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
663 	  if(rfp->fp_pid == PID_FREE) continue;
664           if (rfp->fp_tty == dev) rfp->fp_tty = 0;
665 
666           for (i = 0; i < OPEN_MAX; i++) {
667 		if ((rfilp = rfp->fp_filp[i]) == NULL) continue;
668 		if (rfilp->filp_mode == FILP_CLOSED) continue;
669 		vp = rfilp->filp_vno;
670 		if (!S_ISCHR(vp->v_mode)) continue;
671 		if (vp->v_sdev != dev) continue;
672 		lock_filp(rfilp, VNODE_READ);
673 		(void) cdev_close(dev); /* Ignore any errors. */
674 		/* FIXME: missing select check */
675 		rfilp->filp_mode = FILP_CLOSED;
676 		unlock_filp(rfilp);
677           }
678       }
679   }
680 
681   /* Exit done. Mark slot as free. */
682   fp->fp_endpoint = NONE;
683   fp->fp_pid = PID_FREE;
684   fp->fp_flags = FP_NOFLAGS;
685 }
686 
687 /*===========================================================================*
688  *				pm_exit					     *
689  *===========================================================================*/
690 void pm_exit(void)
691 {
692 /* Perform the file system portion of the exit(status) system call.
693  * This function is called from the context of the exiting process.
694  */
695 
696   free_proc(FP_EXITING);
697 }
698 
699 /*===========================================================================*
700  *				pm_setgid				     *
701  *===========================================================================*/
702 void pm_setgid(proc_e, egid, rgid)
703 endpoint_t proc_e;
704 int egid;
705 int rgid;
706 {
707   register struct fproc *tfp;
708   int slot;
709 
710   okendpt(proc_e, &slot);
711   tfp = &fproc[slot];
712 
713   tfp->fp_effgid =  egid;
714   tfp->fp_realgid = rgid;
715 }
716 
717 
718 /*===========================================================================*
719  *				pm_setgroups				     *
720  *===========================================================================*/
721 void pm_setgroups(proc_e, ngroups, groups)
722 endpoint_t proc_e;
723 int ngroups;
724 gid_t *groups;
725 {
726   struct fproc *rfp;
727   int slot;
728 
729   okendpt(proc_e, &slot);
730   rfp = &fproc[slot];
731   if (ngroups * sizeof(gid_t) > sizeof(rfp->fp_sgroups))
732 	panic("VFS: pm_setgroups: too much data to copy");
733   if (sys_datacopy_wrapper(who_e, (vir_bytes) groups, SELF, (vir_bytes) rfp->fp_sgroups,
734 		   ngroups * sizeof(gid_t)) == OK) {
735 	rfp->fp_ngroups = ngroups;
736   } else
737 	panic("VFS: pm_setgroups: datacopy failed");
738 }
739 
740 
741 /*===========================================================================*
742  *				pm_setuid				     *
743  *===========================================================================*/
744 void pm_setuid(proc_e, euid, ruid)
745 endpoint_t proc_e;
746 int euid;
747 int ruid;
748 {
749   struct fproc *tfp;
750   int slot;
751 
752   okendpt(proc_e, &slot);
753   tfp = &fproc[slot];
754 
755   tfp->fp_effuid =  euid;
756   tfp->fp_realuid = ruid;
757 }
758 
759 /*===========================================================================*
760  *				pm_setsid				     *
761  *===========================================================================*/
762 void pm_setsid(endpoint_t proc_e)
763 {
764 /* Perform the VFS side of the SETSID call, i.e. get rid of the controlling
765  * terminal of a process, and make the process a session leader.
766  */
767   struct fproc *rfp;
768   int slot;
769 
770   /* Make the process a session leader with no controlling tty. */
771   okendpt(proc_e, &slot);
772   rfp = &fproc[slot];
773   rfp->fp_flags |= FP_SESLDR;
774   rfp->fp_tty = 0;
775 }
776 
777 /*===========================================================================*
778  *				do_svrctl				     *
779  *===========================================================================*/
780 int do_svrctl(void)
781 {
782   unsigned long svrctl;
783   vir_bytes ptr;
784 
785   svrctl = job_m_in.m_lc_svrctl.request;
786   ptr = job_m_in.m_lc_svrctl.arg;
787 
788   if (IOCGROUP(svrctl) != 'F') return(EINVAL);
789 
790   switch (svrctl) {
791     case VFSSETPARAM:
792     case VFSGETPARAM:
793 	{
794 		struct sysgetenv sysgetenv;
795 		char search_key[64];
796 		char val[64];
797 		int r, s;
798 
799 		/* Copy sysgetenv structure to VFS */
800 		if (sys_datacopy_wrapper(who_e, ptr, SELF, (vir_bytes) &sysgetenv,
801 				 sizeof(sysgetenv)) != OK)
802 			return(EFAULT);
803 
804 		/* Basic sanity checking */
805 		if (svrctl == VFSSETPARAM) {
806 			if (sysgetenv.keylen <= 0 ||
807 			    sysgetenv.keylen > (sizeof(search_key) - 1) ||
808 			    sysgetenv.vallen <= 0 ||
809 			    sysgetenv.vallen >= sizeof(val)) {
810 				return(EINVAL);
811 			}
812 		}
813 
814 		/* Copy parameter "key" */
815 		if ((s = sys_datacopy_wrapper(who_e, (vir_bytes) sysgetenv.key,
816 				      SELF, (vir_bytes) search_key,
817 				      sysgetenv.keylen)) != OK)
818 			return(s);
819 		search_key[sysgetenv.keylen] = '\0'; /* Limit string */
820 
821 		/* Is it a parameter we know? */
822 		if (svrctl == VFSSETPARAM) {
823 			if (!strcmp(search_key, "verbose")) {
824 				int verbose_val;
825 				if ((s = sys_datacopy_wrapper(who_e,
826 				    (vir_bytes) sysgetenv.val, SELF,
827 				    (vir_bytes) &val, sysgetenv.vallen)) != OK)
828 					return(s);
829 				val[sysgetenv.vallen] = '\0'; /* Limit string */
830 				verbose_val = atoi(val);
831 				if (verbose_val < 0 || verbose_val > 4) {
832 					return(EINVAL);
833 				}
834 				verbose = verbose_val;
835 				r = OK;
836 			} else {
837 				r = ESRCH;
838 			}
839 		} else { /* VFSGETPARAM */
840 			char small_buf[60];
841 
842 			r = ESRCH;
843 			if (!strcmp(search_key, "print_traces")) {
844 				mthread_stacktraces();
845 				sysgetenv.val = 0;
846 				sysgetenv.vallen = 0;
847 				r = OK;
848 			} else if (!strcmp(search_key, "active_threads")) {
849 				int active = NR_WTHREADS - worker_available();
850 				snprintf(small_buf, sizeof(small_buf) - 1,
851 					 "%d", active);
852 				sysgetenv.vallen = strlen(small_buf);
853 				r = OK;
854 			}
855 
856 			if (r == OK) {
857 				if ((s = sys_datacopy_wrapper(SELF,
858 				    (vir_bytes) &sysgetenv, who_e, ptr,
859 				    sizeof(sysgetenv))) != OK)
860 					return(s);
861 				if (sysgetenv.val != 0) {
862 					if ((s = sys_datacopy_wrapper(SELF,
863 					    (vir_bytes) small_buf, who_e,
864 					    (vir_bytes) sysgetenv.val,
865 					    sysgetenv.vallen)) != OK)
866 						return(s);
867 				}
868 			}
869 		}
870 
871 		return(r);
872 	}
873     default:
874 	return(EINVAL);
875   }
876 }
877 
878 /*===========================================================================*
879  *				pm_dumpcore				     *
880  *===========================================================================*/
881 int pm_dumpcore(int csig, vir_bytes exe_name)
882 {
883   int r = OK, core_fd;
884   struct filp *f;
885   char core_path[PATH_MAX];
886   char proc_name[PROC_NAME_LEN];
887 
888   /* if a process is blocked, scratch(fp).file.fd_nr holds the fd it's blocked
889    * on. free it up for use by common_open().
890    */
891   if (fp_is_blocked(fp))
892           unpause();
893 
894   /* open core file */
895   snprintf(core_path, PATH_MAX, "%s.%d", CORE_NAME, fp->fp_pid);
896   core_fd = common_open(core_path, O_WRONLY | O_CREAT | O_TRUNC, CORE_MODE);
897   if (core_fd < 0) { r = core_fd; goto core_exit; }
898 
899   /* get process' name */
900   r = sys_datacopy_wrapper(PM_PROC_NR, exe_name, VFS_PROC_NR, (vir_bytes) proc_name,
901 			PROC_NAME_LEN);
902   if (r != OK) goto core_exit;
903   proc_name[PROC_NAME_LEN - 1] = '\0';
904 
905   if ((f = get_filp(core_fd, VNODE_WRITE)) == NULL) { r=EBADF; goto core_exit; }
906   write_elf_core_file(f, csig, proc_name);
907   unlock_filp(f);
908   (void) close_fd(fp, core_fd);	        /* ignore failure, we're exiting anyway */
909 
910 core_exit:
911   if(csig)
912 	  free_proc(FP_EXITING);
913   return(r);
914 }
915 
916 /*===========================================================================*
917  *				 ds_event				     *
918  *===========================================================================*/
919 void
920 ds_event(void)
921 {
922   char key[DS_MAX_KEYLEN];
923   char *blkdrv_prefix = "drv.blk.";
924   char *chrdrv_prefix = "drv.chr.";
925   u32_t value;
926   int type, r, is_blk;
927   endpoint_t owner_endpoint;
928 
929   /* Get the event and the owner from DS. */
930   while ((r = ds_check(key, &type, &owner_endpoint)) == OK) {
931 	/* Only check for block and character driver up events. */
932 	if (!strncmp(key, blkdrv_prefix, strlen(blkdrv_prefix))) {
933 		is_blk = TRUE;
934 	} else if (!strncmp(key, chrdrv_prefix, strlen(chrdrv_prefix))) {
935 		is_blk = FALSE;
936 	} else {
937 		continue;
938 	}
939 
940 	if ((r = ds_retrieve_u32(key, &value)) != OK) {
941 		printf("VFS: ds_event: ds_retrieve_u32 failed\n");
942 		break;
943 	}
944 	if (value != DS_DRIVER_UP) continue;
945 
946 	/* Perform up. */
947 	dmap_endpt_up(owner_endpoint, is_blk);
948   }
949 
950   if (r != ENOENT) printf("VFS: ds_event: ds_check failed: %d\n", r);
951 }
952 
953 /* A function to be called on panic(). */
954 void panic_hook(void)
955 {
956   printf("VFS mthread stacktraces:\n");
957   mthread_stacktraces();
958 }
959 
960 /*===========================================================================*
961  *				do_getrusage				     *
962  *===========================================================================*/
963 int do_getrusage(void)
964 {
965 	int res;
966 	struct rusage r_usage;
967 
968 	if ((res = sys_datacopy_wrapper(who_e, m_in.m_lc_vfs_rusage.addr, SELF,
969 		(vir_bytes) &r_usage, (vir_bytes) sizeof(r_usage))) < 0)
970 		return res;
971 
972 	r_usage.ru_inblock = 0;
973 	r_usage.ru_oublock = 0;
974 	r_usage.ru_ixrss = fp->text_size;
975 	r_usage.ru_idrss = fp->data_size;
976 	r_usage.ru_isrss = DEFAULT_STACK_LIMIT;
977 
978 	return sys_datacopy_wrapper(SELF, (vir_bytes) &r_usage, who_e,
979 		m_in.m_lc_vfs_rusage.addr, (phys_bytes) sizeof(r_usage));
980 }
981