1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
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
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20 /*-
21 * Copyright (c) 1991, 1993
22 * The Regents of the University of California. All rights reserved.
23 *
24 * This code is derived from software contributed to Berkeley by
25 * Donn Seeley at Berkeley Software Design, Inc.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56 #if 0
57 #include <Security/Authorization.h>
58 #include <Security/AuthorizationTags.h>
59 #include <Security/AuthSession.h>
60 #endif
61
62 #include <sys/types.h>
63 #include <sys/event.h>
64 #include <sys/queue.h>
65 #include <sys/param.h>
66 #include <sys/mount.h>
67 #include <sys/sysctl.h>
68 #include <sys/wait.h>
69 #include <sys/time.h>
70 #include <sys/resource.h>
71
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <signal.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <stdarg.h>
78 #include <stdbool.h>
79 #include <string.h>
80 #include <syslog.h>
81 #include <time.h>
82 #include <ttyent.h>
83 #include <unistd.h>
84 #include <paths.h>
85 #include <util.h>
86 #include <libgen.h>
87 #include <paths.h>
88 #include <termios.h>
89
90 #include "launchd.h"
91
92 #define _PATH_RUNCOM "/etc/rc"
93
94 /*
95 * Sleep times; used to prevent thrashing.
96 */
97 #define GETTY_SPACING 5 /* N secs minimum getty spacing */
98 #define GETTY_SLEEP 30 /* sleep N secs after spacing problem */
99 #define STALL_TIMEOUT 30 /* wait N secs after warning */
100 #define DEATH_WATCH 10 /* wait N secs for procs to die */
101 #define FAILED_HW_PASS 5 /* wait N secs before croaking user */
102
103 static void stall(const char *, ...);
104
105 static void single_user_callback(void *, struct kevent *);
106 static kq_callback kqsingle_user_callback = single_user_callback;
107 static void runcom_callback(void *, struct kevent *);
108 static kq_callback kqruncom_callback = runcom_callback;
109
110 static void single_user(void);
111 static void runcom(void);
112
113 static bool runcom_safe = false;
114 static bool runcom_netboot = false;
115 static bool single_user_mode = false;
116 static bool run_runcom = true;
117 static pid_t single_user_pid = 0;
118 static pid_t runcom_pid = 0;
119
120 static void setctty(const char *, int);
121
122 // gvdl@next.com 14 Aug 1995
123 // - from ~apps/loginwindow_proj/loginwindow/common.h
124 #define REALLY_EXIT_TO_CONSOLE 229
125
126 // From old init.c
127 // These flags are used in the se_flags field of the init_session structure
128 #define SE_SHUTDOWN 0x1 /* session won't be restarted */
129
130 // The flags below control what sort of getty is launched.
131 #define SE_GETTY_LAUNCH 0x30 /* What type of getty to launch */
132 #define SE_COMMON 0x00 /* Usual command that is run - getty */
133 #define SE_ONERROR 0x10 /* Command to run if error condition occurs.
134 * This will almost always be the windowserver
135 * and loginwindow. This is so if the w.s.
136 * ever dies, that the naive user (stan)
137 * doesn't ever see the console window. */
138 #define SE_ONOPTION 0x20 /* Command to run when loginwindow exits with
139 * special error code (229). This signifies
140 * that the user typed "console" at l.w. and
141 * l.w. wants to exit and have init run getty
142 * which will then put up a console window. */
143
144 typedef struct _se_command {
145 char *path; /* what to run on that port */
146 char **argv; /* pre-parsed argument array */
147 } se_cmd_t;
148
149 typedef struct init_session {
150 kq_callback se_callback; /* run loop callback */
151 int se_index; /* index of entry in ttys file */
152 pid_t se_process; /* controlling process */
153 time_t se_started; /* used to avoid thrashing */
154 int se_flags; /* status of session */
155 char *se_device; /* filename of port */
156 se_cmd_t se_getty; /* what to run on that port */
157 se_cmd_t se_onerror; /* See SE_ONERROR above */
158 se_cmd_t se_onoption; /* See SE_ONOPTION above */
159 TAILQ_ENTRY(init_session) tqe;
160 } *session_t;
161
162 static TAILQ_HEAD(sessionshead, init_session) sessions = TAILQ_HEAD_INITIALIZER(sessions);
163
164 static void session_new(int, struct ttyent *);
165 static void session_free(session_t);
166 static void session_launch(session_t);
167 static void session_reap(session_t);
168 static void session_callback(void *, struct kevent *);
169
170 static char **construct_argv(char *);
171 static void setsecuritylevel(int);
172 static int getsecuritylevel(void);
173 static int setupargv(session_t, struct ttyent *);
174 static bool should_fsck(void);
175
176 void
init_boot(bool sflag __unused)177 init_boot(bool sflag __unused)
178 {
179 #if 0
180 int nbmib[2] = { CTL_KERN, KERN_NETBOOT };
181 int sbmib[2] = { CTL_KERN, KERN_SAFEBOOT };
182 uint32_t v = 0;
183 size_t vsz = sizeof(v);
184
185 if (sflag) {
186 single_user_mode = true;
187 run_runcom = false;
188 }
189
190 if (launchd_assumes(sysctl(nbmib, 2, &v, &vsz, NULL, 0) != -1)) {
191 if (v != 0)
192 runcom_netboot = true;
193 }
194 if (launchd_assumes(sysctl(sbmib, 2, &v, &vsz, NULL, 0) != -1)) {
195 if (v != 0)
196 runcom_safe = true;
197 }
198 #endif
199 }
200
201 void
init_pre_kevent(bool sflag)202 init_pre_kevent(bool sflag)
203 {
204 session_t s;
205
206 if (sflag) {
207 single_user_mode = 1;
208 run_runcom = 0;
209 }
210 syslog(LOG_EMERG, "starting init_pre_kevent() single_user_pid=%d runcom_pid=%d\n",
211 single_user_pid, runcom_pid);
212 syslog(LOG_EMERG, "... single_user_mode=%d run_runcom=%d\n", single_user_mode, run_runcom);
213 if (single_user_pid || runcom_pid) {
214 syslog(LOG_ERR, "skipping()\n");
215 return;
216 }
217 if (single_user_mode) {
218 syslog(LOG_ERR, "single_user()\n");
219 return single_user();
220 }
221 if (run_runcom) {
222 syslog(LOG_EMERG, "runcom()\n");
223 return runcom();
224 }
225 /*
226 * If the administrator has not set the security level to -1
227 * to indicate that the kernel should not run multiuser in secure
228 * mode, and the run script has not set a higher level of security
229 * than level 1, then put the kernel into secure mode.
230 */
231 if (getsecuritylevel() == 0) {
232 syslog(LOG_ERR, "setsecuritylevel()");
233 setsecuritylevel(1);
234 }
235 TAILQ_FOREACH(s, &sessions, tqe) {
236 if (s->se_process == 0) {
237 syslog(LOG_ERR, "session_launch()");
238 session_launch(s);
239 }
240 }
241 syslog(LOG_ERR, "done init_pre_kevent()\n");
242 }
243
244 static void
stall(const char * message,...)245 stall(const char *message, ...)
246 {
247 va_list ap;
248 va_start(ap, message);
249
250 vsyslog(LOG_ERR, message, ap);
251 va_end(ap);
252 sleep(STALL_TIMEOUT);
253 }
254
255 static int
getsecuritylevel(void)256 getsecuritylevel(void)
257 {
258 int name[2], curlevel;
259 size_t len;
260
261 name[0] = CTL_KERN;
262 name[1] = KERN_SECURELVL;
263 len = sizeof (curlevel);
264 if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
265 syslog(LOG_ALERT, "cannot get kernel security level: %m");
266 return -1;
267 }
268 return curlevel;
269 }
270
271 static void
setsecuritylevel(int newlevel)272 setsecuritylevel(int newlevel)
273 {
274 int name[2], curlevel;
275
276 curlevel = getsecuritylevel();
277 if (newlevel == curlevel)
278 return;
279 name[0] = CTL_KERN;
280 name[1] = KERN_SECURELVL;
281 if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
282 syslog(LOG_ALERT, "cannot change kernel security level from %d to %d: %m",
283 curlevel, newlevel);
284 return;
285 }
286 syslog(LOG_INFO, "kernel security level changed from %d to %d",
287 curlevel, newlevel);
288 }
289
290 /*
291 * Start a session and allocate a controlling terminal.
292 * Only called by children of init after forking.
293 */
294 static void
setctty(const char * name,int flags)295 setctty(const char *name, int flags)
296 {
297 int fd;
298
299 revoke(name);
300 if ((fd = open(name, flags | O_RDWR)) == -1) {
301 stall("can't open %s: %m", name);
302 launchd_exit(EXIT_FAILURE);
303 }
304 if (login_tty(fd) == -1) {
305 stall("can't get %s for controlling terminal: %m", name);
306 launchd_exit(EXIT_FAILURE);
307 }
308 }
309
310 static void
single_user(void)311 single_user(void)
312 {
313 bool runcom_fsck = should_fsck();
314 const char *argv[2];
315
316 if (getsecuritylevel() > 0)
317 setsecuritylevel(0);
318
319 if ((single_user_pid = launchd_fork()) == -1) {
320 syslog(LOG_ERR, "can't fork single-user shell, trying again: %m");
321 return;
322 } else if (single_user_pid == 0) {
323 #if 0
324 setctty(_PATH_CONSOLE, O_POPUP);
325 #endif
326 setenv("TERM", "vt100", 1);
327 setenv("SafeBoot", runcom_safe ? "-x" : "", 1);
328 setenv("VerboseFlag", "-v", 1); /* single user mode implies verbose mode */
329 setenv("FsckSlash", runcom_fsck ? "-F" : "", 1);
330 setenv("NetBoot", runcom_netboot ? "-N" : "", 1);
331
332 if (runcom_fsck) {
333 fprintf(stdout, "Singleuser boot -- fsck not done\n");
334 fprintf(stdout, "Root device is mounted read-only\n\n");
335 fprintf(stdout, "If you want to make modifications to files:\n");
336 fprintf(stdout, "\t/sbin/fsck -fy\n\t/sbin/mount -uw /\n\n");
337 fprintf(stdout, "If you wish to boot the system:\n");
338 fprintf(stdout, "\texit\n\n");
339 fflush(stdout);
340 }
341
342 argv[0] = "-sh";
343 argv[1] = NULL;
344 execv(_PATH_BSHELL, __DECONST(char *const *, argv));
345 syslog(LOG_ERR, "can't exec %s for single user: %m", _PATH_BSHELL);
346 sleep(STALL_TIMEOUT);
347 launchd_exit(EXIT_FAILURE);
348 } else {
349 if (kevent_mod(single_user_pid, EVFILT_PROC, EV_ADD,
350 NOTE_EXIT, 0, &kqsingle_user_callback) == -1)
351 single_user_callback(NULL, NULL);
352 }
353 }
354
355 static void
single_user_callback(void * obj,struct kevent * kev)356 single_user_callback(void *obj __attribute__((unused)), struct kevent *kev __attribute__((unused)))
357 {
358 int status;
359
360 if (!launchd_assumes(waitpid(single_user_pid, &status, 0) == single_user_pid))
361 return;
362
363 if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS) {
364 syslog(LOG_INFO, "single user shell terminated, restarting");
365 run_runcom = true;
366 single_user_mode = false;
367 } else {
368 syslog(LOG_INFO, "single user shell terminated.");
369 run_runcom = false;
370 if (WTERMSIG(status) != SIGKILL)
371 single_user_mode = true;
372 }
373
374 single_user_pid = 0;
375 }
376
377 static struct timeval runcom_start_tv = { 0, 0 };
378 /*
379 * Run the system startup script.
380 */
381 static void
runcom(void)382 runcom(void)
383 {
384 bool runcom_fsck = should_fsck();
385 char *argv[4];
386 struct termios term;
387 int vdisable;
388
389 gettimeofday(&runcom_start_tv, NULL);
390 syslog(LOG_ERR, "launchd_fork()\n");
391 if ((runcom_pid = launchd_fork()) == -1) {
392 syslog(LOG_ERR, "can't fork for %s on %s: %m", _PATH_BSHELL, _PATH_RUNCOM);
393 sleep(STALL_TIMEOUT);
394 runcom_pid = 0;
395 single_user_mode = true;
396 return;
397 } else if (runcom_pid > 0) {
398 run_runcom = false;
399 if (kevent_mod(runcom_pid, EVFILT_PROC, EV_ADD,
400 NOTE_EXIT, 0, &kqruncom_callback) == -1) {
401 syslog(LOG_ERR, "runcom_callback() ... ");
402 runcom_callback(NULL, NULL);
403 syslog(LOG_ERR, "done\n");
404 }
405 return;
406 }
407 syslog(LOG_ERR, "setctty()\n");
408 setctty(_PATH_CONSOLE, 0);
409
410 syslog(LOG_ERR, "fpathconf()\n");
411 sleep(1);
412 if ((vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE)) == -1) {
413 syslog(LOG_ERR, "fpathconf(\"%s\") %m", _PATH_CONSOLE);
414 } else if (tcgetattr(STDIN_FILENO, &term) == -1) {
415 syslog(LOG_ERR, "tcgetattr(\"%s\") %m", _PATH_CONSOLE);
416 } else {
417 term.c_cc[VINTR] = vdisable;
418 term.c_cc[VKILL] = vdisable;
419 term.c_cc[VQUIT] = vdisable;
420 term.c_cc[VSUSP] = vdisable;
421 term.c_cc[VSTART] = vdisable;
422 term.c_cc[VSTOP] = vdisable;
423 term.c_cc[VDSUSP] = vdisable;
424 sleep(1);
425 syslog(LOG_ERR, "tcsetattr(STDIN_FILENO) ...");
426 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) == -1)
427 syslog(LOG_WARNING, "tcsetattr(\"%s\") %m", _PATH_CONSOLE);
428 syslog(LOG_ERR, "done\n");
429 }
430 sleep(1);
431 syslog(LOG_ERR, "setenv\n");
432 setenv("SafeBoot", runcom_safe ? "-x" : "", 1);
433 setenv("FsckSlash", runcom_fsck ? "-F" : "", 1);
434 setenv("NetBoot", runcom_netboot ? "-N" : "", 1);
435 syslog(LOG_ERR, "execv\n");
436 {
437 char _sh[] = "sh";
438 int err;
439
440 argv[0] = _sh;
441 argv[1] = __DECONST(char *, _PATH_RUNCOM);
442 argv[2] = 0;
443 syslog(LOG_ERR, "execv(%s, %p)\n", _PATH_BSHELL, argv);
444 err = execv(_PATH_BSHELL, argv);
445 syslog(LOG_ERR, "execv err=%d errno=%d", err, errno);
446 sleep(2);
447 }
448 stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM);
449 launchd_exit(EXIT_FAILURE);
450 }
451
452 static void
runcom_callback(void * obj,struct kevent * kev)453 runcom_callback(void *obj __attribute__((unused)), struct kevent *kev __attribute__((unused)))
454 {
455 int status;
456 struct timeval runcom_end_tv, runcom_total_tv;
457 double sec;
458
459 gettimeofday(&runcom_end_tv, NULL);
460 timersub(&runcom_end_tv, &runcom_start_tv, &runcom_total_tv);
461 sec = runcom_total_tv.tv_sec;
462 sec += (double)runcom_total_tv.tv_usec / (double)1000000;
463 syslog(LOG_INFO, "%s finished in: %.3f seconds", _PATH_RUNCOM, sec);
464
465 if (launchd_assumes(waitpid(runcom_pid, &status, 0) == runcom_pid)) {
466 runcom_pid = 0;
467 } else {
468 syslog(LOG_ERR, "going to single user mode");
469 single_user_mode = true;
470 return;
471 }
472
473 if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS) {
474 #ifdef notyet
475 logwtmp("~", "reboot", "");
476 #endif
477 return;
478 } else if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGTERM || WTERMSIG(status) == SIGKILL)) {
479 return;
480 }
481
482 syslog(LOG_ERR, "%s on %s terminated abnormally, going to single user mode",
483 _PATH_BSHELL, _PATH_RUNCOM);
484 single_user_mode = true;
485 }
486
487 /*
488 * Construct an argument vector from a command line.
489 */
490 char **
construct_argv(command)491 construct_argv(command)
492 char *command;
493 {
494 int argc = 0;
495 char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
496 * sizeof (char *));
497 static const char separators[] = " \t";
498
499 if ((argv[argc++] = strtok(command, separators)) == 0)
500 return 0;
501 while ((argv[argc++] = strtok(NULL, separators)))
502 continue;
503 return argv;
504 }
505
506 /*
507 * Deallocate a session descriptor.
508 */
509
free_command(se_cmd_t * se_cmd)510 static void free_command(se_cmd_t *se_cmd)
511 {
512 if (se_cmd->path) {
513 free(se_cmd->path);
514 free(se_cmd->argv);
515 }
516 }
517
518 void
session_free(session_t s)519 session_free(session_t s)
520 {
521 TAILQ_REMOVE(&sessions, s, tqe);
522 if (s->se_process) {
523 #ifdef notyet
524 if (kevent_mod(s->se_process, EVFILT_PROC, EV_ADD,
525 NOTE_EXIT, 0, &kqsimple_zombie_reaper) == -1)
526 session_reap(s);
527 else
528 kill(s->se_process, SIGHUP);
529 #endif
530 }
531 free(s->se_device);
532 free_command(&s->se_getty);
533 free_command(&s->se_onerror);
534 free_command(&s->se_onoption);
535 free(s);
536 }
537
setup_command(se_cmd_t * se_cmd,char * command,char * arg)538 static int setup_command(se_cmd_t *se_cmd, char *command, char *arg )
539 {
540 char *commandWithArg;
541
542 asprintf(&commandWithArg, "%s %s", command, arg);
543
544 free_command(se_cmd);
545
546 se_cmd->path = commandWithArg;
547 se_cmd->argv = construct_argv(commandWithArg);
548 if (se_cmd->argv == NULL) {
549 free(se_cmd->path);
550 se_cmd->path = NULL;
551 return 0;
552 }
553 return 1;
554 }
555
556 /*
557 * Calculate getty and if useful window argv vectors.
558 */
559 static int
setupargv(sp,typ)560 setupargv(sp, typ)
561 session_t sp;
562 struct ttyent *typ;
563 {
564 const char *type;
565
566 if ( !setup_command(&sp->se_getty, typ->ty_getty, typ->ty_name) )
567 {
568 type = "getty";
569 goto bad_args;
570 }
571 #if 0
572 if (typ->ty_onerror
573 && !setup_command(&sp->se_onerror, typ->ty_onerror, typ->ty_name) )
574 {
575 type = "onerror";
576 goto bad_args;
577 }
578
579 if (typ->ty_onoption
580 && !setup_command(&sp->se_onoption, typ->ty_onoption, typ->ty_name) )
581 {
582 type = "onoption";
583 goto bad_args;
584 }
585 #endif
586 return 1;
587
588 bad_args:
589 syslog(LOG_WARNING, "can't parse %s for port %s", type, sp->se_device);
590 return 0;
591 }
592
593
594 /*
595 * Allocate a new session descriptor.
596 */
597 void
session_new(session_index,typ)598 session_new(session_index, typ)
599 int session_index;
600 struct ttyent *typ;
601 {
602 session_t s;
603
604 if ((typ->ty_status & TTY_ON) == 0 ||
605 typ->ty_name == 0 ||
606 typ->ty_getty == 0)
607 return;
608
609 s = calloc(1, sizeof(struct init_session));
610
611 s->se_callback = session_callback;
612 s->se_index = session_index;
613
614 TAILQ_INSERT_TAIL(&sessions, s, tqe);
615
616 asprintf(&s->se_device, "%s%s", _PATH_DEV, typ->ty_name);
617
618 if (setupargv(s, typ) == 0)
619 session_free(s);
620 }
621
622 static void
session_launch(session_t s)623 session_launch(session_t s)
624 {
625 pid_t pid;
626 sigset_t mask;
627 se_cmd_t *se_cmd;
628 const char *session_type = NULL;
629 time_t current_time = time(NULL);
630 bool is_loginwindow = false;
631
632 // Setup the default values;
633 switch (s->se_flags & SE_GETTY_LAUNCH) {
634 case SE_ONOPTION:
635 if (s->se_onoption.path) {
636 se_cmd = &s->se_onoption;
637 session_type = "onoption";
638 break;
639 }
640 /* No break */
641 case SE_ONERROR:
642 if (s->se_onerror.path) {
643 se_cmd = &s->se_onerror;
644 session_type = "onerror";
645 break;
646 }
647 /* No break */
648 case SE_COMMON:
649 default:
650 se_cmd = &s->se_getty;
651 session_type = "getty";
652 break;
653 }
654
655 if (strcmp(se_cmd->argv[0], "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow") == 0)
656 is_loginwindow = true;
657
658 pid = launchd_fork();
659
660 if (pid == -1) {
661 syslog(LOG_ERR, "can't fork for %s on port %s: %m",
662 session_type, s->se_device);
663 return;
664 }
665
666 if (pid) {
667 s->se_process = pid;
668 s->se_started = time(NULL);
669 s->se_flags &= ~SE_GETTY_LAUNCH; // clear down getty launch type
670 if (kevent_mod(pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, &s->se_callback) == -1)
671 session_reap(s);
672 return;
673 }
674
675 if (current_time > s->se_started &&
676 current_time - s->se_started < GETTY_SPACING) {
677 syslog(LOG_WARNING, "%s repeating too quickly on port %s, sleeping",
678 session_type, s->se_device);
679 sleep(GETTY_SLEEP);
680 }
681
682 sigemptyset(&mask);
683 sigprocmask(SIG_SETMASK, &mask, NULL);
684
685
686 if (!is_loginwindow)
687 launchd_SessionCreate();
688
689 execv(se_cmd->argv[0], se_cmd->argv);
690 stall("can't exec %s '%s' for port %s: %m", session_type,
691 se_cmd->argv[0], s->se_device);
692 launchd_exit(EXIT_FAILURE);
693 }
694
695 static void
session_callback(void * obj,struct kevent * kev)696 session_callback(void *obj, struct kevent *kev __attribute__((unused)))
697 {
698 session_t s = obj;
699
700 session_reap(s);
701 if (s->se_flags & SE_SHUTDOWN) {
702 session_free(s);
703 } else {
704 session_launch(s);
705 }
706 }
707
708 static void
session_reap(session_t s)709 session_reap(session_t s)
710 {
711 char *line;
712 int status;
713
714 if (!launchd_assumes(waitpid(s->se_process, &status, 0) == s->se_process))
715 return;
716
717 if (WIFSIGNALED(status)) {
718 syslog(LOG_WARNING, "%s port %s exited abnormally: %s",
719 s->se_getty.path, s->se_device, strsignal(WTERMSIG(status)));
720 s->se_flags |= SE_ONERROR;
721 } else if (WEXITSTATUS(status) == REALLY_EXIT_TO_CONSOLE) {
722 /* WIFEXITED(status) assumed */
723 s->se_flags |= SE_ONOPTION;
724 } else {
725 s->se_flags |= SE_ONERROR;
726 }
727
728 s->se_process = 0;
729 line = s->se_device + sizeof(_PATH_DEV) - 1;
730
731 #ifdef notyet
732 if (logout(line))
733 logwtmp(line, "", "");
734 #endif
735 }
736
737 /*
738 * This is an n-squared algorithm. We hope it isn't run often...
739 */
740 void
update_ttys(void)741 update_ttys(void)
742 {
743 session_t sp;
744 struct ttyent *typ;
745 int session_index = 0;
746 int devlen;
747
748 devlen = sizeof(_PATH_DEV) - 1;
749 while ((typ = getttyent())) {
750 ++session_index;
751
752 TAILQ_FOREACH(sp, &sessions, tqe) {
753 if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
754 break;
755 }
756
757 if (sp == NULL) {
758 session_new(session_index, typ);
759 continue;
760 }
761
762 if (sp->se_index != session_index) {
763 syslog(LOG_INFO, "port %s changed utmp index from %d to %d",
764 sp->se_device, sp->se_index,
765 session_index);
766 sp->se_index = session_index;
767 }
768
769 if ((typ->ty_status & TTY_ON) == 0 ||
770 typ->ty_getty == 0) {
771 session_free(sp);
772 continue;
773 }
774
775 sp->se_flags &= ~SE_SHUTDOWN;
776
777 if (setupargv(sp, typ) == 0) {
778 syslog(LOG_WARNING, "can't parse getty for port %s",
779 sp->se_device);
780 session_free(sp);
781 }
782 }
783
784 endttyent();
785 }
786
787 /*
788 * Block further logins.
789 */
790 void
catatonia(void)791 catatonia(void)
792 {
793 session_t s;
794
795 TAILQ_FOREACH(s, &sessions, tqe)
796 s->se_flags |= SE_SHUTDOWN;
797 }
798
init_check_pid(pid_t p)799 bool init_check_pid(pid_t p)
800 {
801 session_t s;
802
803 TAILQ_FOREACH(s, &sessions, tqe) {
804 if (s->se_process == p)
805 return true;
806 }
807
808 if (single_user_pid == p)
809 return true;
810
811 if (runcom_pid == p)
812 return true;
813
814 return false;
815 }
816
817 bool
should_fsck(void)818 should_fsck(void)
819 {
820 struct statfs sfs;
821 bool r = true;
822
823 if (launchd_assumes(statfs("/", &sfs) != -1)) {
824 if (!(sfs.f_flags & MNT_RDONLY)) {
825 r = false;
826 }
827 }
828
829 return r;
830 }
831