2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Mach Operating System
28 * Copyright (c) 1987 Carnegie-Mellon University
29 * All rights reserved. The CMU software License Agreement specifies
30 * the terms and conditions for use and redistribution.
36 * Copyright (c) 1982, 1986, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 * (c) UNIX System Laboratories, Inc.
39 * All or some portions of this file are derived from material licensed
40 * to the University of California by American Telephone and Telegraph
41 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
42 * the permission of UNIX System Laboratories, Inc.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
74 #include <machine/reg.h>
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/filedesc.h>
79 #include <sys/kernel.h>
83 #include <sys/socketvar.h>
84 #include <sys/malloc.h>
85 #include <sys/namei.h>
86 #include <sys/mount.h>
87 #include <sys/vnode.h>
93 #include <sys/kdebug.h>
94 #include <sys/signal.h>
96 #include <mach/vm_param.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_kern.h>
100 #include <vm/vm_shared_memory_server.h>
102 #include <kern/thread.h>
103 #include <kern/task.h>
105 #include <kern/ast.h>
106 #include <kern/mach_loader.h>
107 #include <mach-o/fat.h>
108 #include <mach-o/loader.h>
109 #include <machine/vmparam.h>
111 #include <sys/ktrace.h>
116 extern vm_map_t bsd_pageable_map
;
118 #define ROUND_PTR(type, addr) \
119 (type *)( ( (unsigned)(addr) + 16 - 1) \
122 static int load_return_to_errno(load_return_t lrtn
);
123 int execve(struct proc
*p
, struct execve_args
*uap
, register_t
*retval
);
124 static int execargs_alloc(vm_offset_t
*addrp
);
125 static int execargs_free(vm_offset_t addr
);
128 execv(p
, args
, retval
)
133 ((struct execve_args
*)args
)->envp
= NULL
;
134 return (execve(p
, args
, retval
));
139 execve(p
, uap
, retval
)
140 register struct proc
*p
;
141 register struct execve_args
*uap
;
144 register struct ucred
*cred
= p
->p_ucred
;
145 register struct filedesc
*fdp
= p
->p_fd
;
148 int na
, ne
, ucp
, ap
, cc
;
155 struct vattr origvattr
;
156 vm_offset_t execargs
;
158 struct ps_strings ps
;
163 struct mach_header
*mach_header
;
164 struct fat_header
*fat_header
;
165 struct fat_arch fat_arch
;
167 load_result_t load_result
;
168 struct uthread
*uthread
;
172 boolean_t clean_regions
= FALSE
;
174 /* #! and name of interpreter */
175 char ex_shell
[SHSIZE
];
176 /* Mach-O executable */
177 struct mach_header mach_header
;
179 struct fat_header fat_header
;
184 int savedpathlen
= 0;
185 vm_offset_t
*execargsp
;
189 thread_act_t thr_act
;
192 unsigned long arch_offset
=0;
193 unsigned long arch_size
= 0;
194 char *ws_cache_name
= NULL
; /* used for pre-heat */
196 task
= current_task();
197 thr_act
= current_act();
198 uthread
= get_bsdthread_info(thr_act
);
200 if (uthread
->uu_flag
& P_VFORK
) {
201 vfexec
= 1; /* Mark in exec */
203 if (task
!= kernel_task
) {
204 numthreads
= get_task_numacts(task
);
205 if (numthreads
<= 0 )
207 if (numthreads
> 1) {
213 error
= execargs_alloc(&execargs
);
217 savedpath
= execargs
;
220 * To support new app package launching for Mac OS X, the dyld
221 * needs the first argument to execve() stored on the user stack.
222 * Copyin the "path" at the begining of the "execargs" buffer
225 * We have to do this before namei() because in case of
226 * symbolic links, namei() would overwrite the original "path".
227 * In case the last symbolic link resolved was a relative pathname
228 * we would lose the original "path", which could be an
229 * absolute pathname. This might be unacceptable for dyld.
231 /* XXX We could optimize to avoid copyinstr in the namei() */
233 error
= copyinstr(uap
->fname
, savedpath
, MAXPATHLEN
, &savedpathlen
);
237 * copyinstr will put in savedpathlen, the count of
238 * characters (including NULL) in the path.
241 if(app_profile
!= 0) {
243 /* grab the name of the file out of its path */
244 /* we will need this for lookup within the */
246 ws_cache_name
= savedpath
+ savedpathlen
;
247 while (ws_cache_name
[0] != '/') {
248 if(ws_cache_name
== savedpath
) {
257 /* Save the name aside for future use */
258 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
260 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| SAVENAME
,
261 UIO_USERSPACE
, uap
->fname
, p
);
262 if ((error
= namei(&nd
)))
265 VOP_LEASE(vp
, p
, p
->p_ucred
, LEASE_READ
);
267 if ((error
= VOP_GETATTR(vp
, &origvattr
, p
->p_ucred
, p
)))
270 /* Check mount point */
271 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
277 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_flag
& P_TRACED
))
278 origvattr
.va_mode
&= ~(VSUID
| VSGID
);
280 *(&vattr
) = *(&origvattr
);
283 error
= check_exec_access(p
, vp
, &vattr
);
288 * Read in first few bytes of file for segment sizes, magic number:
289 * 407 = plain executable
291 * 413 = demand paged RO text
292 * Also an ASCII line beginning with #! is
293 * the file name of a ``shell'' and arguments may be prepended
294 * to the argument list if given here.
296 * SHELL NAMES ARE LIMITED IN LENGTH.
298 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
302 exdata
.ex_shell
[0] = '\0'; /* for zero length files */
304 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
, sizeof (exdata
), 0,
305 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
, p
);
311 if (resid
> sizeof(exdata
) - min(sizeof(exdata
.mach_header
),
312 sizeof(exdata
.fat_header
))
313 && exdata
.ex_shell
[0] != '#') {
318 mach_header
= &exdata
.mach_header
;
319 fat_header
= &exdata
.fat_header
;
320 if (mach_header
->magic
== MH_MAGIC
)
322 else if (fat_header
->magic
== FAT_MAGIC
||
323 fat_header
->magic
== FAT_CIGAM
)
325 else if (mach_header
->magic
== MH_CIGAM
) {
329 if (exdata
.ex_shell
[0] != '#' ||
330 exdata
.ex_shell
[1] != '!' ||
335 cp
= &exdata
.ex_shell
[2]; /* skip "#!" */
336 while (cp
< &exdata
.ex_shell
[SHSIZE
]) {
339 else if (*cp
== '\n') {
349 cp
= &exdata
.ex_shell
[2];
353 while (*cp
&& *cp
!= ' ')
362 bcopy((caddr_t
)cp
, (caddr_t
)cfarg
, SHSIZE
);
366 * Support for new app package launching for Mac OS X.
367 * We are about to retry the execve() by changing the path to the
368 * interpreter name. Need to re-initialize the savedpath and
369 * savedpathlen. +1 for NULL.
371 savedpathlen
= (cpnospace
- execnamep
+ 1);
372 error
= copystr(execnamep
, savedpath
, savedpathlen
, &savedpathlen
);
376 /* Save the name aside for future use */
377 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
381 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
382 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
383 (FOLLOW
| LOCKLEAF
| SAVENAME
);
384 nd
.ni_segflg
= UIO_SYSSPACE
;
385 nd
.ni_dirp
= execnamep
;
386 if ((error
= namei(&nd
)))
389 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
390 if ((error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)))
396 * Collect arguments on "file" in swap space.
403 * Support for new app package launching for Mac OS X allocates
404 * the "path" at the begining.
405 * execargs get allocated after that
407 cp
= (char *) execargsp
; /* running pointer for copy */
409 * size of execargs less sizeof "path",
410 * a pointer to "path" and a NULL poiter
412 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
414 * Copy arguments into file in argdev area.
416 if (uap
->argp
) for (;;) {
419 if (indir
&& na
== 0) {
420 sharg
= nd
.ni_cnd
.cn_nameptr
;
422 uap
->argp
++; /* ignore argv[0] */
423 } else if (indir
&& (na
== 1 && cfarg
[0])) {
426 } else if (indir
&& (na
== 1 || (na
== 2 && cfarg
[0])))
427 ap
= (int)uap
->fname
;
428 else if (uap
->argp
) {
429 ap
= fuword((caddr_t
)uap
->argp
);
432 if (ap
== NULL
&& uap
->envp
) {
434 if ((ap
= fuword((caddr_t
)uap
->envp
)) != NULL
)
445 if (nc
>= (NCARGS
- savedpathlen
- 2*NBPW
-1)) {
450 error
= copystr(sharg
, cp
, (unsigned)cc
, &len
);
453 error
= copyinstr((caddr_t
)ap
, cp
, (unsigned)cc
,
460 } while (error
== ENAMETOOLONG
);
465 nc
= (nc
+ NBPW
-1) & ~(NBPW
-1);
468 * If we have a fat file, find "our" executable.
472 * Look up our architecture in the fat file.
474 lret
= fatfile_getarch(vp
, (vm_offset_t
)fat_header
, &fat_arch
);
475 if (lret
!= LOAD_SUCCESS
) {
476 error
= load_return_to_errno(lret
);
479 /* Read the Mach-O header out of it */
480 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
.mach_header
,
481 sizeof (exdata
.mach_header
),
483 UIO_SYSSPACE
, (IO_UNIT
|IO_NODELOCKED
), cred
, &resid
, p
);
489 /* Did we read a complete header? */
495 /* Is what we found a Mach-O executable */
496 if (mach_header
->magic
!= MH_MAGIC
) {
501 arch_offset
= fat_arch
.offset
;
502 arch_size
= fat_arch
.size
;
505 * Load the Mach-O file.
508 arch_size
= (u_long
)vattr
.va_size
;
512 kern_return_t result
;
514 result
= task_create_local(task
, FALSE
, FALSE
, &new_task
);
515 if (result
!= KERN_SUCCESS
)
516 printf("execve: task_create failed. Code: 0x%x\n", result
);
518 set_bsdtask_info(new_task
, p
);
522 map
= get_task_map(new_task
);
523 result
= thread_create(new_task
, &thr_act
);
524 if (result
!= KERN_SUCCESS
)
525 printf("execve: thread_create failed. Code: 0x%x\n", result
);
526 uthread
= get_bsdthread_info(thr_act
);
533 * Load the Mach-O file.
535 VOP_UNLOCK(vp
, 0, p
);
537 tws_handle_startup_file(task
, cred
->cr_uid
,
538 ws_cache_name
, vp
, &clean_regions
);
541 lret
= load_machfile(vp
, mach_header
, arch_offset
,
542 arch_size
, &load_result
, thr_act
, map
, clean_regions
);
544 if (lret
!= LOAD_SUCCESS
) {
545 error
= load_return_to_errno(lret
);
549 /* load_machfile() maps the vnode */
553 * deal with set[ug]id.
555 p
->p_flag
&= ~P_SUGID
;
556 if (((origvattr
.va_mode
& VSUID
) != 0 &&
557 p
->p_ucred
->cr_uid
!= origvattr
.va_uid
)
558 || (origvattr
.va_mode
& VSGID
) != 0 &&
559 p
->p_ucred
->cr_gid
!= origvattr
.va_gid
) {
560 p
->p_ucred
= crcopy(cred
);
563 * If process is being ktraced, turn off - unless
566 if (p
->p_tracep
&& !(p
->p_traceflag
& KTRFAC_ROOT
)) {
567 struct vnode
*tvp
= p
->p_tracep
;
573 if (origvattr
.va_mode
& VSUID
)
574 p
->p_ucred
->cr_uid
= origvattr
.va_uid
;
575 if (origvattr
.va_mode
& VSGID
)
576 p
->p_ucred
->cr_gid
= origvattr
.va_gid
;
578 set_security_token(p
);
579 p
->p_flag
|= P_SUGID
;
581 /* Radar 2261856; setuid security hole fix */
582 /* Patch from OpenBSD: A. Ramesh */
584 * XXX For setuid processes, attempt to ensure that
585 * stdin, stdout, and stderr are already allocated.
586 * We do not want userland to accidentally allocate
587 * descriptors in this range which has implied meaning
590 for (i
= 0; i
< 3; i
++) {
591 extern struct fileops vnops
;
592 struct nameidata nd1
;
596 if (p
->p_fd
->fd_ofiles
[i
] == NULL
) {
597 if ((error
= falloc(p
, &fp
, &indx
)) != 0)
599 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
,
601 if ((error
= vn_open(&nd1
, FREAD
, 0)) != 0) {
603 p
->p_fd
->fd_ofiles
[indx
] = NULL
;
607 fp
->f_type
= DTYPE_VNODE
;
609 fp
->f_data
= (caddr_t
)nd1
.ni_vp
;
610 VOP_UNLOCK(nd1
.ni_vp
, 0, p
);
614 p
->p_cred
->p_svuid
= p
->p_ucred
->cr_uid
;
615 p
->p_cred
->p_svgid
= p
->p_ucred
->cr_gid
;
617 if (!vfexec
&& (p
->p_flag
& P_TRACED
))
623 VOP_LOCK(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
627 if (load_result
.unixproc
&&
628 create_unix_stack(get_task_map(task
),
629 load_result
.user_stack
, load_result
.customstack
, p
)) {
630 error
= load_return_to_errno(LOAD_NOSPACE
);
635 uthread
->uu_ar0
= (void *)get_user_regs(thr_act
);
639 * Copy back arglist if necessary.
645 old_map
= vm_map_switch(get_task_map(task
));
647 if (load_result
.unixproc
) {
650 ucp
= ucp
- nc
- NBPW
; /* begining of the STRING AREA */
653 * Support for new app package launching for Mac OS X allocates
654 * the "path" at the begining of the execargs buffer.
655 * copy it just before the string area.
657 savedpathlen
= (savedpathlen
+ NBPW
-1) & ~(NBPW
-1);
659 pathptr
= ucp
- savedpathlen
;
660 error
= copyoutstr(savedpath
, (caddr_t
)pathptr
,
661 (unsigned)savedpathlen
, &len
);
664 vm_map_switch(old_map
);
668 /* Save a NULL pointer below it */
669 (void) suword((caddr_t
)(pathptr
- NBPW
), 0);
671 /* Save the pointer to "path" just below it */
672 (void) suword((caddr_t
)(pathptr
- 2*NBPW
), pathptr
);
675 * na includes arg[] and env[].
676 * NBPW for 2 NULL one each ofter arg[argc -1] and env[n]
678 * skip over saved path, NBPW for pointer to path,
679 * and NBPW for the NULL after pointer to path.
681 ap
= ucp
- na
*NBPW
- 3*NBPW
- savedpathlen
- 2*NBPW
;
683 thread_setuserstack(thr_act
, ap
); /* Set the stack */
685 uthread
->uu_ar0
[SP
] = ap
;
687 (void) suword((caddr_t
)ap
, na
-ne
); /* argc */
691 cp
= (char *) execargsp
;
692 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
693 ps
.ps_argvstr
= (char *)ucp
; /* first argv string */
694 ps
.ps_nargvstr
= na
- ne
; /* argc */
698 (void) suword((caddr_t
)ap
, 0);
700 ps
.ps_envstr
= (char *)ucp
;
705 (void) suword((caddr_t
)ap
, ucp
);
707 error
= copyoutstr(cp
, (caddr_t
)ucp
,
713 } while (error
== ENAMETOOLONG
);
715 break; /* bad stack - user's problem */
717 (void) suword((caddr_t
)ap
, 0);
720 if (load_result
.dynlinker
) {
722 ap
= thread_adjuserstack(thr_act
, -4); /* Adjust the stack */
724 ap
= uthread
->uu_ar0
[SP
] -= 4;
726 (void) suword((caddr_t
)ap
, load_result
.mach_header
);
730 vm_map_switch(old_map
);
733 thread_setentrypoint(thr_act
, load_result
.entry_point
); /* Set the entry point */
735 uthread
->uu_ar0
[PC
] = load_result
.entry_point
;
737 #error architecture not implemented!
744 * Reset signal state.
746 execsigs(p
, thr_act
);
749 * Close file descriptors
750 * which specify close-on-exec.
753 /* FIXME: Till vmspace inherit is fixed: */
754 if (!vfexec
&& p
->vm_shm
)
756 /* Clean up the semaphores */
760 * Remember file name for accounting.
762 p
->p_acflag
&= ~AFORK
;
763 if (nd
.ni_cnd
.cn_namelen
> MAXCOMLEN
)
764 nd
.ni_cnd
.cn_namelen
= MAXCOMLEN
;
765 bcopy((caddr_t
)nd
.ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
766 (unsigned)nd
.ni_cnd
.cn_namelen
);
767 p
->p_comm
[nd
.ni_cnd
.cn_namelen
] = '\0';
770 /* This is for kdebug */
771 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
773 /* Collect the pathname for tracing */
774 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
777 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
778 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, getshuttle_thread(thr_act
));
780 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
781 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
785 * mark as execed, wakeup the process that vforked (if any) and tell
786 * it that it now has it's own resources back
789 if (p
->p_pptr
&& (p
->p_flag
& P_PPWAIT
)) {
790 p
->p_flag
&= ~P_PPWAIT
;
791 wakeup((caddr_t
)p
->p_pptr
);
794 if (vfexec
&& (p
->p_flag
& P_TRACED
)) {
795 psignal_vfork(p
, new_task
, thr_act
, SIGTRAP
);
800 task_deallocate(new_task
);
801 act_deallocate(thr_act
);
806 FREE_ZONE(nd
.ni_cnd
.cn_pnbuf
, nd
.ni_cnd
.cn_pnlen
, M_NAMEI
);
811 execargs_free(execargs
);
812 if (!error
&& vfexec
) {
813 vfork_return(current_act(), p
->p_pptr
, p
, retval
);
814 (void) thread_resume(thr_act
);
821 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
824 create_unix_stack(map
, user_stack
, customstack
, p
)
826 vm_offset_t user_stack
;
833 p
->user_stack
= user_stack
;
835 size
= round_page_64(unix_stack_size(p
));
836 addr
= trunc_page_32(user_stack
- size
);
837 return (vm_allocate(map
,&addr
, size
, FALSE
));
839 return(KERN_SUCCESS
);
842 #include <sys/reboot.h>
844 char init_program_name
[128] = "/sbin/mach_init\0";
846 char init_args
[128] = "";
848 struct execve_args init_exec_args
;
849 int init_attempts
= 0;
856 vm_offset_t init_addr
;
860 register_t retval
[2];
865 /* init_args are copied in string form directly from bootstrap */
868 if (boothowto
& RB_INITNAME
) {
869 printf("init program? ");
871 gets(init_program_name
, init_program_name
);
875 if (error
&& ((boothowto
& RB_INITNAME
) == 0) &&
876 (init_attempts
== 1)) {
877 static char other_init
[] = "/etc/mach_init";
878 printf("Load of %s, errno %d, trying %s\n",
879 init_program_name
, error
, other_init
);
881 bcopy(other_init
, init_program_name
,
888 printf("Load of %s failed, errno %d\n",
889 init_program_name
, error
);
891 boothowto
|= RB_INITNAME
;
896 * Copy out program name.
899 init_addr
= VM_MIN_ADDRESS
;
900 (void) vm_allocate(current_map(), &init_addr
,
904 (void) copyout((caddr_t
) init_program_name
,
905 (caddr_t
) (init_addr
),
906 (unsigned) sizeof(init_program_name
)+1);
908 argv
[0] = (char *) init_addr
;
909 init_addr
+= sizeof(init_program_name
);
910 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
913 * Put out first (and only) argument, similarly.
914 * Assumes everything fits in a page as allocated
918 (void) copyout((caddr_t
) init_args
,
919 (caddr_t
) (init_addr
),
920 (unsigned) sizeof(init_args
));
922 argv
[1] = (char *) init_addr
;
923 init_addr
+= sizeof(init_args
);
924 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
927 * Null-end the argument list
930 argv
[2] = (char *) 0;
933 * Copy out the argument list.
936 (void) copyout((caddr_t
) argv
,
937 (caddr_t
) (init_addr
),
938 (unsigned) sizeof(argv
));
941 * Set up argument block for fake call to execve.
944 init_exec_args
.fname
= argv
[0];
945 init_exec_args
.argp
= (char **) init_addr
;
946 init_exec_args
.envp
= 0;
948 /* So that mach_init task
949 * is set with uid,gid 0 token
951 set_security_token(p
);
953 error
= execve(p
,&init_exec_args
,retval
);
958 * Convert a load_return_t to an errno.
961 load_return_to_errno(load_return_t lrtn
)
984 * exec_check_access()
987 check_exec_access(p
, vp
, vap
)
995 if (error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
, p
))
998 if (flag
& P_TRACED
) {
999 if (error
= VOP_ACCESS(vp
, VREAD
, p
->p_ucred
, p
))
1002 if (vp
->v_type
!= VREG
||
1003 (vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)
1008 #include <mach/mach_types.h>
1009 #include <mach/vm_prot.h>
1010 #include <mach/semaphore.h>
1011 #include <mach/sync_policy.h>
1012 #include <kern/clock.h>
1013 #include <mach/kern_return.h>
1015 extern semaphore_t execve_semaphore
;
1018 execargs_alloc(addrp
)
1023 kret
= semaphore_wait(execve_semaphore
);
1024 if (kret
!= KERN_SUCCESS
)
1028 case KERN_INVALID_ADDRESS
:
1029 case KERN_PROTECTION_FAILURE
:
1032 case KERN_OPERATION_TIMED_OUT
:
1036 kret
= kmem_alloc_pageable(bsd_pageable_map
, addrp
, NCARGS
);
1037 if (kret
!= KERN_SUCCESS
)
1049 kmem_free(bsd_pageable_map
, addr
, NCARGS
);
1051 kret
= semaphore_signal(execve_semaphore
);
1053 case KERN_INVALID_ADDRESS
:
1054 case KERN_PROTECTION_FAILURE
:
1057 case KERN_OPERATION_TIMED_OUT
: