linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask)
@ 2016-04-14  9:34 Richard W.M. Jones
  2016-04-14  9:34 ` [PATCH] procfs: expose umask in /proc/<PID>/status Richard W.M. Jones
  2016-04-14  9:36 ` [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask) Richard W.M. Jones
  0 siblings, 2 replies; 4+ messages in thread
From: Richard W.M. Jones @ 2016-04-14  9:34 UTC (permalink / raw)
  To: corbet
  Cc: akpm, vbabka, mhocko, hughd, koct9i, chenhanxiao, n-horiguchi,
	ross.zwisler, john.stultz, minchan, jmarchan, hannes, nathans,
	andriy.shevchenko, keescook, gorcunov, joe, linux, mingo,
	cmetcalf, iago, luto, linux-doc, linux-kernel, gorcunov, fw,
	walters

It's not possible to read the process umask without also modifying it,
which is what umask(2) does.  A library cannot read umask safely,
especially if the main program might be multithreaded.

Add a new status line ("Umask") in /proc/<PID>/status.  It contains
the file mode creation mask (umask) in octal.  It is only shown for
tasks which have task->fs.

For the library this allows me to read the umask from
/proc/self/status.

This patch is adapted from one originally written by Pierre Carrier:
https://lkml.org/lkml/2012/5/4/451

Rich.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] procfs: expose umask in /proc/<PID>/status
  2016-04-14  9:34 [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask) Richard W.M. Jones
@ 2016-04-14  9:34 ` Richard W.M. Jones
  2016-04-14 10:21   ` Jerome Marchand
  2016-04-14  9:36 ` [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask) Richard W.M. Jones
  1 sibling, 1 reply; 4+ messages in thread
From: Richard W.M. Jones @ 2016-04-14  9:34 UTC (permalink / raw)
  To: corbet
  Cc: akpm, vbabka, mhocko, hughd, koct9i, chenhanxiao, n-horiguchi,
	ross.zwisler, john.stultz, minchan, jmarchan, hannes, nathans,
	andriy.shevchenko, keescook, gorcunov, joe, linux, mingo,
	cmetcalf, iago, luto, linux-doc, linux-kernel, gorcunov, fw,
	walters

It's not possible to read the process umask without also modifying it,
which is what umask(2) does.  A library cannot read umask safely,
especially if the main program might be multithreaded.

Add a new status line ("Umask") in /proc/<PID>/status.  It contains
the file mode creation mask (umask) in octal.  It is only shown for
tasks which have task->fs.

This patch is adapted from one originally written by Pierre Carrier.

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 Documentation/filesystems/proc.txt |  1 +
 fs/proc/array.c                    | 20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/Documentation/filesystems/proc.txt b/Documentation/filesystems/proc.txt
index 7f5607a..e8d0075 100644
--- a/Documentation/filesystems/proc.txt
+++ b/Documentation/filesystems/proc.txt
@@ -225,6 +225,7 @@ Table 1-2: Contents of the status files (as of 4.1)
  TracerPid                   PID of process tracing this process (0 if not)
  Uid                         Real, effective, saved set, and  file system UIDs
  Gid                         Real, effective, saved set, and  file system GIDs
+ Umask                       file mode creation mask
  FDSize                      number of file descriptor slots currently allocated
  Groups                      supplementary group list
  NStgid                      descendant namespace thread group ID hierarchy
diff --git a/fs/proc/array.c b/fs/proc/array.c
index b6c00ce..03e8d3f 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -83,6 +83,7 @@
 #include <linux/tracehook.h>
 #include <linux/string_helpers.h>
 #include <linux/user_namespace.h>
+#include <linux/fs_struct.h>
 
 #include <asm/pgtable.h>
 #include <asm/processor.h>
@@ -139,12 +140,25 @@ static inline const char *get_task_state(struct task_struct *tsk)
 	return task_state_array[fls(state)];
 }
 
+static inline int get_task_umask(struct task_struct *tsk)
+{
+	struct fs_struct *fs;
+	int umask = -ENOENT;
+
+	task_lock(tsk);
+	fs = tsk->fs;
+	if (fs)
+		umask = fs->umask;
+	task_unlock(tsk);
+	return umask;
+}
+
 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
 				struct pid *pid, struct task_struct *p)
 {
 	struct user_namespace *user_ns = seq_user_ns(m);
 	struct group_info *group_info;
-	int g;
+	int g, umask;
 	struct task_struct *tracer;
 	const struct cred *cred;
 	pid_t ppid, tpid = 0, tgid, ngid;
@@ -162,6 +176,10 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
 	ngid = task_numa_group_id(p);
 	cred = get_task_cred(p);
 
+	umask = get_task_umask(p);
+	if (umask >= 0)
+		seq_printf(m, "Umask:\t0%o\n", umask);
+
 	task_lock(p);
 	if (p->files)
 		max_fds = files_fdtable(p->files)->max_fds;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask)
  2016-04-14  9:34 [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask) Richard W.M. Jones
  2016-04-14  9:34 ` [PATCH] procfs: expose umask in /proc/<PID>/status Richard W.M. Jones
@ 2016-04-14  9:36 ` Richard W.M. Jones
  1 sibling, 0 replies; 4+ messages in thread
From: Richard W.M. Jones @ 2016-04-14  9:36 UTC (permalink / raw)
  To: corbet
  Cc: akpm, vbabka, mhocko, hughd, koct9i, chenhanxiao, n-horiguchi,
	ross.zwisler, john.stultz, minchan, jmarchan, hannes, nathans,
	andriy.shevchenko, keescook, gorcunov, joe, linux, mingo,
	cmetcalf, iago, luto, linux-doc, linux-kernel, gorcunov, fw,
	walters

On Thu, Apr 14, 2016 at 10:34:48AM +0100, Richard W.M. Jones wrote:
> It's not possible to read the process umask without also modifying it,
> which is what umask(2) does.  A library cannot read umask safely,
> especially if the main program might be multithreaded.
> 
> Add a new status line ("Umask") in /proc/<PID>/status.  It contains
> the file mode creation mask (umask) in octal.  It is only shown for
> tasks which have task->fs.
> 
> For the library this allows me to read the umask from
> /proc/self/status.
> 
> This patch is adapted from one originally written by Pierre Carrier:
> https://lkml.org/lkml/2012/5/4/451

Sorry, I meant to add an example of what this looks like:

$ grep Umask /proc/1/status 
Umask:	  022
$ grep Umask /proc/2/status 
Umask:	  022
$ grep Umask /proc/self/status 
Umask:	  022
$ umask 002
$ grep Umask /proc/self/status 
Umask:	  02

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] procfs: expose umask in /proc/<PID>/status
  2016-04-14  9:34 ` [PATCH] procfs: expose umask in /proc/<PID>/status Richard W.M. Jones
@ 2016-04-14 10:21   ` Jerome Marchand
  0 siblings, 0 replies; 4+ messages in thread
From: Jerome Marchand @ 2016-04-14 10:21 UTC (permalink / raw)
  To: Richard W.M. Jones, corbet
  Cc: akpm, vbabka, mhocko, hughd, koct9i, chenhanxiao, n-horiguchi,
	ross.zwisler, john.stultz, minchan, hannes, nathans,
	andriy.shevchenko, keescook, gorcunov, joe, linux, mingo,
	cmetcalf, iago, luto, linux-doc, linux-kernel, gorcunov, fw,
	walters


[-- Attachment #1.1: Type: text/plain, Size: 3280 bytes --]

On 04/14/2016 11:34 AM, Richard W.M. Jones wrote:
> It's not possible to read the process umask without also modifying it,
> which is what umask(2) does.  A library cannot read umask safely,
> especially if the main program might be multithreaded.
> 
> Add a new status line ("Umask") in /proc/<PID>/status.  It contains
> the file mode creation mask (umask) in octal.  It is only shown for
> tasks which have task->fs.
> 
> This patch is adapted from one originally written by Pierre Carrier.
> 
> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> ---
>  Documentation/filesystems/proc.txt |  1 +
>  fs/proc/array.c                    | 20 +++++++++++++++++++-
>  2 files changed, 20 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/filesystems/proc.txt b/Documentation/filesystems/proc.txt
> index 7f5607a..e8d0075 100644
> --- a/Documentation/filesystems/proc.txt
> +++ b/Documentation/filesystems/proc.txt
> @@ -225,6 +225,7 @@ Table 1-2: Contents of the status files (as of 4.1)
>   TracerPid                   PID of process tracing this process (0 if not)
>   Uid                         Real, effective, saved set, and  file system UIDs
>   Gid                         Real, effective, saved set, and  file system GIDs
> + Umask                       file mode creation mask
>   FDSize                      number of file descriptor slots currently allocated
>   Groups                      supplementary group list
>   NStgid                      descendant namespace thread group ID hierarchy
> diff --git a/fs/proc/array.c b/fs/proc/array.c
> index b6c00ce..03e8d3f 100644
> --- a/fs/proc/array.c
> +++ b/fs/proc/array.c
> @@ -83,6 +83,7 @@
>  #include <linux/tracehook.h>
>  #include <linux/string_helpers.h>
>  #include <linux/user_namespace.h>
> +#include <linux/fs_struct.h>
>  
>  #include <asm/pgtable.h>
>  #include <asm/processor.h>
> @@ -139,12 +140,25 @@ static inline const char *get_task_state(struct task_struct *tsk)
>  	return task_state_array[fls(state)];
>  }
>  
> +static inline int get_task_umask(struct task_struct *tsk)
> +{
> +	struct fs_struct *fs;
> +	int umask = -ENOENT;
> +
> +	task_lock(tsk);
> +	fs = tsk->fs;
> +	if (fs)
> +		umask = fs->umask;
> +	task_unlock(tsk);
> +	return umask;
> +}
> +
>  static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
>  				struct pid *pid, struct task_struct *p)
>  {
>  	struct user_namespace *user_ns = seq_user_ns(m);
>  	struct group_info *group_info;
> -	int g;
> +	int g, umask;
>  	struct task_struct *tracer;
>  	const struct cred *cred;
>  	pid_t ppid, tpid = 0, tgid, ngid;
> @@ -162,6 +176,10 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
>  	ngid = task_numa_group_id(p);
>  	cred = get_task_cred(p);
>  
> +	umask = get_task_umask(p);
> +	if (umask >= 0)
> +		seq_printf(m, "Umask:\t0%o\n", umask);

It seems to me that umasks are usually displayed in the form 0XXX, such
as the output of umask command. So what about:

seq_printf(m, "Umask:\t%#04o\n", umask);

Provided printk() supports those flags, of course.

Thanks,
Jerome

> +
>  	task_lock(p);
>  	if (p->files)
>  		max_fds = files_fdtable(p->files)->max_fds;
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-04-14 10:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-14  9:34 [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask) Richard W.M. Jones
2016-04-14  9:34 ` [PATCH] procfs: expose umask in /proc/<PID>/status Richard W.M. Jones
2016-04-14 10:21   ` Jerome Marchand
2016-04-14  9:36 ` [PATCH] procfs: expose umask in /proc/<PID>/status (formerly umask2, formerly getumask) Richard W.M. Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).