2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Mach Operating System
25 * Copyright (c) 1987 Carnegie-Mellon University
26 * All rights reserved. The CMU software License Agreement specifies
27 * the terms and conditions for use and redistribution.
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
71 #include <machine/reg.h>
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/filedesc.h>
76 #include <sys/kernel.h>
80 #include <sys/socketvar.h>
81 #include <sys/malloc.h>
82 #include <sys/namei.h>
83 #include <sys/mount.h>
84 #include <sys/vnode.h>
90 #include <sys/kdebug.h>
91 #include <sys/signal.h>
93 #include <mach/vm_param.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_shared_memory_server.h>
99 #include <kern/thread.h>
100 #include <kern/task.h>
102 #include <kern/ast.h>
103 #include <kern/mach_loader.h>
104 #include <mach-o/fat.h>
105 #include <mach-o/loader.h>
106 #include <machine/vmparam.h>
108 #include <sys/ktrace.h>
113 extern vm_map_t bsd_pageable_map
;
115 #define ROUND_PTR(type, addr) \
116 (type *)( ( (unsigned)(addr) + 16 - 1) \
119 static int load_return_to_errno(load_return_t lrtn
);
120 int execve(struct proc
*p
, struct execve_args
*uap
, register_t
*retval
);
121 static int execargs_alloc(vm_offset_t
*addrp
);
122 static int execargs_free(vm_offset_t addr
);
125 execv(p
, args
, retval
)
130 ((struct execve_args
*)args
)->envp
= NULL
;
131 return (execve(p
, args
, retval
));
136 execve(p
, uap
, retval
)
137 register struct proc
*p
;
138 register struct execve_args
*uap
;
141 register struct ucred
*cred
= p
->p_ucred
;
142 register struct filedesc
*fdp
= p
->p_fd
;
145 int na
, ne
, ucp
, ap
, cc
;
152 struct vattr origvattr
;
153 vm_offset_t execargs
;
155 struct ps_strings ps
;
160 struct mach_header
*mach_header
;
161 struct fat_header
*fat_header
;
162 struct fat_arch fat_arch
;
164 load_result_t load_result
;
165 struct uthread
*uthread
;
169 boolean_t new_shared_regions
= FALSE
;
171 /* #! and name of interpreter */
172 char ex_shell
[SHSIZE
];
173 /* Mach-O executable */
174 struct mach_header mach_header
;
176 struct fat_header fat_header
;
181 int savedpathlen
= 0;
182 vm_offset_t
*execargsp
;
186 thread_act_t thr_act
;
189 unsigned long arch_offset
=0;
190 unsigned long arch_size
= 0;
191 char *ws_cache_name
= NULL
; /* used for pre-heat */
193 task
= current_task();
194 thr_act
= current_act();
195 uthread
= get_bsdthread_info(thr_act
);
197 if (uthread
->uu_flag
& P_VFORK
) {
198 vfexec
= 1; /* Mark in exec */
200 if (task
!= kernel_task
) {
201 numthreads
= get_task_numacts(task
);
202 if (numthreads
<= 0 )
204 if (numthreads
> 1) {
210 error
= execargs_alloc(&execargs
);
214 savedpath
= execargs
;
217 * To support new app package launching for Mac OS X, the dyld
218 * needs the first argument to execve() stored on the user stack.
219 * Copyin the "path" at the begining of the "execargs" buffer
222 * We have to do this before namei() because in case of
223 * symbolic links, namei() would overwrite the original "path".
224 * In case the last symbolic link resolved was a relative pathname
225 * we would lose the original "path", which could be an
226 * absolute pathname. This might be unacceptable for dyld.
228 /* XXX We could optimize to avoid copyinstr in the namei() */
230 error
= copyinstr(uap
->fname
, savedpath
, MAXPATHLEN
, &savedpathlen
);
234 * copyinstr will put in savedpathlen, the count of
235 * characters (including NULL) in the path.
238 if(app_profile
!= 0) {
240 /* grab the name of the file out of its path */
241 /* we will need this for lookup within the */
243 ws_cache_name
= savedpath
+ savedpathlen
;
244 while (ws_cache_name
[0] != '/') {
245 if(ws_cache_name
== savedpath
) {
254 /* Save the name aside for future use */
255 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
257 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| SAVENAME
,
258 UIO_USERSPACE
, uap
->fname
, p
);
259 if ((error
= namei(&nd
)))
262 VOP_LEASE(vp
, p
, p
->p_ucred
, LEASE_READ
);
264 if ((error
= VOP_GETATTR(vp
, &origvattr
, p
->p_ucred
, p
)))
267 /* Check mount point */
268 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
274 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_flag
& P_TRACED
))
275 origvattr
.va_mode
&= ~(VSUID
| VSGID
);
277 *(&vattr
) = *(&origvattr
);
280 error
= check_exec_access(p
, vp
, &vattr
);
285 * Read in first few bytes of file for segment sizes, magic number:
286 * 407 = plain executable
288 * 413 = demand paged RO text
289 * Also an ASCII line beginning with #! is
290 * the file name of a ``shell'' and arguments may be prepended
291 * to the argument list if given here.
293 * SHELL NAMES ARE LIMITED IN LENGTH.
295 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
299 exdata
.ex_shell
[0] = '\0'; /* for zero length files */
301 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
, sizeof (exdata
), 0,
302 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
, p
);
308 if (resid
> sizeof(exdata
) - min(sizeof(exdata
.mach_header
),
309 sizeof(exdata
.fat_header
))
310 && exdata
.ex_shell
[0] != '#') {
315 mach_header
= &exdata
.mach_header
;
316 fat_header
= &exdata
.fat_header
;
317 if (mach_header
->magic
== MH_MAGIC
)
319 else if (fat_header
->magic
== FAT_MAGIC
||
320 fat_header
->magic
== FAT_CIGAM
)
322 else if (mach_header
->magic
== MH_CIGAM
) {
326 if (exdata
.ex_shell
[0] != '#' ||
327 exdata
.ex_shell
[1] != '!' ||
332 cp
= &exdata
.ex_shell
[2]; /* skip "#!" */
333 while (cp
< &exdata
.ex_shell
[SHSIZE
]) {
336 else if (*cp
== '\n') {
346 cp
= &exdata
.ex_shell
[2];
350 while (*cp
&& *cp
!= ' ')
359 bcopy((caddr_t
)cp
, (caddr_t
)cfarg
, SHSIZE
);
363 * Support for new app package launching for Mac OS X.
364 * We are about to retry the execve() by changing the path to the
365 * interpreter name. Need to re-initialize the savedpath and
366 * savedpathlen. +1 for NULL.
368 savedpathlen
= (cpnospace
- execnamep
+ 1);
369 error
= copystr(execnamep
, savedpath
, savedpathlen
, &savedpathlen
);
373 /* Save the name aside for future use */
374 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
378 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
379 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
380 (FOLLOW
| LOCKLEAF
| SAVENAME
);
381 nd
.ni_segflg
= UIO_SYSSPACE
;
382 nd
.ni_dirp
= execnamep
;
383 if ((error
= namei(&nd
)))
386 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
387 if ((error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)))
393 * Collect arguments on "file" in swap space.
400 * Support for new app package launching for Mac OS X allocates
401 * the "path" at the begining.
402 * execargs get allocated after that
404 cp
= (char *) execargsp
; /* running pointer for copy */
406 * size of execargs less sizeof "path",
407 * a pointer to "path" and a NULL poiter
409 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
411 * Copy arguments into file in argdev area.
413 if (uap
->argp
) for (;;) {
416 if (indir
&& na
== 0) {
417 sharg
= nd
.ni_cnd
.cn_nameptr
;
419 uap
->argp
++; /* ignore argv[0] */
420 } else if (indir
&& (na
== 1 && cfarg
[0])) {
423 } else if (indir
&& (na
== 1 || (na
== 2 && cfarg
[0])))
424 ap
= (int)uap
->fname
;
425 else if (uap
->argp
) {
426 ap
= fuword((caddr_t
)uap
->argp
);
429 if (ap
== NULL
&& uap
->envp
) {
431 if ((ap
= fuword((caddr_t
)uap
->envp
)) != NULL
)
442 if (nc
>= (NCARGS
- savedpathlen
- 2*NBPW
-1)) {
447 error
= copystr(sharg
, cp
, (unsigned)cc
, &len
);
450 error
= copyinstr((caddr_t
)ap
, cp
, (unsigned)cc
,
457 } while (error
== ENAMETOOLONG
);
462 nc
= (nc
+ NBPW
-1) & ~(NBPW
-1);
465 * If we have a fat file, find "our" executable.
469 * Look up our architecture in the fat file.
471 lret
= fatfile_getarch(vp
, (vm_offset_t
)fat_header
, &fat_arch
);
472 if (lret
!= LOAD_SUCCESS
) {
473 error
= load_return_to_errno(lret
);
476 /* Read the Mach-O header out of it */
477 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
.mach_header
,
478 sizeof (exdata
.mach_header
),
480 UIO_SYSSPACE
, (IO_UNIT
|IO_NODELOCKED
), cred
, &resid
, p
);
486 /* Did we read a complete header? */
492 /* Is what we found a Mach-O executable */
493 if (mach_header
->magic
!= MH_MAGIC
) {
498 arch_offset
= fat_arch
.offset
;
499 arch_size
= fat_arch
.size
;
502 * Load the Mach-O file.
505 arch_size
= (u_long
)vattr
.va_size
;
509 kern_return_t result
;
511 result
= task_create_local(task
, FALSE
, FALSE
, &new_task
);
512 if (result
!= KERN_SUCCESS
)
513 printf("execve: task_create failed. Code: 0x%x\n", result
);
515 set_bsdtask_info(new_task
, p
);
519 map
= get_task_map(new_task
);
520 result
= thread_create(new_task
, &thr_act
);
521 if (result
!= KERN_SUCCESS
)
522 printf("execve: thread_create failed. Code: 0x%x\n", result
);
523 uthread
= get_bsdthread_info(thr_act
);
530 * Load the Mach-O file.
532 VOP_UNLOCK(vp
, 0, p
);
534 tws_handle_startup_file(task
, cred
->cr_uid
,
535 ws_cache_name
, vp
, &new_shared_regions
);
537 if (new_shared_regions
) {
538 shared_region_mapping_t new_shared_region
;
539 shared_region_mapping_t old_shared_region
;
541 if (shared_file_create_system_region(&new_shared_region
))
542 panic("couldn't create system_shared_region\n");
544 vm_get_shared_region(task
, &old_shared_region
);
545 vm_set_shared_region(task
, new_shared_region
);
547 shared_region_mapping_dealloc(old_shared_region
);
550 lret
= load_machfile(vp
, mach_header
, arch_offset
,
551 arch_size
, &load_result
, thr_act
, map
);
553 if (lret
!= LOAD_SUCCESS
) {
554 error
= load_return_to_errno(lret
);
558 /* load_machfile() maps the vnode */
562 * deal with set[ug]id.
564 p
->p_flag
&= ~P_SUGID
;
565 if (((origvattr
.va_mode
& VSUID
) != 0 &&
566 p
->p_ucred
->cr_uid
!= origvattr
.va_uid
)
567 || (origvattr
.va_mode
& VSGID
) != 0 &&
568 p
->p_ucred
->cr_gid
!= origvattr
.va_gid
) {
569 p
->p_ucred
= crcopy(cred
);
572 * If process is being ktraced, turn off - unless
575 if (p
->p_tracep
&& !(p
->p_traceflag
& KTRFAC_ROOT
)) {
576 struct vnode
*tvp
= p
->p_tracep
;
582 if (origvattr
.va_mode
& VSUID
)
583 p
->p_ucred
->cr_uid
= origvattr
.va_uid
;
584 if (origvattr
.va_mode
& VSGID
)
585 p
->p_ucred
->cr_gid
= origvattr
.va_gid
;
587 set_security_token(p
);
588 p
->p_flag
|= P_SUGID
;
590 /* Radar 2261856; setuid security hole fix */
591 /* Patch from OpenBSD: A. Ramesh */
593 * XXX For setuid processes, attempt to ensure that
594 * stdin, stdout, and stderr are already allocated.
595 * We do not want userland to accidentally allocate
596 * descriptors in this range which has implied meaning
599 for (i
= 0; i
< 3; i
++) {
600 extern struct fileops vnops
;
601 struct nameidata nd1
;
605 if (p
->p_fd
->fd_ofiles
[i
] == NULL
) {
606 if ((error
= falloc(p
, &fp
, &indx
)) != 0)
608 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
,
610 if ((error
= vn_open(&nd1
, FREAD
, 0)) != 0) {
612 p
->p_fd
->fd_ofiles
[indx
] = NULL
;
616 fp
->f_type
= DTYPE_VNODE
;
618 fp
->f_data
= (caddr_t
)nd1
.ni_vp
;
619 VOP_UNLOCK(nd1
.ni_vp
, 0, p
);
623 p
->p_cred
->p_svuid
= p
->p_ucred
->cr_uid
;
624 p
->p_cred
->p_svgid
= p
->p_ucred
->cr_gid
;
626 if (!vfexec
&& (p
->p_flag
& P_TRACED
))
632 VOP_LOCK(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
636 if (load_result
.unixproc
&&
637 create_unix_stack(get_task_map(task
),
638 load_result
.user_stack
, load_result
.customstack
, p
)) {
639 error
= load_return_to_errno(LOAD_NOSPACE
);
644 uthread
->uu_ar0
= (void *)get_user_regs(thr_act
);
648 * Copy back arglist if necessary.
654 old_map
= vm_map_switch(get_task_map(task
));
656 if (load_result
.unixproc
) {
659 ucp
= ucp
- nc
- NBPW
; /* begining of the STRING AREA */
662 * Support for new app package launching for Mac OS X allocates
663 * the "path" at the begining of the execargs buffer.
664 * copy it just before the string area.
666 savedpathlen
= (savedpathlen
+ NBPW
-1) & ~(NBPW
-1);
668 pathptr
= ucp
- savedpathlen
;
669 error
= copyoutstr(savedpath
, (caddr_t
)pathptr
,
670 (unsigned)savedpathlen
, &len
);
673 vm_map_switch(old_map
);
677 /* Save a NULL pointer below it */
678 (void) suword((caddr_t
)(pathptr
- NBPW
), 0);
680 /* Save the pointer to "path" just below it */
681 (void) suword((caddr_t
)(pathptr
- 2*NBPW
), pathptr
);
684 * na includes arg[] and env[].
685 * NBPW for 2 NULL one each ofter arg[argc -1] and env[n]
687 * skip over saved path, NBPW for pointer to path,
688 * and NBPW for the NULL after pointer to path.
690 ap
= ucp
- na
*NBPW
- 3*NBPW
- savedpathlen
- 2*NBPW
;
692 thread_setuserstack(thr_act
, ap
); /* Set the stack */
694 uthread
->uu_ar0
[SP
] = ap
;
696 (void) suword((caddr_t
)ap
, na
-ne
); /* argc */
700 cp
= (char *) execargsp
;
701 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
702 ps
.ps_argvstr
= (char *)ucp
; /* first argv string */
703 ps
.ps_nargvstr
= na
- ne
; /* argc */
707 (void) suword((caddr_t
)ap
, 0);
709 ps
.ps_envstr
= (char *)ucp
;
714 (void) suword((caddr_t
)ap
, ucp
);
716 error
= copyoutstr(cp
, (caddr_t
)ucp
,
722 } while (error
== ENAMETOOLONG
);
724 break; /* bad stack - user's problem */
726 (void) suword((caddr_t
)ap
, 0);
729 if (load_result
.dynlinker
) {
731 ap
= thread_adjuserstack(thr_act
, -4); /* Adjust the stack */
733 ap
= uthread
->uu_ar0
[SP
] -= 4;
735 (void) suword((caddr_t
)ap
, load_result
.mach_header
);
739 vm_map_switch(old_map
);
742 thread_setentrypoint(thr_act
, load_result
.entry_point
); /* Set the entry point */
744 uthread
->uu_ar0
[PC
] = load_result
.entry_point
;
746 #error architecture not implemented!
753 * Reset signal state.
755 execsigs(p
, thr_act
);
758 * Close file descriptors
759 * which specify close-on-exec.
762 /* FIXME: Till vmspace inherit is fixed: */
763 if (!vfexec
&& p
->vm_shm
)
765 /* Clean up the semaphores */
769 * Remember file name for accounting.
771 p
->p_acflag
&= ~AFORK
;
772 if (nd
.ni_cnd
.cn_namelen
> MAXCOMLEN
)
773 nd
.ni_cnd
.cn_namelen
= MAXCOMLEN
;
774 bcopy((caddr_t
)nd
.ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
775 (unsigned)nd
.ni_cnd
.cn_namelen
);
776 p
->p_comm
[nd
.ni_cnd
.cn_namelen
] = '\0';
779 /* This is for kdebug */
780 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
782 /* Collect the pathname for tracing */
783 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
786 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
787 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, getshuttle_thread(thr_act
));
789 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
790 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
794 * mark as execed, wakeup the process that vforked (if any) and tell
795 * it that it now has it's own resources back
798 if (p
->p_pptr
&& (p
->p_flag
& P_PPWAIT
)) {
799 p
->p_flag
&= ~P_PPWAIT
;
800 wakeup((caddr_t
)p
->p_pptr
);
803 if (vfexec
&& (p
->p_flag
& P_TRACED
)) {
804 psignal_vfork(p
, new_task
, thr_act
, SIGTRAP
);
809 task_deallocate(new_task
);
810 act_deallocate(thr_act
);
815 FREE_ZONE(nd
.ni_cnd
.cn_pnbuf
, nd
.ni_cnd
.cn_pnlen
, M_NAMEI
);
820 execargs_free(execargs
);
821 if (!error
&& vfexec
) {
822 vfork_return(current_act(), p
->p_pptr
, p
, retval
);
823 (void) thread_resume(thr_act
);
830 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
833 create_unix_stack(map
, user_stack
, customstack
, p
)
835 vm_offset_t user_stack
;
842 p
->user_stack
= user_stack
;
844 size
= round_page(unix_stack_size(p
));
845 addr
= trunc_page(user_stack
- size
);
846 return (vm_allocate(map
,&addr
, size
, FALSE
));
848 return(KERN_SUCCESS
);
851 #include <sys/reboot.h>
853 char init_program_name
[128] = "/sbin/mach_init\0";
855 char init_args
[128] = "";
857 struct execve_args init_exec_args
;
858 int init_attempts
= 0;
865 vm_offset_t init_addr
;
869 register_t retval
[2];
874 /* init_args are copied in string form directly from bootstrap */
877 if (boothowto
& RB_INITNAME
) {
878 printf("init program? ");
880 gets(init_program_name
, init_program_name
);
884 if (error
&& ((boothowto
& RB_INITNAME
) == 0) &&
885 (init_attempts
== 1)) {
886 static char other_init
[] = "/etc/mach_init";
887 printf("Load of %s, errno %d, trying %s\n",
888 init_program_name
, error
, other_init
);
890 bcopy(other_init
, init_program_name
,
897 printf("Load of %s failed, errno %d\n",
898 init_program_name
, error
);
900 boothowto
|= RB_INITNAME
;
905 * Copy out program name.
908 init_addr
= VM_MIN_ADDRESS
;
909 (void) vm_allocate(current_map(), &init_addr
,
913 (void) copyout((caddr_t
) init_program_name
,
914 (caddr_t
) (init_addr
),
915 (unsigned) sizeof(init_program_name
)+1);
917 argv
[0] = (char *) init_addr
;
918 init_addr
+= sizeof(init_program_name
);
919 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
922 * Put out first (and only) argument, similarly.
923 * Assumes everything fits in a page as allocated
927 (void) copyout((caddr_t
) init_args
,
928 (caddr_t
) (init_addr
),
929 (unsigned) sizeof(init_args
));
931 argv
[1] = (char *) init_addr
;
932 init_addr
+= sizeof(init_args
);
933 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
936 * Null-end the argument list
939 argv
[2] = (char *) 0;
942 * Copy out the argument list.
945 (void) copyout((caddr_t
) argv
,
946 (caddr_t
) (init_addr
),
947 (unsigned) sizeof(argv
));
950 * Set up argument block for fake call to execve.
953 init_exec_args
.fname
= argv
[0];
954 init_exec_args
.argp
= (char **) init_addr
;
955 init_exec_args
.envp
= 0;
957 /* So that mach_init task
958 * is set with uid,gid 0 token
960 set_security_token(p
);
962 error
= execve(p
,&init_exec_args
,retval
);
967 * Convert a load_return_t to an errno.
970 load_return_to_errno(load_return_t lrtn
)
993 * exec_check_access()
996 check_exec_access(p
, vp
, vap
)
1004 if (error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
, p
))
1007 if (flag
& P_TRACED
) {
1008 if (error
= VOP_ACCESS(vp
, VREAD
, p
->p_ucred
, p
))
1011 if (vp
->v_type
!= VREG
||
1012 (vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)
1017 #include <mach/mach_types.h>
1018 #include <mach/vm_prot.h>
1019 #include <mach/semaphore.h>
1020 #include <mach/sync_policy.h>
1021 #include <kern/clock.h>
1022 #include <mach/kern_return.h>
1024 extern semaphore_t execve_semaphore
;
1027 execargs_alloc(addrp
)
1032 kret
= semaphore_wait(execve_semaphore
);
1033 if (kret
!= KERN_SUCCESS
)
1037 case KERN_INVALID_ADDRESS
:
1038 case KERN_PROTECTION_FAILURE
:
1041 case KERN_OPERATION_TIMED_OUT
:
1045 kret
= kmem_alloc_pageable(bsd_pageable_map
, addrp
, NCARGS
);
1046 if (kret
!= KERN_SUCCESS
)
1058 kmem_free(bsd_pageable_map
, addr
, NCARGS
);
1060 kret
= semaphore_signal(execve_semaphore
);
1062 case KERN_INVALID_ADDRESS
:
1063 case KERN_PROTECTION_FAILURE
:
1066 case KERN_OPERATION_TIMED_OUT
: