]>
Commit | Line | Data |
---|---|---|
e91b9f68 A |
1 | /* |
2 | * Copyright (c) 2005 Apple Computer, Inc. All rights reserved. | |
3 | * | |
ed34e3c3 | 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ |
e91b9f68 | 5 | * |
ed34e3c3 A |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at | |
e91b9f68 | 9 | * |
ed34e3c3 A |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
e91b9f68 A |
16 | * limitations under the License. |
17 | * | |
ed34e3c3 | 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ |
e91b9f68 | 19 | */ |
ed34e3c3 | 20 | |
5b0a4722 A |
21 | static const char *const __rcs_file_version__ = "$Revision: 23408 $"; |
22 | ||
23 | #include "config.h" | |
24 | #include "launchd.h" | |
ed34e3c3 | 25 | |
e91b9f68 A |
26 | #include <Security/Authorization.h> |
27 | #include <Security/AuthorizationTags.h> | |
28 | #include <Security/AuthSession.h> | |
e91b9f68 A |
29 | #include <sys/types.h> |
30 | #include <sys/queue.h> | |
31 | #include <sys/event.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/ucred.h> | |
34 | #include <sys/fcntl.h> | |
35 | #include <sys/un.h> | |
36 | #include <sys/wait.h> | |
37 | #include <sys/sysctl.h> | |
38 | #include <sys/sockio.h> | |
39 | #include <sys/time.h> | |
40 | #include <sys/resource.h> | |
41 | #include <sys/ioctl.h> | |
42 | #include <sys/mount.h> | |
ed34e3c3 | 43 | #include <sys/kern_event.h> |
5b0a4722 | 44 | #include <sys/reboot.h> |
e91b9f68 A |
45 | #include <net/if.h> |
46 | #include <netinet/in.h> | |
47 | #include <netinet/in_var.h> | |
48 | #include <netinet6/nd6.h> | |
ed34e3c3 | 49 | #include <ifaddrs.h> |
e91b9f68 A |
50 | #include <unistd.h> |
51 | #include <signal.h> | |
52 | #include <errno.h> | |
e91b9f68 A |
53 | #include <libgen.h> |
54 | #include <stdio.h> | |
55 | #include <stdlib.h> | |
56 | #include <stdarg.h> | |
57 | #include <stdbool.h> | |
e91b9f68 A |
58 | #include <paths.h> |
59 | #include <pwd.h> | |
60 | #include <grp.h> | |
ed34e3c3 | 61 | #include <ttyent.h> |
e91b9f68 A |
62 | #include <dlfcn.h> |
63 | #include <dirent.h> | |
ed34e3c3 | 64 | #include <string.h> |
5b0a4722 A |
65 | #include <setjmp.h> |
66 | #include <spawn.h> | |
67 | #include <sched.h> | |
e91b9f68 | 68 | |
5b0a4722 A |
69 | #include "libbootstrap_public.h" |
70 | #include "libvproc_public.h" | |
71 | #include "libvproc_internal.h" | |
72 | #include "liblaunch_public.h" | |
73 | ||
74 | #include "launchd_runtime.h" | |
ed34e3c3 A |
75 | #include "launchd_core_logic.h" |
76 | #include "launchd_unix_ipc.h" | |
77 | ||
e91b9f68 | 78 | #define LAUNCHD_CONF ".launchd.conf" |
e91b9f68 | 79 | #define SECURITY_LIB "/System/Library/Frameworks/Security.framework/Versions/A/Security" |
5b0a4722 A |
80 | #define SHUTDOWN_LOG_DIR "/var/log/shutdown" |
81 | ||
e91b9f68 A |
82 | |
83 | extern char **environ; | |
84 | ||
ed34e3c3 | 85 | static void pfsystem_callback(void *, struct kevent *); |
e91b9f68 | 86 | |
ed34e3c3 A |
87 | static kq_callback kqpfsystem_callback = pfsystem_callback; |
88 | ||
5b0a4722 | 89 | static void pid1_magic_init(void); |
e91b9f68 | 90 | |
aa59983a | 91 | static void testfd_or_openfd(int fd, const char *path, int flags); |
ed34e3c3 A |
92 | static bool get_network_state(void); |
93 | static void monitor_networking_state(void); | |
5b0a4722 A |
94 | static void fatal_signal_handler(int sig, siginfo_t *si, void *uap); |
95 | static void handle_pid1_crashes_separately(void); | |
96 | static void prep_shutdown_log_dir(void); | |
ed34e3c3 | 97 | |
ed34e3c3 | 98 | static bool re_exec_in_single_user_mode = false; |
5b0a4722 A |
99 | static void *crash_addr; |
100 | static pid_t crash_pid; | |
ed34e3c3 | 101 | |
5b0a4722 A |
102 | static bool shutdown_in_progress = false; |
103 | bool debug_shutdown_hangs = false; | |
ed34e3c3 | 104 | bool network_up = false; |
e91b9f68 | 105 | |
ed34e3c3 A |
106 | int |
107 | main(int argc, char *const *argv) | |
e91b9f68 | 108 | { |
5b0a4722 A |
109 | bool sflag = false; |
110 | int ch; | |
fc89531e | 111 | |
5b0a4722 A |
112 | testfd_or_openfd(STDIN_FILENO, _PATH_DEVNULL, O_RDONLY); |
113 | testfd_or_openfd(STDOUT_FILENO, _PATH_DEVNULL, O_WRONLY); | |
114 | testfd_or_openfd(STDERR_FILENO, _PATH_DEVNULL, O_WRONLY); | |
e91b9f68 | 115 | |
5b0a4722 | 116 | while ((ch = getopt(argc, argv, "s")) != -1) { |
e91b9f68 | 117 | switch (ch) { |
5b0a4722 | 118 | case 's': sflag = true; break; /* single user */ |
ed34e3c3 | 119 | case '?': /* we should do something with the global optopt variable here */ |
e91b9f68 | 120 | default: |
5b0a4722 | 121 | fprintf(stderr, "%s: ignoring unknown arguments\n", getprogname()); |
e91b9f68 A |
122 | break; |
123 | } | |
124 | } | |
e91b9f68 | 125 | |
5b0a4722 A |
126 | if (getpid() != 1 && getppid() != 1) { |
127 | fprintf(stderr, "%s: This program is not meant to be run directly.\n", getprogname()); | |
128 | exit(EXIT_FAILURE); | |
e91b9f68 A |
129 | } |
130 | ||
5b0a4722 | 131 | launchd_runtime_init(); |
e91b9f68 | 132 | |
5b0a4722 | 133 | if (NULL == getenv("PATH")) { |
ed34e3c3 | 134 | setenv("PATH", _PATH_STDPATH, 1); |
5b0a4722 | 135 | } |
aa59983a | 136 | |
ed34e3c3 | 137 | if (getpid() == 1) { |
5b0a4722 | 138 | pid1_magic_init(); |
ed34e3c3 | 139 | } else { |
5b0a4722 | 140 | ipc_server_init(); |
aa59983a A |
141 | } |
142 | ||
ed34e3c3 | 143 | monitor_networking_state(); |
e91b9f68 | 144 | |
5b0a4722 A |
145 | if (getpid() == 1) { |
146 | handle_pid1_crashes_separately(); | |
e91b9f68 A |
147 | } |
148 | ||
5b0a4722 | 149 | jobmgr_init(sflag); |
e91b9f68 | 150 | |
5b0a4722 | 151 | launchd_runtime_init2(); |
e91b9f68 | 152 | |
5b0a4722 | 153 | launchd_runtime(); |
e91b9f68 | 154 | } |
e91b9f68 | 155 | |
5b0a4722 A |
156 | void |
157 | handle_pid1_crashes_separately(void) | |
e91b9f68 | 158 | { |
5b0a4722 | 159 | struct sigaction fsa; |
ab36757d | 160 | |
5b0a4722 A |
161 | fsa.sa_sigaction = fatal_signal_handler; |
162 | fsa.sa_flags = SA_SIGINFO; | |
163 | sigemptyset(&fsa.sa_mask); | |
e91b9f68 | 164 | |
5b0a4722 A |
165 | launchd_assumes(sigaction(SIGILL, &fsa, NULL) != -1); |
166 | launchd_assumes(sigaction(SIGFPE, &fsa, NULL) != -1); | |
167 | launchd_assumes(sigaction(SIGBUS, &fsa, NULL) != -1); | |
168 | launchd_assumes(sigaction(SIGSEGV, &fsa, NULL) != -1); | |
e91b9f68 A |
169 | } |
170 | ||
5b0a4722 | 171 | #define PID1_CRASH_LOGFILE "/var/log/launchd-pid1.crash" |
e91b9f68 | 172 | |
5b0a4722 A |
173 | /* This hack forces the dynamic linker to resolve these symbols ASAP */ |
174 | static __attribute__((unused)) typeof(sync) *__junk_dyld_trick1 = sync; | |
175 | static __attribute__((unused)) typeof(sleep) *__junk_dyld_trick2 = sleep; | |
176 | static __attribute__((unused)) typeof(reboot) *__junk_dyld_trick3 = reboot; | |
ab36757d | 177 | |
5b0a4722 A |
178 | void |
179 | fatal_signal_handler(int sig, siginfo_t *si, void *uap) | |
180 | { | |
181 | const char *doom_why = "at instruction"; | |
182 | char *sample_args[] = { "/usr/bin/sample", "1", "1", "-file", PID1_CRASH_LOGFILE, NULL }; | |
183 | pid_t sample_p; | |
184 | int wstatus; | |
e91b9f68 | 185 | |
5b0a4722 A |
186 | crash_addr = si->si_addr; |
187 | crash_pid = si->si_pid; | |
ab36757d | 188 | |
5b0a4722 | 189 | unlink(PID1_CRASH_LOGFILE); |
e91b9f68 | 190 | |
5b0a4722 A |
191 | switch ((sample_p = vfork())) { |
192 | case 0: | |
193 | execve(sample_args[0], sample_args, environ); | |
194 | _exit(EXIT_FAILURE); | |
195 | break; | |
196 | default: | |
197 | waitpid(sample_p, &wstatus, 0); | |
198 | break; | |
199 | case -1: | |
200 | break; | |
e91b9f68 A |
201 | } |
202 | ||
5b0a4722 A |
203 | switch (sig) { |
204 | default: | |
205 | case 0: | |
206 | break; | |
207 | case SIGBUS: | |
208 | case SIGSEGV: | |
209 | doom_why = "trying to read/write"; | |
210 | case SIGILL: | |
211 | case SIGFPE: | |
212 | runtime_syslog(LOG_EMERG, "We crashed %s: %p (sent by PID %u)", doom_why, crash_addr, crash_pid); | |
213 | sync(); | |
214 | sleep(3); | |
215 | reboot(0); | |
216 | break; | |
e91b9f68 | 217 | } |
ed34e3c3 | 218 | } |
e91b9f68 | 219 | |
ed34e3c3 | 220 | void |
5b0a4722 | 221 | pid1_magic_init(void) |
ed34e3c3 A |
222 | { |
223 | launchd_assumes(setsid() != -1); | |
224 | launchd_assumes(chdir("/") != -1); | |
225 | launchd_assumes(setlogin("root") != -1); | |
226 | launchd_assumes(mount("fdesc", "/dev", MNT_UNION, NULL) != -1); | |
e91b9f68 A |
227 | } |
228 | ||
e91b9f68 | 229 | |
ed34e3c3 A |
230 | int |
231 | _fd(int fd) | |
e91b9f68 | 232 | { |
5b0a4722 | 233 | if (fd >= 0) { |
ed34e3c3 | 234 | launchd_assumes(fcntl(fd, F_SETFD, 1) != -1); |
5b0a4722 | 235 | } |
ed34e3c3 A |
236 | return fd; |
237 | } | |
fc89531e | 238 | |
ed34e3c3 | 239 | void |
5b0a4722 | 240 | prep_shutdown_log_dir(void) |
ed34e3c3 | 241 | { |
5b0a4722 | 242 | launchd_assumes(mkdir(SHUTDOWN_LOG_DIR, S_IRWXU) != -1 || errno == EEXIST); |
e91b9f68 A |
243 | } |
244 | ||
ed34e3c3 A |
245 | void |
246 | launchd_shutdown(void) | |
e91b9f68 | 247 | { |
5b0a4722 A |
248 | struct stat sb; |
249 | ||
250 | if (shutdown_in_progress) { | |
251 | return; | |
252 | } | |
ed34e3c3 | 253 | |
5b0a4722 | 254 | shutdown_in_progress = true; |
fc89531e | 255 | |
5b0a4722 A |
256 | if (getpid() == 1 && stat("/var/db/debugShutdownHangs", &sb) != -1) { |
257 | /* | |
258 | * When this changes to a more sustainable API, update this: | |
259 | * http://howto.apple.com/db.cgi?Debugging_Apps_Non-Responsive_At_Shutdown | |
260 | */ | |
261 | runtime_setlogmask(LOG_UPTO(LOG_DEBUG)); | |
262 | prep_shutdown_log_dir(); | |
263 | debug_shutdown_hangs = true; | |
264 | } | |
fc89531e | 265 | |
5b0a4722 | 266 | launchd_assert(jobmgr_shutdown(root_jobmgr) != NULL); |
e91b9f68 A |
267 | } |
268 | ||
ed34e3c3 A |
269 | void |
270 | launchd_single_user(void) | |
e91b9f68 | 271 | { |
5b0a4722 | 272 | runtime_syslog(LOG_NOTICE, "Going to single-user mode"); |
fc89531e | 273 | |
ed34e3c3 | 274 | re_exec_in_single_user_mode = true; |
e91b9f68 | 275 | |
5b0a4722 | 276 | launchd_shutdown(); |
e91b9f68 | 277 | |
5b0a4722 | 278 | sleep(3); |
e91b9f68 | 279 | |
5b0a4722 | 280 | runtime_kill(-1, SIGKILL); |
e91b9f68 A |
281 | } |
282 | ||
ed34e3c3 A |
283 | void |
284 | launchd_SessionCreate(void) | |
e91b9f68 | 285 | { |
ed34e3c3 A |
286 | OSStatus (*sescr)(SessionCreationFlags flags, SessionAttributeBits attributes); |
287 | void *seclib; | |
e91b9f68 | 288 | |
ed34e3c3 | 289 | if (launchd_assumes((seclib = dlopen(SECURITY_LIB, RTLD_LAZY)) != NULL)) { |
5b0a4722 | 290 | if (launchd_assumes((sescr = dlsym(seclib, "SessionCreate")) != NULL)) { |
ed34e3c3 | 291 | launchd_assumes(sescr(0, 0) == noErr); |
5b0a4722 | 292 | } |
ed34e3c3 | 293 | launchd_assumes(dlclose(seclib) != -1); |
e91b9f68 A |
294 | } |
295 | } | |
296 | ||
ed34e3c3 A |
297 | void |
298 | testfd_or_openfd(int fd, const char *path, int flags) | |
e91b9f68 | 299 | { |
ed34e3c3 | 300 | int tmpfd; |
e91b9f68 | 301 | |
ed34e3c3 | 302 | if (-1 != (tmpfd = dup(fd))) { |
5b0a4722 | 303 | launchd_assumes(runtime_close(tmpfd) == 0); |
ed34e3c3 | 304 | } else { |
5b0a4722 A |
305 | if (-1 == (tmpfd = open(path, flags | O_NOCTTY, DEFFILEMODE))) { |
306 | runtime_syslog(LOG_ERR, "open(\"%s\", ...): %m", path); | |
ed34e3c3 A |
307 | } else if (tmpfd != fd) { |
308 | launchd_assumes(dup2(tmpfd, fd) != -1); | |
5b0a4722 | 309 | launchd_assumes(runtime_close(tmpfd) == 0); |
e91b9f68 | 310 | } |
e91b9f68 A |
311 | } |
312 | } | |
313 | ||
ed34e3c3 A |
314 | bool |
315 | get_network_state(void) | |
316 | { | |
317 | struct ifaddrs *ifa, *ifai; | |
318 | bool up = false; | |
5b0a4722 A |
319 | int r; |
320 | ||
321 | /* Workaround 4978696: getifaddrs() reports false ENOMEM */ | |
322 | while ((r = getifaddrs(&ifa)) == -1 && errno == ENOMEM) { | |
323 | runtime_syslog(LOG_DEBUG, "Worked around bug: 4978696"); | |
324 | launchd_assumes(sched_yield() != -1); | |
325 | } | |
ed34e3c3 | 326 | |
5b0a4722 | 327 | if (!launchd_assumes(r != -1)) { |
ed34e3c3 | 328 | return network_up; |
5b0a4722 | 329 | } |
ed34e3c3 A |
330 | |
331 | for (ifai = ifa; ifai; ifai = ifai->ifa_next) { | |
5b0a4722 | 332 | if (!(ifai->ifa_flags & IFF_UP)) { |
ed34e3c3 | 333 | continue; |
5b0a4722 A |
334 | } |
335 | if (ifai->ifa_flags & IFF_LOOPBACK) { | |
ed34e3c3 | 336 | continue; |
5b0a4722 A |
337 | } |
338 | if (ifai->ifa_addr->sa_family != AF_INET && ifai->ifa_addr->sa_family != AF_INET6) { | |
ed34e3c3 | 339 | continue; |
5b0a4722 | 340 | } |
ed34e3c3 | 341 | up = true; |
e91b9f68 A |
342 | break; |
343 | } | |
ed34e3c3 A |
344 | |
345 | freeifaddrs(ifa); | |
346 | ||
347 | return up; | |
e91b9f68 A |
348 | } |
349 | ||
ed34e3c3 A |
350 | void |
351 | monitor_networking_state(void) | |
e91b9f68 | 352 | { |
ed34e3c3 A |
353 | int pfs = _fd(socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT)); |
354 | struct kev_request kev_req; | |
e91b9f68 | 355 | |
ed34e3c3 | 356 | network_up = get_network_state(); |
e91b9f68 | 357 | |
5b0a4722 | 358 | if (!launchd_assumes(pfs != -1)) { |
ed34e3c3 | 359 | return; |
5b0a4722 | 360 | } |
e91b9f68 | 361 | |
ed34e3c3 A |
362 | memset(&kev_req, 0, sizeof(kev_req)); |
363 | kev_req.vendor_code = KEV_VENDOR_APPLE; | |
364 | kev_req.kev_class = KEV_NETWORK_CLASS; | |
e91b9f68 | 365 | |
ed34e3c3 | 366 | if (!launchd_assumes(ioctl(pfs, SIOCSKEVFILT, &kev_req) != -1)) { |
5b0a4722 | 367 | runtime_close(pfs); |
e91b9f68 | 368 | return; |
e91b9f68 A |
369 | } |
370 | ||
ed34e3c3 | 371 | launchd_assumes(kevent_mod(pfs, EVFILT_READ, EV_ADD, 0, 0, &kqpfsystem_callback) != -1); |
e91b9f68 A |
372 | } |
373 | ||
ed34e3c3 A |
374 | void |
375 | pfsystem_callback(void *obj, struct kevent *kev) | |
e91b9f68 | 376 | { |
ed34e3c3 A |
377 | bool new_networking_state; |
378 | char buf[1024]; | |
e91b9f68 | 379 | |
ed34e3c3 | 380 | launchd_assumes(read(kev->ident, &buf, sizeof(buf)) != -1); |
e91b9f68 | 381 | |
ed34e3c3 | 382 | new_networking_state = get_network_state(); |
e91b9f68 | 383 | |
ed34e3c3 A |
384 | if (new_networking_state != network_up) { |
385 | network_up = new_networking_state; | |
5b0a4722 | 386 | jobmgr_dispatch_all_semaphores(root_jobmgr); |
2a27bb34 | 387 | } |
e91b9f68 A |
388 | } |
389 | ||
6a39f10b | 390 | void |
ed34e3c3 | 391 | _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test) |
6a39f10b | 392 | { |
ed34e3c3 A |
393 | int saved_errno = errno; |
394 | char buf[100]; | |
395 | const char *file = strrchr(path, '/'); | |
396 | char *rcs_rev_tmp = strchr(rcs_rev, ' '); | |
6a39f10b | 397 | |
ed34e3c3 A |
398 | if (!file) { |
399 | file = path; | |
6a39f10b | 400 | } else { |
ed34e3c3 | 401 | file += 1; |
e91b9f68 | 402 | } |
aa59983a | 403 | |
ed34e3c3 A |
404 | if (!rcs_rev_tmp) { |
405 | strlcpy(buf, rcs_rev, sizeof(buf)); | |
aa59983a | 406 | } else { |
ed34e3c3 A |
407 | strlcpy(buf, rcs_rev_tmp + 1, sizeof(buf)); |
408 | rcs_rev_tmp = strchr(buf, ' '); | |
5b0a4722 | 409 | if (rcs_rev_tmp) { |
ed34e3c3 | 410 | *rcs_rev_tmp = '\0'; |
2a27bb34 | 411 | } |
2a27bb34 A |
412 | } |
413 | ||
5b0a4722 | 414 | runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); |
fc89531e | 415 | } |