[PATCH 1/2] fork: move procdesc-related parameters into a dedicated struct
Mateusz Guzik
mjguzik at gmail.com
Mon Feb 1 05:13:09 UTC 2016
From: Mateusz Guzik <mjg at freebsd.org>
This reduces the number of arguments to fork1.
---
sys/compat/cloudabi/cloudabi_proc.c | 10 +++++++---
sys/compat/linux/linux_fork.c | 7 +++----
sys/kern/init_main.c | 2 +-
sys/kern/kern_fork.c | 29 +++++++++++++++++------------
sys/kern/kern_kthread.c | 2 +-
sys/sys/proc.h | 5 +++--
sys/sys/procdesc.h | 6 ++++++
7 files changed, 38 insertions(+), 23 deletions(-)
diff --git a/sys/compat/cloudabi/cloudabi_proc.c b/sys/compat/cloudabi/cloudabi_proc.c
index d917337..e98471b 100644
--- a/sys/compat/cloudabi/cloudabi_proc.c
+++ b/sys/compat/cloudabi/cloudabi_proc.c
@@ -28,6 +28,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/capsicum.h>
+#include <sys/procdesc.h>
#include <sys/filedesc.h>
#include <sys/imgact.h>
#include <sys/lock.h>
@@ -75,16 +76,19 @@ int
cloudabi_sys_proc_fork(struct thread *td,
struct cloudabi_sys_proc_fork_args *uap)
{
+ struct procdesc_req pdr;
struct filecaps fcaps = {};
struct proc *p2;
- int error, fd;
+ int error;
+ pdr.pdr_flags = 0;
+ pdr.pdr_fcaps = &fcaps;
cap_rights_init(&fcaps.fc_rights, CAP_FSTAT, CAP_EVENT);
- error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2, &fd, 0, &fcaps);
+ error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2, &pdr);
if (error != 0)
return (error);
/* Return the file descriptor to the parent process. */
- td->td_retval[0] = fd;
+ td->td_retval[0] = pdr.pdr_fd;
return (0);
}
diff --git a/sys/compat/linux/linux_fork.c b/sys/compat/linux/linux_fork.c
index d0f73ad..7cbe216 100644
--- a/sys/compat/linux/linux_fork.c
+++ b/sys/compat/linux/linux_fork.c
@@ -73,8 +73,7 @@ linux_fork(struct thread *td, struct linux_fork_args *args)
printf(ARGS(fork, ""));
#endif
- if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0,
- NULL)) != 0)
+ if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL)) != 0)
return (error);
td2 = FIRST_THREAD_IN_PROC(p2);
@@ -107,7 +106,7 @@ linux_vfork(struct thread *td, struct linux_vfork_args *args)
#endif
if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED,
- 0, &p2, NULL, 0, NULL)) != 0)
+ 0, &p2, NULL)) != 0)
return (error);
td2 = FIRST_THREAD_IN_PROC(p2);
@@ -170,7 +169,7 @@ linux_clone_proc(struct thread *td, struct linux_clone_args *args)
if (args->flags & LINUX_CLONE_VFORK)
ff |= RFPPWAIT;
- error = fork1(td, ff, 0, &p2, NULL, 0, NULL);
+ error = fork1(td, ff, 0, &p2, NULL);
if (error)
return (error);
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 8d5580b..7d0443a 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -833,7 +833,7 @@ create_init(const void *udata __unused)
int error;
error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc,
- NULL, 0, NULL);
+ NULL);
if (error)
panic("cannot fork init: %d\n", error);
KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1"));
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index e7d7276..8cc56b7 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -104,7 +104,7 @@ sys_fork(struct thread *td, struct fork_args *uap)
int error;
struct proc *p2;
- error = fork1(td, RFFDG | RFPROC, 0, &p2, NULL, 0, NULL);
+ error = fork1(td, RFFDG | RFPROC, 0, &p2, NULL);
if (error == 0) {
td->td_retval[0] = p2->p_pid;
td->td_retval[1] = 0;
@@ -118,20 +118,22 @@ sys_pdfork(td, uap)
struct thread *td;
struct pdfork_args *uap;
{
- int error, fd;
+ struct procdesc_req pdr;
struct proc *p2;
+ int error;
/*
* It is necessary to return fd by reference because 0 is a valid file
* descriptor number, and the child needs to be able to distinguish
* itself from the parent using the return value.
*/
- error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2,
- &fd, uap->flags, NULL);
+ pdr.pdr_flags = uap->flags;
+ pdr.pdr_fcaps = NULL;
+ error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2, &pdr);
if (error == 0) {
td->td_retval[0] = p2->p_pid;
td->td_retval[1] = 0;
- error = copyout(&fd, uap->fdp, sizeof(fd));
+ error = copyout(&pdr.pdr_fd, uap->fdp, sizeof(pdr.pdr_fd));
}
return (error);
}
@@ -144,7 +146,7 @@ sys_vfork(struct thread *td, struct vfork_args *uap)
struct proc *p2;
flags = RFFDG | RFPROC | RFPPWAIT | RFMEM;
- error = fork1(td, flags, 0, &p2, NULL, 0, NULL);
+ error = fork1(td, flags, 0, &p2, NULL);
if (error == 0) {
td->td_retval[0] = p2->p_pid;
td->td_retval[1] = 0;
@@ -163,7 +165,7 @@ sys_rfork(struct thread *td, struct rfork_args *uap)
return (EINVAL);
AUDIT_ARG_FFLAGS(uap->flags);
- error = fork1(td, uap->flags, 0, &p2, NULL, 0, NULL);
+ error = fork1(td, uap->flags, 0, &p2, NULL);
if (error == 0) {
td->td_retval[0] = p2 ? p2->p_pid : 0;
td->td_retval[1] = 0;
@@ -762,14 +764,14 @@ do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2,
int
fork1(struct thread *td, int flags, int pages, struct proc **procp,
- int *procdescp, int pdflags, struct filecaps *fcaps)
+ struct procdesc_req *pdr)
{
struct proc *p1, *newproc;
struct thread *td2;
struct vmspace *vm2;
struct file *fp_procdesc;
vm_ooffset_t mem_charged;
- int error, nprocs_new, ok;
+ int error, nprocs_new, ok, pdflags;
static int curfail;
static struct timeval lastfail;
@@ -789,14 +791,16 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp,
if ((flags & RFTSIGZMB) != 0 && (u_int)RFTSIGNUM(flags) > _SIG_MAXSIG)
return (EINVAL);
+ pdflags = 0;
if ((flags & RFPROCDESC) != 0) {
/* Can't not create a process yet get a process descriptor. */
if ((flags & RFPROC) == 0)
return (EINVAL);
/* Must provide a place to put a procdesc if creating one. */
- if (procdescp == NULL)
+ if (pdr == NULL)
return (EINVAL);
+ pdflags = pdr->pdr_flags;
}
p1 = td->td_proc;
@@ -845,7 +849,8 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp,
* later.
*/
if (flags & RFPROCDESC) {
- error = falloc_caps(td, &fp_procdesc, procdescp, 0, fcaps);
+ error = falloc_caps(td, &fp_procdesc, &pdr->pdr_fd, 0,
+ pdr->pdr_fcaps);
if (error != 0)
goto fail2;
}
@@ -962,7 +967,7 @@ fail2:
vmspace_free(vm2);
uma_zfree(proc_zone, newproc);
if ((flags & RFPROCDESC) != 0 && fp_procdesc != NULL) {
- fdclose(td, fp_procdesc, *procdescp);
+ fdclose(td, fp_procdesc, pdr->pdr_fd);
fdrop(fp_procdesc, td);
}
atomic_add_int(&nprocs, -1);
diff --git a/sys/kern/kern_kthread.c b/sys/kern/kern_kthread.c
index 2072dc7..0673f68 100644
--- a/sys/kern/kern_kthread.c
+++ b/sys/kern/kern_kthread.c
@@ -89,7 +89,7 @@ kproc_create(void (*func)(void *), void *arg,
panic("kproc_create called too soon");
error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
- pages, &p2, NULL, 0, NULL);
+ pages, &p2, NULL);
if (error)
return error;
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index f2f4a9d..60efdcd 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -171,6 +171,7 @@ struct nlminfo;
struct p_sched;
struct proc;
struct procdesc;
+struct procdesc_req;
struct racct;
struct sbuf;
struct sleepqueue;
@@ -930,8 +931,8 @@ int enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp,
int enterthispgrp(struct proc *p, struct pgrp *pgrp);
void faultin(struct proc *p);
void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
-int fork1(struct thread *, int, int, struct proc **, int *, int,
- struct filecaps *);
+int fork1(struct thread *, int, int, struct proc **,
+ struct procdesc_req *);
void fork_exit(void (*)(void *, struct trapframe *), void *,
struct trapframe *);
void fork_return(struct thread *, struct trapframe *);
diff --git a/sys/sys/procdesc.h b/sys/sys/procdesc.h
index 1a3bc98..ee7abf8 100644
--- a/sys/sys/procdesc.h
+++ b/sys/sys/procdesc.h
@@ -73,6 +73,12 @@ struct procdesc {
struct mtx pd_lock; /* Protect data + events. */
};
+struct procdesc_req {
+ int pdr_fd;
+ int pdr_flags;
+ struct filecaps *pdr_fcaps;
+};
+
/*
* Locking macros for the procdesc itself.
*/
--
2.7.0
More information about the freebsd-hackers
mailing list