xref: /minix/minix/servers/vfs/misc.c (revision 03ac74ed)
1433d6423SLionel Sambuc /* This file contains a collection of miscellaneous procedures.  Some of them
2433d6423SLionel Sambuc  * perform simple system calls.  Some others do a little part of system calls
3433d6423SLionel Sambuc  * that are mostly performed by the Memory Manager.
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  * The entry points into this file are
6433d6423SLionel Sambuc  *   do_fcntl:	  perform the FCNTL system call
7433d6423SLionel Sambuc  *   do_sync:	  perform the SYNC system call
8433d6423SLionel Sambuc  *   do_fsync:	  perform the FSYNC system call
9433d6423SLionel Sambuc  *   pm_setsid:	  perform VFS's side of setsid system call
10433d6423SLionel Sambuc  *   pm_reboot:	  sync disks and prepare for shutdown
11433d6423SLionel Sambuc  *   pm_fork:	  adjust the tables after PM has performed a FORK system call
12433d6423SLionel Sambuc  *   do_exec:	  handle files with FD_CLOEXEC on after PM has done an EXEC
13433d6423SLionel Sambuc  *   do_exit:	  a process has exited; note that in the tables
14433d6423SLionel Sambuc  *   do_set:	  set uid or gid for some process
15433d6423SLionel Sambuc  *   do_revive:	  revive a process that was waiting for something (e.g. TTY)
16433d6423SLionel Sambuc  *   do_svrctl:	  file system control
17433d6423SLionel Sambuc  *   do_getsysinfo:	request copy of FS data structure
18433d6423SLionel Sambuc  *   pm_dumpcore: create a core dump
19433d6423SLionel Sambuc  */
20433d6423SLionel Sambuc 
21433d6423SLionel Sambuc #include "fs.h"
22433d6423SLionel Sambuc #include <fcntl.h>
23433d6423SLionel Sambuc #include <assert.h>
24433d6423SLionel Sambuc #include <unistd.h>
25433d6423SLionel Sambuc #include <string.h>
26433d6423SLionel Sambuc #include <minix/callnr.h>
27433d6423SLionel Sambuc #include <minix/safecopies.h>
28433d6423SLionel Sambuc #include <minix/endpoint.h>
29433d6423SLionel Sambuc #include <minix/com.h>
30433d6423SLionel Sambuc #include <minix/sysinfo.h>
31433d6423SLionel Sambuc #include <minix/u64.h>
32433d6423SLionel Sambuc #include <sys/ptrace.h>
33433d6423SLionel Sambuc #include <sys/svrctl.h>
34433d6423SLionel Sambuc #include <sys/resource.h>
35433d6423SLionel Sambuc #include "file.h"
36433d6423SLionel Sambuc #include <minix/vfsif.h>
37433d6423SLionel Sambuc #include "vnode.h"
38433d6423SLionel Sambuc #include "vmnt.h"
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc #define CORE_NAME	"core"
41433d6423SLionel Sambuc #define CORE_MODE	0777	/* mode to use on core image files */
42433d6423SLionel Sambuc 
43433d6423SLionel Sambuc #if ENABLE_SYSCALL_STATS
44433d6423SLionel Sambuc unsigned long calls_stats[NR_VFS_CALLS];
45433d6423SLionel Sambuc #endif
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc static void free_proc(int flags);
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc /*===========================================================================*
50433d6423SLionel Sambuc  *				do_getsysinfo				     *
51433d6423SLionel Sambuc  *===========================================================================*/
do_getsysinfo(void)52433d6423SLionel Sambuc int do_getsysinfo(void)
53433d6423SLionel Sambuc {
546ad322a9SDavid van Moolenbroek   struct fproc *rfp;
556ad322a9SDavid van Moolenbroek   struct fproc_light *rfpl;
56e3b8d4bbSDavid van Moolenbroek   struct smap *sp;
57433d6423SLionel Sambuc   vir_bytes src_addr, dst_addr;
58433d6423SLionel Sambuc   size_t len, buf_size;
59433d6423SLionel Sambuc   int what;
60433d6423SLionel Sambuc 
61433d6423SLionel Sambuc   what = job_m_in.m_lsys_getsysinfo.what;
62433d6423SLionel Sambuc   dst_addr = job_m_in.m_lsys_getsysinfo.where;
63433d6423SLionel Sambuc   buf_size = job_m_in.m_lsys_getsysinfo.size;
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc   /* Only su may call do_getsysinfo. This call may leak information (and is not
66433d6423SLionel Sambuc    * stable enough to be part of the API/ABI). In the future, requests from
67433d6423SLionel Sambuc    * non-system processes should be denied.
68433d6423SLionel Sambuc    */
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc   if (!super_user) return(EPERM);
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc   switch(what) {
73433d6423SLionel Sambuc     case SI_PROC_TAB:
74433d6423SLionel Sambuc 	src_addr = (vir_bytes) fproc;
75433d6423SLionel Sambuc 	len = sizeof(struct fproc) * NR_PROCS;
76433d6423SLionel Sambuc 	break;
77433d6423SLionel Sambuc     case SI_DMAP_TAB:
78433d6423SLionel Sambuc 	src_addr = (vir_bytes) dmap;
79433d6423SLionel Sambuc 	len = sizeof(struct dmap) * NR_DEVICES;
80433d6423SLionel Sambuc 	break;
816ad322a9SDavid van Moolenbroek     case SI_PROCLIGHT_TAB:
826ad322a9SDavid van Moolenbroek 	/* Fill the light process table for the MIB service upon request. */
836ad322a9SDavid van Moolenbroek 	rfpl = &fproc_light[0];
846ad322a9SDavid van Moolenbroek 	for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++, rfpl++) {
856ad322a9SDavid van Moolenbroek 		rfpl->fpl_tty = rfp->fp_tty;
866ad322a9SDavid van Moolenbroek 		rfpl->fpl_blocked_on = rfp->fp_blocked_on;
87232819ddSDavid van Moolenbroek 		if (rfp->fp_blocked_on == FP_BLOCKED_ON_CDEV)
88232819ddSDavid van Moolenbroek 			rfpl->fpl_task = rfp->fp_cdev.endpt;
89e3b8d4bbSDavid van Moolenbroek 		else if (rfp->fp_blocked_on == FP_BLOCKED_ON_SDEV &&
90e3b8d4bbSDavid van Moolenbroek 		    (sp = get_smap_by_dev(rfp->fp_sdev.dev, NULL)) != NULL)
91e3b8d4bbSDavid van Moolenbroek 			rfpl->fpl_task = sp->smap_endpt;
92232819ddSDavid van Moolenbroek 		else
93232819ddSDavid van Moolenbroek 			rfpl->fpl_task = NONE;
946ad322a9SDavid van Moolenbroek 	}
956ad322a9SDavid van Moolenbroek 	src_addr = (vir_bytes) fproc_light;
966ad322a9SDavid van Moolenbroek 	len = sizeof(fproc_light);
976ad322a9SDavid van Moolenbroek 	break;
98433d6423SLionel Sambuc #if ENABLE_SYSCALL_STATS
99433d6423SLionel Sambuc     case SI_CALL_STATS:
100433d6423SLionel Sambuc 	src_addr = (vir_bytes) calls_stats;
101433d6423SLionel Sambuc 	len = sizeof(calls_stats);
102433d6423SLionel Sambuc 	break;
103433d6423SLionel Sambuc #endif
104433d6423SLionel Sambuc     default:
105433d6423SLionel Sambuc 	return(EINVAL);
106433d6423SLionel Sambuc   }
107433d6423SLionel Sambuc 
108433d6423SLionel Sambuc   if (len != buf_size)
109433d6423SLionel Sambuc 	return(EINVAL);
110433d6423SLionel Sambuc 
111433d6423SLionel Sambuc   return sys_datacopy_wrapper(SELF, src_addr, who_e, dst_addr, len);
112433d6423SLionel Sambuc }
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc /*===========================================================================*
115433d6423SLionel Sambuc  *				do_fcntl				     *
116433d6423SLionel Sambuc  *===========================================================================*/
do_fcntl(void)117433d6423SLionel Sambuc int do_fcntl(void)
118433d6423SLionel Sambuc {
119433d6423SLionel Sambuc /* Perform the fcntl(fd, cmd, ...) system call. */
120232819ddSDavid van Moolenbroek   struct filp *f;
121232819ddSDavid van Moolenbroek   int fd, new_fd, fl, r = OK, fcntl_req, fcntl_argx;
122232819ddSDavid van Moolenbroek   vir_bytes addr;
123433d6423SLionel Sambuc   tll_access_t locktype;
124433d6423SLionel Sambuc 
125232819ddSDavid van Moolenbroek   fd = job_m_in.m_lc_vfs_fcntl.fd;
126433d6423SLionel Sambuc   fcntl_req = job_m_in.m_lc_vfs_fcntl.cmd;
127433d6423SLionel Sambuc   fcntl_argx = job_m_in.m_lc_vfs_fcntl.arg_int;
128232819ddSDavid van Moolenbroek   addr = job_m_in.m_lc_vfs_fcntl.arg_ptr;
129433d6423SLionel Sambuc 
130433d6423SLionel Sambuc   /* Is the file descriptor valid? */
131433d6423SLionel Sambuc   locktype = (fcntl_req == F_FREESP) ? VNODE_WRITE : VNODE_READ;
132232819ddSDavid van Moolenbroek   if ((f = get_filp(fd, locktype)) == NULL)
133433d6423SLionel Sambuc 	return(err_code);
134433d6423SLionel Sambuc 
135433d6423SLionel Sambuc   switch (fcntl_req) {
136433d6423SLionel Sambuc     case F_DUPFD:
137424cad2cSDavid van Moolenbroek     case F_DUPFD_CLOEXEC:
138433d6423SLionel Sambuc 	/* This replaces the old dup() system call. */
139433d6423SLionel Sambuc 	if (fcntl_argx < 0 || fcntl_argx >= OPEN_MAX) r = EINVAL;
140433d6423SLionel Sambuc 	else if ((r = get_fd(fp, fcntl_argx, 0, &new_fd, NULL)) == OK) {
141433d6423SLionel Sambuc 		f->filp_count++;
142433d6423SLionel Sambuc 		fp->fp_filp[new_fd] = f;
143424cad2cSDavid van Moolenbroek 		assert(!FD_ISSET(new_fd, &fp->fp_cloexec_set));
144424cad2cSDavid van Moolenbroek 		if (fcntl_req == F_DUPFD_CLOEXEC)
145424cad2cSDavid van Moolenbroek 			FD_SET(new_fd, &fp->fp_cloexec_set);
146433d6423SLionel Sambuc 		r = new_fd;
147433d6423SLionel Sambuc 	}
148433d6423SLionel Sambuc 	break;
149433d6423SLionel Sambuc 
150433d6423SLionel Sambuc     case F_GETFD:
151433d6423SLionel Sambuc 	/* Get close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
152433d6423SLionel Sambuc 	r = 0;
153232819ddSDavid van Moolenbroek 	if (FD_ISSET(fd, &fp->fp_cloexec_set))
154433d6423SLionel Sambuc 		r = FD_CLOEXEC;
155433d6423SLionel Sambuc 	break;
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc     case F_SETFD:
158433d6423SLionel Sambuc 	/* Set close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
159433d6423SLionel Sambuc 	if (fcntl_argx & FD_CLOEXEC)
160232819ddSDavid van Moolenbroek 		FD_SET(fd, &fp->fp_cloexec_set);
161433d6423SLionel Sambuc 	else
162232819ddSDavid van Moolenbroek 		FD_CLR(fd, &fp->fp_cloexec_set);
163433d6423SLionel Sambuc 	break;
164433d6423SLionel Sambuc 
165433d6423SLionel Sambuc     case F_GETFL:
166433d6423SLionel Sambuc 	/* Get file status flags (O_NONBLOCK and O_APPEND). */
167433d6423SLionel Sambuc 	fl = f->filp_flags & (O_NONBLOCK | O_APPEND | O_ACCMODE);
168433d6423SLionel Sambuc 	r = fl;
169433d6423SLionel Sambuc 	break;
170433d6423SLionel Sambuc 
171433d6423SLionel Sambuc     case F_SETFL:
172433d6423SLionel Sambuc 	/* Set file status flags (O_NONBLOCK and O_APPEND). */
173433d6423SLionel Sambuc 	fl = O_NONBLOCK | O_APPEND;
174433d6423SLionel Sambuc 	f->filp_flags = (f->filp_flags & ~fl) | (fcntl_argx & fl);
175433d6423SLionel Sambuc 	break;
176433d6423SLionel Sambuc 
177433d6423SLionel Sambuc     case F_GETLK:
178433d6423SLionel Sambuc     case F_SETLK:
179433d6423SLionel Sambuc     case F_SETLKW:
180433d6423SLionel Sambuc 	/* Set or clear a file lock. */
181232819ddSDavid van Moolenbroek 	r = lock_op(fd, fcntl_req, addr);
182433d6423SLionel Sambuc 	break;
183433d6423SLionel Sambuc 
184433d6423SLionel Sambuc     case F_FREESP:
185433d6423SLionel Sambuc      {
186433d6423SLionel Sambuc 	/* Free a section of a file */
187433d6423SLionel Sambuc 	off_t start, end, offset;
188433d6423SLionel Sambuc 	struct flock flock_arg;
189433d6423SLionel Sambuc 
190433d6423SLionel Sambuc 	/* Check if it's a regular file. */
191433d6423SLionel Sambuc 	if (!S_ISREG(f->filp_vno->v_mode)) r = EINVAL;
192433d6423SLionel Sambuc 	else if (!(f->filp_mode & W_BIT)) r = EBADF;
193433d6423SLionel Sambuc 	else {
194433d6423SLionel Sambuc 		/* Copy flock data from userspace. */
195232819ddSDavid van Moolenbroek 		r = sys_datacopy_wrapper(who_e, addr, SELF,
196232819ddSDavid van Moolenbroek 		    (vir_bytes)&flock_arg, sizeof(flock_arg));
197433d6423SLionel Sambuc 	}
198433d6423SLionel Sambuc 
199433d6423SLionel Sambuc 	if (r != OK) break;
200433d6423SLionel Sambuc 
201433d6423SLionel Sambuc 	/* Convert starting offset to signed. */
202433d6423SLionel Sambuc 	offset = (off_t) flock_arg.l_start;
203433d6423SLionel Sambuc 
204433d6423SLionel Sambuc 	/* Figure out starting position base. */
205433d6423SLionel Sambuc 	switch(flock_arg.l_whence) {
206433d6423SLionel Sambuc 	  case SEEK_SET: start = 0; break;
207433d6423SLionel Sambuc 	  case SEEK_CUR: start = f->filp_pos; break;
208433d6423SLionel Sambuc 	  case SEEK_END: start = f->filp_vno->v_size; break;
209433d6423SLionel Sambuc 	  default: r = EINVAL;
210433d6423SLionel Sambuc 	}
211433d6423SLionel Sambuc 	if (r != OK) break;
212433d6423SLionel Sambuc 
213433d6423SLionel Sambuc 	/* Check for overflow or underflow. */
214433d6423SLionel Sambuc 	if (offset > 0 && start + offset < start) r = EINVAL;
215433d6423SLionel Sambuc 	else if (offset < 0 && start + offset > start) r = EINVAL;
216433d6423SLionel Sambuc 	else {
217433d6423SLionel Sambuc 		start += offset;
218433d6423SLionel Sambuc 		if (start < 0) r = EINVAL;
219433d6423SLionel Sambuc 	}
220433d6423SLionel Sambuc 	if (r != OK) break;
221433d6423SLionel Sambuc 
222433d6423SLionel Sambuc 	if (flock_arg.l_len != 0) {
223433d6423SLionel Sambuc 		if (start >= f->filp_vno->v_size) r = EINVAL;
224433d6423SLionel Sambuc 		else if ((end = start + flock_arg.l_len) <= start) r = EINVAL;
225433d6423SLionel Sambuc 		else if (end > f->filp_vno->v_size) end = f->filp_vno->v_size;
226433d6423SLionel Sambuc 	} else {
227433d6423SLionel Sambuc                 end = 0;
228433d6423SLionel Sambuc 	}
229433d6423SLionel Sambuc 	if (r != OK) break;
230433d6423SLionel Sambuc 
231433d6423SLionel Sambuc 	r = req_ftrunc(f->filp_vno->v_fs_e, f->filp_vno->v_inode_nr,start,end);
232433d6423SLionel Sambuc 
233433d6423SLionel Sambuc 	if (r == OK && flock_arg.l_len == 0)
234433d6423SLionel Sambuc 		f->filp_vno->v_size = start;
235433d6423SLionel Sambuc 
236433d6423SLionel Sambuc 	break;
237433d6423SLionel Sambuc      }
238433d6423SLionel Sambuc     case F_GETNOSIGPIPE:
2391f945e80SDavid van Moolenbroek 	r = !!(f->filp_flags & O_NOSIGPIPE);
240433d6423SLionel Sambuc 	break;
241433d6423SLionel Sambuc     case F_SETNOSIGPIPE:
2421f945e80SDavid van Moolenbroek 	if (fcntl_argx)
2431f945e80SDavid van Moolenbroek 		f->filp_flags |= O_NOSIGPIPE;
2441f945e80SDavid van Moolenbroek 	else
2451f945e80SDavid van Moolenbroek 		f->filp_flags &= ~O_NOSIGPIPE;
246433d6423SLionel Sambuc 	break;
247433d6423SLionel Sambuc     case F_FLUSH_FS_CACHE:
248433d6423SLionel Sambuc     {
249433d6423SLionel Sambuc 	struct vnode *vn = f->filp_vno;
250433d6423SLionel Sambuc 	mode_t mode = f->filp_vno->v_mode;
251433d6423SLionel Sambuc 	if (!super_user) {
252433d6423SLionel Sambuc 		r = EPERM;
253433d6423SLionel Sambuc 	} else if (S_ISBLK(mode)) {
254433d6423SLionel Sambuc 		/* Block device; flush corresponding device blocks. */
255433d6423SLionel Sambuc 		r = req_flush(vn->v_bfs_e, vn->v_sdev);
256433d6423SLionel Sambuc 	} else if (S_ISREG(mode) || S_ISDIR(mode)) {
257433d6423SLionel Sambuc 		/* Directory or regular file; flush hosting FS blocks. */
258433d6423SLionel Sambuc 		r = req_flush(vn->v_fs_e, vn->v_dev);
259433d6423SLionel Sambuc 	} else {
260433d6423SLionel Sambuc 		/* Remaining cases.. Meaning unclear. */
261433d6423SLionel Sambuc 		r = ENODEV;
262433d6423SLionel Sambuc 	}
263433d6423SLionel Sambuc 	break;
264433d6423SLionel Sambuc     }
265433d6423SLionel Sambuc     default:
266433d6423SLionel Sambuc 	r = EINVAL;
267433d6423SLionel Sambuc   }
268433d6423SLionel Sambuc 
269433d6423SLionel Sambuc   unlock_filp(f);
270433d6423SLionel Sambuc   return(r);
271433d6423SLionel Sambuc }
272433d6423SLionel Sambuc 
273433d6423SLionel Sambuc /*===========================================================================*
274433d6423SLionel Sambuc  *				do_sync					     *
275433d6423SLionel Sambuc  *===========================================================================*/
do_sync(void)276433d6423SLionel Sambuc int do_sync(void)
277433d6423SLionel Sambuc {
278433d6423SLionel Sambuc   struct vmnt *vmp;
279433d6423SLionel Sambuc   int r = OK;
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc   for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
282433d6423SLionel Sambuc 	if ((r = lock_vmnt(vmp, VMNT_READ)) != OK)
283433d6423SLionel Sambuc 		break;
284433d6423SLionel Sambuc 	if (vmp->m_dev != NO_DEV && vmp->m_fs_e != NONE &&
285433d6423SLionel Sambuc 		 vmp->m_root_node != NULL) {
286433d6423SLionel Sambuc 		req_sync(vmp->m_fs_e);
287433d6423SLionel Sambuc 	}
288433d6423SLionel Sambuc 	unlock_vmnt(vmp);
289433d6423SLionel Sambuc   }
290433d6423SLionel Sambuc 
291433d6423SLionel Sambuc   return(r);
292433d6423SLionel Sambuc }
293433d6423SLionel Sambuc 
294433d6423SLionel Sambuc /*===========================================================================*
295433d6423SLionel Sambuc  *				do_fsync				     *
296433d6423SLionel Sambuc  *===========================================================================*/
do_fsync(void)297433d6423SLionel Sambuc int do_fsync(void)
298433d6423SLionel Sambuc {
299433d6423SLionel Sambuc /* Perform the fsync() system call. */
300433d6423SLionel Sambuc   struct filp *rfilp;
301433d6423SLionel Sambuc   struct vmnt *vmp;
302433d6423SLionel Sambuc   dev_t dev;
303232819ddSDavid van Moolenbroek   int fd, r = OK;
304433d6423SLionel Sambuc 
305232819ddSDavid van Moolenbroek   fd = job_m_in.m_lc_vfs_fsync.fd;
306433d6423SLionel Sambuc 
307232819ddSDavid van Moolenbroek   if ((rfilp = get_filp(fd, VNODE_READ)) == NULL)
308433d6423SLionel Sambuc 	return(err_code);
309433d6423SLionel Sambuc 
310433d6423SLionel Sambuc   dev = rfilp->filp_vno->v_dev;
311433d6423SLionel Sambuc   unlock_filp(rfilp);
312433d6423SLionel Sambuc 
313433d6423SLionel Sambuc   for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
314433d6423SLionel Sambuc 	if (vmp->m_dev != dev) continue;
315433d6423SLionel Sambuc 	if ((r = lock_vmnt(vmp, VMNT_READ)) != OK)
316433d6423SLionel Sambuc 		break;
317433d6423SLionel Sambuc 	if (vmp->m_dev != NO_DEV && vmp->m_dev == dev &&
318433d6423SLionel Sambuc 		vmp->m_fs_e != NONE && vmp->m_root_node != NULL) {
319433d6423SLionel Sambuc 
320433d6423SLionel Sambuc 		req_sync(vmp->m_fs_e);
321433d6423SLionel Sambuc 	}
322433d6423SLionel Sambuc 	unlock_vmnt(vmp);
323433d6423SLionel Sambuc   }
324433d6423SLionel Sambuc 
325433d6423SLionel Sambuc   return(r);
326433d6423SLionel Sambuc }
327433d6423SLionel Sambuc 
dupvm(struct fproc * rfp,int pfd,int * vmfd,struct filp ** newfilp)328433d6423SLionel Sambuc int dupvm(struct fproc *rfp, int pfd, int *vmfd, struct filp **newfilp)
329433d6423SLionel Sambuc {
330433d6423SLionel Sambuc 	int result, procfd;
331433d6423SLionel Sambuc 	struct filp *f = NULL;
332433d6423SLionel Sambuc 	struct fproc *vmf = fproc_addr(VM_PROC_NR);
333433d6423SLionel Sambuc 
334433d6423SLionel Sambuc 	*newfilp = NULL;
335433d6423SLionel Sambuc 
336433d6423SLionel Sambuc 	if ((f = get_filp2(rfp, pfd, VNODE_READ)) == NULL) {
337433d6423SLionel Sambuc 		printf("VFS dupvm: get_filp2 failed\n");
338433d6423SLionel Sambuc 		return EBADF;
339433d6423SLionel Sambuc 	}
340433d6423SLionel Sambuc 
341433d6423SLionel Sambuc 	if(!(f->filp_vno->v_vmnt->m_fs_flags & RES_HASPEEK)) {
342433d6423SLionel Sambuc 		unlock_filp(f);
343433d6423SLionel Sambuc #if 0	/* Noisy diagnostic for mmap() by ld.so */
344433d6423SLionel Sambuc 		printf("VFS dupvm: no peek available\n");
345433d6423SLionel Sambuc #endif
346433d6423SLionel Sambuc 		return EINVAL;
347433d6423SLionel Sambuc 	}
348433d6423SLionel Sambuc 
349433d6423SLionel Sambuc 	assert(f->filp_vno);
350433d6423SLionel Sambuc 	assert(f->filp_vno->v_vmnt);
351433d6423SLionel Sambuc 
352433d6423SLionel Sambuc 	if (!S_ISREG(f->filp_vno->v_mode) && !S_ISBLK(f->filp_vno->v_mode)) {
353433d6423SLionel Sambuc 		printf("VFS: mmap regular/blockdev only; dev 0x%llx ino %llu has mode 0%o\n",
354433d6423SLionel Sambuc 			f->filp_vno->v_dev, f->filp_vno->v_inode_nr, f->filp_vno->v_mode);
355433d6423SLionel Sambuc 		unlock_filp(f);
356433d6423SLionel Sambuc 		return EINVAL;
357433d6423SLionel Sambuc 	}
358433d6423SLionel Sambuc 
359433d6423SLionel Sambuc 	/* get free FD in VM */
360433d6423SLionel Sambuc 	if((result=get_fd(vmf, 0, 0, &procfd, NULL)) != OK) {
361433d6423SLionel Sambuc 		unlock_filp(f);
362433d6423SLionel Sambuc 		printf("VFS dupvm: getfd failed\n");
363433d6423SLionel Sambuc 		return result;
364433d6423SLionel Sambuc 	}
365433d6423SLionel Sambuc 
366433d6423SLionel Sambuc 	*vmfd = procfd;
367433d6423SLionel Sambuc 
368433d6423SLionel Sambuc 	f->filp_count++;
369433d6423SLionel Sambuc 	assert(f->filp_count > 0);
370433d6423SLionel Sambuc 	vmf->fp_filp[procfd] = f;
371433d6423SLionel Sambuc 
372433d6423SLionel Sambuc 	*newfilp = f;
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc 	return OK;
375433d6423SLionel Sambuc }
376433d6423SLionel Sambuc 
377433d6423SLionel Sambuc /*===========================================================================*
378433d6423SLionel Sambuc  *				do_vm_call				     *
379433d6423SLionel Sambuc  *===========================================================================*/
do_vm_call(void)380433d6423SLionel Sambuc int do_vm_call(void)
381433d6423SLionel Sambuc {
382433d6423SLionel Sambuc /* A call that VM does to VFS.
383433d6423SLionel Sambuc  * We must reply with the fixed type VM_VFS_REPLY (and put our result info
384433d6423SLionel Sambuc  * in the rest of the message) so VM can tell the difference between a
385433d6423SLionel Sambuc  * request from VFS and a reply to this call.
386433d6423SLionel Sambuc  */
387433d6423SLionel Sambuc 	int req = job_m_in.VFS_VMCALL_REQ;
388433d6423SLionel Sambuc 	int req_fd = job_m_in.VFS_VMCALL_FD;
389433d6423SLionel Sambuc 	u32_t req_id = job_m_in.VFS_VMCALL_REQID;
390433d6423SLionel Sambuc 	endpoint_t ep = job_m_in.VFS_VMCALL_ENDPOINT;
391433d6423SLionel Sambuc 	u64_t offset = job_m_in.VFS_VMCALL_OFFSET;
392433d6423SLionel Sambuc 	u32_t length = job_m_in.VFS_VMCALL_LENGTH;
393433d6423SLionel Sambuc 	int result = OK;
394433d6423SLionel Sambuc 	int slot;
395e1cdaee1SLionel Sambuc 	struct fproc *rfp;
396e1cdaee1SLionel Sambuc #if !defined(NDEBUG)
397e1cdaee1SLionel Sambuc 	struct fproc *vmf;
398e1cdaee1SLionel Sambuc #endif /* !defined(NDEBUG) */
399433d6423SLionel Sambuc 	struct filp *f = NULL;
400433d6423SLionel Sambuc 	int r;
401433d6423SLionel Sambuc 
402433d6423SLionel Sambuc 	if(job_m_in.m_source != VM_PROC_NR)
403433d6423SLionel Sambuc 		return ENOSYS;
404433d6423SLionel Sambuc 
405433d6423SLionel Sambuc 	if(isokendpt(ep, &slot) != OK) rfp = NULL;
406433d6423SLionel Sambuc 	else rfp = &fproc[slot];
407433d6423SLionel Sambuc 
408e1cdaee1SLionel Sambuc #if !defined(NDEBUG)
409433d6423SLionel Sambuc 	vmf = fproc_addr(VM_PROC_NR);
410e1cdaee1SLionel Sambuc #endif /* !defined(NDEBUG) */
411433d6423SLionel Sambuc 	assert(fp == vmf);
412433d6423SLionel Sambuc 	assert(rfp != vmf);
413433d6423SLionel Sambuc 
414433d6423SLionel Sambuc 	switch(req) {
415433d6423SLionel Sambuc 		case VMVFSREQ_FDLOOKUP:
416433d6423SLionel Sambuc 		{
417433d6423SLionel Sambuc 			int procfd;
418433d6423SLionel Sambuc 
419433d6423SLionel Sambuc 			/* Lookup fd in referenced process. */
420433d6423SLionel Sambuc 
421433d6423SLionel Sambuc 			if(!rfp) {
422433d6423SLionel Sambuc 				printf("VFS: why isn't ep %d here?!\n", ep);
423433d6423SLionel Sambuc 				result = ESRCH;
424433d6423SLionel Sambuc 				goto reqdone;
425433d6423SLionel Sambuc 			}
426433d6423SLionel Sambuc 
427433d6423SLionel Sambuc 			if((result = dupvm(rfp, req_fd, &procfd, &f)) != OK) {
428433d6423SLionel Sambuc #if 0   /* Noisy diagnostic for mmap() by ld.so */
429433d6423SLionel Sambuc 				printf("vfs: dupvm failed\n");
430433d6423SLionel Sambuc #endif
431433d6423SLionel Sambuc 				goto reqdone;
432433d6423SLionel Sambuc 			}
433433d6423SLionel Sambuc 
434433d6423SLionel Sambuc 			if(S_ISBLK(f->filp_vno->v_mode)) {
435433d6423SLionel Sambuc 				assert(f->filp_vno->v_sdev != NO_DEV);
436433d6423SLionel Sambuc 				job_m_out.VMV_DEV = f->filp_vno->v_sdev;
437433d6423SLionel Sambuc 				job_m_out.VMV_INO = VMC_NO_INODE;
438433d6423SLionel Sambuc 				job_m_out.VMV_SIZE_PAGES = LONG_MAX;
439433d6423SLionel Sambuc 			} else {
440433d6423SLionel Sambuc 				job_m_out.VMV_DEV = f->filp_vno->v_dev;
441433d6423SLionel Sambuc 				job_m_out.VMV_INO = f->filp_vno->v_inode_nr;
442433d6423SLionel Sambuc 				job_m_out.VMV_SIZE_PAGES =
443433d6423SLionel Sambuc 					roundup(f->filp_vno->v_size,
444433d6423SLionel Sambuc 						PAGE_SIZE)/PAGE_SIZE;
445433d6423SLionel Sambuc 			}
446433d6423SLionel Sambuc 
447433d6423SLionel Sambuc 			job_m_out.VMV_FD = procfd;
448433d6423SLionel Sambuc 
449433d6423SLionel Sambuc 			result = OK;
450433d6423SLionel Sambuc 
451433d6423SLionel Sambuc 			break;
452433d6423SLionel Sambuc 		}
453433d6423SLionel Sambuc 		case VMVFSREQ_FDCLOSE:
454433d6423SLionel Sambuc 		{
455491d647aSDavid van Moolenbroek 			result = close_fd(fp, req_fd, FALSE /*may_suspend*/);
456433d6423SLionel Sambuc 			if(result != OK) {
457433d6423SLionel Sambuc 				printf("VFS: VM fd close for fd %d, %d (%d)\n",
458433d6423SLionel Sambuc 					req_fd, fp->fp_endpoint, result);
459433d6423SLionel Sambuc 			}
460433d6423SLionel Sambuc 			break;
461433d6423SLionel Sambuc 		}
462433d6423SLionel Sambuc 		case VMVFSREQ_FDIO:
463433d6423SLionel Sambuc 		{
464433d6423SLionel Sambuc 			result = actual_lseek(fp, req_fd, SEEK_SET, offset,
465433d6423SLionel Sambuc 				NULL);
466433d6423SLionel Sambuc 
467433d6423SLionel Sambuc 			if(result == OK) {
468433d6423SLionel Sambuc 				result = actual_read_write_peek(fp, PEEKING,
469433d6423SLionel Sambuc 					req_fd, /* vir_bytes */ 0, length);
470433d6423SLionel Sambuc 			}
471433d6423SLionel Sambuc 
472433d6423SLionel Sambuc 			break;
473433d6423SLionel Sambuc 		}
474433d6423SLionel Sambuc 		default:
475433d6423SLionel Sambuc 			panic("VFS: bad request code from VM\n");
476433d6423SLionel Sambuc 			break;
477433d6423SLionel Sambuc 	}
478433d6423SLionel Sambuc 
479433d6423SLionel Sambuc reqdone:
480433d6423SLionel Sambuc 	if(f)
481433d6423SLionel Sambuc 		unlock_filp(f);
482433d6423SLionel Sambuc 
483433d6423SLionel Sambuc 	/* fp is VM still. */
484433d6423SLionel Sambuc 	assert(fp == vmf);
485433d6423SLionel Sambuc 	job_m_out.VMV_ENDPOINT = ep;
486433d6423SLionel Sambuc 	job_m_out.VMV_RESULT = result;
487433d6423SLionel Sambuc 	job_m_out.VMV_REQID = req_id;
488433d6423SLionel Sambuc 
489433d6423SLionel Sambuc 	/* Reply asynchronously as VM may not be able to receive
490433d6423SLionel Sambuc 	 * an ipc_sendnb() message.
491433d6423SLionel Sambuc 	 */
492433d6423SLionel Sambuc 	job_m_out.m_type = VM_VFS_REPLY;
493433d6423SLionel Sambuc 	r = asynsend3(VM_PROC_NR, &job_m_out, 0);
494433d6423SLionel Sambuc 	if(r != OK) printf("VFS: couldn't asynsend3() to VM\n");
495433d6423SLionel Sambuc 
496433d6423SLionel Sambuc 	/* VFS does not reply any further */
497433d6423SLionel Sambuc 	return SUSPEND;
498433d6423SLionel Sambuc }
499433d6423SLionel Sambuc 
500433d6423SLionel Sambuc /*===========================================================================*
501433d6423SLionel Sambuc  *				pm_reboot				     *
502433d6423SLionel Sambuc  *===========================================================================*/
503a0814afbSRichard Sailer void
pm_reboot(void)504a0814afbSRichard Sailer pm_reboot(void)
505433d6423SLionel Sambuc {
506433d6423SLionel Sambuc /* Perform the VFS side of the reboot call. This call is performed from the PM
507433d6423SLionel Sambuc  * process context.
508433d6423SLionel Sambuc  */
509433d6423SLionel Sambuc   message m_out;
510433d6423SLionel Sambuc   int i, r;
511433d6423SLionel Sambuc   struct fproc *rfp, *pmfp;
512433d6423SLionel Sambuc 
513433d6423SLionel Sambuc   pmfp = fp;
514433d6423SLionel Sambuc 
515433d6423SLionel Sambuc   do_sync();
516433d6423SLionel Sambuc 
517433d6423SLionel Sambuc   /* Do exit processing for all leftover processes and servers, but don't
518433d6423SLionel Sambuc    * actually exit them (if they were really gone, PM will tell us about it).
519433d6423SLionel Sambuc    * Skip processes that handle parts of the file system; we first need to give
520433d6423SLionel Sambuc    * them the chance to unmount (which should be possible as all normal
521433d6423SLionel Sambuc    * processes have no open files anymore).
522433d6423SLionel Sambuc    */
523433d6423SLionel Sambuc   /* This is the only place where we allow special modification of "fp". The
524433d6423SLionel Sambuc    * reboot procedure should really be implemented as a PM message broadcasted
525433d6423SLionel Sambuc    * to all processes, so that each process will be shut down cleanly by a
526433d6423SLionel Sambuc    * thread operating on its behalf. Doing everything here is simpler, but it
527433d6423SLionel Sambuc    * requires an exception to the strict model of having "fp" be the process
528433d6423SLionel Sambuc    * that owns the current worker thread.
529433d6423SLionel Sambuc    */
530433d6423SLionel Sambuc   for (i = 0; i < NR_PROCS; i++) {
531433d6423SLionel Sambuc 	rfp = &fproc[i];
532433d6423SLionel Sambuc 
533433d6423SLionel Sambuc 	/* Don't just free the proc right away, but let it finish what it was
534433d6423SLionel Sambuc 	 * doing first */
535433d6423SLionel Sambuc 	if (rfp != fp) lock_proc(rfp);
536433d6423SLionel Sambuc 	if (rfp->fp_endpoint != NONE && find_vmnt(rfp->fp_endpoint) == NULL) {
537433d6423SLionel Sambuc 		worker_set_proc(rfp);	/* temporarily fake process context */
538433d6423SLionel Sambuc 		free_proc(0);
539433d6423SLionel Sambuc 		worker_set_proc(pmfp);	/* restore original process context */
540433d6423SLionel Sambuc 	}
541433d6423SLionel Sambuc 	if (rfp != fp) unlock_proc(rfp);
542433d6423SLionel Sambuc   }
543433d6423SLionel Sambuc 
544433d6423SLionel Sambuc   do_sync();
545433d6423SLionel Sambuc   unmount_all(0 /* Don't force */);
546433d6423SLionel Sambuc 
547433d6423SLionel Sambuc   /* Try to exit all processes again including File Servers */
548433d6423SLionel Sambuc   for (i = 0; i < NR_PROCS; i++) {
549433d6423SLionel Sambuc 	rfp = &fproc[i];
550433d6423SLionel Sambuc 
551433d6423SLionel Sambuc 	/* Don't just free the proc right away, but let it finish what it was
552433d6423SLionel Sambuc 	 * doing first */
553433d6423SLionel Sambuc 	if (rfp != fp) lock_proc(rfp);
554433d6423SLionel Sambuc 	if (rfp->fp_endpoint != NONE) {
555433d6423SLionel Sambuc 		worker_set_proc(rfp);	/* temporarily fake process context */
556433d6423SLionel Sambuc 		free_proc(0);
557433d6423SLionel Sambuc 		worker_set_proc(pmfp);	/* restore original process context */
558433d6423SLionel Sambuc 	}
559433d6423SLionel Sambuc 	if (rfp != fp) unlock_proc(rfp);
560433d6423SLionel Sambuc   }
561433d6423SLionel Sambuc 
562433d6423SLionel Sambuc   do_sync();
563433d6423SLionel Sambuc   unmount_all(1 /* Force */);
564433d6423SLionel Sambuc 
565433d6423SLionel Sambuc   /* Reply to PM for synchronization */
566433d6423SLionel Sambuc   memset(&m_out, 0, sizeof(m_out));
567433d6423SLionel Sambuc 
568433d6423SLionel Sambuc   m_out.m_type = VFS_PM_REBOOT_REPLY;
569433d6423SLionel Sambuc 
570433d6423SLionel Sambuc   if ((r = ipc_send(PM_PROC_NR, &m_out)) != OK)
571433d6423SLionel Sambuc 	panic("pm_reboot: ipc_send failed: %d", r);
572433d6423SLionel Sambuc }
573433d6423SLionel Sambuc 
574433d6423SLionel Sambuc /*===========================================================================*
575433d6423SLionel Sambuc  *				pm_fork					     *
576433d6423SLionel Sambuc  *===========================================================================*/
pm_fork(endpoint_t pproc,endpoint_t cproc,pid_t cpid)577433d6423SLionel Sambuc void pm_fork(endpoint_t pproc, endpoint_t cproc, pid_t cpid)
578433d6423SLionel Sambuc {
579433d6423SLionel Sambuc /* Perform those aspects of the fork() system call that relate to files.
580433d6423SLionel Sambuc  * In particular, let the child inherit its parent's file descriptors.
581433d6423SLionel Sambuc  * The parent and child parameters tell who forked off whom. The file
582433d6423SLionel Sambuc  * system uses the same slot numbers as the kernel.  Only PM makes this call.
583433d6423SLionel Sambuc  */
584*03ac74edSLionel Sambuc   struct fproc *cp;
585*03ac74edSLionel Sambuc #if !defined(NDEBUG)
586*03ac74edSLionel Sambuc   struct fproc *pp;
587*03ac74edSLionel Sambuc #endif /* !defined(NDEBUG) */
588433d6423SLionel Sambuc   int i, parentno, childno;
589433d6423SLionel Sambuc   mutex_t c_fp_lock;
590433d6423SLionel Sambuc 
591433d6423SLionel Sambuc   /* Check up-to-dateness of fproc. */
592433d6423SLionel Sambuc   okendpt(pproc, &parentno);
593433d6423SLionel Sambuc 
594433d6423SLionel Sambuc   /* PM gives child endpoint, which implies process slot information.
595433d6423SLionel Sambuc    * Don't call isokendpt, because that will verify if the endpoint
596433d6423SLionel Sambuc    * number is correct in fproc, which it won't be.
597433d6423SLionel Sambuc    */
598433d6423SLionel Sambuc   childno = _ENDPOINT_P(cproc);
599433d6423SLionel Sambuc   if (childno < 0 || childno >= NR_PROCS)
600433d6423SLionel Sambuc 	panic("VFS: bogus child for forking: %d", cproc);
601433d6423SLionel Sambuc   if (fproc[childno].fp_pid != PID_FREE)
602433d6423SLionel Sambuc 	panic("VFS: forking on top of in-use child: %d", childno);
603433d6423SLionel Sambuc 
604433d6423SLionel Sambuc   /* Copy the parent's fproc struct to the child. */
605433d6423SLionel Sambuc   /* However, the mutex variables belong to a slot and must stay the same. */
606433d6423SLionel Sambuc   c_fp_lock = fproc[childno].fp_lock;
607433d6423SLionel Sambuc   fproc[childno] = fproc[parentno];
608433d6423SLionel Sambuc   fproc[childno].fp_lock = c_fp_lock;
609433d6423SLionel Sambuc 
610433d6423SLionel Sambuc   /* Increase the counters in the 'filp' table. */
611433d6423SLionel Sambuc   cp = &fproc[childno];
612*03ac74edSLionel Sambuc #if !defined(NDEBUG)
613433d6423SLionel Sambuc   pp = &fproc[parentno];
614*03ac74edSLionel Sambuc #endif /* !defined(NDEBUG) */
615433d6423SLionel Sambuc 
616433d6423SLionel Sambuc   for (i = 0; i < OPEN_MAX; i++)
617433d6423SLionel Sambuc 	if (cp->fp_filp[i] != NULL) cp->fp_filp[i]->filp_count++;
618433d6423SLionel Sambuc 
619433d6423SLionel Sambuc   /* Fill in new process and endpoint id. */
620433d6423SLionel Sambuc   cp->fp_pid = cpid;
621433d6423SLionel Sambuc   cp->fp_endpoint = cproc;
622433d6423SLionel Sambuc 
623*03ac74edSLionel Sambuc #if !defined(NDEBUG)
624232819ddSDavid van Moolenbroek   /* A forking process cannot possibly be suspended on anything. */
625232819ddSDavid van Moolenbroek   assert(pp->fp_blocked_on == FP_BLOCKED_ON_NONE);
626*03ac74edSLionel Sambuc #endif /* !defined(NDEBUG) */
627433d6423SLionel Sambuc 
628433d6423SLionel Sambuc   /* A child is not a process leader, not being revived, etc. */
629433d6423SLionel Sambuc   cp->fp_flags = FP_NOFLAGS;
630433d6423SLionel Sambuc 
631433d6423SLionel Sambuc   /* Record the fact that both root and working dir have another user. */
632433d6423SLionel Sambuc   if (cp->fp_rd) dup_vnode(cp->fp_rd);
633433d6423SLionel Sambuc   if (cp->fp_wd) dup_vnode(cp->fp_wd);
634433d6423SLionel Sambuc }
635433d6423SLionel Sambuc 
636433d6423SLionel Sambuc /*===========================================================================*
637433d6423SLionel Sambuc  *				free_proc				     *
638433d6423SLionel Sambuc  *===========================================================================*/
free_proc(int flags)639433d6423SLionel Sambuc static void free_proc(int flags)
640433d6423SLionel Sambuc {
641433d6423SLionel Sambuc   int i;
642433d6423SLionel Sambuc   register struct fproc *rfp;
643433d6423SLionel Sambuc   register struct filp *rfilp;
644433d6423SLionel Sambuc   register struct vnode *vp;
645433d6423SLionel Sambuc   dev_t dev;
646433d6423SLionel Sambuc 
647433d6423SLionel Sambuc   if (fp->fp_endpoint == NONE)
648433d6423SLionel Sambuc 	panic("free_proc: already free");
649433d6423SLionel Sambuc 
650433d6423SLionel Sambuc   if (fp_is_blocked(fp))
651433d6423SLionel Sambuc 	unpause();
652433d6423SLionel Sambuc 
653433d6423SLionel Sambuc   /* Loop on file descriptors, closing any that are open. */
654433d6423SLionel Sambuc   for (i = 0; i < OPEN_MAX; i++) {
655491d647aSDavid van Moolenbroek 	(void) close_fd(fp, i, FALSE /*may_suspend*/);
656433d6423SLionel Sambuc   }
657433d6423SLionel Sambuc 
658433d6423SLionel Sambuc   /* Release root and working directories. */
659433d6423SLionel Sambuc   if (fp->fp_rd) { put_vnode(fp->fp_rd); fp->fp_rd = NULL; }
660433d6423SLionel Sambuc   if (fp->fp_wd) { put_vnode(fp->fp_wd); fp->fp_wd = NULL; }
661433d6423SLionel Sambuc 
662433d6423SLionel Sambuc   /* The rest of these actions is only done when processes actually exit. */
663433d6423SLionel Sambuc   if (!(flags & FP_EXITING)) return;
664433d6423SLionel Sambuc 
665433d6423SLionel Sambuc   fp->fp_flags |= FP_EXITING;
666433d6423SLionel Sambuc 
667433d6423SLionel Sambuc   /* Check if any process is SUSPENDed on this driver.
668433d6423SLionel Sambuc    * If a driver exits, unmap its entries in the dmap table.
669433d6423SLionel Sambuc    * (unmapping has to be done after the first step, because the
670e3b8d4bbSDavid van Moolenbroek    * dmap/smap tables are used in the first step.)
671433d6423SLionel Sambuc    */
672433d6423SLionel Sambuc   unsuspend_by_endpt(fp->fp_endpoint);
673433d6423SLionel Sambuc   dmap_unmap_by_endpt(fp->fp_endpoint);
674e3b8d4bbSDavid van Moolenbroek   smap_unmap_by_endpt(fp->fp_endpoint);
675433d6423SLionel Sambuc 
676433d6423SLionel Sambuc   worker_stop_by_endpt(fp->fp_endpoint); /* Unblock waiting threads */
677433d6423SLionel Sambuc   vmnt_unmap_by_endpt(fp->fp_endpoint); /* Invalidate open files if this
678433d6423SLionel Sambuc 					     * was an active FS */
679433d6423SLionel Sambuc 
680433d6423SLionel Sambuc   /* If a session leader exits and it has a controlling tty, then revoke
681433d6423SLionel Sambuc    * access to its controlling tty from all other processes using it.
682433d6423SLionel Sambuc    */
683433d6423SLionel Sambuc   if ((fp->fp_flags & FP_SESLDR) && fp->fp_tty != 0) {
684433d6423SLionel Sambuc       dev = fp->fp_tty;
685433d6423SLionel Sambuc       for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
686433d6423SLionel Sambuc 	  if(rfp->fp_pid == PID_FREE) continue;
687433d6423SLionel Sambuc           if (rfp->fp_tty == dev) rfp->fp_tty = 0;
688433d6423SLionel Sambuc 
689433d6423SLionel Sambuc           for (i = 0; i < OPEN_MAX; i++) {
690433d6423SLionel Sambuc 		if ((rfilp = rfp->fp_filp[i]) == NULL) continue;
691433d6423SLionel Sambuc 		if (rfilp->filp_mode == FILP_CLOSED) continue;
692433d6423SLionel Sambuc 		vp = rfilp->filp_vno;
693433d6423SLionel Sambuc 		if (!S_ISCHR(vp->v_mode)) continue;
694433d6423SLionel Sambuc 		if (vp->v_sdev != dev) continue;
695433d6423SLionel Sambuc 		lock_filp(rfilp, VNODE_READ);
696433d6423SLionel Sambuc 		(void) cdev_close(dev); /* Ignore any errors. */
69727d0ecdbSDavid van Moolenbroek 		/* FIXME: missing select check */
698433d6423SLionel Sambuc 		rfilp->filp_mode = FILP_CLOSED;
699433d6423SLionel Sambuc 		unlock_filp(rfilp);
700433d6423SLionel Sambuc           }
701433d6423SLionel Sambuc       }
702433d6423SLionel Sambuc   }
703433d6423SLionel Sambuc 
704433d6423SLionel Sambuc   /* Exit done. Mark slot as free. */
705433d6423SLionel Sambuc   fp->fp_endpoint = NONE;
706433d6423SLionel Sambuc   fp->fp_pid = PID_FREE;
707433d6423SLionel Sambuc   fp->fp_flags = FP_NOFLAGS;
708433d6423SLionel Sambuc }
709433d6423SLionel Sambuc 
710433d6423SLionel Sambuc /*===========================================================================*
711433d6423SLionel Sambuc  *				pm_exit					     *
712433d6423SLionel Sambuc  *===========================================================================*/
pm_exit(void)713433d6423SLionel Sambuc void pm_exit(void)
714433d6423SLionel Sambuc {
715433d6423SLionel Sambuc /* Perform the file system portion of the exit(status) system call.
716433d6423SLionel Sambuc  * This function is called from the context of the exiting process.
717433d6423SLionel Sambuc  */
718433d6423SLionel Sambuc 
719433d6423SLionel Sambuc   free_proc(FP_EXITING);
720433d6423SLionel Sambuc }
721433d6423SLionel Sambuc 
722433d6423SLionel Sambuc /*===========================================================================*
723433d6423SLionel Sambuc  *				pm_setgid				     *
724433d6423SLionel Sambuc  *===========================================================================*/
725a0814afbSRichard Sailer void
pm_setgid(endpoint_t proc_e,int egid,int rgid)726a0814afbSRichard Sailer pm_setgid(endpoint_t proc_e, int egid, int rgid)
727433d6423SLionel Sambuc {
728433d6423SLionel Sambuc   register struct fproc *tfp;
729433d6423SLionel Sambuc   int slot;
730433d6423SLionel Sambuc 
731433d6423SLionel Sambuc   okendpt(proc_e, &slot);
732433d6423SLionel Sambuc   tfp = &fproc[slot];
733433d6423SLionel Sambuc 
734433d6423SLionel Sambuc   tfp->fp_effgid =  egid;
735433d6423SLionel Sambuc   tfp->fp_realgid = rgid;
736433d6423SLionel Sambuc }
737433d6423SLionel Sambuc 
738433d6423SLionel Sambuc 
739433d6423SLionel Sambuc /*===========================================================================*
740433d6423SLionel Sambuc  *				pm_setgroups				     *
741433d6423SLionel Sambuc  *===========================================================================*/
742a0814afbSRichard Sailer void
pm_setgroups(endpoint_t proc_e,int ngroups,gid_t * groups)743a0814afbSRichard Sailer pm_setgroups(endpoint_t proc_e, int ngroups, gid_t *groups)
744433d6423SLionel Sambuc {
745433d6423SLionel Sambuc   struct fproc *rfp;
746433d6423SLionel Sambuc   int slot;
747433d6423SLionel Sambuc 
748433d6423SLionel Sambuc   okendpt(proc_e, &slot);
749433d6423SLionel Sambuc   rfp = &fproc[slot];
750433d6423SLionel Sambuc   if (ngroups * sizeof(gid_t) > sizeof(rfp->fp_sgroups))
751433d6423SLionel Sambuc 	panic("VFS: pm_setgroups: too much data to copy");
752433d6423SLionel Sambuc   if (sys_datacopy_wrapper(who_e, (vir_bytes) groups, SELF, (vir_bytes) rfp->fp_sgroups,
753433d6423SLionel Sambuc 		   ngroups * sizeof(gid_t)) == OK) {
754433d6423SLionel Sambuc 	rfp->fp_ngroups = ngroups;
755433d6423SLionel Sambuc   } else
756433d6423SLionel Sambuc 	panic("VFS: pm_setgroups: datacopy failed");
757433d6423SLionel Sambuc }
758433d6423SLionel Sambuc 
759433d6423SLionel Sambuc 
760433d6423SLionel Sambuc /*===========================================================================*
761433d6423SLionel Sambuc  *				pm_setuid				     *
762433d6423SLionel Sambuc  *===========================================================================*/
763a0814afbSRichard Sailer void
pm_setuid(endpoint_t proc_e,int euid,int ruid)764a0814afbSRichard Sailer pm_setuid(endpoint_t proc_e, int euid, int ruid)
765433d6423SLionel Sambuc {
766433d6423SLionel Sambuc   struct fproc *tfp;
767433d6423SLionel Sambuc   int slot;
768433d6423SLionel Sambuc 
769433d6423SLionel Sambuc   okendpt(proc_e, &slot);
770433d6423SLionel Sambuc   tfp = &fproc[slot];
771433d6423SLionel Sambuc 
772433d6423SLionel Sambuc   tfp->fp_effuid =  euid;
773433d6423SLionel Sambuc   tfp->fp_realuid = ruid;
774433d6423SLionel Sambuc }
775433d6423SLionel Sambuc 
776433d6423SLionel Sambuc /*===========================================================================*
777433d6423SLionel Sambuc  *				pm_setsid				     *
778433d6423SLionel Sambuc  *===========================================================================*/
pm_setsid(endpoint_t proc_e)779433d6423SLionel Sambuc void pm_setsid(endpoint_t proc_e)
780433d6423SLionel Sambuc {
781433d6423SLionel Sambuc /* Perform the VFS side of the SETSID call, i.e. get rid of the controlling
782433d6423SLionel Sambuc  * terminal of a process, and make the process a session leader.
783433d6423SLionel Sambuc  */
784433d6423SLionel Sambuc   struct fproc *rfp;
785433d6423SLionel Sambuc   int slot;
786433d6423SLionel Sambuc 
787433d6423SLionel Sambuc   /* Make the process a session leader with no controlling tty. */
788433d6423SLionel Sambuc   okendpt(proc_e, &slot);
789433d6423SLionel Sambuc   rfp = &fproc[slot];
790433d6423SLionel Sambuc   rfp->fp_flags |= FP_SESLDR;
791433d6423SLionel Sambuc   rfp->fp_tty = 0;
792433d6423SLionel Sambuc }
793433d6423SLionel Sambuc 
794433d6423SLionel Sambuc /*===========================================================================*
795433d6423SLionel Sambuc  *				do_svrctl				     *
796433d6423SLionel Sambuc  *===========================================================================*/
do_svrctl(void)797433d6423SLionel Sambuc int do_svrctl(void)
798433d6423SLionel Sambuc {
799f737eea6SDavid van Moolenbroek   unsigned long svrctl;
800433d6423SLionel Sambuc   vir_bytes ptr;
801433d6423SLionel Sambuc 
802f737eea6SDavid van Moolenbroek   svrctl = job_m_in.m_lc_svrctl.request;
803f737eea6SDavid van Moolenbroek   ptr = job_m_in.m_lc_svrctl.arg;
804f737eea6SDavid van Moolenbroek 
805f737eea6SDavid van Moolenbroek   if (IOCGROUP(svrctl) != 'F') return(EINVAL);
806433d6423SLionel Sambuc 
807433d6423SLionel Sambuc   switch (svrctl) {
808433d6423SLionel Sambuc     case VFSSETPARAM:
809433d6423SLionel Sambuc     case VFSGETPARAM:
810433d6423SLionel Sambuc 	{
811433d6423SLionel Sambuc 		struct sysgetenv sysgetenv;
812433d6423SLionel Sambuc 		char search_key[64];
813433d6423SLionel Sambuc 		char val[64];
814433d6423SLionel Sambuc 		int r, s;
815433d6423SLionel Sambuc 
816433d6423SLionel Sambuc 		/* Copy sysgetenv structure to VFS */
817433d6423SLionel Sambuc 		if (sys_datacopy_wrapper(who_e, ptr, SELF, (vir_bytes) &sysgetenv,
818433d6423SLionel Sambuc 				 sizeof(sysgetenv)) != OK)
819433d6423SLionel Sambuc 			return(EFAULT);
820433d6423SLionel Sambuc 
821433d6423SLionel Sambuc 		/* Basic sanity checking */
822433d6423SLionel Sambuc 		if (svrctl == VFSSETPARAM) {
823433d6423SLionel Sambuc 			if (sysgetenv.keylen <= 0 ||
824433d6423SLionel Sambuc 			    sysgetenv.keylen > (sizeof(search_key) - 1) ||
825433d6423SLionel Sambuc 			    sysgetenv.vallen <= 0 ||
826433d6423SLionel Sambuc 			    sysgetenv.vallen >= sizeof(val)) {
827433d6423SLionel Sambuc 				return(EINVAL);
828433d6423SLionel Sambuc 			}
829433d6423SLionel Sambuc 		}
830433d6423SLionel Sambuc 
831433d6423SLionel Sambuc 		/* Copy parameter "key" */
832433d6423SLionel Sambuc 		if ((s = sys_datacopy_wrapper(who_e, (vir_bytes) sysgetenv.key,
833433d6423SLionel Sambuc 				      SELF, (vir_bytes) search_key,
834433d6423SLionel Sambuc 				      sysgetenv.keylen)) != OK)
835433d6423SLionel Sambuc 			return(s);
836433d6423SLionel Sambuc 		search_key[sysgetenv.keylen] = '\0'; /* Limit string */
837433d6423SLionel Sambuc 
838433d6423SLionel Sambuc 		/* Is it a parameter we know? */
839433d6423SLionel Sambuc 		if (svrctl == VFSSETPARAM) {
840433d6423SLionel Sambuc 			if (!strcmp(search_key, "verbose")) {
841433d6423SLionel Sambuc 				int verbose_val;
842433d6423SLionel Sambuc 				if ((s = sys_datacopy_wrapper(who_e,
843433d6423SLionel Sambuc 				    (vir_bytes) sysgetenv.val, SELF,
844433d6423SLionel Sambuc 				    (vir_bytes) &val, sysgetenv.vallen)) != OK)
845433d6423SLionel Sambuc 					return(s);
846433d6423SLionel Sambuc 				val[sysgetenv.vallen] = '\0'; /* Limit string */
847433d6423SLionel Sambuc 				verbose_val = atoi(val);
848433d6423SLionel Sambuc 				if (verbose_val < 0 || verbose_val > 4) {
849433d6423SLionel Sambuc 					return(EINVAL);
850433d6423SLionel Sambuc 				}
851433d6423SLionel Sambuc 				verbose = verbose_val;
852433d6423SLionel Sambuc 				r = OK;
853433d6423SLionel Sambuc 			} else {
854433d6423SLionel Sambuc 				r = ESRCH;
855433d6423SLionel Sambuc 			}
856433d6423SLionel Sambuc 		} else { /* VFSGETPARAM */
857433d6423SLionel Sambuc 			char small_buf[60];
858433d6423SLionel Sambuc 
859433d6423SLionel Sambuc 			r = ESRCH;
860433d6423SLionel Sambuc 			if (!strcmp(search_key, "print_traces")) {
861433d6423SLionel Sambuc 				mthread_stacktraces();
862433d6423SLionel Sambuc 				sysgetenv.val = 0;
863433d6423SLionel Sambuc 				sysgetenv.vallen = 0;
864433d6423SLionel Sambuc 				r = OK;
86563faa8feSDavid van Moolenbroek 			} else if (!strcmp(search_key, "print_select")) {
86663faa8feSDavid van Moolenbroek 				select_dump();
86763faa8feSDavid van Moolenbroek 				sysgetenv.val = 0;
86863faa8feSDavid van Moolenbroek 				sysgetenv.vallen = 0;
86963faa8feSDavid van Moolenbroek 				r = OK;
870433d6423SLionel Sambuc 			} else if (!strcmp(search_key, "active_threads")) {
871433d6423SLionel Sambuc 				int active = NR_WTHREADS - worker_available();
872433d6423SLionel Sambuc 				snprintf(small_buf, sizeof(small_buf) - 1,
873433d6423SLionel Sambuc 					 "%d", active);
874433d6423SLionel Sambuc 				sysgetenv.vallen = strlen(small_buf);
875433d6423SLionel Sambuc 				r = OK;
876433d6423SLionel Sambuc 			}
877433d6423SLionel Sambuc 
878433d6423SLionel Sambuc 			if (r == OK) {
879433d6423SLionel Sambuc 				if ((s = sys_datacopy_wrapper(SELF,
880433d6423SLionel Sambuc 				    (vir_bytes) &sysgetenv, who_e, ptr,
881433d6423SLionel Sambuc 				    sizeof(sysgetenv))) != OK)
882433d6423SLionel Sambuc 					return(s);
883433d6423SLionel Sambuc 				if (sysgetenv.val != 0) {
884433d6423SLionel Sambuc 					if ((s = sys_datacopy_wrapper(SELF,
885433d6423SLionel Sambuc 					    (vir_bytes) small_buf, who_e,
886433d6423SLionel Sambuc 					    (vir_bytes) sysgetenv.val,
887433d6423SLionel Sambuc 					    sysgetenv.vallen)) != OK)
888433d6423SLionel Sambuc 						return(s);
889433d6423SLionel Sambuc 				}
890433d6423SLionel Sambuc 			}
891433d6423SLionel Sambuc 		}
892433d6423SLionel Sambuc 
893433d6423SLionel Sambuc 		return(r);
894433d6423SLionel Sambuc 	}
895433d6423SLionel Sambuc     default:
896433d6423SLionel Sambuc 	return(EINVAL);
897433d6423SLionel Sambuc   }
898433d6423SLionel Sambuc }
899433d6423SLionel Sambuc 
900433d6423SLionel Sambuc /*===========================================================================*
901433d6423SLionel Sambuc  *				pm_dumpcore				     *
902433d6423SLionel Sambuc  *===========================================================================*/
pm_dumpcore(int csig,vir_bytes exe_name)903433d6423SLionel Sambuc int pm_dumpcore(int csig, vir_bytes exe_name)
904433d6423SLionel Sambuc {
9059f15e7b3SDavid van Moolenbroek   int r, core_fd;
906433d6423SLionel Sambuc   struct filp *f;
907433d6423SLionel Sambuc   char core_path[PATH_MAX];
908433d6423SLionel Sambuc   char proc_name[PROC_NAME_LEN];
909433d6423SLionel Sambuc 
910232819ddSDavid van Moolenbroek   /* In effect, the coredump is generated through the use of calls as if made
911232819ddSDavid van Moolenbroek    * by the process itself.  As such, the process must not be doing anything
912232819ddSDavid van Moolenbroek    * else.  Therefore, if the process was blocked on anything, unblock it
913232819ddSDavid van Moolenbroek    * first.  This step is the reason we cannot use this function to generate a
914232819ddSDavid van Moolenbroek    * core dump of a process while it is still running (i.e., without
915232819ddSDavid van Moolenbroek    * terminating it), as it changes the state of the process.
916433d6423SLionel Sambuc    */
917433d6423SLionel Sambuc   if (fp_is_blocked(fp))
918433d6423SLionel Sambuc           unpause();
919433d6423SLionel Sambuc 
920433d6423SLionel Sambuc   /* open core file */
921433d6423SLionel Sambuc   snprintf(core_path, PATH_MAX, "%s.%d", CORE_NAME, fp->fp_pid);
9229f15e7b3SDavid van Moolenbroek   r = core_fd = common_open(core_path, O_WRONLY | O_CREAT | O_TRUNC,
92356ac45c1SDavid van Moolenbroek 	CORE_MODE, FALSE /*for_exec*/);
9249f15e7b3SDavid van Moolenbroek   if (r < 0) goto core_exit;
925433d6423SLionel Sambuc 
9269f15e7b3SDavid van Moolenbroek   /* get process name */
9279f15e7b3SDavid van Moolenbroek   r = sys_datacopy_wrapper(PM_PROC_NR, exe_name, VFS_PROC_NR,
9289f15e7b3SDavid van Moolenbroek 	(vir_bytes) proc_name, PROC_NAME_LEN);
929433d6423SLionel Sambuc   if (r != OK) goto core_exit;
930433d6423SLionel Sambuc   proc_name[PROC_NAME_LEN - 1] = '\0';
931433d6423SLionel Sambuc 
9329f15e7b3SDavid van Moolenbroek   /* write the core dump */
9339f15e7b3SDavid van Moolenbroek   f = get_filp(core_fd, VNODE_WRITE);
9349f15e7b3SDavid van Moolenbroek   assert(f != NULL);
935433d6423SLionel Sambuc   write_elf_core_file(f, csig, proc_name);
936433d6423SLionel Sambuc   unlock_filp(f);
937433d6423SLionel Sambuc 
938433d6423SLionel Sambuc core_exit:
9399f15e7b3SDavid van Moolenbroek   /* The core file descriptor will be closed as part of the process exit. */
940433d6423SLionel Sambuc   free_proc(FP_EXITING);
9419f15e7b3SDavid van Moolenbroek 
942433d6423SLionel Sambuc   return(r);
943433d6423SLionel Sambuc }
944433d6423SLionel Sambuc 
945433d6423SLionel Sambuc /*===========================================================================*
946433d6423SLionel Sambuc  *				 ds_event				     *
947433d6423SLionel Sambuc  *===========================================================================*/
948433d6423SLionel Sambuc void
ds_event(void)949433d6423SLionel Sambuc ds_event(void)
950433d6423SLionel Sambuc {
951433d6423SLionel Sambuc   char key[DS_MAX_KEYLEN];
952433d6423SLionel Sambuc   char *blkdrv_prefix = "drv.blk.";
953433d6423SLionel Sambuc   char *chrdrv_prefix = "drv.chr.";
954e3b8d4bbSDavid van Moolenbroek   char *sckdrv_prefix = "drv.sck.";
955433d6423SLionel Sambuc   u32_t value;
956e3b8d4bbSDavid van Moolenbroek   int type, ftype, r;
957433d6423SLionel Sambuc   endpoint_t owner_endpoint;
958433d6423SLionel Sambuc 
959433d6423SLionel Sambuc   /* Get the event and the owner from DS. */
960433d6423SLionel Sambuc   while ((r = ds_check(key, &type, &owner_endpoint)) == OK) {
961e3b8d4bbSDavid van Moolenbroek 	/* Only check for block, character, socket driver up events. */
962433d6423SLionel Sambuc 	if (!strncmp(key, blkdrv_prefix, strlen(blkdrv_prefix))) {
963e3b8d4bbSDavid van Moolenbroek 		ftype = S_IFBLK;
964433d6423SLionel Sambuc 	} else if (!strncmp(key, chrdrv_prefix, strlen(chrdrv_prefix))) {
965e3b8d4bbSDavid van Moolenbroek 		ftype = S_IFCHR;
966e3b8d4bbSDavid van Moolenbroek 	} else if (!strncmp(key, sckdrv_prefix, strlen(sckdrv_prefix))) {
967e3b8d4bbSDavid van Moolenbroek 		ftype = S_IFSOCK;
968433d6423SLionel Sambuc 	} else {
969433d6423SLionel Sambuc 		continue;
970433d6423SLionel Sambuc 	}
971433d6423SLionel Sambuc 
972433d6423SLionel Sambuc 	if ((r = ds_retrieve_u32(key, &value)) != OK) {
973433d6423SLionel Sambuc 		printf("VFS: ds_event: ds_retrieve_u32 failed\n");
974433d6423SLionel Sambuc 		break;
975433d6423SLionel Sambuc 	}
976433d6423SLionel Sambuc 	if (value != DS_DRIVER_UP) continue;
977433d6423SLionel Sambuc 
978433d6423SLionel Sambuc 	/* Perform up. */
979e3b8d4bbSDavid van Moolenbroek 	if (ftype == S_IFBLK || ftype == S_IFCHR)
980e3b8d4bbSDavid van Moolenbroek 		dmap_endpt_up(owner_endpoint, (ftype == S_IFBLK));
981e3b8d4bbSDavid van Moolenbroek 	else
982e3b8d4bbSDavid van Moolenbroek 		smap_endpt_up(owner_endpoint);
983433d6423SLionel Sambuc   }
984433d6423SLionel Sambuc 
985433d6423SLionel Sambuc   if (r != ENOENT) printf("VFS: ds_event: ds_check failed: %d\n", r);
986433d6423SLionel Sambuc }
987433d6423SLionel Sambuc 
988433d6423SLionel Sambuc /* A function to be called on panic(). */
panic_hook(void)989433d6423SLionel Sambuc void panic_hook(void)
990433d6423SLionel Sambuc {
991433d6423SLionel Sambuc   printf("VFS mthread stacktraces:\n");
992433d6423SLionel Sambuc   mthread_stacktraces();
993433d6423SLionel Sambuc }
994433d6423SLionel Sambuc 
995433d6423SLionel Sambuc /*===========================================================================*
996433d6423SLionel Sambuc  *				do_getrusage				     *
997433d6423SLionel Sambuc  *===========================================================================*/
do_getrusage(void)998433d6423SLionel Sambuc int do_getrusage(void)
999433d6423SLionel Sambuc {
1000bc2d75faSDavid van Moolenbroek 	/* Obsolete vfs_getrusage(2) call from userland. The getrusage call is
1001bc2d75faSDavid van Moolenbroek 	 * now fully handled by PM, and for any future fields that should be
1002bc2d75faSDavid van Moolenbroek 	 * supplied by VFS, VFS should be queried by PM rather than by the user
1003bc2d75faSDavid van Moolenbroek 	 * program directly.  TODO: remove this call after the next release.
1004bc2d75faSDavid van Moolenbroek 	 */
1005bc2d75faSDavid van Moolenbroek 	return OK;
1006433d6423SLionel Sambuc }
1007