kern_exec.c revision 1.345 1 1.345 martin /* $NetBSD: kern_exec.c,v 1.345 2012/03/10 08:46:45 martin Exp $ */
2 1.277 ad
3 1.277 ad /*-
4 1.277 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.277 ad * All rights reserved.
6 1.277 ad *
7 1.277 ad * Redistribution and use in source and binary forms, with or without
8 1.277 ad * modification, are permitted provided that the following conditions
9 1.277 ad * are met:
10 1.277 ad * 1. Redistributions of source code must retain the above copyright
11 1.277 ad * notice, this list of conditions and the following disclaimer.
12 1.277 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.277 ad * notice, this list of conditions and the following disclaimer in the
14 1.277 ad * documentation and/or other materials provided with the distribution.
15 1.277 ad *
16 1.277 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.277 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.277 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.277 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.277 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.277 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.277 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.277 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.277 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.277 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.277 ad * POSSIBILITY OF SUCH DAMAGE.
27 1.277 ad */
28 1.55 cgd
29 1.55 cgd /*-
30 1.77 cgd * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
31 1.55 cgd * Copyright (C) 1992 Wolfgang Solfrank.
32 1.55 cgd * Copyright (C) 1992 TooLs GmbH.
33 1.55 cgd * All rights reserved.
34 1.55 cgd *
35 1.55 cgd * Redistribution and use in source and binary forms, with or without
36 1.55 cgd * modification, are permitted provided that the following conditions
37 1.55 cgd * are met:
38 1.55 cgd * 1. Redistributions of source code must retain the above copyright
39 1.55 cgd * notice, this list of conditions and the following disclaimer.
40 1.55 cgd * 2. Redistributions in binary form must reproduce the above copyright
41 1.55 cgd * notice, this list of conditions and the following disclaimer in the
42 1.55 cgd * documentation and/or other materials provided with the distribution.
43 1.55 cgd * 3. All advertising materials mentioning features or use of this software
44 1.55 cgd * must display the following acknowledgement:
45 1.55 cgd * This product includes software developed by TooLs GmbH.
46 1.55 cgd * 4. The name of TooLs GmbH may not be used to endorse or promote products
47 1.55 cgd * derived from this software without specific prior written permission.
48 1.55 cgd *
49 1.55 cgd * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
50 1.55 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 1.55 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 1.55 cgd * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 1.55 cgd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 1.55 cgd * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 1.55 cgd * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 1.55 cgd * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 1.55 cgd * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 1.55 cgd * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 1.55 cgd */
60 1.146 lukem
61 1.146 lukem #include <sys/cdefs.h>
62 1.345 martin __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.345 2012/03/10 08:46:45 martin Exp $");
63 1.89 mrg
64 1.325 jmcneill #include "opt_exec.h"
65 1.92 thorpej #include "opt_ktrace.h"
66 1.285 apb #include "opt_modular.h"
67 1.124 jdolecek #include "opt_syscall_debug.h"
68 1.226 dogcow #include "veriexec.h"
69 1.232 elad #include "opt_pax.h"
70 1.55 cgd
71 1.55 cgd #include <sys/param.h>
72 1.55 cgd #include <sys/systm.h>
73 1.55 cgd #include <sys/filedesc.h>
74 1.55 cgd #include <sys/kernel.h>
75 1.55 cgd #include <sys/proc.h>
76 1.55 cgd #include <sys/mount.h>
77 1.55 cgd #include <sys/malloc.h>
78 1.265 yamt #include <sys/kmem.h>
79 1.55 cgd #include <sys/namei.h>
80 1.55 cgd #include <sys/vnode.h>
81 1.55 cgd #include <sys/file.h>
82 1.55 cgd #include <sys/acct.h>
83 1.337 martin #include <sys/atomic.h>
84 1.55 cgd #include <sys/exec.h>
85 1.55 cgd #include <sys/ktrace.h>
86 1.278 pooka #include <sys/uidinfo.h>
87 1.55 cgd #include <sys/wait.h>
88 1.55 cgd #include <sys/mman.h>
89 1.155 gmcgarry #include <sys/ras.h>
90 1.55 cgd #include <sys/signalvar.h>
91 1.55 cgd #include <sys/stat.h>
92 1.124 jdolecek #include <sys/syscall.h>
93 1.218 elad #include <sys/kauth.h>
94 1.253 ad #include <sys/lwpctl.h>
95 1.260 christos #include <sys/pax.h>
96 1.263 ad #include <sys/cpu.h>
97 1.282 ad #include <sys/module.h>
98 1.289 pooka #include <sys/syscallvar.h>
99 1.56 cgd #include <sys/syscallargs.h>
100 1.222 elad #if NVERIEXEC > 0
101 1.197 blymn #include <sys/verified_exec.h>
102 1.222 elad #endif /* NVERIEXEC > 0 */
103 1.294 darran #include <sys/sdt.h>
104 1.337 martin #include <sys/spawn.h>
105 1.337 martin #include <sys/prot.h>
106 1.330 tls #include <sys/cprng.h>
107 1.55 cgd
108 1.88 mrg #include <uvm/uvm_extern.h>
109 1.88 mrg
110 1.55 cgd #include <machine/reg.h>
111 1.55 cgd
112 1.244 dsl #include <compat/common/compat_util.h>
113 1.244 dsl
114 1.171 chs static int exec_sigcode_map(struct proc *, const struct emul *);
115 1.171 chs
116 1.143 christos #ifdef DEBUG_EXEC
117 1.305 matt #define DPRINTF(a) printf a
118 1.312 christos #define COPYPRINTF(s, a, b) printf("%s, %d: copyout%s @%p %zu\n", __func__, \
119 1.312 christos __LINE__, (s), (a), (b))
120 1.143 christos #else
121 1.143 christos #define DPRINTF(a)
122 1.312 christos #define COPYPRINTF(s, a, b)
123 1.143 christos #endif /* DEBUG_EXEC */
124 1.165 thorpej
125 1.130 jdolecek /*
126 1.294 darran * DTrace SDT provider definitions
127 1.294 darran */
128 1.294 darran SDT_PROBE_DEFINE(proc,,,exec,
129 1.294 darran "char *", NULL,
130 1.294 darran NULL, NULL, NULL, NULL,
131 1.294 darran NULL, NULL, NULL, NULL);
132 1.294 darran SDT_PROBE_DEFINE(proc,,,exec_success,
133 1.294 darran "char *", NULL,
134 1.294 darran NULL, NULL, NULL, NULL,
135 1.294 darran NULL, NULL, NULL, NULL);
136 1.294 darran SDT_PROBE_DEFINE(proc,,,exec_failure,
137 1.294 darran "int", NULL,
138 1.294 darran NULL, NULL, NULL, NULL,
139 1.294 darran NULL, NULL, NULL, NULL);
140 1.294 darran
141 1.294 darran /*
142 1.130 jdolecek * Exec function switch:
143 1.130 jdolecek *
144 1.130 jdolecek * Note that each makecmds function is responsible for loading the
145 1.130 jdolecek * exec package with the necessary functions for any exec-type-specific
146 1.130 jdolecek * handling.
147 1.130 jdolecek *
148 1.130 jdolecek * Functions for specific exec types should be defined in their own
149 1.130 jdolecek * header file.
150 1.130 jdolecek */
151 1.138 lukem static const struct execsw **execsw = NULL;
152 1.138 lukem static int nexecs;
153 1.138 lukem
154 1.282 ad u_int exec_maxhdrsz; /* must not be static - used by netbsd32 */
155 1.130 jdolecek
156 1.130 jdolecek /* list of dynamically loaded execsw entries */
157 1.282 ad static LIST_HEAD(execlist_head, exec_entry) ex_head =
158 1.282 ad LIST_HEAD_INITIALIZER(ex_head);
159 1.130 jdolecek struct exec_entry {
160 1.138 lukem LIST_ENTRY(exec_entry) ex_list;
161 1.282 ad SLIST_ENTRY(exec_entry) ex_slist;
162 1.282 ad const struct execsw *ex_sw;
163 1.130 jdolecek };
164 1.130 jdolecek
165 1.203 christos #ifndef __HAVE_SYSCALL_INTERN
166 1.203 christos void syscall(void);
167 1.203 christos #endif
168 1.203 christos
169 1.173 christos /* NetBSD emul struct */
170 1.282 ad struct emul emul_netbsd = {
171 1.291 rmind .e_name = "netbsd",
172 1.291 rmind .e_path = NULL,
173 1.133 mycroft #ifndef __HAVE_MINIMAL_EMUL
174 1.291 rmind .e_flags = EMUL_HAS_SYS___syscall,
175 1.291 rmind .e_errno = NULL,
176 1.291 rmind .e_nosys = SYS_syscall,
177 1.291 rmind .e_nsysent = SYS_NSYSENT,
178 1.133 mycroft #endif
179 1.291 rmind .e_sysent = sysent,
180 1.124 jdolecek #ifdef SYSCALL_DEBUG
181 1.291 rmind .e_syscallnames = syscallnames,
182 1.124 jdolecek #else
183 1.291 rmind .e_syscallnames = NULL,
184 1.124 jdolecek #endif
185 1.291 rmind .e_sendsig = sendsig,
186 1.291 rmind .e_trapsignal = trapsignal,
187 1.291 rmind .e_tracesig = NULL,
188 1.291 rmind .e_sigcode = NULL,
189 1.291 rmind .e_esigcode = NULL,
190 1.291 rmind .e_sigobject = NULL,
191 1.291 rmind .e_setregs = setregs,
192 1.291 rmind .e_proc_exec = NULL,
193 1.291 rmind .e_proc_fork = NULL,
194 1.291 rmind .e_proc_exit = NULL,
195 1.291 rmind .e_lwp_fork = NULL,
196 1.291 rmind .e_lwp_exit = NULL,
197 1.133 mycroft #ifdef __HAVE_SYSCALL_INTERN
198 1.291 rmind .e_syscall_intern = syscall_intern,
199 1.133 mycroft #else
200 1.291 rmind .e_syscall = syscall,
201 1.133 mycroft #endif
202 1.291 rmind .e_sysctlovly = NULL,
203 1.291 rmind .e_fault = NULL,
204 1.291 rmind .e_vm_default_addr = uvm_default_mapaddr,
205 1.291 rmind .e_usertrap = NULL,
206 1.291 rmind .e_ucsize = sizeof(ucontext_t),
207 1.291 rmind .e_startlwp = startlwp
208 1.124 jdolecek };
209 1.124 jdolecek
210 1.55 cgd /*
211 1.130 jdolecek * Exec lock. Used to control access to execsw[] structures.
212 1.130 jdolecek * This must not be static so that netbsd32 can access it, too.
213 1.130 jdolecek */
214 1.237 ad krwlock_t exec_lock;
215 1.183 junyoung
216 1.259 ad static kmutex_t sigobject_lock;
217 1.259 ad
218 1.337 martin /*
219 1.337 martin * Data used between a loadvm and execve part of an "exec" operation
220 1.337 martin */
221 1.337 martin struct execve_data {
222 1.337 martin struct exec_package ed_pack;
223 1.337 martin struct pathbuf *ed_pathbuf;
224 1.337 martin struct vattr ed_attr;
225 1.337 martin struct ps_strings ed_arginfo;
226 1.337 martin char *ed_argp;
227 1.337 martin const char *ed_pathstring;
228 1.337 martin char *ed_resolvedpathbuf;
229 1.337 martin size_t ed_ps_strings_sz;
230 1.337 martin int ed_szsigcode;
231 1.337 martin long ed_argc;
232 1.337 martin long ed_envc;
233 1.337 martin };
234 1.337 martin
235 1.337 martin /*
236 1.337 martin * data passed from parent lwp to child during a posix_spawn()
237 1.337 martin */
238 1.337 martin struct spawn_exec_data {
239 1.337 martin struct execve_data sed_exec;
240 1.337 martin size_t sed_actions_len;
241 1.337 martin struct posix_spawn_file_actions_entry
242 1.337 martin *sed_actions;
243 1.337 martin struct posix_spawnattr *sed_attrs;
244 1.337 martin struct proc *sed_parent;
245 1.337 martin kcondvar_t sed_cv_child_ready;
246 1.337 martin kmutex_t sed_mtx_child;
247 1.337 martin int sed_error;
248 1.337 martin };
249 1.337 martin
250 1.277 ad static void *
251 1.277 ad exec_pool_alloc(struct pool *pp, int flags)
252 1.277 ad {
253 1.277 ad
254 1.277 ad return (void *)uvm_km_alloc(kernel_map, NCARGS, 0,
255 1.277 ad UVM_KMF_PAGEABLE | UVM_KMF_WAITVA);
256 1.277 ad }
257 1.277 ad
258 1.277 ad static void
259 1.277 ad exec_pool_free(struct pool *pp, void *addr)
260 1.277 ad {
261 1.277 ad
262 1.277 ad uvm_km_free(kernel_map, (vaddr_t)addr, NCARGS, UVM_KMF_PAGEABLE);
263 1.277 ad }
264 1.277 ad
265 1.277 ad static struct pool exec_pool;
266 1.277 ad
267 1.277 ad static struct pool_allocator exec_palloc = {
268 1.277 ad .pa_alloc = exec_pool_alloc,
269 1.277 ad .pa_free = exec_pool_free,
270 1.277 ad .pa_pagesz = NCARGS
271 1.277 ad };
272 1.277 ad
273 1.130 jdolecek /*
274 1.55 cgd * check exec:
275 1.55 cgd * given an "executable" described in the exec package's namei info,
276 1.55 cgd * see what we can do with it.
277 1.55 cgd *
278 1.55 cgd * ON ENTRY:
279 1.55 cgd * exec package with appropriate namei info
280 1.212 christos * lwp pointer of exec'ing lwp
281 1.55 cgd * NO SELF-LOCKED VNODES
282 1.55 cgd *
283 1.55 cgd * ON EXIT:
284 1.55 cgd * error: nothing held, etc. exec header still allocated.
285 1.77 cgd * ok: filled exec package, executable's vnode (unlocked).
286 1.55 cgd *
287 1.55 cgd * EXEC SWITCH ENTRY:
288 1.55 cgd * Locked vnode to check, exec package, proc.
289 1.55 cgd *
290 1.55 cgd * EXEC SWITCH EXIT:
291 1.77 cgd * ok: return 0, filled exec package, executable's vnode (unlocked).
292 1.55 cgd * error: destructive:
293 1.55 cgd * everything deallocated execept exec header.
294 1.76 cgd * non-destructive:
295 1.77 cgd * error code, executable's vnode (unlocked),
296 1.76 cgd * exec header unmodified.
297 1.55 cgd */
298 1.55 cgd int
299 1.205 christos /*ARGSUSED*/
300 1.301 dholland check_exec(struct lwp *l, struct exec_package *epp, struct pathbuf *pb)
301 1.55 cgd {
302 1.138 lukem int error, i;
303 1.138 lukem struct vnode *vp;
304 1.295 dholland struct nameidata nd;
305 1.138 lukem size_t resid;
306 1.55 cgd
307 1.303 dholland NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
308 1.295 dholland
309 1.55 cgd /* first get the vnode */
310 1.295 dholland if ((error = namei(&nd)) != 0)
311 1.55 cgd return error;
312 1.295 dholland epp->ep_vp = vp = nd.ni_vp;
313 1.295 dholland /* this cannot overflow as both are size PATH_MAX */
314 1.302 dholland strcpy(epp->ep_resolvedname, nd.ni_pnbuf);
315 1.295 dholland
316 1.296 dholland #ifdef DIAGNOSTIC
317 1.296 dholland /* paranoia (take this out once namei stuff stabilizes) */
318 1.302 dholland memset(nd.ni_pnbuf, '~', PATH_MAX);
319 1.295 dholland #endif
320 1.55 cgd
321 1.84 mycroft /* check access and type */
322 1.55 cgd if (vp->v_type != VREG) {
323 1.81 kleink error = EACCES;
324 1.55 cgd goto bad1;
325 1.55 cgd }
326 1.254 pooka if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
327 1.84 mycroft goto bad1;
328 1.55 cgd
329 1.55 cgd /* get attributes */
330 1.254 pooka if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred)) != 0)
331 1.55 cgd goto bad1;
332 1.55 cgd
333 1.55 cgd /* Check mount point */
334 1.55 cgd if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
335 1.55 cgd error = EACCES;
336 1.55 cgd goto bad1;
337 1.55 cgd }
338 1.141 thorpej if (vp->v_mount->mnt_flag & MNT_NOSUID)
339 1.83 mycroft epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
340 1.55 cgd
341 1.55 cgd /* try to open it */
342 1.254 pooka if ((error = VOP_OPEN(vp, FREAD, l->l_cred)) != 0)
343 1.55 cgd goto bad1;
344 1.55 cgd
345 1.99 wrstuden /* unlock vp, since we need it unlocked from here on out. */
346 1.298 hannken VOP_UNLOCK(vp);
347 1.77 cgd
348 1.222 elad #if NVERIEXEC > 0
349 1.295 dholland error = veriexec_verify(l, vp, epp->ep_resolvedname,
350 1.233 elad epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT,
351 1.236 elad NULL);
352 1.236 elad if (error)
353 1.234 elad goto bad2;
354 1.222 elad #endif /* NVERIEXEC > 0 */
355 1.160 blymn
356 1.232 elad #ifdef PAX_SEGVGUARD
357 1.295 dholland error = pax_segvguard(l, vp, epp->ep_resolvedname, false);
358 1.234 elad if (error)
359 1.234 elad goto bad2;
360 1.232 elad #endif /* PAX_SEGVGUARD */
361 1.232 elad
362 1.55 cgd /* now we have the file, get the exec header */
363 1.74 christos error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
364 1.223 ad UIO_SYSSPACE, 0, l->l_cred, &resid, NULL);
365 1.74 christos if (error)
366 1.55 cgd goto bad2;
367 1.55 cgd epp->ep_hdrvalid = epp->ep_hdrlen - resid;
368 1.55 cgd
369 1.55 cgd /*
370 1.136 eeh * Set up default address space limits. Can be overridden
371 1.136 eeh * by individual exec packages.
372 1.183 junyoung *
373 1.235 rillig * XXX probably should be all done in the exec packages.
374 1.136 eeh */
375 1.136 eeh epp->ep_vm_minaddr = VM_MIN_ADDRESS;
376 1.136 eeh epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
377 1.136 eeh /*
378 1.55 cgd * set up the vmcmds for creation of the process
379 1.55 cgd * address space
380 1.55 cgd */
381 1.55 cgd error = ENOEXEC;
382 1.244 dsl for (i = 0; i < nexecs; i++) {
383 1.68 cgd int newerror;
384 1.68 cgd
385 1.130 jdolecek epp->ep_esch = execsw[i];
386 1.212 christos newerror = (*execsw[i]->es_makecmds)(l, epp);
387 1.244 dsl
388 1.244 dsl if (!newerror) {
389 1.318 reinoud /* Seems ok: check that entry point is not too high */
390 1.323 reinoud if (epp->ep_entry > epp->ep_vm_maxaddr) {
391 1.322 reinoud #ifdef DIAGNOSTIC
392 1.329 reinoud printf("%s: rejecting %p due to "
393 1.331 christos "too high entry address (> %p)\n",
394 1.331 christos __func__, (void *)epp->ep_entry,
395 1.331 christos (void *)epp->ep_vm_maxaddr);
396 1.322 reinoud #endif
397 1.318 reinoud error = ENOEXEC;
398 1.318 reinoud break;
399 1.318 reinoud }
400 1.318 reinoud /* Seems ok: check that entry point is not too low */
401 1.323 reinoud if (epp->ep_entry < epp->ep_vm_minaddr) {
402 1.322 reinoud #ifdef DIAGNOSTIC
403 1.329 reinoud printf("%s: rejecting %p due to "
404 1.331 christos "too low entry address (< %p)\n",
405 1.331 christos __func__, (void *)epp->ep_entry,
406 1.331 christos (void *)epp->ep_vm_minaddr);
407 1.322 reinoud #endif
408 1.244 dsl error = ENOEXEC;
409 1.244 dsl break;
410 1.244 dsl }
411 1.244 dsl
412 1.244 dsl /* check limits */
413 1.244 dsl if ((epp->ep_tsize > MAXTSIZ) ||
414 1.244 dsl (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit
415 1.244 dsl [RLIMIT_DATA].rlim_cur)) {
416 1.322 reinoud #ifdef DIAGNOSTIC
417 1.323 reinoud printf("%s: rejecting due to "
418 1.331 christos "limits (t=%llu > %llu || d=%llu > %llu)\n",
419 1.331 christos __func__,
420 1.331 christos (unsigned long long)epp->ep_tsize,
421 1.331 christos (unsigned long long)MAXTSIZ,
422 1.331 christos (unsigned long long)epp->ep_dsize,
423 1.332 christos (unsigned long long)
424 1.332 christos l->l_proc->p_rlimit[RLIMIT_DATA].rlim_cur);
425 1.322 reinoud #endif
426 1.244 dsl error = ENOMEM;
427 1.244 dsl break;
428 1.244 dsl }
429 1.244 dsl return 0;
430 1.244 dsl }
431 1.244 dsl
432 1.244 dsl if (epp->ep_emul_root != NULL) {
433 1.244 dsl vrele(epp->ep_emul_root);
434 1.244 dsl epp->ep_emul_root = NULL;
435 1.244 dsl }
436 1.244 dsl if (epp->ep_interp != NULL) {
437 1.244 dsl vrele(epp->ep_interp);
438 1.244 dsl epp->ep_interp = NULL;
439 1.244 dsl }
440 1.244 dsl
441 1.68 cgd /* make sure the first "interesting" error code is saved. */
442 1.244 dsl if (error == ENOEXEC)
443 1.68 cgd error = newerror;
444 1.124 jdolecek
445 1.244 dsl if (epp->ep_flags & EXEC_DESTR)
446 1.244 dsl /* Error from "#!" code, tidied up by recursive call */
447 1.55 cgd return error;
448 1.55 cgd }
449 1.55 cgd
450 1.249 pooka /* not found, error */
451 1.249 pooka
452 1.55 cgd /*
453 1.55 cgd * free any vmspace-creation commands,
454 1.55 cgd * and release their references
455 1.55 cgd */
456 1.55 cgd kill_vmcmds(&epp->ep_vmcmds);
457 1.55 cgd
458 1.55 cgd bad2:
459 1.55 cgd /*
460 1.99 wrstuden * close and release the vnode, restore the old one, free the
461 1.55 cgd * pathname buf, and punt.
462 1.55 cgd */
463 1.99 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
464 1.254 pooka VOP_CLOSE(vp, FREAD, l->l_cred);
465 1.99 wrstuden vput(vp);
466 1.55 cgd return error;
467 1.55 cgd
468 1.55 cgd bad1:
469 1.55 cgd /*
470 1.55 cgd * free the namei pathname buffer, and put the vnode
471 1.55 cgd * (which we don't yet have open).
472 1.55 cgd */
473 1.77 cgd vput(vp); /* was still locked */
474 1.55 cgd return error;
475 1.55 cgd }
476 1.55 cgd
477 1.188 chs #ifdef __MACHINE_STACK_GROWS_UP
478 1.188 chs #define STACK_PTHREADSPACE NBPG
479 1.188 chs #else
480 1.188 chs #define STACK_PTHREADSPACE 0
481 1.188 chs #endif
482 1.188 chs
483 1.204 cube static int
484 1.204 cube execve_fetch_element(char * const *array, size_t index, char **value)
485 1.204 cube {
486 1.204 cube return copyin(array + index, value, sizeof(*value));
487 1.204 cube }
488 1.204 cube
489 1.55 cgd /*
490 1.55 cgd * exec system call
491 1.55 cgd */
492 1.75 christos int
493 1.258 dsl sys_execve(struct lwp *l, const struct sys_execve_args *uap, register_t *retval)
494 1.71 thorpej {
495 1.258 dsl /* {
496 1.138 lukem syscallarg(const char *) path;
497 1.138 lukem syscallarg(char * const *) argp;
498 1.138 lukem syscallarg(char * const *) envp;
499 1.258 dsl } */
500 1.204 cube
501 1.204 cube return execve1(l, SCARG(uap, path), SCARG(uap, argp),
502 1.204 cube SCARG(uap, envp), execve_fetch_element);
503 1.204 cube }
504 1.204 cube
505 1.317 manu int
506 1.317 manu sys_fexecve(struct lwp *l, const struct sys_fexecve_args *uap,
507 1.317 manu register_t *retval)
508 1.317 manu {
509 1.317 manu /* {
510 1.317 manu syscallarg(int) fd;
511 1.317 manu syscallarg(char * const *) argp;
512 1.317 manu syscallarg(char * const *) envp;
513 1.317 manu } */
514 1.317 manu
515 1.317 manu return ENOSYS;
516 1.317 manu }
517 1.317 manu
518 1.282 ad /*
519 1.282 ad * Load modules to try and execute an image that we do not understand.
520 1.282 ad * If no execsw entries are present, we load those likely to be needed
521 1.282 ad * in order to run native images only. Otherwise, we autoload all
522 1.282 ad * possible modules that could let us run the binary. XXX lame
523 1.282 ad */
524 1.282 ad static void
525 1.282 ad exec_autoload(void)
526 1.282 ad {
527 1.282 ad #ifdef MODULAR
528 1.282 ad static const char * const native[] = {
529 1.282 ad "exec_elf32",
530 1.282 ad "exec_elf64",
531 1.282 ad "exec_script",
532 1.282 ad NULL
533 1.282 ad };
534 1.282 ad static const char * const compat[] = {
535 1.282 ad "exec_elf32",
536 1.282 ad "exec_elf64",
537 1.282 ad "exec_script",
538 1.282 ad "exec_aout",
539 1.282 ad "exec_coff",
540 1.282 ad "exec_ecoff",
541 1.282 ad "compat_aoutm68k",
542 1.282 ad "compat_freebsd",
543 1.282 ad "compat_ibcs2",
544 1.282 ad "compat_linux",
545 1.282 ad "compat_linux32",
546 1.282 ad "compat_netbsd32",
547 1.282 ad "compat_sunos",
548 1.282 ad "compat_sunos32",
549 1.282 ad "compat_svr4",
550 1.282 ad "compat_svr4_32",
551 1.282 ad "compat_ultrix",
552 1.282 ad NULL
553 1.282 ad };
554 1.282 ad char const * const *list;
555 1.282 ad int i;
556 1.282 ad
557 1.282 ad list = (nexecs == 0 ? native : compat);
558 1.282 ad for (i = 0; list[i] != NULL; i++) {
559 1.282 ad if (module_autoload(list[i], MODULE_CLASS_MISC) != 0) {
560 1.282 ad continue;
561 1.282 ad }
562 1.282 ad yield();
563 1.282 ad }
564 1.282 ad #endif
565 1.282 ad }
566 1.282 ad
567 1.337 martin static int
568 1.337 martin execve_loadvm(struct lwp *l, const char *path, char * const *args,
569 1.337 martin char * const *envs, execve_fetch_element_t fetch_element,
570 1.337 martin struct execve_data * restrict data)
571 1.204 cube {
572 1.153 thorpej int error;
573 1.164 thorpej struct proc *p;
574 1.138 lukem char *dp, *sp;
575 1.248 christos size_t i, len;
576 1.265 yamt struct exec_fakearg *tmpfap;
577 1.282 ad u_int modgen;
578 1.337 martin
579 1.337 martin KASSERT(data != NULL);
580 1.55 cgd
581 1.237 ad p = l->l_proc;
582 1.282 ad modgen = 0;
583 1.164 thorpej
584 1.294 darran SDT_PROBE(proc,,,exec, path, 0, 0, 0, 0);
585 1.294 darran
586 1.149 christos /*
587 1.269 christos * Check if we have exceeded our number of processes limit.
588 1.269 christos * This is so that we handle the case where a root daemon
589 1.269 christos * forked, ran setuid to become the desired user and is trying
590 1.269 christos * to exec. The obvious place to do the reference counting check
591 1.269 christos * is setuid(), but we don't do the reference counting check there
592 1.269 christos * like other OS's do because then all the programs that use setuid()
593 1.269 christos * must be modified to check the return code of setuid() and exit().
594 1.269 christos * It is dangerous to make setuid() fail, because it fails open and
595 1.269 christos * the program will continue to run as root. If we make it succeed
596 1.269 christos * and return an error code, again we are not enforcing the limit.
597 1.269 christos * The best place to enforce the limit is here, when the process tries
598 1.269 christos * to execute a new image, because eventually the process will need
599 1.269 christos * to call exec in order to do something useful.
600 1.269 christos */
601 1.282 ad retry:
602 1.287 christos if ((p->p_flag & PK_SUGID) && kauth_authorize_generic(l->l_cred,
603 1.287 christos KAUTH_GENERIC_ISSUSER, NULL) != 0 && chgproccnt(kauth_cred_getuid(
604 1.287 christos l->l_cred), 0) > p->p_rlimit[RLIMIT_NPROC].rlim_cur)
605 1.269 christos return EAGAIN;
606 1.269 christos
607 1.269 christos /*
608 1.237 ad * Drain existing references and forbid new ones. The process
609 1.237 ad * should be left alone until we're done here. This is necessary
610 1.237 ad * to avoid race conditions - e.g. in ptrace() - that might allow
611 1.237 ad * a local user to illicitly obtain elevated privileges.
612 1.237 ad */
613 1.252 ad rw_enter(&p->p_reflock, RW_WRITER);
614 1.149 christos
615 1.55 cgd /*
616 1.129 jdolecek * Init the namei data to point the file user's program name.
617 1.129 jdolecek * This is done here rather than in check_exec(), so that it's
618 1.129 jdolecek * possible to override this settings if any of makecmd/probe
619 1.129 jdolecek * functions call check_exec() recursively - for example,
620 1.129 jdolecek * see exec_script_makecmds().
621 1.129 jdolecek */
622 1.337 martin error = pathbuf_copyin(path, &data->ed_pathbuf);
623 1.248 christos if (error) {
624 1.312 christos DPRINTF(("%s: pathbuf_copyin path @%p %d\n", __func__,
625 1.312 christos path, error));
626 1.200 elad goto clrflg;
627 1.248 christos }
628 1.337 martin data->ed_pathstring = pathbuf_stringcopy_get(data->ed_pathbuf);
629 1.337 martin
630 1.337 martin data->ed_resolvedpathbuf = PNBUF_GET();
631 1.295 dholland #ifdef DIAGNOSTIC
632 1.337 martin strcpy(data->ed_resolvedpathbuf, "/wrong");
633 1.295 dholland #endif
634 1.55 cgd
635 1.55 cgd /*
636 1.55 cgd * initialize the fields of the exec package.
637 1.55 cgd */
638 1.337 martin data->ed_pack.ep_name = path;
639 1.337 martin data->ed_pack.ep_kname = data->ed_pathstring;
640 1.337 martin data->ed_pack.ep_resolvedname = data->ed_resolvedpathbuf;
641 1.337 martin data->ed_pack.ep_hdr = kmem_alloc(exec_maxhdrsz, KM_SLEEP);
642 1.337 martin data->ed_pack.ep_hdrlen = exec_maxhdrsz;
643 1.337 martin data->ed_pack.ep_hdrvalid = 0;
644 1.337 martin data->ed_pack.ep_emul_arg = NULL;
645 1.337 martin data->ed_pack.ep_emul_arg_free = NULL;
646 1.337 martin data->ed_pack.ep_vmcmds.evs_cnt = 0;
647 1.337 martin data->ed_pack.ep_vmcmds.evs_used = 0;
648 1.337 martin data->ed_pack.ep_vap = &data->ed_attr;
649 1.337 martin data->ed_pack.ep_flags = 0;
650 1.337 martin data->ed_pack.ep_emul_root = NULL;
651 1.337 martin data->ed_pack.ep_interp = NULL;
652 1.337 martin data->ed_pack.ep_esch = NULL;
653 1.337 martin data->ed_pack.ep_pax_flags = 0;
654 1.55 cgd
655 1.237 ad rw_enter(&exec_lock, RW_READER);
656 1.130 jdolecek
657 1.55 cgd /* see if we can run it. */
658 1.337 martin if ((error = check_exec(l, &data->ed_pack, data->ed_pathbuf)) != 0) {
659 1.261 xtraeme if (error != ENOENT) {
660 1.312 christos DPRINTF(("%s: check exec failed %d\n",
661 1.312 christos __func__, error));
662 1.261 xtraeme }
663 1.55 cgd goto freehdr;
664 1.248 christos }
665 1.55 cgd
666 1.55 cgd /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
667 1.55 cgd
668 1.55 cgd /* allocate an argument buffer */
669 1.337 martin data->ed_argp = pool_get(&exec_pool, PR_WAITOK);
670 1.337 martin KASSERT(data->ed_argp != NULL);
671 1.337 martin dp = data->ed_argp;
672 1.337 martin data->ed_argc = 0;
673 1.55 cgd
674 1.55 cgd /* copy the fake args list, if there's one, freeing it as we go */
675 1.337 martin if (data->ed_pack.ep_flags & EXEC_HASARGL) {
676 1.337 martin tmpfap = data->ed_pack.ep_fa;
677 1.265 yamt while (tmpfap->fa_arg != NULL) {
678 1.265 yamt const char *cp;
679 1.55 cgd
680 1.265 yamt cp = tmpfap->fa_arg;
681 1.55 cgd while (*cp)
682 1.55 cgd *dp++ = *cp++;
683 1.276 ad *dp++ = '\0';
684 1.290 dsl ktrexecarg(tmpfap->fa_arg, cp - tmpfap->fa_arg);
685 1.55 cgd
686 1.265 yamt kmem_free(tmpfap->fa_arg, tmpfap->fa_len);
687 1.337 martin tmpfap++; data->ed_argc++;
688 1.55 cgd }
689 1.337 martin kmem_free(data->ed_pack.ep_fa, data->ed_pack.ep_fa_len);
690 1.337 martin data->ed_pack.ep_flags &= ~EXEC_HASARGL;
691 1.55 cgd }
692 1.55 cgd
693 1.55 cgd /* Now get argv & environment */
694 1.204 cube if (args == NULL) {
695 1.312 christos DPRINTF(("%s: null args\n", __func__));
696 1.55 cgd error = EINVAL;
697 1.55 cgd goto bad;
698 1.55 cgd }
699 1.204 cube /* 'i' will index the argp/envp element to be retrieved */
700 1.204 cube i = 0;
701 1.337 martin if (data->ed_pack.ep_flags & EXEC_SKIPARG)
702 1.204 cube i++;
703 1.55 cgd
704 1.55 cgd while (1) {
705 1.337 martin len = data->ed_argp + ARG_MAX - dp;
706 1.248 christos if ((error = (*fetch_element)(args, i, &sp)) != 0) {
707 1.312 christos DPRINTF(("%s: fetch_element args %d\n",
708 1.313 jakllsch __func__, error));
709 1.55 cgd goto bad;
710 1.248 christos }
711 1.55 cgd if (!sp)
712 1.55 cgd break;
713 1.74 christos if ((error = copyinstr(sp, dp, len, &len)) != 0) {
714 1.312 christos DPRINTF(("%s: copyinstr args %d\n", __func__, error));
715 1.55 cgd if (error == ENAMETOOLONG)
716 1.55 cgd error = E2BIG;
717 1.55 cgd goto bad;
718 1.55 cgd }
719 1.247 ad ktrexecarg(dp, len - 1);
720 1.55 cgd dp += len;
721 1.204 cube i++;
722 1.337 martin data->ed_argc++;
723 1.55 cgd }
724 1.55 cgd
725 1.337 martin data->ed_envc = 0;
726 1.74 christos /* environment need not be there */
727 1.204 cube if (envs != NULL) {
728 1.204 cube i = 0;
729 1.55 cgd while (1) {
730 1.337 martin len = data->ed_argp + ARG_MAX - dp;
731 1.248 christos if ((error = (*fetch_element)(envs, i, &sp)) != 0) {
732 1.312 christos DPRINTF(("%s: fetch_element env %d\n",
733 1.312 christos __func__, error));
734 1.55 cgd goto bad;
735 1.248 christos }
736 1.55 cgd if (!sp)
737 1.55 cgd break;
738 1.74 christos if ((error = copyinstr(sp, dp, len, &len)) != 0) {
739 1.312 christos DPRINTF(("%s: copyinstr env %d\n",
740 1.312 christos __func__, error));
741 1.55 cgd if (error == ENAMETOOLONG)
742 1.55 cgd error = E2BIG;
743 1.55 cgd goto bad;
744 1.55 cgd }
745 1.337 martin
746 1.247 ad ktrexecenv(dp, len - 1);
747 1.55 cgd dp += len;
748 1.204 cube i++;
749 1.337 martin data->ed_envc++;
750 1.55 cgd }
751 1.55 cgd }
752 1.61 mycroft
753 1.61 mycroft dp = (char *) ALIGN(dp);
754 1.55 cgd
755 1.337 martin data->ed_szsigcode = data->ed_pack.ep_esch->es_emul->e_esigcode -
756 1.337 martin data->ed_pack.ep_esch->es_emul->e_sigcode;
757 1.65 fvdl
758 1.267 dsl #ifdef __MACHINE_STACK_GROWS_UP
759 1.267 dsl /* See big comment lower down */
760 1.267 dsl #define RTLD_GAP 32
761 1.267 dsl #else
762 1.267 dsl #define RTLD_GAP 0
763 1.267 dsl #endif
764 1.267 dsl
765 1.55 cgd /* Now check if args & environ fit into new stack */
766 1.337 martin if (data->ed_pack.ep_flags & EXEC_32) {
767 1.337 martin data->ed_ps_strings_sz = sizeof(struct ps_strings32);
768 1.337 martin len = ((data->ed_argc + data->ed_envc + 2 +
769 1.337 martin data->ed_pack.ep_esch->es_arglen) *
770 1.267 dsl sizeof(int) + sizeof(int) + dp + RTLD_GAP +
771 1.337 martin data->ed_szsigcode + data->ed_ps_strings_sz + STACK_PTHREADSPACE)
772 1.337 martin - data->ed_argp;
773 1.311 joerg } else {
774 1.337 martin data->ed_ps_strings_sz = sizeof(struct ps_strings);
775 1.337 martin len = ((data->ed_argc + data->ed_envc + 2 +
776 1.337 martin data->ed_pack.ep_esch->es_arglen) *
777 1.267 dsl sizeof(char *) + sizeof(int) + dp + RTLD_GAP +
778 1.337 martin data->ed_szsigcode + data->ed_ps_strings_sz + STACK_PTHREADSPACE)
779 1.337 martin - data->ed_argp;
780 1.311 joerg }
781 1.67 christos
782 1.262 elad #ifdef PAX_ASLR
783 1.262 elad if (pax_aslr_active(l))
784 1.330 tls len += (cprng_fast32() % PAGE_SIZE);
785 1.262 elad #endif /* PAX_ASLR */
786 1.262 elad
787 1.334 christos /* make the stack "safely" aligned */
788 1.335 christos len = STACK_LEN_ALIGN(len, STACK_ALIGNBYTES);
789 1.55 cgd
790 1.337 martin if (len > data->ed_pack.ep_ssize) {
791 1.337 martin /* in effect, compare to initial limit */
792 1.312 christos DPRINTF(("%s: stack limit exceeded %zu\n", __func__, len));
793 1.55 cgd goto bad;
794 1.55 cgd }
795 1.337 martin /* adjust "active stack depth" for process VSZ */
796 1.337 martin data->ed_pack.ep_ssize = len;
797 1.337 martin
798 1.337 martin return 0;
799 1.337 martin
800 1.337 martin bad:
801 1.337 martin /* free the vmspace-creation commands, and release their references */
802 1.337 martin kill_vmcmds(&data->ed_pack.ep_vmcmds);
803 1.337 martin /* kill any opened file descriptor, if necessary */
804 1.337 martin if (data->ed_pack.ep_flags & EXEC_HASFD) {
805 1.337 martin data->ed_pack.ep_flags &= ~EXEC_HASFD;
806 1.337 martin fd_close(data->ed_pack.ep_fd);
807 1.337 martin }
808 1.337 martin /* close and put the exec'd file */
809 1.337 martin vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
810 1.337 martin VOP_CLOSE(data->ed_pack.ep_vp, FREAD, l->l_cred);
811 1.337 martin vput(data->ed_pack.ep_vp);
812 1.337 martin pool_put(&exec_pool, data->ed_argp);
813 1.337 martin
814 1.337 martin freehdr:
815 1.337 martin kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
816 1.337 martin if (data->ed_pack.ep_emul_root != NULL)
817 1.337 martin vrele(data->ed_pack.ep_emul_root);
818 1.337 martin if (data->ed_pack.ep_interp != NULL)
819 1.337 martin vrele(data->ed_pack.ep_interp);
820 1.337 martin
821 1.337 martin rw_exit(&exec_lock);
822 1.337 martin
823 1.337 martin pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
824 1.337 martin pathbuf_destroy(data->ed_pathbuf);
825 1.337 martin PNBUF_PUT(data->ed_resolvedpathbuf);
826 1.337 martin
827 1.337 martin clrflg:
828 1.337 martin rw_exit(&p->p_reflock);
829 1.337 martin
830 1.337 martin if (modgen != module_gen && error == ENOEXEC) {
831 1.337 martin modgen = module_gen;
832 1.337 martin exec_autoload();
833 1.337 martin goto retry;
834 1.337 martin }
835 1.337 martin
836 1.337 martin SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0);
837 1.337 martin return error;
838 1.337 martin }
839 1.337 martin
840 1.337 martin static int
841 1.337 martin execve_runproc(struct lwp *l, struct execve_data * restrict data)
842 1.337 martin {
843 1.337 martin int error = 0;
844 1.337 martin struct proc *p;
845 1.337 martin size_t i;
846 1.337 martin char *stack, *dp;
847 1.337 martin const char *commandname;
848 1.337 martin struct ps_strings32 arginfo32;
849 1.337 martin struct exec_vmcmd *base_vcp;
850 1.337 martin void *aip;
851 1.337 martin struct vmspace *vm;
852 1.337 martin ksiginfo_t ksi;
853 1.337 martin ksiginfoq_t kq;
854 1.337 martin bool proc_is_new;
855 1.337 martin
856 1.337 martin KASSERT(rw_lock_held(&exec_lock));
857 1.337 martin KASSERT(data != NULL);
858 1.337 martin if (data == NULL)
859 1.337 martin return (EINVAL);
860 1.337 martin
861 1.337 martin p = l->l_proc;
862 1.337 martin proc_is_new = p->p_vmspace == NULL;
863 1.337 martin
864 1.337 martin base_vcp = NULL;
865 1.337 martin
866 1.337 martin if (data->ed_pack.ep_flags & EXEC_32)
867 1.337 martin aip = &arginfo32;
868 1.337 martin else
869 1.337 martin aip = &data->ed_arginfo;
870 1.55 cgd
871 1.237 ad /* Get rid of other LWPs. */
872 1.340 rmind if (p->p_nlwps > 1) {
873 1.272 ad mutex_enter(p->p_lock);
874 1.237 ad exit_lwps(l);
875 1.272 ad mutex_exit(p->p_lock);
876 1.237 ad }
877 1.164 thorpej KDASSERT(p->p_nlwps == 1);
878 1.164 thorpej
879 1.253 ad /* Destroy any lwpctl info. */
880 1.253 ad if (p->p_lwpctl != NULL)
881 1.253 ad lwp_ctl_exit();
882 1.253 ad
883 1.164 thorpej /* Remove POSIX timers */
884 1.164 thorpej timers_free(p, TIMERS_POSIX);
885 1.164 thorpej
886 1.86 thorpej /*
887 1.86 thorpej * Do whatever is necessary to prepare the address space
888 1.86 thorpej * for remapping. Note that this might replace the current
889 1.86 thorpej * vmspace with another!
890 1.86 thorpej */
891 1.337 martin uvmspace_exec(l, data->ed_pack.ep_vm_minaddr, data->ed_pack.ep_vm_maxaddr);
892 1.55 cgd
893 1.186 chs /* record proc's vnode, for use by procfs and others */
894 1.186 chs if (p->p_textvp)
895 1.186 chs vrele(p->p_textvp);
896 1.337 martin vref(data->ed_pack.ep_vp);
897 1.337 martin p->p_textvp = data->ed_pack.ep_vp;
898 1.186 chs
899 1.55 cgd /* Now map address space */
900 1.86 thorpej vm = p->p_vmspace;
901 1.337 martin vm->vm_taddr = (void *)data->ed_pack.ep_taddr;
902 1.337 martin vm->vm_tsize = btoc(data->ed_pack.ep_tsize);
903 1.337 martin vm->vm_daddr = (void*)data->ed_pack.ep_daddr;
904 1.337 martin vm->vm_dsize = btoc(data->ed_pack.ep_dsize);
905 1.337 martin vm->vm_ssize = btoc(data->ed_pack.ep_ssize);
906 1.288 mrg vm->vm_issize = 0;
907 1.337 martin vm->vm_maxsaddr = (void *)data->ed_pack.ep_maxsaddr;
908 1.337 martin vm->vm_minsaddr = (void *)data->ed_pack.ep_minsaddr;
909 1.55 cgd
910 1.260 christos #ifdef PAX_ASLR
911 1.260 christos pax_aslr_init(l, vm);
912 1.260 christos #endif /* PAX_ASLR */
913 1.260 christos
914 1.55 cgd /* create the new process's VM space by running the vmcmds */
915 1.55 cgd #ifdef DIAGNOSTIC
916 1.337 martin if (data->ed_pack.ep_vmcmds.evs_used == 0)
917 1.312 christos panic("%s: no vmcmds", __func__);
918 1.55 cgd #endif
919 1.326 reinoud
920 1.326 reinoud #ifdef DEBUG_EXEC
921 1.326 reinoud {
922 1.326 reinoud size_t j;
923 1.337 martin struct exec_vmcmd *vp = &data->ed_pack.ep_vmcmds.evs_cmds[0];
924 1.337 martin DPRINTF(("vmcmds %u\n", data->ed_pack.ep_vmcmds.evs_used));
925 1.337 martin for (j = 0; j < data->ed_pack.ep_vmcmds.evs_used; j++) {
926 1.328 reinoud DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#"
927 1.326 reinoud PRIxVADDR"/%#"PRIxVSIZE" fd@%#"
928 1.326 reinoud PRIxVSIZE" prot=0%o flags=%d\n", j,
929 1.326 reinoud vp[j].ev_proc == vmcmd_map_pagedvn ?
930 1.326 reinoud "pagedvn" :
931 1.326 reinoud vp[j].ev_proc == vmcmd_map_readvn ?
932 1.326 reinoud "readvn" :
933 1.326 reinoud vp[j].ev_proc == vmcmd_map_zero ?
934 1.326 reinoud "zero" : "*unknown*",
935 1.326 reinoud vp[j].ev_addr, vp[j].ev_len,
936 1.326 reinoud vp[j].ev_offset, vp[j].ev_prot,
937 1.327 reinoud vp[j].ev_flags));
938 1.326 reinoud }
939 1.326 reinoud }
940 1.326 reinoud #endif /* DEBUG_EXEC */
941 1.326 reinoud
942 1.337 martin for (i = 0; i < data->ed_pack.ep_vmcmds.evs_used && !error; i++) {
943 1.55 cgd struct exec_vmcmd *vcp;
944 1.55 cgd
945 1.337 martin vcp = &data->ed_pack.ep_vmcmds.evs_cmds[i];
946 1.114 matt if (vcp->ev_flags & VMCMD_RELATIVE) {
947 1.114 matt #ifdef DIAGNOSTIC
948 1.114 matt if (base_vcp == NULL)
949 1.312 christos panic("%s: relative vmcmd with no base",
950 1.312 christos __func__);
951 1.114 matt if (vcp->ev_flags & VMCMD_BASE)
952 1.312 christos panic("%s: illegal base & relative vmcmd",
953 1.312 christos __func__);
954 1.114 matt #endif
955 1.114 matt vcp->ev_addr += base_vcp->ev_addr;
956 1.114 matt }
957 1.212 christos error = (*vcp->ev_proc)(l, vcp);
958 1.143 christos #ifdef DEBUG_EXEC
959 1.111 matt if (error) {
960 1.248 christos size_t j;
961 1.337 martin struct exec_vmcmd *vp =
962 1.337 martin &data->ed_pack.ep_vmcmds.evs_cmds[0];
963 1.327 reinoud DPRINTF(("vmcmds %zu/%u, error %d\n", i,
964 1.337 martin data->ed_pack.ep_vmcmds.evs_used, error));
965 1.337 martin for (j = 0; j < data->ed_pack.ep_vmcmds.evs_used; j++) {
966 1.327 reinoud DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#"
967 1.310 christos PRIxVADDR"/%#"PRIxVSIZE" fd@%#"
968 1.310 christos PRIxVSIZE" prot=0%o flags=%d\n", j,
969 1.310 christos vp[j].ev_proc == vmcmd_map_pagedvn ?
970 1.310 christos "pagedvn" :
971 1.310 christos vp[j].ev_proc == vmcmd_map_readvn ?
972 1.310 christos "readvn" :
973 1.310 christos vp[j].ev_proc == vmcmd_map_zero ?
974 1.310 christos "zero" : "*unknown*",
975 1.310 christos vp[j].ev_addr, vp[j].ev_len,
976 1.143 christos vp[j].ev_offset, vp[j].ev_prot,
977 1.327 reinoud vp[j].ev_flags));
978 1.326 reinoud if (j == i)
979 1.327 reinoud DPRINTF((" ^--- failed\n"));
980 1.326 reinoud }
981 1.111 matt }
982 1.143 christos #endif /* DEBUG_EXEC */
983 1.114 matt if (vcp->ev_flags & VMCMD_BASE)
984 1.114 matt base_vcp = vcp;
985 1.55 cgd }
986 1.55 cgd
987 1.55 cgd /* free the vmspace-creation commands, and release their references */
988 1.337 martin kill_vmcmds(&data->ed_pack.ep_vmcmds);
989 1.55 cgd
990 1.337 martin vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
991 1.337 martin VOP_CLOSE(data->ed_pack.ep_vp, FREAD, l->l_cred);
992 1.337 martin vput(data->ed_pack.ep_vp);
993 1.186 chs
994 1.55 cgd /* if an error happened, deallocate and punt */
995 1.111 matt if (error) {
996 1.312 christos DPRINTF(("%s: vmcmd %zu failed: %d\n", __func__, i - 1, error));
997 1.55 cgd goto exec_abort;
998 1.111 matt }
999 1.55 cgd
1000 1.55 cgd /* remember information about the process */
1001 1.337 martin data->ed_arginfo.ps_nargvstr = data->ed_argc;
1002 1.337 martin data->ed_arginfo.ps_nenvstr = data->ed_envc;
1003 1.55 cgd
1004 1.255 christos /* set command name & other accounting info */
1005 1.337 martin commandname = strrchr(data->ed_pack.ep_resolvedname, '/');
1006 1.295 dholland if (commandname != NULL) {
1007 1.295 dholland commandname++;
1008 1.295 dholland } else {
1009 1.337 martin commandname = data->ed_pack.ep_resolvedname;
1010 1.295 dholland }
1011 1.295 dholland i = min(strlen(commandname), MAXCOMLEN);
1012 1.295 dholland (void)memcpy(p->p_comm, commandname, i);
1013 1.255 christos p->p_comm[i] = '\0';
1014 1.255 christos
1015 1.255 christos dp = PNBUF_GET();
1016 1.255 christos /*
1017 1.255 christos * If the path starts with /, we don't need to do any work.
1018 1.255 christos * This handles the majority of the cases.
1019 1.255 christos * In the future perhaps we could canonicalize it?
1020 1.255 christos */
1021 1.337 martin if (data->ed_pathstring[0] == '/')
1022 1.337 martin (void)strlcpy(data->ed_pack.ep_path = dp, data->ed_pathstring,
1023 1.337 martin MAXPATHLEN);
1024 1.333 dholland #ifdef notyet
1025 1.255 christos /*
1026 1.255 christos * Although this works most of the time [since the entry was just
1027 1.255 christos * entered in the cache] we don't use it because it theoretically
1028 1.255 christos * can fail and it is not the cleanest interface, because there
1029 1.255 christos * could be races. When the namei cache is re-written, this can
1030 1.255 christos * be changed to use the appropriate function.
1031 1.255 christos */
1032 1.255 christos else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p)))
1033 1.337 martin data->ed_pack.ep_path = dp;
1034 1.255 christos #endif
1035 1.255 christos else {
1036 1.333 dholland #ifdef notyet
1037 1.255 christos printf("Cannot get path for pid %d [%s] (error %d)",
1038 1.255 christos (int)p->p_pid, p->p_comm, error);
1039 1.255 christos #endif
1040 1.337 martin data->ed_pack.ep_path = NULL;
1041 1.255 christos PNBUF_PUT(dp);
1042 1.255 christos }
1043 1.255 christos
1044 1.163 chs stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
1045 1.337 martin STACK_PTHREADSPACE + data->ed_ps_strings_sz + data->ed_szsigcode),
1046 1.337 martin data->ed_pack.ep_ssize - (data->ed_ps_strings_sz + data->ed_szsigcode));
1047 1.267 dsl
1048 1.163 chs #ifdef __MACHINE_STACK_GROWS_UP
1049 1.163 chs /*
1050 1.163 chs * The copyargs call always copies into lower addresses
1051 1.163 chs * first, moving towards higher addresses, starting with
1052 1.183 junyoung * the stack pointer that we give. When the stack grows
1053 1.183 junyoung * down, this puts argc/argv/envp very shallow on the
1054 1.267 dsl * stack, right at the first user stack pointer.
1055 1.267 dsl * When the stack grows up, the situation is reversed.
1056 1.163 chs *
1057 1.163 chs * Normally, this is no big deal. But the ld_elf.so _rtld()
1058 1.183 junyoung * function expects to be called with a single pointer to
1059 1.183 junyoung * a region that has a few words it can stash values into,
1060 1.163 chs * followed by argc/argv/envp. When the stack grows down,
1061 1.163 chs * it's easy to decrement the stack pointer a little bit to
1062 1.163 chs * allocate the space for these few words and pass the new
1063 1.163 chs * stack pointer to _rtld. When the stack grows up, however,
1064 1.171 chs * a few words before argc is part of the signal trampoline, XXX
1065 1.163 chs * so we have a problem.
1066 1.163 chs *
1067 1.183 junyoung * Instead of changing how _rtld works, we take the easy way
1068 1.267 dsl * out and steal 32 bytes before we call copyargs.
1069 1.337 martin * This extra space was allowed for when 'pack.ep_ssize' was calculated.
1070 1.163 chs */
1071 1.267 dsl stack += RTLD_GAP;
1072 1.163 chs #endif /* __MACHINE_STACK_GROWS_UP */
1073 1.337 martin
1074 1.337 martin /* Now copy argc, args & environ to new stack */
1075 1.337 martin error = (*data->ed_pack.ep_esch->es_copyargs)(l, &data->ed_pack,
1076 1.337 martin &data->ed_arginfo, &stack, data->ed_argp);
1077 1.163 chs
1078 1.337 martin if (data->ed_pack.ep_path) {
1079 1.337 martin PNBUF_PUT(data->ed_pack.ep_path);
1080 1.337 martin data->ed_pack.ep_path = NULL;
1081 1.255 christos }
1082 1.144 christos if (error) {
1083 1.312 christos DPRINTF(("%s: copyargs failed %d\n", __func__, error));
1084 1.55 cgd goto exec_abort;
1085 1.111 matt }
1086 1.144 christos /* Move the stack back to original point */
1087 1.337 martin stack = (char *)STACK_GROW(vm->vm_minsaddr, data->ed_pack.ep_ssize);
1088 1.55 cgd
1089 1.121 eeh /* fill process ps_strings info */
1090 1.311 joerg p->p_psstrp = (vaddr_t)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
1091 1.337 martin STACK_PTHREADSPACE), data->ed_ps_strings_sz);
1092 1.311 joerg
1093 1.337 martin if (data->ed_pack.ep_flags & EXEC_32) {
1094 1.337 martin arginfo32.ps_argvstr = (vaddr_t)data->ed_arginfo.ps_argvstr;
1095 1.337 martin arginfo32.ps_nargvstr = data->ed_arginfo.ps_nargvstr;
1096 1.337 martin arginfo32.ps_envstr = (vaddr_t)data->ed_arginfo.ps_envstr;
1097 1.337 martin arginfo32.ps_nenvstr = data->ed_arginfo.ps_nenvstr;
1098 1.311 joerg }
1099 1.121 eeh
1100 1.55 cgd /* copy out the process's ps_strings structure */
1101 1.337 martin if ((error = copyout(aip, (void *)p->p_psstrp, data->ed_ps_strings_sz))
1102 1.337 martin != 0) {
1103 1.312 christos DPRINTF(("%s: ps_strings copyout %p->%p size %zu failed\n",
1104 1.337 martin __func__, aip, (void *)p->p_psstrp, data->ed_ps_strings_sz));
1105 1.55 cgd goto exec_abort;
1106 1.111 matt }
1107 1.109 simonb
1108 1.307 pooka cwdexec(p);
1109 1.270 ad fd_closeexec(); /* handle close on exec */
1110 1.315 alnsn
1111 1.315 alnsn if (__predict_false(ktrace_on))
1112 1.315 alnsn fd_ktrexecfd();
1113 1.315 alnsn
1114 1.55 cgd execsigs(p); /* reset catched signals */
1115 1.183 junyoung
1116 1.164 thorpej l->l_ctxlink = NULL; /* reset ucontext link */
1117 1.55 cgd
1118 1.255 christos
1119 1.55 cgd p->p_acflag &= ~AFORK;
1120 1.272 ad mutex_enter(p->p_lock);
1121 1.238 pavel p->p_flag |= PK_EXEC;
1122 1.272 ad mutex_exit(p->p_lock);
1123 1.237 ad
1124 1.237 ad /*
1125 1.237 ad * Stop profiling.
1126 1.237 ad */
1127 1.237 ad if ((p->p_stflag & PST_PROFIL) != 0) {
1128 1.237 ad mutex_spin_enter(&p->p_stmutex);
1129 1.237 ad stopprofclock(p);
1130 1.237 ad mutex_spin_exit(&p->p_stmutex);
1131 1.237 ad }
1132 1.237 ad
1133 1.237 ad /*
1134 1.275 ad * It's OK to test PL_PPWAIT unlocked here, as other LWPs have
1135 1.237 ad * exited and exec()/exit() are the only places it will be cleared.
1136 1.237 ad */
1137 1.275 ad if ((p->p_lflag & PL_PPWAIT) != 0) {
1138 1.271 ad mutex_enter(proc_lock);
1139 1.308 pooka l->l_lwpctl = NULL; /* was on loan from blocked parent */
1140 1.275 ad p->p_lflag &= ~PL_PPWAIT;
1141 1.237 ad cv_broadcast(&p->p_pptr->p_waitcv);
1142 1.271 ad mutex_exit(proc_lock);
1143 1.55 cgd }
1144 1.55 cgd
1145 1.55 cgd /*
1146 1.237 ad * Deal with set[ug]id. MNT_NOSUID has already been used to disable
1147 1.237 ad * s[ug]id. It's OK to check for PSL_TRACED here as we have blocked
1148 1.237 ad * out additional references on the process for the moment.
1149 1.55 cgd */
1150 1.237 ad if ((p->p_slflag & PSL_TRACED) == 0 &&
1151 1.141 thorpej
1152 1.337 martin (((data->ed_attr.va_mode & S_ISUID) != 0 &&
1153 1.337 martin kauth_cred_geteuid(l->l_cred) != data->ed_attr.va_uid) ||
1154 1.141 thorpej
1155 1.337 martin ((data->ed_attr.va_mode & S_ISGID) != 0 &&
1156 1.337 martin kauth_cred_getegid(l->l_cred) != data->ed_attr.va_gid))) {
1157 1.141 thorpej /*
1158 1.141 thorpej * Mark the process as SUGID before we do
1159 1.141 thorpej * anything that might block.
1160 1.141 thorpej */
1161 1.237 ad proc_crmod_enter();
1162 1.240 thorpej proc_crmod_leave(NULL, NULL, true);
1163 1.152 christos
1164 1.152 christos /* Make sure file descriptors 0..2 are in use. */
1165 1.270 ad if ((error = fd_checkstd()) != 0) {
1166 1.312 christos DPRINTF(("%s: fdcheckstd failed %d\n",
1167 1.312 christos __func__, error));
1168 1.152 christos goto exec_abort;
1169 1.209 christos }
1170 1.141 thorpej
1171 1.220 ad /*
1172 1.220 ad * Copy the credential so other references don't see our
1173 1.220 ad * changes.
1174 1.220 ad */
1175 1.221 ad l->l_cred = kauth_cred_copy(l->l_cred);
1176 1.55 cgd #ifdef KTRACE
1177 1.55 cgd /*
1178 1.268 elad * If the persistent trace flag isn't set, turn off.
1179 1.55 cgd */
1180 1.237 ad if (p->p_tracep) {
1181 1.247 ad mutex_enter(&ktrace_lock);
1182 1.268 elad if (!(p->p_traceflag & KTRFAC_PERSISTENT))
1183 1.237 ad ktrderef(p);
1184 1.247 ad mutex_exit(&ktrace_lock);
1185 1.237 ad }
1186 1.55 cgd #endif
1187 1.337 martin if (data->ed_attr.va_mode & S_ISUID)
1188 1.337 martin kauth_cred_seteuid(l->l_cred, data->ed_attr.va_uid);
1189 1.337 martin if (data->ed_attr.va_mode & S_ISGID)
1190 1.337 martin kauth_cred_setegid(l->l_cred, data->ed_attr.va_gid);
1191 1.210 christos } else {
1192 1.221 ad if (kauth_cred_geteuid(l->l_cred) ==
1193 1.221 ad kauth_cred_getuid(l->l_cred) &&
1194 1.221 ad kauth_cred_getegid(l->l_cred) ==
1195 1.221 ad kauth_cred_getgid(l->l_cred))
1196 1.238 pavel p->p_flag &= ~PK_SUGID;
1197 1.210 christos }
1198 1.220 ad
1199 1.220 ad /*
1200 1.220 ad * Copy the credential so other references don't see our changes.
1201 1.220 ad * Test to see if this is necessary first, since in the common case
1202 1.220 ad * we won't need a private reference.
1203 1.220 ad */
1204 1.221 ad if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) ||
1205 1.221 ad kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) {
1206 1.221 ad l->l_cred = kauth_cred_copy(l->l_cred);
1207 1.221 ad kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred));
1208 1.221 ad kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred));
1209 1.220 ad }
1210 1.155 gmcgarry
1211 1.221 ad /* Update the master credentials. */
1212 1.227 ad if (l->l_cred != p->p_cred) {
1213 1.227 ad kauth_cred_t ocred;
1214 1.227 ad
1215 1.227 ad kauth_cred_hold(l->l_cred);
1216 1.272 ad mutex_enter(p->p_lock);
1217 1.227 ad ocred = p->p_cred;
1218 1.227 ad p->p_cred = l->l_cred;
1219 1.272 ad mutex_exit(p->p_lock);
1220 1.227 ad kauth_cred_free(ocred);
1221 1.227 ad }
1222 1.221 ad
1223 1.155 gmcgarry #if defined(__HAVE_RAS)
1224 1.155 gmcgarry /*
1225 1.155 gmcgarry * Remove all RASs from the address space.
1226 1.155 gmcgarry */
1227 1.251 ad ras_purgeall();
1228 1.155 gmcgarry #endif
1229 1.107 fvdl
1230 1.107 fvdl doexechooks(p);
1231 1.55 cgd
1232 1.55 cgd /* setup new registers and do misc. setup. */
1233 1.337 martin (*data->ed_pack.ep_esch->es_emul->e_setregs)(l, &data->ed_pack,
1234 1.337 martin (vaddr_t)stack);
1235 1.337 martin if (data->ed_pack.ep_esch->es_setregs)
1236 1.337 martin (*data->ed_pack.ep_esch->es_setregs)(l, &data->ed_pack,
1237 1.337 martin (vaddr_t)stack);
1238 1.55 cgd
1239 1.309 joerg /* Provide a consistent LWP private setting */
1240 1.309 joerg (void)lwp_setprivate(l, NULL);
1241 1.309 joerg
1242 1.316 matt /* Discard all PCU state; need to start fresh */
1243 1.316 matt pcu_discard_all(l);
1244 1.316 matt
1245 1.171 chs /* map the process's signal trampoline code */
1246 1.337 martin if ((error = exec_sigcode_map(p, data->ed_pack.ep_esch->es_emul)) != 0) {
1247 1.312 christos DPRINTF(("%s: map sigcode failed %d\n", __func__, error));
1248 1.171 chs goto exec_abort;
1249 1.209 christos }
1250 1.171 chs
1251 1.337 martin pool_put(&exec_pool, data->ed_argp);
1252 1.276 ad
1253 1.276 ad /* notify others that we exec'd */
1254 1.276 ad KNOTE(&p->p_klist, NOTE_EXEC);
1255 1.276 ad
1256 1.337 martin kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
1257 1.122 jdolecek
1258 1.339 martin SDT_PROBE(proc,,,exec_success, data->ed_pack.ep_name, 0, 0, 0, 0);
1259 1.294 darran
1260 1.244 dsl /* The emulation root will usually have been found when we looked
1261 1.244 dsl * for the elf interpreter (or similar), if not look now. */
1262 1.337 martin if (data->ed_pack.ep_esch->es_emul->e_path != NULL &&
1263 1.337 martin data->ed_pack.ep_emul_root == NULL)
1264 1.337 martin emul_find_root(l, &data->ed_pack);
1265 1.244 dsl
1266 1.244 dsl /* Any old emulation root got removed by fdcloseexec */
1267 1.259 ad rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER);
1268 1.337 martin p->p_cwdi->cwdi_edir = data->ed_pack.ep_emul_root;
1269 1.259 ad rw_exit(&p->p_cwdi->cwdi_lock);
1270 1.337 martin data->ed_pack.ep_emul_root = NULL;
1271 1.337 martin if (data->ed_pack.ep_interp != NULL)
1272 1.337 martin vrele(data->ed_pack.ep_interp);
1273 1.244 dsl
1274 1.122 jdolecek /*
1275 1.194 peter * Call emulation specific exec hook. This can setup per-process
1276 1.122 jdolecek * p->p_emuldata or do any other per-process stuff an emulation needs.
1277 1.122 jdolecek *
1278 1.122 jdolecek * If we are executing process of different emulation than the
1279 1.122 jdolecek * original forked process, call e_proc_exit() of the old emulation
1280 1.122 jdolecek * first, then e_proc_exec() of new emulation. If the emulation is
1281 1.122 jdolecek * same, the exec hook code should deallocate any old emulation
1282 1.122 jdolecek * resources held previously by this process.
1283 1.122 jdolecek */
1284 1.124 jdolecek if (p->p_emul && p->p_emul->e_proc_exit
1285 1.337 martin && p->p_emul != data->ed_pack.ep_esch->es_emul)
1286 1.122 jdolecek (*p->p_emul->e_proc_exit)(p);
1287 1.122 jdolecek
1288 1.123 jdolecek /*
1289 1.299 chs * This is now LWP 1.
1290 1.299 chs */
1291 1.299 chs mutex_enter(p->p_lock);
1292 1.299 chs p->p_nlwpid = 1;
1293 1.299 chs l->l_lid = 1;
1294 1.299 chs mutex_exit(p->p_lock);
1295 1.299 chs
1296 1.299 chs /*
1297 1.123 jdolecek * Call exec hook. Emulation code may NOT store reference to anything
1298 1.123 jdolecek * from &pack.
1299 1.123 jdolecek */
1300 1.337 martin if (data->ed_pack.ep_esch->es_emul->e_proc_exec)
1301 1.337 martin (*data->ed_pack.ep_esch->es_emul->e_proc_exec)(p, &data->ed_pack);
1302 1.122 jdolecek
1303 1.122 jdolecek /* update p_emul, the old value is no longer needed */
1304 1.337 martin p->p_emul = data->ed_pack.ep_esch->es_emul;
1305 1.148 thorpej
1306 1.148 thorpej /* ...and the same for p_execsw */
1307 1.337 martin p->p_execsw = data->ed_pack.ep_esch;
1308 1.148 thorpej
1309 1.133 mycroft #ifdef __HAVE_SYSCALL_INTERN
1310 1.133 mycroft (*p->p_emul->e_syscall_intern)(p);
1311 1.133 mycroft #endif
1312 1.247 ad ktremul();
1313 1.85 mycroft
1314 1.252 ad /* Allow new references from the debugger/procfs. */
1315 1.341 martin rw_exit(&p->p_reflock);
1316 1.237 ad rw_exit(&exec_lock);
1317 1.162 manu
1318 1.271 ad mutex_enter(proc_lock);
1319 1.237 ad
1320 1.237 ad if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) {
1321 1.237 ad KSI_INIT_EMPTY(&ksi);
1322 1.237 ad ksi.ksi_signo = SIGTRAP;
1323 1.237 ad ksi.ksi_lid = l->l_lid;
1324 1.237 ad kpsignal(p, &ksi, NULL);
1325 1.237 ad }
1326 1.162 manu
1327 1.237 ad if (p->p_sflag & PS_STOPEXEC) {
1328 1.237 ad KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
1329 1.175 dsl p->p_pptr->p_nstopchild++;
1330 1.237 ad p->p_pptr->p_waited = 0;
1331 1.272 ad mutex_enter(p->p_lock);
1332 1.237 ad ksiginfo_queue_init(&kq);
1333 1.237 ad sigclearall(p, &contsigmask, &kq);
1334 1.237 ad lwp_lock(l);
1335 1.237 ad l->l_stat = LSSTOP;
1336 1.162 manu p->p_stat = SSTOP;
1337 1.164 thorpej p->p_nrlwps--;
1338 1.304 rmind lwp_unlock(l);
1339 1.272 ad mutex_exit(p->p_lock);
1340 1.271 ad mutex_exit(proc_lock);
1341 1.304 rmind lwp_lock(l);
1342 1.245 yamt mi_switch(l);
1343 1.237 ad ksiginfo_queue_drain(&kq);
1344 1.237 ad KERNEL_LOCK(l->l_biglocks, l);
1345 1.237 ad } else {
1346 1.271 ad mutex_exit(proc_lock);
1347 1.162 manu }
1348 1.162 manu
1349 1.337 martin pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
1350 1.337 martin pathbuf_destroy(data->ed_pathbuf);
1351 1.337 martin PNBUF_PUT(data->ed_resolvedpathbuf);
1352 1.327 reinoud DPRINTF(("%s finished\n", __func__));
1353 1.85 mycroft return (EJUSTRETURN);
1354 1.55 cgd
1355 1.138 lukem exec_abort:
1356 1.294 darran SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0);
1357 1.297 rmind rw_exit(&p->p_reflock);
1358 1.297 rmind rw_exit(&exec_lock);
1359 1.297 rmind
1360 1.337 martin pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
1361 1.337 martin pathbuf_destroy(data->ed_pathbuf);
1362 1.337 martin PNBUF_PUT(data->ed_resolvedpathbuf);
1363 1.130 jdolecek
1364 1.55 cgd /*
1365 1.55 cgd * the old process doesn't exist anymore. exit gracefully.
1366 1.55 cgd * get rid of the (new) address space we have created, if any, get rid
1367 1.55 cgd * of our namei data and vnode, and exit noting failure
1368 1.55 cgd */
1369 1.88 mrg uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
1370 1.88 mrg VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
1371 1.337 martin exec_free_emul_arg(&data->ed_pack);
1372 1.337 martin pool_put(&exec_pool, data->ed_argp);
1373 1.337 martin kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
1374 1.337 martin if (data->ed_pack.ep_emul_root != NULL)
1375 1.337 martin vrele(data->ed_pack.ep_emul_root);
1376 1.337 martin if (data->ed_pack.ep_interp != NULL)
1377 1.337 martin vrele(data->ed_pack.ep_interp);
1378 1.237 ad
1379 1.252 ad /* Acquire the sched-state mutex (exit1() will release it). */
1380 1.337 martin if (!proc_is_new) {
1381 1.337 martin mutex_enter(p->p_lock);
1382 1.337 martin exit1(l, W_EXITCODE(error, SIGABRT));
1383 1.337 martin }
1384 1.55 cgd
1385 1.55 cgd /* NOTREACHED */
1386 1.55 cgd return 0;
1387 1.67 christos }
1388 1.67 christos
1389 1.144 christos int
1390 1.337 martin execve1(struct lwp *l, const char *path, char * const *args,
1391 1.337 martin char * const *envs, execve_fetch_element_t fetch_element)
1392 1.337 martin {
1393 1.337 martin struct execve_data data;
1394 1.337 martin int error;
1395 1.337 martin
1396 1.337 martin error = execve_loadvm(l, path, args, envs, fetch_element, &data);
1397 1.337 martin if (error)
1398 1.337 martin return error;
1399 1.337 martin error = execve_runproc(l, &data);
1400 1.337 martin return error;
1401 1.337 martin }
1402 1.337 martin
1403 1.337 martin int
1404 1.231 yamt copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo,
1405 1.231 yamt char **stackp, void *argp)
1406 1.67 christos {
1407 1.138 lukem char **cpp, *dp, *sp;
1408 1.138 lukem size_t len;
1409 1.138 lukem void *nullp;
1410 1.138 lukem long argc, envc;
1411 1.144 christos int error;
1412 1.138 lukem
1413 1.144 christos cpp = (char **)*stackp;
1414 1.138 lukem nullp = NULL;
1415 1.138 lukem argc = arginfo->ps_nargvstr;
1416 1.138 lukem envc = arginfo->ps_nenvstr;
1417 1.305 matt if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) {
1418 1.312 christos COPYPRINTF("", cpp - 1, sizeof(argc));
1419 1.144 christos return error;
1420 1.305 matt }
1421 1.67 christos
1422 1.244 dsl dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen);
1423 1.67 christos sp = argp;
1424 1.67 christos
1425 1.67 christos /* XXX don't copy them out, remap them! */
1426 1.69 mycroft arginfo->ps_argvstr = cpp; /* remember location of argv for later */
1427 1.67 christos
1428 1.305 matt for (; --argc >= 0; sp += len, dp += len) {
1429 1.305 matt if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) {
1430 1.312 christos COPYPRINTF("", cpp - 1, sizeof(dp));
1431 1.305 matt return error;
1432 1.305 matt }
1433 1.305 matt if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) {
1434 1.313 jakllsch COPYPRINTF("str", dp, (size_t)ARG_MAX);
1435 1.144 christos return error;
1436 1.305 matt }
1437 1.305 matt }
1438 1.67 christos
1439 1.305 matt if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) {
1440 1.312 christos COPYPRINTF("", cpp - 1, sizeof(nullp));
1441 1.144 christos return error;
1442 1.305 matt }
1443 1.67 christos
1444 1.69 mycroft arginfo->ps_envstr = cpp; /* remember location of envp for later */
1445 1.67 christos
1446 1.305 matt for (; --envc >= 0; sp += len, dp += len) {
1447 1.305 matt if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) {
1448 1.312 christos COPYPRINTF("", cpp - 1, sizeof(dp));
1449 1.144 christos return error;
1450 1.305 matt }
1451 1.305 matt if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) {
1452 1.313 jakllsch COPYPRINTF("str", dp, (size_t)ARG_MAX);
1453 1.305 matt return error;
1454 1.305 matt }
1455 1.337 martin
1456 1.305 matt }
1457 1.67 christos
1458 1.305 matt if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) {
1459 1.312 christos COPYPRINTF("", cpp - 1, sizeof(nullp));
1460 1.144 christos return error;
1461 1.305 matt }
1462 1.67 christos
1463 1.144 christos *stackp = (char *)cpp;
1464 1.144 christos return 0;
1465 1.55 cgd }
1466 1.130 jdolecek
1467 1.130 jdolecek
1468 1.130 jdolecek /*
1469 1.282 ad * Add execsw[] entries.
1470 1.130 jdolecek */
1471 1.130 jdolecek int
1472 1.282 ad exec_add(struct execsw *esp, int count)
1473 1.130 jdolecek {
1474 1.282 ad struct exec_entry *it;
1475 1.282 ad int i;
1476 1.130 jdolecek
1477 1.283 ad if (count == 0) {
1478 1.283 ad return 0;
1479 1.283 ad }
1480 1.130 jdolecek
1481 1.282 ad /* Check for duplicates. */
1482 1.237 ad rw_enter(&exec_lock, RW_WRITER);
1483 1.282 ad for (i = 0; i < count; i++) {
1484 1.282 ad LIST_FOREACH(it, &ex_head, ex_list) {
1485 1.282 ad /* assume unique (makecmds, probe_func, emulation) */
1486 1.282 ad if (it->ex_sw->es_makecmds == esp[i].es_makecmds &&
1487 1.282 ad it->ex_sw->u.elf_probe_func ==
1488 1.282 ad esp[i].u.elf_probe_func &&
1489 1.282 ad it->ex_sw->es_emul == esp[i].es_emul) {
1490 1.282 ad rw_exit(&exec_lock);
1491 1.282 ad return EEXIST;
1492 1.130 jdolecek }
1493 1.130 jdolecek }
1494 1.130 jdolecek }
1495 1.130 jdolecek
1496 1.282 ad /* Allocate new entries. */
1497 1.282 ad for (i = 0; i < count; i++) {
1498 1.282 ad it = kmem_alloc(sizeof(*it), KM_SLEEP);
1499 1.282 ad it->ex_sw = &esp[i];
1500 1.282 ad LIST_INSERT_HEAD(&ex_head, it, ex_list);
1501 1.130 jdolecek }
1502 1.130 jdolecek
1503 1.130 jdolecek /* update execsw[] */
1504 1.130 jdolecek exec_init(0);
1505 1.237 ad rw_exit(&exec_lock);
1506 1.282 ad return 0;
1507 1.130 jdolecek }
1508 1.130 jdolecek
1509 1.130 jdolecek /*
1510 1.130 jdolecek * Remove execsw[] entry.
1511 1.130 jdolecek */
1512 1.130 jdolecek int
1513 1.282 ad exec_remove(struct execsw *esp, int count)
1514 1.130 jdolecek {
1515 1.282 ad struct exec_entry *it, *next;
1516 1.282 ad int i;
1517 1.282 ad const struct proclist_desc *pd;
1518 1.282 ad proc_t *p;
1519 1.282 ad
1520 1.283 ad if (count == 0) {
1521 1.283 ad return 0;
1522 1.283 ad }
1523 1.130 jdolecek
1524 1.282 ad /* Abort if any are busy. */
1525 1.237 ad rw_enter(&exec_lock, RW_WRITER);
1526 1.282 ad for (i = 0; i < count; i++) {
1527 1.282 ad mutex_enter(proc_lock);
1528 1.282 ad for (pd = proclists; pd->pd_list != NULL; pd++) {
1529 1.282 ad PROCLIST_FOREACH(p, pd->pd_list) {
1530 1.282 ad if (p->p_execsw == &esp[i]) {
1531 1.282 ad mutex_exit(proc_lock);
1532 1.282 ad rw_exit(&exec_lock);
1533 1.282 ad return EBUSY;
1534 1.282 ad }
1535 1.282 ad }
1536 1.282 ad }
1537 1.282 ad mutex_exit(proc_lock);
1538 1.282 ad }
1539 1.130 jdolecek
1540 1.282 ad /* None are busy, so remove them all. */
1541 1.282 ad for (i = 0; i < count; i++) {
1542 1.282 ad for (it = LIST_FIRST(&ex_head); it != NULL; it = next) {
1543 1.282 ad next = LIST_NEXT(it, ex_list);
1544 1.282 ad if (it->ex_sw == &esp[i]) {
1545 1.282 ad LIST_REMOVE(it, ex_list);
1546 1.282 ad kmem_free(it, sizeof(*it));
1547 1.282 ad break;
1548 1.282 ad }
1549 1.282 ad }
1550 1.130 jdolecek }
1551 1.130 jdolecek
1552 1.130 jdolecek /* update execsw[] */
1553 1.130 jdolecek exec_init(0);
1554 1.237 ad rw_exit(&exec_lock);
1555 1.282 ad return 0;
1556 1.130 jdolecek }
1557 1.130 jdolecek
1558 1.130 jdolecek /*
1559 1.130 jdolecek * Initialize exec structures. If init_boot is true, also does necessary
1560 1.130 jdolecek * one-time initialization (it's called from main() that way).
1561 1.147 jdolecek * Once system is multiuser, this should be called with exec_lock held,
1562 1.130 jdolecek * i.e. via exec_{add|remove}().
1563 1.130 jdolecek */
1564 1.130 jdolecek int
1565 1.138 lukem exec_init(int init_boot)
1566 1.130 jdolecek {
1567 1.282 ad const struct execsw **sw;
1568 1.282 ad struct exec_entry *ex;
1569 1.282 ad SLIST_HEAD(,exec_entry) first;
1570 1.282 ad SLIST_HEAD(,exec_entry) any;
1571 1.282 ad SLIST_HEAD(,exec_entry) last;
1572 1.282 ad int i, sz;
1573 1.130 jdolecek
1574 1.130 jdolecek if (init_boot) {
1575 1.130 jdolecek /* do one-time initializations */
1576 1.237 ad rw_init(&exec_lock);
1577 1.259 ad mutex_init(&sigobject_lock, MUTEX_DEFAULT, IPL_NONE);
1578 1.277 ad pool_init(&exec_pool, NCARGS, 0, 0, PR_NOALIGN|PR_NOTOUCH,
1579 1.277 ad "execargs", &exec_palloc, IPL_NONE);
1580 1.277 ad pool_sethardlimit(&exec_pool, maxexec, "should not happen", 0);
1581 1.282 ad } else {
1582 1.282 ad KASSERT(rw_write_held(&exec_lock));
1583 1.282 ad }
1584 1.130 jdolecek
1585 1.282 ad /* Sort each entry onto the appropriate queue. */
1586 1.282 ad SLIST_INIT(&first);
1587 1.282 ad SLIST_INIT(&any);
1588 1.282 ad SLIST_INIT(&last);
1589 1.282 ad sz = 0;
1590 1.282 ad LIST_FOREACH(ex, &ex_head, ex_list) {
1591 1.282 ad switch(ex->ex_sw->es_prio) {
1592 1.282 ad case EXECSW_PRIO_FIRST:
1593 1.282 ad SLIST_INSERT_HEAD(&first, ex, ex_slist);
1594 1.282 ad break;
1595 1.282 ad case EXECSW_PRIO_ANY:
1596 1.282 ad SLIST_INSERT_HEAD(&any, ex, ex_slist);
1597 1.282 ad break;
1598 1.282 ad case EXECSW_PRIO_LAST:
1599 1.282 ad SLIST_INSERT_HEAD(&last, ex, ex_slist);
1600 1.282 ad break;
1601 1.282 ad default:
1602 1.312 christos panic("%s", __func__);
1603 1.282 ad break;
1604 1.130 jdolecek }
1605 1.282 ad sz++;
1606 1.130 jdolecek }
1607 1.130 jdolecek
1608 1.130 jdolecek /*
1609 1.282 ad * Create new execsw[]. Ensure we do not try a zero-sized
1610 1.282 ad * allocation.
1611 1.130 jdolecek */
1612 1.282 ad sw = kmem_alloc(sz * sizeof(struct execsw *) + 1, KM_SLEEP);
1613 1.282 ad i = 0;
1614 1.282 ad SLIST_FOREACH(ex, &first, ex_slist) {
1615 1.282 ad sw[i++] = ex->ex_sw;
1616 1.282 ad }
1617 1.282 ad SLIST_FOREACH(ex, &any, ex_slist) {
1618 1.282 ad sw[i++] = ex->ex_sw;
1619 1.282 ad }
1620 1.282 ad SLIST_FOREACH(ex, &last, ex_slist) {
1621 1.282 ad sw[i++] = ex->ex_sw;
1622 1.130 jdolecek }
1623 1.183 junyoung
1624 1.282 ad /* Replace old execsw[] and free used memory. */
1625 1.282 ad if (execsw != NULL) {
1626 1.282 ad kmem_free(__UNCONST(execsw),
1627 1.282 ad nexecs * sizeof(struct execsw *) + 1);
1628 1.130 jdolecek }
1629 1.282 ad execsw = sw;
1630 1.282 ad nexecs = sz;
1631 1.130 jdolecek
1632 1.282 ad /* Figure out the maximum size of an exec header. */
1633 1.282 ad exec_maxhdrsz = sizeof(int);
1634 1.130 jdolecek for (i = 0; i < nexecs; i++) {
1635 1.130 jdolecek if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1636 1.130 jdolecek exec_maxhdrsz = execsw[i]->es_hdrsz;
1637 1.130 jdolecek }
1638 1.130 jdolecek
1639 1.130 jdolecek return 0;
1640 1.130 jdolecek }
1641 1.171 chs
1642 1.171 chs static int
1643 1.171 chs exec_sigcode_map(struct proc *p, const struct emul *e)
1644 1.171 chs {
1645 1.171 chs vaddr_t va;
1646 1.171 chs vsize_t sz;
1647 1.171 chs int error;
1648 1.171 chs struct uvm_object *uobj;
1649 1.171 chs
1650 1.184 drochner sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
1651 1.184 drochner
1652 1.184 drochner if (e->e_sigobject == NULL || sz == 0) {
1653 1.171 chs return 0;
1654 1.171 chs }
1655 1.171 chs
1656 1.171 chs /*
1657 1.171 chs * If we don't have a sigobject for this emulation, create one.
1658 1.171 chs *
1659 1.171 chs * sigobject is an anonymous memory object (just like SYSV shared
1660 1.171 chs * memory) that we keep a permanent reference to and that we map
1661 1.171 chs * in all processes that need this sigcode. The creation is simple,
1662 1.171 chs * we create an object, add a permanent reference to it, map it in
1663 1.171 chs * kernel space, copy out the sigcode to it and unmap it.
1664 1.189 jdolecek * We map it with PROT_READ|PROT_EXEC into the process just
1665 1.189 jdolecek * the way sys_mmap() would map it.
1666 1.171 chs */
1667 1.171 chs
1668 1.171 chs uobj = *e->e_sigobject;
1669 1.171 chs if (uobj == NULL) {
1670 1.259 ad mutex_enter(&sigobject_lock);
1671 1.259 ad if ((uobj = *e->e_sigobject) == NULL) {
1672 1.259 ad uobj = uao_create(sz, 0);
1673 1.259 ad (*uobj->pgops->pgo_reference)(uobj);
1674 1.259 ad va = vm_map_min(kernel_map);
1675 1.259 ad if ((error = uvm_map(kernel_map, &va, round_page(sz),
1676 1.259 ad uobj, 0, 0,
1677 1.259 ad UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
1678 1.259 ad UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
1679 1.259 ad printf("kernel mapping failed %d\n", error);
1680 1.259 ad (*uobj->pgops->pgo_detach)(uobj);
1681 1.259 ad mutex_exit(&sigobject_lock);
1682 1.259 ad return (error);
1683 1.259 ad }
1684 1.259 ad memcpy((void *)va, e->e_sigcode, sz);
1685 1.171 chs #ifdef PMAP_NEED_PROCWR
1686 1.259 ad pmap_procwr(&proc0, va, sz);
1687 1.171 chs #endif
1688 1.259 ad uvm_unmap(kernel_map, va, va + round_page(sz));
1689 1.259 ad *e->e_sigobject = uobj;
1690 1.259 ad }
1691 1.259 ad mutex_exit(&sigobject_lock);
1692 1.171 chs }
1693 1.171 chs
1694 1.172 enami /* Just a hint to uvm_map where to put it. */
1695 1.195 fvdl va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr,
1696 1.195 fvdl round_page(sz));
1697 1.187 chs
1698 1.187 chs #ifdef __alpha__
1699 1.187 chs /*
1700 1.187 chs * Tru64 puts /sbin/loader at the end of user virtual memory,
1701 1.187 chs * which causes the above calculation to put the sigcode at
1702 1.187 chs * an invalid address. Put it just below the text instead.
1703 1.187 chs */
1704 1.193 jmc if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) {
1705 1.187 chs va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz);
1706 1.187 chs }
1707 1.187 chs #endif
1708 1.187 chs
1709 1.171 chs (*uobj->pgops->pgo_reference)(uobj);
1710 1.171 chs error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
1711 1.171 chs uobj, 0, 0,
1712 1.171 chs UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
1713 1.171 chs UVM_ADV_RANDOM, 0));
1714 1.171 chs if (error) {
1715 1.312 christos DPRINTF(("%s, %d: map %p "
1716 1.305 matt "uvm_map %#"PRIxVSIZE"@%#"PRIxVADDR" failed %d\n",
1717 1.312 christos __func__, __LINE__, &p->p_vmspace->vm_map, round_page(sz),
1718 1.312 christos va, error));
1719 1.171 chs (*uobj->pgops->pgo_detach)(uobj);
1720 1.171 chs return (error);
1721 1.171 chs }
1722 1.171 chs p->p_sigctx.ps_sigcode = (void *)va;
1723 1.171 chs return (0);
1724 1.171 chs }
1725 1.336 matt
1726 1.337 martin /*
1727 1.337 martin * A child lwp of a posix_spawn operation starts here and ends up in
1728 1.337 martin * cpu_spawn_return, dealing with all filedescriptor and scheduler
1729 1.337 martin * manipulations in between.
1730 1.337 martin */
1731 1.337 martin static void
1732 1.337 martin spawn_return(void *arg)
1733 1.337 martin {
1734 1.337 martin struct spawn_exec_data *spawn_data = arg;
1735 1.337 martin struct lwp *l = curlwp;
1736 1.337 martin int error, newfd;
1737 1.337 martin size_t i;
1738 1.337 martin const struct posix_spawn_file_actions_entry *fae;
1739 1.337 martin register_t retval;
1740 1.341 martin bool have_reflock;
1741 1.341 martin
1742 1.345 martin /* we have been created non-preemptable */
1743 1.345 martin KASSERT(l->l_nopreempt == 1);
1744 1.345 martin
1745 1.341 martin /*
1746 1.341 martin * The following actions may block, so we need a temporary
1747 1.341 martin * vmspace - borrow the kernel one
1748 1.341 martin */
1749 1.341 martin l->l_proc->p_vmspace = proc0.p_vmspace;
1750 1.341 martin pmap_activate(l);
1751 1.341 martin KPREEMPT_ENABLE(l);
1752 1.341 martin
1753 1.341 martin /* don't allow debugger access yet */
1754 1.341 martin rw_enter(&l->l_proc->p_reflock, RW_WRITER);
1755 1.341 martin have_reflock = true;
1756 1.337 martin
1757 1.338 martin error = 0;
1758 1.337 martin /* handle posix_spawn_file_actions */
1759 1.337 martin if (spawn_data->sed_actions != NULL) {
1760 1.337 martin for (i = 0; i < spawn_data->sed_actions_len; i++) {
1761 1.337 martin fae = &spawn_data->sed_actions[i];
1762 1.337 martin switch (fae->fae_action) {
1763 1.337 martin case FAE_OPEN:
1764 1.338 martin if (fd_getfile(fae->fae_fildes) != NULL) {
1765 1.338 martin error = fd_close(fae->fae_fildes);
1766 1.338 martin if (error)
1767 1.338 martin break;
1768 1.338 martin }
1769 1.337 martin error = fd_open(fae->fae_path, fae->fae_oflag,
1770 1.337 martin fae->fae_mode, &newfd);
1771 1.338 martin if (error)
1772 1.338 martin break;
1773 1.337 martin if (newfd != fae->fae_fildes) {
1774 1.337 martin error = dodup(l, newfd,
1775 1.337 martin fae->fae_fildes, 0, &retval);
1776 1.337 martin if (fd_getfile(newfd) != NULL)
1777 1.337 martin fd_close(newfd);
1778 1.337 martin }
1779 1.337 martin break;
1780 1.337 martin case FAE_DUP2:
1781 1.337 martin error = dodup(l, fae->fae_fildes,
1782 1.337 martin fae->fae_newfildes, 0, &retval);
1783 1.337 martin break;
1784 1.337 martin case FAE_CLOSE:
1785 1.337 martin if (fd_getfile(fae->fae_fildes) == NULL) {
1786 1.337 martin error = EBADF;
1787 1.337 martin break;
1788 1.337 martin }
1789 1.337 martin error = fd_close(fae->fae_fildes);
1790 1.337 martin break;
1791 1.337 martin }
1792 1.337 martin if (error)
1793 1.337 martin goto report_error;
1794 1.337 martin }
1795 1.337 martin }
1796 1.337 martin
1797 1.337 martin /* handle posix_spawnattr */
1798 1.337 martin if (spawn_data->sed_attrs != NULL) {
1799 1.337 martin struct sigaction sigact;
1800 1.337 martin sigact._sa_u._sa_handler = SIG_DFL;
1801 1.337 martin sigact.sa_flags = 0;
1802 1.337 martin
1803 1.337 martin /*
1804 1.337 martin * set state to SSTOP so that this proc can be found by pid.
1805 1.337 martin * see proc_enterprp, do_sched_setparam below
1806 1.337 martin */
1807 1.337 martin l->l_proc->p_stat = SSTOP;
1808 1.337 martin
1809 1.337 martin /* Set process group */
1810 1.337 martin if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETPGROUP) {
1811 1.337 martin pid_t mypid = l->l_proc->p_pid,
1812 1.337 martin pgrp = spawn_data->sed_attrs->sa_pgroup;
1813 1.337 martin
1814 1.337 martin if (pgrp == 0)
1815 1.337 martin pgrp = mypid;
1816 1.337 martin
1817 1.337 martin error = proc_enterpgrp(spawn_data->sed_parent,
1818 1.337 martin mypid, pgrp, false);
1819 1.337 martin if (error)
1820 1.337 martin goto report_error;
1821 1.337 martin }
1822 1.337 martin
1823 1.337 martin /* Set scheduler policy */
1824 1.337 martin if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSCHEDULER)
1825 1.337 martin error = do_sched_setparam(l->l_proc->p_pid, 0,
1826 1.337 martin spawn_data->sed_attrs->sa_schedpolicy,
1827 1.337 martin &spawn_data->sed_attrs->sa_schedparam);
1828 1.337 martin else if (spawn_data->sed_attrs->sa_flags
1829 1.337 martin & POSIX_SPAWN_SETSCHEDPARAM) {
1830 1.337 martin error = do_sched_setparam(spawn_data->sed_parent->p_pid, 0,
1831 1.337 martin SCHED_NONE, &spawn_data->sed_attrs->sa_schedparam);
1832 1.337 martin }
1833 1.337 martin if (error)
1834 1.337 martin goto report_error;
1835 1.337 martin
1836 1.337 martin /* Reset user ID's */
1837 1.337 martin if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_RESETIDS) {
1838 1.337 martin error = do_setresuid(l, -1,
1839 1.337 martin kauth_cred_getgid(l->l_cred), -1,
1840 1.337 martin ID_E_EQ_R | ID_E_EQ_S);
1841 1.337 martin if (error)
1842 1.337 martin goto report_error;
1843 1.337 martin error = do_setresuid(l, -1,
1844 1.337 martin kauth_cred_getuid(l->l_cred), -1,
1845 1.337 martin ID_E_EQ_R | ID_E_EQ_S);
1846 1.337 martin if (error)
1847 1.337 martin goto report_error;
1848 1.337 martin }
1849 1.337 martin
1850 1.337 martin /* Set signal masks/defaults */
1851 1.337 martin if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGMASK) {
1852 1.337 martin mutex_enter(l->l_proc->p_lock);
1853 1.337 martin error = sigprocmask1(l, SIG_SETMASK,
1854 1.337 martin &spawn_data->sed_attrs->sa_sigmask, NULL);
1855 1.337 martin mutex_exit(l->l_proc->p_lock);
1856 1.337 martin if (error)
1857 1.337 martin goto report_error;
1858 1.337 martin }
1859 1.337 martin
1860 1.337 martin if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGDEF) {
1861 1.337 martin for (i = 1; i <= NSIG; i++) {
1862 1.337 martin if (sigismember(
1863 1.337 martin &spawn_data->sed_attrs->sa_sigdefault, i))
1864 1.337 martin sigaction1(l, i, &sigact, NULL, NULL,
1865 1.337 martin 0);
1866 1.337 martin }
1867 1.337 martin }
1868 1.337 martin }
1869 1.337 martin
1870 1.341 martin /* stop using kernel vmspace */
1871 1.341 martin KPREEMPT_DISABLE(l);
1872 1.341 martin pmap_deactivate(l);
1873 1.341 martin l->l_proc->p_vmspace = NULL;
1874 1.337 martin
1875 1.337 martin /* now do the real exec */
1876 1.337 martin rw_enter(&exec_lock, RW_READER);
1877 1.337 martin error = execve_runproc(l, &spawn_data->sed_exec);
1878 1.341 martin have_reflock = false;
1879 1.337 martin if (error == EJUSTRETURN)
1880 1.337 martin error = 0;
1881 1.337 martin else if (error)
1882 1.337 martin goto report_error;
1883 1.337 martin
1884 1.345 martin /* we now have our own vmspace */
1885 1.345 martin KPREEMPT_ENABLE(l);
1886 1.345 martin
1887 1.337 martin /* done, signal parent */
1888 1.337 martin mutex_enter(&spawn_data->sed_mtx_child);
1889 1.337 martin cv_signal(&spawn_data->sed_cv_child_ready);
1890 1.337 martin mutex_exit(&spawn_data->sed_mtx_child);
1891 1.337 martin
1892 1.337 martin /* and finaly: leave to userland for the first time */
1893 1.337 martin cpu_spawn_return(l);
1894 1.337 martin
1895 1.337 martin /* NOTREACHED */
1896 1.337 martin return;
1897 1.337 martin
1898 1.337 martin report_error:
1899 1.341 martin if (have_reflock)
1900 1.341 martin rw_exit(&l->l_proc->p_reflock);
1901 1.341 martin
1902 1.341 martin /* stop using kernel vmspace (if we haven't already) */
1903 1.341 martin if (l->l_proc->p_vmspace) {
1904 1.341 martin KPREEMPT_DISABLE(l);
1905 1.341 martin pmap_deactivate(l);
1906 1.341 martin l->l_proc->p_vmspace = NULL;
1907 1.341 martin KPREEMPT_ENABLE(l);
1908 1.337 martin }
1909 1.337 martin
1910 1.337 martin /*
1911 1.337 martin * Set error value for parent to pick up (and take over ownership
1912 1.337 martin * of spawn_data again), signal parent and exit this process.
1913 1.337 martin */
1914 1.337 martin mutex_enter(&spawn_data->sed_mtx_child);
1915 1.337 martin spawn_data->sed_error = error;
1916 1.337 martin cv_signal(&spawn_data->sed_cv_child_ready);
1917 1.337 martin mutex_exit(&spawn_data->sed_mtx_child);
1918 1.337 martin mutex_enter(l->l_proc->p_lock);
1919 1.337 martin exit1(l, W_EXITCODE(error, SIGABRT));
1920 1.337 martin }
1921 1.337 martin
1922 1.342 christos static void
1923 1.344 christos posix_spawn_fa_free(struct posix_spawn_file_actions *fa, size_t len)
1924 1.342 christos {
1925 1.342 christos
1926 1.344 christos for (size_t i = 0; i < len; i++) {
1927 1.342 christos struct posix_spawn_file_actions_entry *fae = &fa->fae[i];
1928 1.342 christos if (fae->fae_action != FAE_OPEN)
1929 1.342 christos continue;
1930 1.342 christos kmem_free(fae->fae_path, strlen(fae->fae_path) + 1);
1931 1.342 christos }
1932 1.343 christos if (fa->len)
1933 1.343 christos kmem_free(fa->fae, sizeof(*fa->fae) * fa->len);
1934 1.342 christos kmem_free(fa, sizeof(*fa));
1935 1.342 christos }
1936 1.342 christos
1937 1.342 christos static int
1938 1.342 christos posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap,
1939 1.342 christos const struct posix_spawn_file_actions *ufa)
1940 1.342 christos {
1941 1.342 christos struct posix_spawn_file_actions *fa;
1942 1.342 christos struct posix_spawn_file_actions_entry *fae;
1943 1.342 christos char *pbuf = NULL;
1944 1.342 christos int error;
1945 1.344 christos size_t i = 0;
1946 1.342 christos
1947 1.342 christos fa = kmem_alloc(sizeof(*fa), KM_SLEEP);
1948 1.342 christos error = copyin(ufa, fa, sizeof(*fa));
1949 1.342 christos if (error) {
1950 1.342 christos fa->fae = NULL;
1951 1.342 christos fa->len = 0;
1952 1.342 christos goto out;
1953 1.342 christos }
1954 1.342 christos
1955 1.342 christos if (fa->len == 0)
1956 1.342 christos return 0;
1957 1.342 christos
1958 1.342 christos size_t fal = fa->len * sizeof(*fae);
1959 1.342 christos fae = fa->fae;
1960 1.342 christos fa->fae = kmem_alloc(fal, KM_SLEEP);
1961 1.342 christos error = copyin(fae, fa->fae, fal);
1962 1.344 christos if (error)
1963 1.342 christos goto out;
1964 1.342 christos
1965 1.342 christos pbuf = PNBUF_GET();
1966 1.344 christos for (; i < fa->len; i++) {
1967 1.342 christos fae = &fa->fae[i];
1968 1.342 christos if (fae->fae_action != FAE_OPEN)
1969 1.342 christos continue;
1970 1.342 christos error = copyinstr(fae->fae_path, pbuf, MAXPATHLEN, &fal);
1971 1.344 christos if (error)
1972 1.342 christos goto out;
1973 1.342 christos fae->fae_path = kmem_alloc(fal, KM_SLEEP);
1974 1.342 christos memcpy(fae->fae_path, pbuf, fal);
1975 1.342 christos }
1976 1.342 christos PNBUF_PUT(pbuf);
1977 1.342 christos *fap = fa;
1978 1.342 christos return 0;
1979 1.342 christos out:
1980 1.342 christos if (pbuf)
1981 1.342 christos PNBUF_PUT(pbuf);
1982 1.344 christos posix_spawn_fa_free(fa, i);
1983 1.342 christos return error;
1984 1.342 christos }
1985 1.342 christos
1986 1.337 martin int
1987 1.337 martin sys_posix_spawn(struct lwp *l1, const struct sys_posix_spawn_args *uap,
1988 1.337 martin register_t *retval)
1989 1.337 martin {
1990 1.337 martin /* {
1991 1.337 martin syscallarg(pid_t *) pid;
1992 1.337 martin syscallarg(const char *) path;
1993 1.337 martin syscallarg(const struct posix_spawn_file_actions *) file_actions;
1994 1.337 martin syscallarg(const struct posix_spawnattr *) attrp;
1995 1.337 martin syscallarg(char *const *) argv;
1996 1.337 martin syscallarg(char *const *) envp;
1997 1.337 martin } */
1998 1.337 martin
1999 1.337 martin struct proc *p1, *p2;
2000 1.337 martin struct plimit *p1_lim;
2001 1.337 martin struct lwp *l2;
2002 1.342 christos int error = 0, tnprocs, count;
2003 1.337 martin struct posix_spawn_file_actions *fa = NULL;
2004 1.337 martin struct posix_spawnattr *sa = NULL;
2005 1.337 martin struct spawn_exec_data *spawn_data;
2006 1.337 martin uid_t uid;
2007 1.337 martin vaddr_t uaddr;
2008 1.337 martin pid_t pid;
2009 1.337 martin bool have_exec_lock = false;
2010 1.337 martin
2011 1.337 martin p1 = l1->l_proc;
2012 1.337 martin uid = kauth_cred_getuid(l1->l_cred);
2013 1.337 martin tnprocs = atomic_inc_uint_nv(&nprocs);
2014 1.337 martin
2015 1.337 martin /*
2016 1.337 martin * Although process entries are dynamically created, we still keep
2017 1.337 martin * a global limit on the maximum number we will create.
2018 1.337 martin */
2019 1.337 martin if (__predict_false(tnprocs >= maxproc))
2020 1.337 martin error = -1;
2021 1.337 martin else
2022 1.337 martin error = kauth_authorize_process(l1->l_cred,
2023 1.337 martin KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL);
2024 1.337 martin
2025 1.337 martin if (error) {
2026 1.337 martin atomic_dec_uint(&nprocs);
2027 1.337 martin *retval = EAGAIN;
2028 1.337 martin return 0;
2029 1.337 martin }
2030 1.337 martin
2031 1.337 martin /*
2032 1.337 martin * Enforce limits.
2033 1.337 martin */
2034 1.337 martin count = chgproccnt(uid, 1);
2035 1.337 martin if (kauth_authorize_generic(l1->l_cred, KAUTH_GENERIC_ISSUSER, NULL) !=
2036 1.337 martin 0 && __predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) {
2037 1.337 martin error = EAGAIN;
2038 1.337 martin goto error_exit;
2039 1.337 martin }
2040 1.337 martin
2041 1.337 martin /* copy in file_actions struct */
2042 1.337 martin if (SCARG(uap, file_actions) != NULL) {
2043 1.342 christos error = posix_spawn_fa_alloc(&fa, SCARG(uap, file_actions));
2044 1.342 christos if (error)
2045 1.337 martin goto error_exit;
2046 1.337 martin }
2047 1.342 christos
2048 1.337 martin /* copyin posix_spawnattr struct */
2049 1.337 martin if (SCARG(uap, attrp) != NULL) {
2050 1.342 christos sa = kmem_alloc(sizeof(*sa), KM_SLEEP);
2051 1.342 christos error = copyin(SCARG(uap, attrp), sa, sizeof(*sa));
2052 1.337 martin if (error)
2053 1.337 martin goto error_exit;
2054 1.337 martin }
2055 1.337 martin
2056 1.337 martin /*
2057 1.337 martin * Do the first part of the exec now, collect state
2058 1.337 martin * in spawn_data.
2059 1.337 martin */
2060 1.337 martin spawn_data = kmem_zalloc(sizeof(*spawn_data), KM_SLEEP);
2061 1.337 martin error = execve_loadvm(l1, SCARG(uap, path), SCARG(uap, argv),
2062 1.337 martin SCARG(uap, envp), execve_fetch_element, &spawn_data->sed_exec);
2063 1.337 martin if (error == EJUSTRETURN)
2064 1.337 martin error = 0;
2065 1.337 martin else if (error)
2066 1.337 martin goto error_exit;
2067 1.337 martin
2068 1.337 martin have_exec_lock = true;
2069 1.337 martin
2070 1.337 martin /*
2071 1.337 martin * Allocate virtual address space for the U-area now, while it
2072 1.337 martin * is still easy to abort the fork operation if we're out of
2073 1.337 martin * kernel virtual address space.
2074 1.337 martin */
2075 1.337 martin uaddr = uvm_uarea_alloc();
2076 1.337 martin if (__predict_false(uaddr == 0)) {
2077 1.337 martin error = ENOMEM;
2078 1.337 martin goto error_exit;
2079 1.337 martin }
2080 1.337 martin
2081 1.337 martin /*
2082 1.337 martin * Allocate new proc. Leave it's p_vmspace NULL for now.
2083 1.337 martin * This is a point of no return, we will have to go through
2084 1.337 martin * the child proc to properly clean it up past this point.
2085 1.337 martin */
2086 1.337 martin p2 = proc_alloc();
2087 1.337 martin pid = p2->p_pid;
2088 1.337 martin
2089 1.337 martin /*
2090 1.337 martin * Make a proc table entry for the new process.
2091 1.337 martin * Start by zeroing the section of proc that is zero-initialized,
2092 1.337 martin * then copy the section that is copied directly from the parent.
2093 1.337 martin */
2094 1.337 martin memset(&p2->p_startzero, 0,
2095 1.337 martin (unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero));
2096 1.337 martin memcpy(&p2->p_startcopy, &p1->p_startcopy,
2097 1.337 martin (unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy));
2098 1.337 martin p2->p_vmspace = NULL;
2099 1.337 martin
2100 1.337 martin CIRCLEQ_INIT(&p2->p_sigpend.sp_info);
2101 1.337 martin
2102 1.337 martin LIST_INIT(&p2->p_lwps);
2103 1.337 martin LIST_INIT(&p2->p_sigwaiters);
2104 1.337 martin
2105 1.337 martin /*
2106 1.337 martin * Duplicate sub-structures as needed.
2107 1.337 martin * Increase reference counts on shared objects.
2108 1.337 martin * Inherit flags we want to keep. The flags related to SIGCHLD
2109 1.337 martin * handling are important in order to keep a consistent behaviour
2110 1.337 martin * for the child after the fork. If we are a 32-bit process, the
2111 1.337 martin * child will be too.
2112 1.337 martin */
2113 1.337 martin p2->p_flag =
2114 1.337 martin p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32);
2115 1.337 martin p2->p_emul = p1->p_emul;
2116 1.337 martin p2->p_execsw = p1->p_execsw;
2117 1.337 martin
2118 1.337 martin mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
2119 1.337 martin mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
2120 1.337 martin rw_init(&p2->p_reflock);
2121 1.337 martin cv_init(&p2->p_waitcv, "wait");
2122 1.337 martin cv_init(&p2->p_lwpcv, "lwpwait");
2123 1.337 martin
2124 1.337 martin p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
2125 1.337 martin
2126 1.337 martin kauth_proc_fork(p1, p2);
2127 1.337 martin
2128 1.337 martin p2->p_raslist = NULL;
2129 1.337 martin p2->p_fd = fd_copy();
2130 1.337 martin
2131 1.337 martin /* XXX racy */
2132 1.337 martin p2->p_mqueue_cnt = p1->p_mqueue_cnt;
2133 1.337 martin
2134 1.337 martin p2->p_cwdi = cwdinit();
2135 1.337 martin
2136 1.337 martin /*
2137 1.337 martin * Note: p_limit (rlimit stuff) is copy-on-write, so normally
2138 1.337 martin * we just need increase pl_refcnt.
2139 1.337 martin */
2140 1.337 martin p1_lim = p1->p_limit;
2141 1.337 martin if (!p1_lim->pl_writeable) {
2142 1.337 martin lim_addref(p1_lim);
2143 1.337 martin p2->p_limit = p1_lim;
2144 1.337 martin } else {
2145 1.337 martin p2->p_limit = lim_copy(p1->p_limit);
2146 1.337 martin }
2147 1.337 martin
2148 1.337 martin p2->p_lflag = 0;
2149 1.337 martin p2->p_sflag = 0;
2150 1.337 martin p2->p_slflag = 0;
2151 1.337 martin p2->p_pptr = p1;
2152 1.337 martin p2->p_ppid = p1->p_pid;
2153 1.337 martin LIST_INIT(&p2->p_children);
2154 1.337 martin
2155 1.337 martin p2->p_aio = NULL;
2156 1.337 martin
2157 1.337 martin #ifdef KTRACE
2158 1.337 martin /*
2159 1.337 martin * Copy traceflag and tracefile if enabled.
2160 1.337 martin * If not inherited, these were zeroed above.
2161 1.337 martin */
2162 1.337 martin if (p1->p_traceflag & KTRFAC_INHERIT) {
2163 1.337 martin mutex_enter(&ktrace_lock);
2164 1.337 martin p2->p_traceflag = p1->p_traceflag;
2165 1.337 martin if ((p2->p_tracep = p1->p_tracep) != NULL)
2166 1.337 martin ktradref(p2);
2167 1.337 martin mutex_exit(&ktrace_lock);
2168 1.337 martin }
2169 1.337 martin #endif
2170 1.337 martin
2171 1.337 martin /*
2172 1.337 martin * Create signal actions for the child process.
2173 1.337 martin */
2174 1.337 martin p2->p_sigacts = sigactsinit(p1, 0);
2175 1.337 martin mutex_enter(p1->p_lock);
2176 1.337 martin p2->p_sflag |=
2177 1.337 martin (p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP));
2178 1.337 martin sched_proc_fork(p1, p2);
2179 1.337 martin mutex_exit(p1->p_lock);
2180 1.337 martin
2181 1.337 martin p2->p_stflag = p1->p_stflag;
2182 1.337 martin
2183 1.337 martin /*
2184 1.337 martin * p_stats.
2185 1.337 martin * Copy parts of p_stats, and zero out the rest.
2186 1.337 martin */
2187 1.337 martin p2->p_stats = pstatscopy(p1->p_stats);
2188 1.337 martin
2189 1.337 martin /* copy over machdep flags to the new proc */
2190 1.337 martin cpu_proc_fork(p1, p2);
2191 1.337 martin
2192 1.337 martin /*
2193 1.337 martin * Prepare remaining parts of spawn data
2194 1.337 martin */
2195 1.342 christos if (fa && fa->len) {
2196 1.342 christos spawn_data->sed_actions_len = fa->len;
2197 1.342 christos spawn_data->sed_actions = fa->fae;
2198 1.337 martin }
2199 1.342 christos if (sa)
2200 1.337 martin spawn_data->sed_attrs = sa;
2201 1.337 martin
2202 1.337 martin spawn_data->sed_parent = p1;
2203 1.337 martin cv_init(&spawn_data->sed_cv_child_ready, "pspawn");
2204 1.337 martin mutex_init(&spawn_data->sed_mtx_child, MUTEX_DEFAULT, IPL_NONE);
2205 1.337 martin mutex_enter(&spawn_data->sed_mtx_child);
2206 1.337 martin
2207 1.337 martin /* create LWP */
2208 1.337 martin lwp_create(l1, p2, uaddr, 0, NULL, 0, spawn_return, spawn_data,
2209 1.337 martin &l2, l1->l_class);
2210 1.337 martin l2->l_ctxlink = NULL; /* reset ucontext link */
2211 1.337 martin
2212 1.337 martin /*
2213 1.337 martin * Copy the credential so other references don't see our changes.
2214 1.337 martin * Test to see if this is necessary first, since in the common case
2215 1.337 martin * we won't need a private reference.
2216 1.337 martin */
2217 1.337 martin if (kauth_cred_geteuid(l2->l_cred) != kauth_cred_getsvuid(l2->l_cred) ||
2218 1.337 martin kauth_cred_getegid(l2->l_cred) != kauth_cred_getsvgid(l2->l_cred)) {
2219 1.337 martin l2->l_cred = kauth_cred_copy(l2->l_cred);
2220 1.337 martin kauth_cred_setsvuid(l2->l_cred, kauth_cred_geteuid(l2->l_cred));
2221 1.337 martin kauth_cred_setsvgid(l2->l_cred, kauth_cred_getegid(l2->l_cred));
2222 1.337 martin }
2223 1.337 martin
2224 1.337 martin /* Update the master credentials. */
2225 1.337 martin if (l2->l_cred != p2->p_cred) {
2226 1.337 martin kauth_cred_t ocred;
2227 1.337 martin
2228 1.337 martin kauth_cred_hold(l2->l_cred);
2229 1.337 martin mutex_enter(p2->p_lock);
2230 1.337 martin ocred = p2->p_cred;
2231 1.337 martin p2->p_cred = l2->l_cred;
2232 1.337 martin mutex_exit(p2->p_lock);
2233 1.337 martin kauth_cred_free(ocred);
2234 1.337 martin }
2235 1.337 martin
2236 1.345 martin l2->l_nopreempt = 1; /* start it non-preemptable */
2237 1.345 martin
2238 1.337 martin /*
2239 1.337 martin * It's now safe for the scheduler and other processes to see the
2240 1.337 martin * child process.
2241 1.337 martin */
2242 1.337 martin mutex_enter(proc_lock);
2243 1.337 martin
2244 1.337 martin if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT)
2245 1.337 martin p2->p_lflag |= PL_CONTROLT;
2246 1.337 martin
2247 1.337 martin LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
2248 1.337 martin p2->p_exitsig = SIGCHLD; /* signal for parent on exit */
2249 1.337 martin
2250 1.337 martin LIST_INSERT_AFTER(p1, p2, p_pglist);
2251 1.337 martin LIST_INSERT_HEAD(&allproc, p2, p_list);
2252 1.337 martin
2253 1.337 martin p2->p_trace_enabled = trace_is_enabled(p2);
2254 1.337 martin #ifdef __HAVE_SYSCALL_INTERN
2255 1.337 martin (*p2->p_emul->e_syscall_intern)(p2);
2256 1.337 martin #endif
2257 1.337 martin
2258 1.337 martin /*
2259 1.337 martin * Make child runnable, set start time, and add to run queue except
2260 1.337 martin * if the parent requested the child to start in SSTOP state.
2261 1.337 martin */
2262 1.337 martin mutex_enter(p2->p_lock);
2263 1.337 martin
2264 1.337 martin getmicrotime(&p2->p_stats->p_start);
2265 1.337 martin
2266 1.337 martin lwp_lock(l2);
2267 1.337 martin KASSERT(p2->p_nrlwps == 1);
2268 1.337 martin p2->p_nrlwps = 1;
2269 1.337 martin p2->p_stat = SACTIVE;
2270 1.337 martin l2->l_stat = LSRUN;
2271 1.337 martin sched_enqueue(l2, false);
2272 1.337 martin lwp_unlock(l2);
2273 1.337 martin
2274 1.337 martin mutex_exit(p2->p_lock);
2275 1.337 martin mutex_exit(proc_lock);
2276 1.337 martin
2277 1.337 martin cv_wait(&spawn_data->sed_cv_child_ready, &spawn_data->sed_mtx_child);
2278 1.337 martin mutex_exit(&spawn_data->sed_mtx_child);
2279 1.337 martin error = spawn_data->sed_error;
2280 1.337 martin
2281 1.341 martin rw_exit(&p1->p_reflock);
2282 1.337 martin rw_exit(&exec_lock);
2283 1.337 martin have_exec_lock = false;
2284 1.337 martin
2285 1.342 christos if (fa)
2286 1.344 christos posix_spawn_fa_free(fa, fa->len);
2287 1.337 martin
2288 1.342 christos if (sa)
2289 1.342 christos kmem_free(sa, sizeof(*sa));
2290 1.337 martin
2291 1.337 martin cv_destroy(&spawn_data->sed_cv_child_ready);
2292 1.337 martin mutex_destroy(&spawn_data->sed_mtx_child);
2293 1.337 martin
2294 1.337 martin kmem_free(spawn_data, sizeof(*spawn_data));
2295 1.337 martin
2296 1.337 martin if (error == 0 && SCARG(uap, pid) != NULL)
2297 1.337 martin error = copyout(&pid, SCARG(uap, pid), sizeof(pid));
2298 1.337 martin
2299 1.337 martin *retval = error;
2300 1.337 martin return 0;
2301 1.337 martin
2302 1.337 martin error_exit:
2303 1.337 martin if (have_exec_lock)
2304 1.337 martin rw_exit(&exec_lock);
2305 1.337 martin
2306 1.342 christos if (fa)
2307 1.344 christos posix_spawn_fa_free(fa, fa->len);
2308 1.337 martin
2309 1.342 christos if (sa)
2310 1.337 martin kmem_free(sa, sizeof(*sa));
2311 1.337 martin
2312 1.337 martin (void)chgproccnt(uid, -1);
2313 1.337 martin atomic_dec_uint(&nprocs);
2314 1.337 martin
2315 1.337 martin *retval = error;
2316 1.337 martin return 0;
2317 1.337 martin }
2318 1.337 martin
2319 1.336 matt void
2320 1.336 matt exec_free_emul_arg(struct exec_package *epp)
2321 1.336 matt {
2322 1.336 matt if (epp->ep_emul_arg_free != NULL) {
2323 1.336 matt KASSERT(epp->ep_emul_arg != NULL);
2324 1.336 matt (*epp->ep_emul_arg_free)(epp->ep_emul_arg);
2325 1.336 matt epp->ep_emul_arg_free = NULL;
2326 1.336 matt epp->ep_emul_arg = NULL;
2327 1.336 matt } else {
2328 1.336 matt KASSERT(epp->ep_emul_arg == NULL);
2329 1.336 matt }
2330 1.336 matt }
2331