commit c5b6ed500553bf848b25d1eca7e560b283484557 Author: Mikolaj Golub Date: Tue Feb 21 23:22:23 2012 +0200 Add sysctl to retrieve or set umask of another process. Submitted by: Dmitry Banschikov Discussed with: kib, rwatson MFC after: 2 weeks diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 41b6f2b..50e5368 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -60,6 +60,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -2471,6 +2472,49 @@ sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS) return (error); } +/* + * This sysctl allows a process to retrieve or/and set umask of + * another process. + */ +static int +sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS) +{ + int *name = (int *)arg1; + u_int namelen = arg2; + struct proc *p; + int error; + u_short fd_cmask; + + if (namelen != 1) + return (EINVAL); + + if (req->newptr != NULL && req->newlen != sizeof(fd_cmask)) + return (EINVAL); + + error = pget((pid_t)name[0], PGET_WANTREAD, &p); + if (error != 0) + return (error); + + FILEDESC_SLOCK(p->p_fd); + fd_cmask = p->p_fd->fd_cmask; + FILEDESC_SUNLOCK(p->p_fd); + error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask)); + if (error != 0) + goto errout; + + if (req->newptr != NULL) { + error = SYSCTL_IN(req, &fd_cmask, sizeof(fd_cmask)); + if (error == 0) { + FILEDESC_XLOCK(p->p_fd); + p->p_fd->fd_cmask = fd_cmask & ALLPERMS; + FILEDESC_XUNLOCK(p->p_fd); + } + } +errout: + PRELE(p); + return (error); +} + SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| @@ -2572,3 +2616,7 @@ static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW | static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings, "Process ps_strings location"); + +static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RW | + CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_umask, + "Process umask"); diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index fc2b85a..9000a51 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -563,6 +563,7 @@ SYSCTL_ALLOWED_TYPES(UINT64, uint64_t *a; unsigned long long *b; ); #define KERN_PROC_AUXV 36 /* get ELF auxiliary vector */ #define KERN_PROC_RLIMIT 37 /* process resource limits */ #define KERN_PROC_PS_STRINGS 38 /* get ps_strings location */ +#define KERN_PROC_UMASK 39 /* process umask */ /* * KERN_IPC identifiers