Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 26 Feb 2012 14:25:48 +0000 (UTC)
From:      Mikolaj Golub <trociny@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r232181 - in head/sys: kern sys
Message-ID:  <201202261425.q1QEPm9g069102@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trociny
Date: Sun Feb 26 14:25:48 2012
New Revision: 232181
URL: http://svn.freebsd.org/changeset/base/232181

Log:
  Add sysctl to retrieve or set umask of another process.
  
  Submitted by:	Dmitry Banschikov <me ubique spb ru>
  Discussed with:	kib, rwatson
  Reviewed by:	kib
  MFC after:	2 weeks

Modified:
  head/sys/kern/kern_proc.c
  head/sys/sys/sysctl.h

Modified: head/sys/kern/kern_proc.c
==============================================================================
--- head/sys/kern/kern_proc.c	Sun Feb 26 13:57:24 2012	(r232180)
+++ head/sys/kern/kern_proc.c	Sun Feb 26 14:25:48 2012	(r232181)
@@ -60,6 +60,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/sched.h>
 #include <sys/smp.h>
 #include <sys/stack.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/filedesc.h>
 #include <sys/tty.h>
@@ -2471,6 +2472,49 @@ sysctl_kern_proc_ps_strings(SYSCTL_HANDL
 	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
 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");

Modified: head/sys/sys/sysctl.h
==============================================================================
--- head/sys/sys/sysctl.h	Sun Feb 26 13:57:24 2012	(r232180)
+++ head/sys/sys/sysctl.h	Sun Feb 26 14:25:48 2012	(r232181)
@@ -563,6 +563,7 @@ SYSCTL_ALLOWED_TYPES(UINT64, uint64_t *a
 #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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201202261425.q1QEPm9g069102>