2 * Copyright (c) 2002 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@
23 #include <mach/mach.h>
24 #include <mach/mach_error.h>
30 #include <servers/bootstrap.h>
32 int main(int argc
, char *argv
[])
34 mach_port_t root_bootstrap_port
;
42 fprintf(stderr
, "usage: %s executable [args...]\n", argv
[0]);
47 fprintf(stderr
, "%s: permission denied: must be run as root\n", argv
[0]);
53 /* get init's task port */
54 ret
= task_for_pid((mach_task_self
)(), 1, &init_task
);
55 if (ret
!= KERN_SUCCESS
) {
56 fprintf(stderr
, "%s: task_for_pid() failed: permission denied\n",
60 /* extract its bootstrap port, which should be the root */
61 ret
= task_get_bootstrap_port(init_task
, &root_bootstrap_port
);
62 if (ret
!= KERN_SUCCESS
) {
63 fprintf(stderr
, "%s: task_get_bootstrap_port() failed: %s\n",
64 argv
[0], mach_error_string(ret
));
69 * Keep looping, getting out bootstrap's parent until it repeats, then we
70 * know we are at the root/startupItem context.
73 mach_port_t cur_bootstrap
;
75 root_bootstrap_port
= bootstrap_port
;
77 cur_bootstrap
= root_bootstrap_port
;
78 ret
= bootstrap_parent(cur_bootstrap
, &root_bootstrap_port
);
79 if (ret
== BOOTSTRAP_NOT_PRIVILEGED
) {
80 fprintf(stderr
, "%s: must be run as root\n", argv
[0]);
83 } while (root_bootstrap_port
!= cur_bootstrap
);
87 /* set that as our bootstrap port */
88 ret
= task_set_bootstrap_port(mach_task_self(), root_bootstrap_port
);
89 if (ret
!= KERN_SUCCESS
) {
90 fprintf(stderr
, "%s: task_set_bootstrap_port() failed: %s\n",
91 argv
[0], mach_error_string(ret
));
95 /* exec the program called for */
96 execv(argv
[1], &argv
[1]); /* should not return */
97 fprintf(stderr
, "%s: exec failed: %s(%d)\n", argv
[0], strerror(errno
), errno
);