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