Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Implement KERN_PROC_CWD in sysctl(3)



details:   https://anonhg.NetBSD.org/src/rev/cb29b72ffef6
branches:  trunk
changeset: 451722:cb29b72ffef6
user:      kamil <kamil%NetBSD.org@localhost>
date:      Fri May 31 23:01:39 2019 +0000

description:
Implement KERN_PROC_CWD in sysctl(3)

Retrieve specified process current working directory.

Fixes PR kern/50620 by Thomas Klausner.

diffstat:

 sys/kern/kern_proc.c |  63 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 sys/sys/sysctl.h     |   3 +-
 2 files changed, 63 insertions(+), 3 deletions(-)

diffs (115 lines):

diff -r 30e5ec7f6858 -r cb29b72ffef6 sys/kern/kern_proc.c
--- a/sys/kern/kern_proc.c      Fri May 31 20:25:58 2019 +0000
+++ b/sys/kern/kern_proc.c      Fri May 31 23:01:39 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_proc.c,v 1.228 2019/03/01 11:06:57 pgoyette Exp $ */
+/*     $NetBSD: kern_proc.c,v 1.229 2019/05/31 23:01:39 kamil Exp $    */
 
 /*-
  * Copyright (c) 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.228 2019/03/01 11:06:57 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.229 2019/05/31 23:01:39 kamil Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kstack.h"
@@ -246,6 +246,7 @@
 
 static void fill_proc(const struct proc *, struct proc *, bool);
 static int fill_pathname(struct lwp *, pid_t, void *, size_t *);
+static int fill_cwd(struct lwp *, pid_t, void *, size_t *);
 
 static int
 proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
@@ -1945,6 +1946,12 @@
                sysctl_relock();
                return error;
 
+       case KERN_PROC_CWD:
+               sysctl_unlock();
+               error = fill_cwd(l, pid, oldp, oldlenp);
+               sysctl_relock();
+               return error;
+
        case KERN_PROC_ARGV:
        case KERN_PROC_NARGV:
        case KERN_PROC_ENV:
@@ -2577,6 +2584,58 @@
        return error;
 }
 
+static int
+fill_cwd(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
+{
+       int error;
+       struct proc *p;
+       char *path;
+       char *bp, *bend;
+       struct cwdinfo *cwdi;
+       struct vnode *vp;
+       size_t len, lenused;
+       const int flags = GETCWD_CHECK_ACCESS;
+
+       if ((error = proc_find_locked(l, &p, pid)) != 0)
+               return error;
+
+       len = MAXPATHLEN * 4;
+       if (*oldlenp < 2) {
+               if (pid != -1)
+                       mutex_exit(p->p_lock);
+               return ERANGE;
+       }
+
+       path = kmem_alloc(len, KM_SLEEP);
+
+       bp = &path[len];
+       bend = bp;
+       *(--bp) = '\0';
+
+       cwdi = p->p_cwdi;
+       rw_enter(&cwdi->cwdi_lock, RW_READER);
+       vp = cwdi->cwdi_cdir;
+       error = getcwd_common(vp, NULL, &bp, path, len/2, flags, l);
+       rw_exit(&cwdi->cwdi_lock);
+
+       if (error)
+               goto out;
+
+       lenused = bend - bp;
+
+       if (oldp != NULL) {
+               error = sysctl_copyout(l, bp, oldp, lenused);
+               if (error == 0 && *oldlenp < lenused)
+                       error = ENOSPC;
+       }
+       *oldlenp = lenused;
+out:
+       if (pid != -1)
+               mutex_exit(p->p_lock);
+       kmem_free(path, len);
+       return error;
+}
+
 int
 proc_getauxv(struct proc *p, void **buf, size_t *len)
 {
diff -r 30e5ec7f6858 -r cb29b72ffef6 sys/sys/sysctl.h
--- a/sys/sys/sysctl.h  Fri May 31 20:25:58 2019 +0000
+++ b/sys/sys/sysctl.h  Fri May 31 23:01:39 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sysctl.h,v 1.229 2018/12/05 18:16:51 christos Exp $    */
+/*     $NetBSD: sysctl.h,v 1.230 2019/05/31 23:01:39 kamil Exp $       */
 
 /*
  * Copyright (c) 1989, 1993
@@ -585,6 +585,7 @@
 #define        KERN_PROC_ENV           3       /* environ */
 #define        KERN_PROC_NENV          4       /* number of strings in above */
 #define        KERN_PROC_PATHNAME      5       /* path to executable */
+#define        KERN_PROC_CWD           6       /* current working dir */
 
 /*
  * KERN_SYSVIPC subtypes



Home | Main Index | Thread Index | Old Index