summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorguenther <guenther@openbsd.org>2015-02-10 05:28:18 +0000
committerguenther <guenther@openbsd.org>2015-02-10 05:28:18 +0000
commitbf00d64f48f270898729a797c3259daf3864ff15 (patch)
tree29f2f7c45b25c612d5128a8ef45d678c283ee035
parentManually expand ASN1_ITEM_rptr macros that should have been expanded with (diff)
downloadwireguard-openbsd-bf00d64f48f270898729a797c3259daf3864ff15.tar.xz
wireguard-openbsd-bf00d64f48f270898729a797c3259daf3864ff15.zip
Factor out the common bits of process_new() and main()'s code for
setting up process0, 'cause I'm sick of forgetting to update main() when touching process_new() ok blambert@ miod@
-rw-r--r--sys/kern/init_main.c21
-rw-r--r--sys/kern/kern_fork.c45
-rw-r--r--sys/sys/proc.h3
3 files changed, 37 insertions, 32 deletions
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index c84a23f80fb..ded66b1cfd8 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: init_main.c,v 1.234 2015/02/09 09:39:09 miod Exp $ */
+/* $OpenBSD: init_main.c,v 1.235 2015/02/10 05:28:18 guenther Exp $ */
/* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */
/*
@@ -257,15 +257,16 @@ main(void *framep)
*/
kqueue_init();
+ /* Create credentials. */
+ p->p_ucred = crget();
+ p->p_ucred->cr_ngroups = 1; /* group 0 */
+
/*
* Create process 0 (the swapper).
*/
+ pr = &process0;
+ process_initialize(pr, p);
- process0.ps_mainproc = p;
- TAILQ_INIT(&process0.ps_threads);
- TAILQ_INSERT_TAIL(&process0.ps_threads, p, p_thr_link);
- process0.ps_refcnt = 1;
- p->p_p = pr = &process0;
LIST_INSERT_HEAD(&allprocess, pr, ps_list);
atomic_setbits_int(&pr->ps_flags, PS_SYSTEM);
@@ -291,14 +292,6 @@ main(void *framep)
/* Init timeouts. */
timeout_set(&p->p_sleep_to, endtsleep, p);
- timeout_set(&pr->ps_realit_to, realitexpire, pr);
-
- /* Create credentials. */
- pr->ps_ucred = crget();
- pr->ps_ucred->cr_ngroups = 1; /* group 0 */
-
- p->p_ucred = pr->ps_ucred; /* prime the thread's cache */
- crhold(p->p_ucred);
/* Initialize signal state for process 0. */
signal_init();
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index ea20aad122b..85439b05065 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_fork.c,v 1.177 2014/11/18 02:37:31 tedu Exp $ */
+/* $OpenBSD: kern_fork.c,v 1.178 2015/02/10 05:28:18 guenther Exp $ */
/* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */
/*
@@ -93,7 +93,6 @@ fork_return(void *arg)
child_return(p);
}
-/*ARGSUSED*/
int
sys_fork(struct proc *p, void *v, register_t *retval)
{
@@ -105,7 +104,6 @@ sys_fork(struct proc *p, void *v, register_t *retval)
return (fork1(p, flags, NULL, 0, fork_return, NULL, retval, NULL));
}
-/*ARGSUSED*/
int
sys_vfork(struct proc *p, void *v, register_t *retval)
{
@@ -151,6 +149,30 @@ tfork_child_return(void *arg)
}
/*
+ * Initialize common bits of a process structure, given the initial thread.
+ */
+void
+process_initialize(struct process *pr, struct proc *p)
+{
+ /* initialize the thread links */
+ pr->ps_mainproc = p;
+ TAILQ_INIT(&pr->ps_threads);
+ TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link);
+ pr->ps_refcnt = 1;
+ p->p_p = pr;
+
+ /* give the process the same creds as the initial thread */
+ pr->ps_ucred = p->p_ucred;
+ crhold(pr->ps_ucred);
+ KASSERT(p->p_ucred->cr_ref >= 2); /* new thread and new process */
+
+ LIST_INIT(&pr->ps_children);
+
+ timeout_set(&pr->ps_realit_to, realitexpire, pr);
+}
+
+
+/*
* Allocate and initialize a new process.
*/
void
@@ -159,13 +181,6 @@ process_new(struct proc *p, struct process *parent, int flags)
struct process *pr;
pr = pool_get(&process_pool, PR_WAITOK);
- pr->ps_mainproc = p;
-
- TAILQ_INIT(&pr->ps_threads);
- TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link);
- pr->ps_pptr = parent;
- LIST_INIT(&pr->ps_children);
- pr->ps_refcnt = 1;
/*
* Make a process structure for the new process.
@@ -177,10 +192,10 @@ process_new(struct proc *p, struct process *parent, int flags)
memcpy(&pr->ps_startcopy, &parent->ps_startcopy,
(caddr_t)&pr->ps_endcopy - (caddr_t)&pr->ps_startcopy);
+ process_initialize(pr, p);
+
/* post-copy fixups */
- pr->ps_ucred = p->p_ucred;
- crhold(pr->ps_ucred);
- KASSERT(p->p_ucred->cr_ref >= 3); /* fork thr, new thr, new process */
+ pr->ps_pptr = parent;
pr->ps_limit->p_refcnt++;
/* bump references to the text vnode (for sysctl) */
@@ -188,14 +203,10 @@ process_new(struct proc *p, struct process *parent, int flags)
if (pr->ps_textvp)
vref(pr->ps_textvp);
- timeout_set(&pr->ps_realit_to, realitexpire, pr);
-
pr->ps_flags = parent->ps_flags & (PS_SUGID | PS_SUGIDEXEC);
if (parent->ps_session->s_ttyvp != NULL)
pr->ps_flags |= parent->ps_flags & PS_CONTROLT;
- p->p_p = pr;
-
/*
* Duplicate sub-structures as needed.
* Increase reference counts on shared objects.
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index a0461180a5e..054c8e9c373 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: proc.h,v 1.198 2015/02/09 04:27:18 dlg Exp $ */
+/* $OpenBSD: proc.h,v 1.199 2015/02/10 05:28:18 guenther Exp $ */
/* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */
/*-
@@ -493,6 +493,7 @@ void exit2(struct proc *);
int dowait4(struct proc *, pid_t, int *, int, struct rusage *,
register_t *);
void cpu_exit(struct proc *);
+void process_initialize(struct process *, struct proc *);
int fork1(struct proc *, int, void *, pid_t *, void (*)(void *),
void *, register_t *, struct proc **);
int groupmember(gid_t, struct ucred *);