Source-Changes-HG archive

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

[src/trunk]: src/sys Merge the kernel portion of the posix-spawn-chdir projec...



details:   https://anonhg.NetBSD.org/src/rev/43e61180308c
branches:  trunk
changeset: 991100:43e61180308c
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Nov 07 13:47:49 2021 +0000

description:
Merge the kernel portion of the posix-spawn-chdir project by Piyush Sachdeva.

diffstat:

 sys/kern/kern_exec.c    |  42 ++++++++++++++++++++++-------
 sys/kern/vfs_syscalls.c |  69 ++++++++++++++++++++++++++++++------------------
 sys/sys/spawn.h         |   8 ++++-
 sys/sys/vfs_syscalls.h  |   4 ++-
 4 files changed, 83 insertions(+), 40 deletions(-)

diffs (261 lines):

diff -r 843b80d3f183 -r 43e61180308c sys/kern/kern_exec.c
--- a/sys/kern/kern_exec.c      Sun Nov 07 13:43:11 2021 +0000
+++ b/sys/kern/kern_exec.c      Sun Nov 07 13:47:49 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exec.c,v 1.510 2021/10/10 18:07:51 thorpej Exp $  */
+/*     $NetBSD: kern_exec.c,v 1.511 2021/11/07 13:47:49 christos Exp $ */
 
 /*-
  * Copyright (c) 2008, 2019, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.510 2021/10/10 18:07:51 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.511 2021/11/07 13:47:49 christos Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -103,6 +103,7 @@
 #include <sys/module.h>
 #include <sys/syscallvar.h>
 #include <sys/syscallargs.h>
+#include <sys/vfs_syscalls.h>
 #if NVERIEXEC > 0
 #include <sys/verified_exec.h>
 #endif /* NVERIEXEC > 0 */
@@ -2132,6 +2133,13 @@
                        }
                        error = fd_close(fae->fae_fildes);
                        break;
+               case FAE_CHDIR:
+                       error = do_sys_chdir(l, fae->fae_chdir_path,
+                           UIO_SYSSPACE, &retval);
+                       break;
+               case FAE_FCHDIR:
+                       error = do_sys_fchdir(l, fae->fae_fildes, &retval);
+                       break;
                }
                if (error)
                        return error;
@@ -2361,15 +2369,27 @@
        exit1(l, 127, 0);
 }
 
+static __inline char **
+posix_spawn_fae_path(struct posix_spawn_file_actions_entry *fae)
+{
+       switch (fae->fae_action) {
+       case FAE_OPEN:
+               return &fae->fae_path;
+       case FAE_CHDIR:
+               return &fae->fae_chdir_path;
+       default:
+               return NULL;
+       }
+}
+    
 void
 posix_spawn_fa_free(struct posix_spawn_file_actions *fa, size_t len)
 {
 
        for (size_t i = 0; i < len; i++) {
-               struct posix_spawn_file_actions_entry *fae = &fa->fae[i];
-               if (fae->fae_action != FAE_OPEN)
-                       continue;
-               kmem_strfree(fae->fae_path);
+               char **pathp = posix_spawn_fae_path(&fa->fae[i]);
+               if (pathp)
+                       kmem_strfree(*pathp);
        }
        if (fa->len > 0)
                kmem_free(fa->fae, sizeof(*fa->fae) * fa->len);
@@ -2408,14 +2428,14 @@
 
        pbuf = PNBUF_GET();
        for (; i < fa->len; i++) {
-               fae = &fa->fae[i];
-               if (fae->fae_action != FAE_OPEN)
+               char **pathp = posix_spawn_fae_path(&fa->fae[i]);
+               if (pathp == NULL)
                        continue;
-               error = copyinstr(fae->fae_path, pbuf, MAXPATHLEN, &fal);
+               error = copyinstr(*pathp, pbuf, MAXPATHLEN, &fal);
                if (error)
                        goto out;
-               fae->fae_path = kmem_alloc(fal, KM_SLEEP);
-               memcpy(fae->fae_path, pbuf, fal);
+               *pathp = kmem_alloc(fal, KM_SLEEP);
+               memcpy(*pathp, pbuf, fal);
        }
        PNBUF_PUT(pbuf);
 
diff -r 843b80d3f183 -r 43e61180308c sys/kern/vfs_syscalls.c
--- a/sys/kern/vfs_syscalls.c   Sun Nov 07 13:43:11 2021 +0000
+++ b/sys/kern/vfs_syscalls.c   Sun Nov 07 13:47:49 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_syscalls.c,v 1.553 2021/09/26 21:29:38 thorpej Exp $       */
+/*     $NetBSD: vfs_syscalls.c,v 1.554 2021/11/07 13:47:50 christos Exp $      */
 
 /*-
  * Copyright (c) 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.553 2021/09/26 21:29:38 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.554 2021/11/07 13:47:50 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_fileassoc.h"
@@ -1459,24 +1459,19 @@
 /*
  * Change current working directory to a given file descriptor.
  */
-/* ARGSUSED */
 int
-sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
+do_sys_fchdir(struct lwp *l, int fd, register_t *retval)
 {
-       /* {
-               syscallarg(int) fd;
-       } */
        struct proc *p = l->l_proc;
        struct cwdinfo *cwdi;
        struct vnode *vp, *tdp;
        struct mount *mp;
        file_t *fp;
-       int error, fd;
+       int error;
 
        /* fd_getvnode() will use the descriptor for us */
-       fd = SCARG(uap, fd);
        if ((error = fd_getvnode(fd, &fp)) != 0)
-               return (error);
+               return error;
        vp = fp->f_vnode;
 
        vref(vp);
@@ -1517,9 +1512,22 @@
        }
        rw_exit(&cwdi->cwdi_lock);
 
- out:
+out:
        fd_putfile(fd);
-       return (error);
+       return error;
+}
+
+/*
+ * Change current working directory to a given file descriptor.
+ */
+/* ARGSUSED */
+int
+sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
+{
+       /* {
+               syscallarg(int) fd;
+       } */
+       return do_sys_fchdir(l, SCARG(uap, fd), retval);
 }
 
 /*
@@ -1559,6 +1567,28 @@
 /*
  * Change current working directory (``.'').
  */
+int
+do_sys_chdir(struct lwp *l, const char *path, enum uio_seg seg,
+    register_t *retval)
+{
+       struct proc *p = l->l_proc;
+       struct cwdinfo * cwdi;
+       int error;
+       struct vnode *vp;
+
+       if ((error = chdir_lookup(path, seg, &vp, l)) != 0)
+               return error;
+       cwdi = p->p_cwdi;
+       rw_enter(&cwdi->cwdi_lock, RW_WRITER);
+       vrele(cwdi->cwdi_cdir);
+       cwdi->cwdi_cdir = vp;
+       rw_exit(&cwdi->cwdi_lock);
+       return 0;
+}
+
+/*
+ * Change current working directory (``.'').
+ */
 /* ARGSUSED */
 int
 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
@@ -1566,20 +1596,7 @@
        /* {
                syscallarg(const char *) path;
        } */
-       struct proc *p = l->l_proc;
-       struct cwdinfo *cwdi;
-       int error;
-       struct vnode *vp;
-
-       if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
-                                 &vp, l)) != 0)
-               return (error);
-       cwdi = p->p_cwdi;
-       rw_enter(&cwdi->cwdi_lock, RW_WRITER);
-       vrele(cwdi->cwdi_cdir);
-       cwdi->cwdi_cdir = vp;
-       rw_exit(&cwdi->cwdi_lock);
-       return (0);
+       return do_sys_chdir(l, SCARG(uap, path), UIO_USERSPACE, retval);
 }
 
 /*
diff -r 843b80d3f183 -r 43e61180308c sys/sys/spawn.h
--- a/sys/sys/spawn.h   Sun Nov 07 13:43:11 2021 +0000
+++ b/sys/sys/spawn.h   Sun Nov 07 13:47:49 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: spawn.h,v 1.6 2017/11/29 20:15:21 snj Exp $    */
+/*     $NetBSD: spawn.h,v 1.7 2021/11/07 13:47:49 christos Exp $       */
 
 /*-
  * Copyright (c) 2008 Ed Schouten <ed%FreeBSD.org@localhost>
@@ -47,7 +47,7 @@
        sigset_t                sa_sigmask;
 };
 
-enum fae_action { FAE_OPEN, FAE_DUP2, FAE_CLOSE };
+enum fae_action { FAE_OPEN, FAE_DUP2, FAE_CLOSE, FAE_CHDIR, FAE_FCHDIR };
 typedef struct posix_spawn_file_actions_entry {
        enum fae_action fae_action;
 
@@ -65,6 +65,10 @@
                        int newfildes;
 #define fae_newfildes  fae_data.dup2.newfildes
                } dup2;
+               struct {
+                       char *path;
+#define fae_chdir_path fae_data.chdir.path
+               } chdir;
        } fae_data;
 } posix_spawn_file_actions_entry_t;
 
diff -r 843b80d3f183 -r 43e61180308c sys/sys/vfs_syscalls.h
--- a/sys/sys/vfs_syscalls.h    Sun Nov 07 13:43:11 2021 +0000
+++ b/sys/sys/vfs_syscalls.h    Sun Nov 07 13:47:49 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_syscalls.h,v 1.28 2020/04/20 21:39:05 ad Exp $        */
+/*     $NetBSD: vfs_syscalls.h,v 1.29 2021/11/07 13:47:49 christos Exp $        */
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -80,6 +80,8 @@
 int    do_sys_symlink(const char *, const char *, enum uio_seg);
 int    do_sys_quotactl(const char *, const struct quotactl_args *);
 void   do_sys_sync(struct lwp *);
+int    do_sys_chdir(struct lwp *, const char *, enum uio_seg, register_t *);
+int    do_sys_fchdir(struct lwp *, int, register_t *);
 int    vfs_syncwait(void);
 
 int    chdir_lookup(const char *, int, struct vnode **, struct lwp *);



Home | Main Index | Thread Index | Old Index