]>
Commit | Line | Data |
---|---|---|
e91b9f68 A |
1 | /* |
2 | * Copyright (c) 2002 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
11 | * | |
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 | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | ||
23 | #include <mach/mach.h> | |
24 | #include <mach/mach_error.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
29 | ||
30 | #include <servers/bootstrap.h> | |
31 | ||
32 | int main(int argc, char *argv[]) | |
33 | { | |
34 | mach_port_t root_bootstrap_port; | |
35 | kern_return_t ret; | |
36 | #if 0 | |
37 | task_t init_task; | |
38 | int err; | |
39 | #endif | |
40 | ||
41 | if (argc < 2) { | |
42 | fprintf(stderr, "usage: %s executable [args...]\n", argv[0]); | |
43 | exit(1); | |
44 | } | |
45 | ||
46 | if (geteuid() != 0) { | |
47 | fprintf(stderr, "%s: permission denied: must be run as root\n", argv[0]); | |
48 | return(EPERM); | |
49 | exit(1); | |
50 | } | |
51 | ||
52 | #if 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", | |
57 | argv[0]); | |
58 | exit(1); | |
59 | } | |
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)); | |
65 | exit(2); | |
66 | } | |
67 | #else | |
68 | /* | |
69 | * Keep looping, getting out bootstrap's parent until it repeats, then we | |
70 | * know we are at the root/startupItem context. | |
71 | */ | |
72 | { | |
73 | mach_port_t cur_bootstrap; | |
74 | ||
75 | root_bootstrap_port = bootstrap_port; | |
76 | do { | |
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]); | |
81 | exit(1); | |
82 | } | |
83 | } while (root_bootstrap_port != cur_bootstrap); | |
84 | } | |
85 | #endif | |
86 | ||
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)); | |
92 | exit(3); | |
93 | } | |
94 | ||
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); | |
98 | return(4); | |
99 | } |