import / small alignment of many arm includes
[minix3.git] / servers / vfs / misc.c
blobb78bf6a635cee08d149dcb7bb66df0d0a6d01a6d
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.
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
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"
41 #define CORE_NAME "core"
42 #define CORE_MODE 0777 /* mode to use on core image files */
44 #if ENABLE_SYSCALL_STATS
45 unsigned long calls_stats[NR_VFS_CALLS];
46 #endif
48 static void free_proc(int flags);
50 /*===========================================================================*
51 * do_getsysinfo *
52 *===========================================================================*/
53 int do_getsysinfo(void)
55 vir_bytes src_addr, dst_addr;
56 size_t len, buf_size;
57 int what;
59 what = job_m_in.SI_WHAT;
60 dst_addr = (vir_bytes) job_m_in.SI_WHERE;
61 buf_size = (size_t) job_m_in.SI_SIZE;
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.
68 if (!super_user) return(EPERM);
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);
89 if (len != buf_size)
90 return(EINVAL);
92 return sys_datacopy(SELF, src_addr, who_e, dst_addr, len);
95 /*===========================================================================*
96 * do_fcntl *
97 *===========================================================================*/
98 int do_fcntl(void)
100 /* Perform the fcntl(fd, cmd, ...) system call. */
102 register struct filp *f;
103 int new_fd, fl, r = OK, fcntl_req, fcntl_argx;
104 tll_access_t locktype;
106 scratch(fp).file.fd_nr = job_m_in.VFS_FCNTL_FD;
107 scratch(fp).io.io_buffer = job_m_in.VFS_FCNTL_ARG_PTR;
108 scratch(fp).io.io_nbytes = job_m_in.VFS_FCNTL_CMD;
109 fcntl_req = job_m_in.VFS_FCNTL_CMD;
110 fcntl_argx = job_m_in.VFS_FCNTL_ARG_INT;
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);
117 switch (fcntl_req) {
118 case F_DUPFD:
119 /* This replaces the old dup() system call. */
120 if (fcntl_argx < 0 || fcntl_argx >= OPEN_MAX) r = EINVAL;
121 else if ((r = get_fd(fp, fcntl_argx, 0, &new_fd, NULL)) == OK) {
122 f->filp_count++;
123 fp->fp_filp[new_fd] = f;
124 r = new_fd;
126 break;
128 case F_GETFD:
129 /* Get close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
130 r = 0;
131 if (FD_ISSET(scratch(fp).file.fd_nr, &fp->fp_cloexec_set))
132 r = FD_CLOEXEC;
133 break;
135 case F_SETFD:
136 /* Set close-on-exec flag (FD_CLOEXEC in POSIX Table 6-2). */
137 if (fcntl_argx & FD_CLOEXEC)
138 FD_SET(scratch(fp).file.fd_nr, &fp->fp_cloexec_set);
139 else
140 FD_CLR(scratch(fp).file.fd_nr, &fp->fp_cloexec_set);
141 break;
143 case F_GETFL:
144 /* Get file status flags (O_NONBLOCK and O_APPEND). */
145 fl = f->filp_flags & (O_NONBLOCK | O_APPEND | O_ACCMODE);
146 r = fl;
147 break;
149 case F_SETFL:
150 /* Set file status flags (O_NONBLOCK and O_APPEND). */
151 fl = O_NONBLOCK | O_APPEND | O_REOPEN;
152 f->filp_flags = (f->filp_flags & ~fl) | (fcntl_argx & fl);
153 break;
155 case F_GETLK:
156 case F_SETLK:
157 case F_SETLKW:
158 /* Set or clear a file lock. */
159 r = lock_op(f, fcntl_req);
160 break;
162 case F_FREESP:
164 /* Free a section of a file */
165 off_t start, end, offset;
166 struct flock flock_arg;
168 /* Check if it's a regular file. */
169 if (!S_ISREG(f->filp_vno->v_mode)) r = EINVAL;
170 else if (!(f->filp_mode & W_BIT)) r = EBADF;
171 else {
172 /* Copy flock data from userspace. */
173 r = sys_datacopy(who_e, (vir_bytes) scratch(fp).io.io_buffer,
174 SELF, (vir_bytes) &flock_arg, sizeof(flock_arg));
177 if (r != OK) break;
179 /* Convert starting offset to signed. */
180 offset = (off_t) flock_arg.l_start;
182 /* Figure out starting position base. */
183 switch(flock_arg.l_whence) {
184 case SEEK_SET: start = 0; break;
185 case SEEK_CUR: start = f->filp_pos; break;
186 case SEEK_END: start = f->filp_vno->v_size; break;
187 default: r = EINVAL;
189 if (r != OK) break;
191 /* Check for overflow or underflow. */
192 if (offset > 0 && start + offset < start) r = EINVAL;
193 else if (offset < 0 && start + offset > start) r = EINVAL;
194 else {
195 start += offset;
196 if (start < 0) r = EINVAL;
198 if (r != OK) break;
200 if (flock_arg.l_len != 0) {
201 if (start >= f->filp_vno->v_size) r = EINVAL;
202 else if ((end = start + flock_arg.l_len) <= start) r = EINVAL;
203 else if (end > f->filp_vno->v_size) end = f->filp_vno->v_size;
204 } else {
205 end = 0;
207 if (r != OK) break;
209 r = req_ftrunc(f->filp_vno->v_fs_e, f->filp_vno->v_inode_nr,start,end);
211 if (r == OK && flock_arg.l_len == 0)
212 f->filp_vno->v_size = start;
214 break;
216 case F_GETNOSIGPIPE:
217 /* POSIX: return value other than -1 is flag is set, else -1 */
218 r = -1;
219 if (f->filp_flags & O_NOSIGPIPE)
220 r = 0;
221 break;
222 case F_SETNOSIGPIPE:
223 fl = (O_NOSIGPIPE);
224 f->filp_flags = (f->filp_flags & ~fl) | (fcntl_argx & fl);
225 break;
226 default:
227 r = EINVAL;
230 unlock_filp(f);
231 return(r);
234 /*===========================================================================*
235 * do_sync *
236 *===========================================================================*/
237 int do_sync(void)
239 struct vmnt *vmp;
240 int r = OK;
242 for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
243 if ((r = lock_vmnt(vmp, VMNT_READ)) != OK)
244 break;
245 if (vmp->m_dev != NO_DEV && vmp->m_fs_e != NONE &&
246 vmp->m_root_node != NULL) {
247 req_sync(vmp->m_fs_e);
249 unlock_vmnt(vmp);
252 return(r);
255 /*===========================================================================*
256 * do_fsync *
257 *===========================================================================*/
258 int do_fsync(void)
260 /* Perform the fsync() system call. */
261 struct filp *rfilp;
262 struct vmnt *vmp;
263 dev_t dev;
264 int r = OK;
266 scratch(fp).file.fd_nr = job_m_in.VFS_FSYNC_FD;
268 if ((rfilp = get_filp(scratch(fp).file.fd_nr, VNODE_READ)) == NULL)
269 return(err_code);
271 dev = rfilp->filp_vno->v_dev;
272 unlock_filp(rfilp);
274 for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
275 if (vmp->m_dev != dev) continue;
276 if ((r = lock_vmnt(vmp, VMNT_READ)) != OK)
277 break;
278 if (vmp->m_dev != NO_DEV && vmp->m_dev == dev &&
279 vmp->m_fs_e != NONE && vmp->m_root_node != NULL) {
281 req_sync(vmp->m_fs_e);
283 unlock_vmnt(vmp);
286 return(r);
289 int dupvm(struct fproc *rfp, int pfd, int *vmfd, struct filp **newfilp)
291 int result, procfd;
292 struct filp *f = NULL;
293 struct fproc *vmf = fproc_addr(VM_PROC_NR);
295 *newfilp = NULL;
297 if ((f = get_filp2(rfp, pfd, VNODE_READ)) == NULL) {
298 printf("VFS dupvm: get_filp2 failed\n");
299 return EBADF;
302 if(!(f->filp_vno->v_vmnt->m_fs_flags & RES_HASPEEK)) {
303 unlock_filp(f);
304 #if 0 /* Noisy diagnostic for mmap() by ld.so */
305 printf("VFS dupvm: no peek available\n");
306 #endif
307 return EINVAL;
310 assert(f->filp_vno);
311 assert(f->filp_vno->v_vmnt);
313 if (!S_ISREG(f->filp_vno->v_mode) && !S_ISBLK(f->filp_vno->v_mode)) {
314 printf("VFS: mmap regular/blockdev only; dev 0x%llx ino %llu has mode 0%o\n",
315 f->filp_vno->v_dev, f->filp_vno->v_inode_nr, f->filp_vno->v_mode);
316 unlock_filp(f);
317 return EINVAL;
320 /* get free FD in VM */
321 if((result=get_fd(vmf, 0, 0, &procfd, NULL)) != OK) {
322 unlock_filp(f);
323 printf("VFS dupvm: getfd failed\n");
324 return result;
327 *vmfd = procfd;
329 f->filp_count++;
330 assert(f->filp_count > 0);
331 vmf->fp_filp[procfd] = f;
333 *newfilp = f;
335 return OK;
338 /*===========================================================================*
339 * do_vm_call *
340 *===========================================================================*/
341 int do_vm_call(void)
343 /* A call that VM does to VFS.
344 * We must reply with the fixed type VM_VFS_REPLY (and put our result info
345 * in the rest of the message) so VM can tell the difference between a
346 * request from VFS and a reply to this call.
348 int req = job_m_in.VFS_VMCALL_REQ;
349 int req_fd = job_m_in.VFS_VMCALL_FD;
350 u32_t req_id = job_m_in.VFS_VMCALL_REQID;
351 endpoint_t ep = job_m_in.VFS_VMCALL_ENDPOINT;
352 u64_t offset = make64(job_m_in.VFS_VMCALL_OFFSET_LO,
353 job_m_in.VFS_VMCALL_OFFSET_HI);
354 u32_t length = job_m_in.VFS_VMCALL_LENGTH;
355 int result = OK;
356 int slot;
357 struct fproc *rfp, *vmf;
358 struct filp *f = NULL;
359 int r;
361 if(job_m_in.m_source != VM_PROC_NR)
362 return ENOSYS;
364 if(isokendpt(ep, &slot) != OK) rfp = NULL;
365 else rfp = &fproc[slot];
367 vmf = fproc_addr(VM_PROC_NR);
368 assert(fp == vmf);
369 assert(rfp != vmf);
371 switch(req) {
372 case VMVFSREQ_FDLOOKUP:
374 int procfd;
376 /* Lookup fd in referenced process. */
378 if(!rfp) {
379 printf("VFS: why isn't ep %d here?!\n", ep);
380 result = ESRCH;
381 goto reqdone;
384 if((result = dupvm(rfp, req_fd, &procfd, &f)) != OK) {
385 #if 0 /* Noisy diagnostic for mmap() by ld.so */
386 printf("vfs: dupvm failed\n");
387 #endif
388 goto reqdone;
391 if(S_ISBLK(f->filp_vno->v_mode)) {
392 assert(f->filp_vno->v_sdev != NO_DEV);
393 job_m_out.VMV_DEV = f->filp_vno->v_sdev;
394 job_m_out.VMV_INO = VMC_NO_INODE;
395 job_m_out.VMV_SIZE_PAGES = LONG_MAX;
396 } else {
397 job_m_out.VMV_DEV = f->filp_vno->v_dev;
398 job_m_out.VMV_INO = f->filp_vno->v_inode_nr;
399 job_m_out.VMV_SIZE_PAGES =
400 roundup(f->filp_vno->v_size,
401 PAGE_SIZE)/PAGE_SIZE;
404 job_m_out.VMV_FD = procfd;
406 result = OK;
408 break;
410 case VMVFSREQ_FDCLOSE:
412 result = close_fd(fp, req_fd);
413 if(result != OK) {
414 printf("VFS: VM fd close for fd %d, %d (%d)\n",
415 req_fd, fp->fp_endpoint, result);
417 break;
419 case VMVFSREQ_FDIO:
421 result = actual_lseek(fp, req_fd, SEEK_SET, offset,
422 NULL);
424 if(result == OK) {
425 result = actual_read_write_peek(fp, PEEKING,
426 req_fd, NULL, length);
429 break;
431 default:
432 panic("VFS: bad request code from VM\n");
433 break;
436 reqdone:
437 if(f)
438 unlock_filp(f);
440 /* fp is VM still. */
441 assert(fp == vmf);
442 job_m_out.VMV_ENDPOINT = ep;
443 job_m_out.VMV_RESULT = result;
444 job_m_out.VMV_REQID = req_id;
446 /* Reply asynchronously as VM may not be able to receive
447 * an ipc_sendnb() message.
449 job_m_out.m_type = VM_VFS_REPLY;
450 r = asynsend3(VM_PROC_NR, &job_m_out, 0);
451 if(r != OK) printf("VFS: couldn't asynsend3() to VM\n");
453 /* VFS does not reply any further */
454 return SUSPEND;
457 /*===========================================================================*
458 * pm_reboot *
459 *===========================================================================*/
460 void pm_reboot()
462 /* Perform the VFS side of the reboot call. This call is performed from the PM
463 * process context.
465 message m_out;
466 int i, r;
467 struct fproc *rfp, *pmfp;
469 pmfp = fp;
471 do_sync();
473 /* Do exit processing for all leftover processes and servers, but don't
474 * actually exit them (if they were really gone, PM will tell us about it).
475 * Skip processes that handle parts of the file system; we first need to give
476 * them the chance to unmount (which should be possible as all normal
477 * processes have no open files anymore).
479 /* This is the only place where we allow special modification of "fp". The
480 * reboot procedure should really be implemented as a PM message broadcasted
481 * to all processes, so that each process will be shut down cleanly by a
482 * thread operating on its behalf. Doing everything here is simpler, but it
483 * requires an exception to the strict model of having "fp" be the process
484 * that owns the current worker thread.
486 for (i = 0; i < NR_PROCS; i++) {
487 rfp = &fproc[i];
489 /* Don't just free the proc right away, but let it finish what it was
490 * doing first */
491 if (rfp != fp) lock_proc(rfp);
492 if (rfp->fp_endpoint != NONE && find_vmnt(rfp->fp_endpoint) == NULL) {
493 worker_set_proc(rfp); /* temporarily fake process context */
494 free_proc(0);
495 worker_set_proc(pmfp); /* restore original process context */
497 if (rfp != fp) unlock_proc(rfp);
500 do_sync();
501 unmount_all(0 /* Don't force */);
503 /* Try to exit all processes again including File Servers */
504 for (i = 0; i < NR_PROCS; i++) {
505 rfp = &fproc[i];
507 /* Don't just free the proc right away, but let it finish what it was
508 * doing first */
509 if (rfp != fp) lock_proc(rfp);
510 if (rfp->fp_endpoint != NONE) {
511 worker_set_proc(rfp); /* temporarily fake process context */
512 free_proc(0);
513 worker_set_proc(pmfp); /* restore original process context */
515 if (rfp != fp) unlock_proc(rfp);
518 do_sync();
519 unmount_all(1 /* Force */);
521 /* Reply to PM for synchronization */
522 memset(&m_out, 0, sizeof(m_out));
524 m_out.m_type = VFS_PM_REBOOT_REPLY;
526 if ((r = ipc_send(PM_PROC_NR, &m_out)) != OK)
527 panic("pm_reboot: ipc_send failed: %d", r);
530 /*===========================================================================*
531 * pm_fork *
532 *===========================================================================*/
533 void pm_fork(endpoint_t pproc, endpoint_t cproc, pid_t cpid)
535 /* Perform those aspects of the fork() system call that relate to files.
536 * In particular, let the child inherit its parent's file descriptors.
537 * The parent and child parameters tell who forked off whom. The file
538 * system uses the same slot numbers as the kernel. Only PM makes this call.
541 struct fproc *cp, *pp;
542 int i, parentno, childno;
543 mutex_t c_fp_lock;
545 /* Check up-to-dateness of fproc. */
546 okendpt(pproc, &parentno);
548 /* PM gives child endpoint, which implies process slot information.
549 * Don't call isokendpt, because that will verify if the endpoint
550 * number is correct in fproc, which it won't be.
552 childno = _ENDPOINT_P(cproc);
553 if (childno < 0 || childno >= NR_PROCS)
554 panic("VFS: bogus child for forking: %d", cproc);
555 if (fproc[childno].fp_pid != PID_FREE)
556 panic("VFS: forking on top of in-use child: %d", childno);
558 /* Copy the parent's fproc struct to the child. */
559 /* However, the mutex variables belong to a slot and must stay the same. */
560 c_fp_lock = fproc[childno].fp_lock;
561 fproc[childno] = fproc[parentno];
562 fproc[childno].fp_lock = c_fp_lock;
564 /* Increase the counters in the 'filp' table. */
565 cp = &fproc[childno];
566 pp = &fproc[parentno];
568 for (i = 0; i < OPEN_MAX; i++)
569 if (cp->fp_filp[i] != NULL) cp->fp_filp[i]->filp_count++;
571 /* Fill in new process and endpoint id. */
572 cp->fp_pid = cpid;
573 cp->fp_endpoint = cproc;
575 /* A forking process never has an outstanding grant, as it isn't blocking on
576 * I/O. */
577 if (GRANT_VALID(pp->fp_grant)) {
578 panic("VFS: fork: pp (endpoint %d) has grant %d\n", pp->fp_endpoint,
579 pp->fp_grant);
581 if (GRANT_VALID(cp->fp_grant)) {
582 panic("VFS: fork: cp (endpoint %d) has grant %d\n", cp->fp_endpoint,
583 cp->fp_grant);
586 /* A child is not a process leader, not being revived, etc. */
587 cp->fp_flags = FP_NOFLAGS;
589 /* Record the fact that both root and working dir have another user. */
590 if (cp->fp_rd) dup_vnode(cp->fp_rd);
591 if (cp->fp_wd) dup_vnode(cp->fp_wd);
594 /*===========================================================================*
595 * free_proc *
596 *===========================================================================*/
597 static void free_proc(int flags)
599 int i;
600 register struct fproc *rfp;
601 register struct filp *rfilp;
602 register struct vnode *vp;
603 dev_t dev;
605 if (fp->fp_endpoint == NONE)
606 panic("free_proc: already free");
608 if (fp_is_blocked(fp))
609 unpause();
611 /* Loop on file descriptors, closing any that are open. */
612 for (i = 0; i < OPEN_MAX; i++) {
613 (void) close_fd(fp, i);
616 /* Release root and working directories. */
617 if (fp->fp_rd) { put_vnode(fp->fp_rd); fp->fp_rd = NULL; }
618 if (fp->fp_wd) { put_vnode(fp->fp_wd); fp->fp_wd = NULL; }
620 /* The rest of these actions is only done when processes actually exit. */
621 if (!(flags & FP_EXITING)) return;
623 fp->fp_flags |= FP_EXITING;
625 /* Check if any process is SUSPENDed on this driver.
626 * If a driver exits, unmap its entries in the dmap table.
627 * (unmapping has to be done after the first step, because the
628 * dmap table is used in the first step.)
630 unsuspend_by_endpt(fp->fp_endpoint);
631 dmap_unmap_by_endpt(fp->fp_endpoint);
633 worker_stop_by_endpt(fp->fp_endpoint); /* Unblock waiting threads */
634 vmnt_unmap_by_endpt(fp->fp_endpoint); /* Invalidate open files if this
635 * was an active FS */
637 /* If a session leader exits and it has a controlling tty, then revoke
638 * access to its controlling tty from all other processes using it.
640 if ((fp->fp_flags & FP_SESLDR) && fp->fp_tty != 0) {
641 dev = fp->fp_tty;
642 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
643 if(rfp->fp_pid == PID_FREE) continue;
644 if (rfp->fp_tty == dev) rfp->fp_tty = 0;
646 for (i = 0; i < OPEN_MAX; i++) {
647 if ((rfilp = rfp->fp_filp[i]) == NULL) continue;
648 if (rfilp->filp_mode == FILP_CLOSED) continue;
649 vp = rfilp->filp_vno;
650 if (!S_ISCHR(vp->v_mode)) continue;
651 if (vp->v_sdev != dev) continue;
652 lock_filp(rfilp, VNODE_READ);
653 (void) cdev_close(dev); /* Ignore any errors. */
655 rfilp->filp_mode = FILP_CLOSED;
656 unlock_filp(rfilp);
661 /* Exit done. Mark slot as free. */
662 fp->fp_endpoint = NONE;
663 fp->fp_pid = PID_FREE;
664 fp->fp_flags = FP_NOFLAGS;
667 /*===========================================================================*
668 * pm_exit *
669 *===========================================================================*/
670 void pm_exit(void)
672 /* Perform the file system portion of the exit(status) system call.
673 * This function is called from the context of the exiting process.
676 free_proc(FP_EXITING);
679 /*===========================================================================*
680 * pm_setgid *
681 *===========================================================================*/
682 void pm_setgid(proc_e, egid, rgid)
683 endpoint_t proc_e;
684 int egid;
685 int rgid;
687 register struct fproc *tfp;
688 int slot;
690 okendpt(proc_e, &slot);
691 tfp = &fproc[slot];
693 tfp->fp_effgid = egid;
694 tfp->fp_realgid = rgid;
698 /*===========================================================================*
699 * pm_setgroups *
700 *===========================================================================*/
701 void pm_setgroups(proc_e, ngroups, groups)
702 endpoint_t proc_e;
703 int ngroups;
704 gid_t *groups;
706 struct fproc *rfp;
707 int slot;
709 okendpt(proc_e, &slot);
710 rfp = &fproc[slot];
711 if (ngroups * sizeof(gid_t) > sizeof(rfp->fp_sgroups))
712 panic("VFS: pm_setgroups: too much data to copy");
713 if (sys_datacopy(who_e, (vir_bytes) groups, SELF, (vir_bytes) rfp->fp_sgroups,
714 ngroups * sizeof(gid_t)) == OK) {
715 rfp->fp_ngroups = ngroups;
716 } else
717 panic("VFS: pm_setgroups: datacopy failed");
721 /*===========================================================================*
722 * pm_setuid *
723 *===========================================================================*/
724 void pm_setuid(proc_e, euid, ruid)
725 endpoint_t proc_e;
726 int euid;
727 int ruid;
729 struct fproc *tfp;
730 int slot;
732 okendpt(proc_e, &slot);
733 tfp = &fproc[slot];
735 tfp->fp_effuid = euid;
736 tfp->fp_realuid = ruid;
739 /*===========================================================================*
740 * pm_setsid *
741 *===========================================================================*/
742 void pm_setsid(endpoint_t proc_e)
744 /* Perform the VFS side of the SETSID call, i.e. get rid of the controlling
745 * terminal of a process, and make the process a session leader.
747 struct fproc *rfp;
748 int slot;
750 /* Make the process a session leader with no controlling tty. */
751 okendpt(proc_e, &slot);
752 rfp = &fproc[slot];
753 rfp->fp_flags |= FP_SESLDR;
754 rfp->fp_tty = 0;
757 /*===========================================================================*
758 * do_svrctl *
759 *===========================================================================*/
760 int do_svrctl(void)
762 unsigned int svrctl;
763 vir_bytes ptr;
765 svrctl = job_m_in.SVRCTL_REQ;
766 ptr = (vir_bytes) job_m_in.SVRCTL_ARG;
767 if (((svrctl >> 8) & 0xFF) != 'M') return(EINVAL);
769 switch (svrctl) {
770 case VFSSETPARAM:
771 case VFSGETPARAM:
773 struct sysgetenv sysgetenv;
774 char search_key[64];
775 char val[64];
776 int r, s;
778 /* Copy sysgetenv structure to VFS */
779 if (sys_datacopy(who_e, ptr, SELF, (vir_bytes) &sysgetenv,
780 sizeof(sysgetenv)) != OK)
781 return(EFAULT);
783 /* Basic sanity checking */
784 if (svrctl == VFSSETPARAM) {
785 if (sysgetenv.keylen <= 0 ||
786 sysgetenv.keylen > (sizeof(search_key) - 1) ||
787 sysgetenv.vallen <= 0 ||
788 sysgetenv.vallen >= sizeof(val)) {
789 return(EINVAL);
793 /* Copy parameter "key" */
794 if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.key,
795 SELF, (vir_bytes) search_key,
796 sysgetenv.keylen)) != OK)
797 return(s);
798 search_key[sysgetenv.keylen] = '\0'; /* Limit string */
800 /* Is it a parameter we know? */
801 if (svrctl == VFSSETPARAM) {
802 if (!strcmp(search_key, "verbose")) {
803 int verbose_val;
804 if ((s = sys_datacopy(who_e,
805 (vir_bytes) sysgetenv.val, SELF,
806 (vir_bytes) &val, sysgetenv.vallen)) != OK)
807 return(s);
808 val[sysgetenv.vallen] = '\0'; /* Limit string */
809 verbose_val = atoi(val);
810 if (verbose_val < 0 || verbose_val > 4) {
811 return(EINVAL);
813 verbose = verbose_val;
814 r = OK;
815 } else {
816 r = ESRCH;
818 } else { /* VFSGETPARAM */
819 char small_buf[60];
821 r = ESRCH;
822 if (!strcmp(search_key, "print_traces")) {
823 mthread_stacktraces();
824 sysgetenv.val = 0;
825 sysgetenv.vallen = 0;
826 r = OK;
827 } else if (!strcmp(search_key, "active_threads")) {
828 int active = NR_WTHREADS - worker_available();
829 snprintf(small_buf, sizeof(small_buf) - 1,
830 "%d", active);
831 sysgetenv.vallen = strlen(small_buf);
832 r = OK;
835 if (r == OK) {
836 if ((s = sys_datacopy(SELF,
837 (vir_bytes) &sysgetenv, who_e, ptr,
838 sizeof(sysgetenv))) != OK)
839 return(s);
840 if (sysgetenv.val != 0) {
841 if ((s = sys_datacopy(SELF,
842 (vir_bytes) small_buf, who_e,
843 (vir_bytes) sysgetenv.val,
844 sysgetenv.vallen)) != OK)
845 return(s);
850 return(r);
852 default:
853 return(EINVAL);
857 /*===========================================================================*
858 * pm_dumpcore *
859 *===========================================================================*/
860 int pm_dumpcore(int csig, vir_bytes exe_name)
862 int r = OK, core_fd;
863 struct filp *f;
864 char core_path[PATH_MAX];
865 char proc_name[PROC_NAME_LEN];
867 /* if a process is blocked, scratch(fp).file.fd_nr holds the fd it's blocked
868 * on. free it up for use by common_open().
870 if (fp_is_blocked(fp))
871 unpause();
873 /* open core file */
874 snprintf(core_path, PATH_MAX, "%s.%d", CORE_NAME, fp->fp_pid);
875 core_fd = common_open(core_path, O_WRONLY | O_CREAT | O_TRUNC, CORE_MODE);
876 if (core_fd < 0) { r = core_fd; goto core_exit; }
878 /* get process' name */
879 r = sys_datacopy(PM_PROC_NR, exe_name, VFS_PROC_NR, (vir_bytes) proc_name,
880 PROC_NAME_LEN);
881 if (r != OK) goto core_exit;
882 proc_name[PROC_NAME_LEN - 1] = '\0';
884 if ((f = get_filp(core_fd, VNODE_WRITE)) == NULL) { r=EBADF; goto core_exit; }
885 write_elf_core_file(f, csig, proc_name);
886 unlock_filp(f);
887 (void) close_fd(fp, core_fd); /* ignore failure, we're exiting anyway */
889 core_exit:
890 if(csig)
891 free_proc(FP_EXITING);
892 return(r);
895 /*===========================================================================*
896 * ds_event *
897 *===========================================================================*/
898 void
899 ds_event(void)
901 char key[DS_MAX_KEYLEN];
902 char *blkdrv_prefix = "drv.blk.";
903 char *chrdrv_prefix = "drv.chr.";
904 u32_t value;
905 int type, r, is_blk;
906 endpoint_t owner_endpoint;
908 /* Get the event and the owner from DS. */
909 while ((r = ds_check(key, &type, &owner_endpoint)) == OK) {
910 /* Only check for block and character driver up events. */
911 if (!strncmp(key, blkdrv_prefix, strlen(blkdrv_prefix))) {
912 is_blk = TRUE;
913 } else if (!strncmp(key, chrdrv_prefix, strlen(chrdrv_prefix))) {
914 is_blk = FALSE;
915 } else {
916 continue;
919 if ((r = ds_retrieve_u32(key, &value)) != OK) {
920 printf("VFS: ds_event: ds_retrieve_u32 failed\n");
921 break;
923 if (value != DS_DRIVER_UP) continue;
925 /* Perform up. */
926 dmap_endpt_up(owner_endpoint, is_blk);
929 if (r != ENOENT) printf("VFS: ds_event: ds_check failed: %d\n", r);
932 /* A function to be called on panic(). */
933 void panic_hook(void)
935 printf("VFS mthread stacktraces:\n");
936 mthread_stacktraces();
939 /*===========================================================================*
940 * do_getrusage *
941 *===========================================================================*/
942 int do_getrusage(void)
944 int res;
945 struct rusage r_usage;
947 if ((res = sys_datacopy(who_e, (vir_bytes) m_in.RU_RUSAGE_ADDR, SELF,
948 (vir_bytes) &r_usage, (vir_bytes) sizeof(r_usage))) < 0)
949 return res;
951 r_usage.ru_inblock = 0;
952 r_usage.ru_oublock = 0;
953 r_usage.ru_ixrss = fp->text_size;
954 r_usage.ru_idrss = fp->data_size;
955 r_usage.ru_isrss = DEFAULT_STACK_LIMIT;
957 return sys_datacopy(SELF, (vir_bytes) &r_usage, who_e,
958 (vir_bytes) m_in.RU_RUSAGE_ADDR, (phys_bytes) sizeof(r_usage));