/[baselayout]/trunk/src/rc.c
Gentoo

Diff of /trunk/src/rc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2936 Revision 3036
18#include <sys/types.h> 18#include <sys/types.h>
19#include <sys/stat.h> 19#include <sys/stat.h>
20#include <sys/utsname.h> 20#include <sys/utsname.h>
21#include <sys/wait.h> 21#include <sys/wait.h>
22#include <errno.h> 22#include <errno.h>
23#include <dirent.h>
23#include <ctype.h> 24#include <ctype.h>
24#include <getopt.h> 25#include <getopt.h>
25#include <libgen.h> 26#include <libgen.h>
26#include <limits.h> 27#include <limits.h>
27#include <stdbool.h> 28#include <stdbool.h>
28#include <stdio.h> 29#include <stdio.h>
29#include <stdlib.h> 30#include <stdlib.h>
30#include <signal.h> 31#include <signal.h>
31#include <string.h> 32#include <string.h>
33#include <strings.h>
32#include <syslog.h> 34#include <syslog.h>
33#include <termios.h> 35#include <termios.h>
34#include <unistd.h> 36#include <unistd.h>
35 37
36#include "builtins.h" 38#include "builtins.h"
61 63
62static char *RUNLEVEL = NULL; 64static char *RUNLEVEL = NULL;
63static char *PREVLEVEL = NULL; 65static char *PREVLEVEL = NULL;
64 66
65static char *applet = NULL; 67static char *applet = NULL;
68static char *runlevel = NULL;
66static char **env = NULL; 69static char **env = NULL;
67static char **newenv = NULL; 70static char **newenv = NULL;
68static char **coldplugged_services = NULL; 71static char **coldplugged_services = NULL;
69static char **stop_services = NULL; 72static char **stop_services = NULL;
70static char **start_services = NULL; 73static char **start_services = NULL;
102 rc_strlist_free (env); 105 rc_strlist_free (env);
103 rc_strlist_free (newenv); 106 rc_strlist_free (newenv);
104 rc_strlist_free (coldplugged_services); 107 rc_strlist_free (coldplugged_services);
105 rc_strlist_free (stop_services); 108 rc_strlist_free (stop_services);
106 rc_strlist_free (start_services); 109 rc_strlist_free (start_services);
107 rc_free_deptree (deptree); 110 rc_deptree_free (deptree);
108 rc_strlist_free (types); 111 rc_strlist_free (types);
109 112
110 /* Clean runlevel start, stop markers */ 113 /* Clean runlevel start, stop markers */
111 if (! rc_in_plugin) { 114 if (! rc_in_plugin) {
112 if (rc_is_dir (RC_STARTING))
113 rc_rm_dir (RC_STARTING, true); 115 rmdir (RC_STARTING);
114 if (rc_is_dir (RC_STOPPING))
115 rc_rm_dir (RC_STOPPING, true); 116 rmdir (RC_STOPPING);
116 } 117 }
118
119 free (runlevel);
117 } 120 }
118 121
119 free (applet); 122 free (applet);
120} 123}
121 124
143 char *fmt = NULL; 146 char *fmt = NULL;
144 int level = 0; 147 int level = 0;
145 148
146 if (strcmp (applet, "eval_ecolors") == 0) { 149 if (strcmp (applet, "eval_ecolors") == 0) {
147 printf ("GOOD='%s'\nWARN='%s'\nBAD='%s'\nHILITE='%s'\nBRACKET='%s'\nNORMAL='%s'\n", 150 printf ("GOOD='%s'\nWARN='%s'\nBAD='%s'\nHILITE='%s'\nBRACKET='%s'\nNORMAL='%s'\n",
148 ecolor (ecolor_good), 151 ecolor (ECOLOR_GOOD),
149 ecolor (ecolor_warn), 152 ecolor (ECOLOR_WARN),
150 ecolor (ecolor_bad), 153 ecolor (ECOLOR_BAD),
151 ecolor (ecolor_hilite), 154 ecolor (ECOLOR_HILITE),
152 ecolor (ecolor_bracket), 155 ecolor (ECOLOR_BRACKET),
153 ecolor (ecolor_normal)); 156 ecolor (ECOLOR_NORMAL));
154 exit (EXIT_SUCCESS); 157 exit (EXIT_SUCCESS);
155 } 158 }
156 159
157 if (argc > 0) { 160 if (argc > 0) {
158 161
188 191
189 if (argc > 0) { 192 if (argc > 0) {
190 for (i = 0; i < argc; i++) 193 for (i = 0; i < argc; i++)
191 l += strlen (argv[i]) + 1; 194 l += strlen (argv[i]) + 1;
192 195
193 message = rc_xmalloc (l); 196 message = xmalloc (l);
194 p = message; 197 p = message;
195 198
196 for (i = 0; i < argc; i++) { 199 for (i = 0; i < argc; i++) {
197 if (i > 0) 200 if (i > 0)
198 *p++ = ' '; 201 *p++ = ' ';
201 } 204 }
202 *p = 0; 205 *p = 0;
203 } 206 }
204 207
205 if (message) 208 if (message)
206 fmt = rc_xstrdup ("%s"); 209 fmt = xstrdup ("%s");
207 210
208 if (strcmp (applet, "einfo") == 0) 211 if (strcmp (applet, "einfo") == 0)
209 einfo (fmt, message); 212 einfo (fmt, message);
210 else if (strcmp (applet, "einfon") == 0) 213 else if (strcmp (applet, "einfon") == 0)
211 einfon (fmt, message); 214 einfon (fmt, message);
267 270
268 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0) 271 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
269 eerrorx ("%s: no service specified", applet); 272 eerrorx ("%s: no service specified", applet);
270 273
271 if (strcmp (applet, "service_started") == 0) 274 if (strcmp (applet, "service_started") == 0)
272 ok = rc_service_state (argv[0], rc_service_started); 275 ok = (rc_service_state (argv[0]) & RC_SERVICE_STARTED);
273 else if (strcmp (applet, "service_stopped") == 0) 276 else if (strcmp (applet, "service_stopped") == 0)
274 ok = rc_service_state (argv[0], rc_service_stopped); 277 ok = (rc_service_state (argv[0]) & RC_SERVICE_STOPPED);
275 else if (strcmp (applet, "service_inactive") == 0) 278 else if (strcmp (applet, "service_inactive") == 0)
276 ok = rc_service_state (argv[0], rc_service_inactive); 279 ok = (rc_service_state (argv[0]) & RC_SERVICE_INACTIVE);
277 else if (strcmp (applet, "service_starting") == 0) 280 else if (strcmp (applet, "service_starting") == 0)
278 ok = rc_service_state (argv[0], rc_service_starting); 281 ok = (rc_service_state (argv[0]) & RC_SERVICE_STOPPING);
279 else if (strcmp (applet, "service_stopping") == 0) 282 else if (strcmp (applet, "service_stopping") == 0)
280 ok = rc_service_state (argv[0], rc_service_stopping); 283 ok = (rc_service_state (argv[0]) & RC_SERVICE_STOPPING);
281 else if (strcmp (applet, "service_coldplugged") == 0) 284 else if (strcmp (applet, "service_coldplugged") == 0)
282 ok = rc_service_state (argv[0], rc_service_coldplugged); 285 ok = (rc_service_state (argv[0]) & RC_SERVICE_COLDPLUGGED);
283 else if (strcmp (applet, "service_wasinactive") == 0) 286 else if (strcmp (applet, "service_wasinactive") == 0)
284 ok = rc_service_state (argv[0], rc_service_wasinactive); 287 ok = (rc_service_state (argv[0]) & RC_SERVICE_WASINACTIVE);
285 else if (strcmp (applet, "service_started_daemon") == 0) { 288 else if (strcmp (applet, "service_started_daemon") == 0) {
286 int idx = 0; 289 int idx = 0;
287 if (argc > 2) 290 if (argc > 2)
288 sscanf (argv[2], "%d", &idx); 291 sscanf (argv[2], "%d", &idx);
289 exit (rc_service_started_daemon (argv[0], argv[1], idx) 292 exit (rc_service_started_daemon (argv[0], argv[1], idx)
301 304
302 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0) 305 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
303 eerrorx ("%s: no service specified", applet); 306 eerrorx ("%s: no service specified", applet);
304 307
305 if (strcmp (applet, "mark_service_started") == 0) 308 if (strcmp (applet, "mark_service_started") == 0)
306 ok = rc_mark_service (argv[0], rc_service_started); 309 ok = rc_service_mark (argv[0], RC_SERVICE_STARTED);
307 else if (strcmp (applet, "mark_service_stopped") == 0) 310 else if (strcmp (applet, "mark_service_stopped") == 0)
308 ok = rc_mark_service (argv[0], rc_service_stopped); 311 ok = rc_service_mark (argv[0], RC_SERVICE_STOPPED);
309 else if (strcmp (applet, "mark_service_inactive") == 0) 312 else if (strcmp (applet, "mark_service_inactive") == 0)
310 ok = rc_mark_service (argv[0], rc_service_inactive); 313 ok = rc_service_mark (argv[0], RC_SERVICE_INACTIVE);
311 else if (strcmp (applet, "mark_service_starting") == 0) 314 else if (strcmp (applet, "mark_service_starting") == 0)
312 ok = rc_mark_service (argv[0], rc_service_starting); 315 ok = rc_service_mark (argv[0], RC_SERVICE_STOPPING);
313 else if (strcmp (applet, "mark_service_stopping") == 0) 316 else if (strcmp (applet, "mark_service_stopping") == 0)
314 ok = rc_mark_service (argv[0], rc_service_stopping); 317 ok = rc_service_mark (argv[0], RC_SERVICE_STOPPING);
315 else if (strcmp (applet, "mark_service_coldplugged") == 0) 318 else if (strcmp (applet, "mark_service_coldplugged") == 0)
316 ok = rc_mark_service (argv[0], rc_service_coldplugged); 319 ok = rc_service_mark (argv[0], RC_SERVICE_COLDPLUGGED);
320 else if (strcmp (applet, "mark_service_failed") == 0)
321 ok = rc_service_mark (argv[0], RC_SERVICE_FAILED);
317 else 322 else
318 eerrorx ("%s: unknown applet", applet); 323 eerrorx ("%s: unknown applet", applet);
319 324
320 /* If we're marking ourselves then we need to inform our parent runscript 325 /* If we're marking ourselves then we need to inform our parent runscript
321 process so they do not mark us based on our exit code */ 326 process so they do not mark us based on our exit code */
328 if (runscript_pid && sscanf (runscript_pid, "%d", &pid) == 1) 333 if (runscript_pid && sscanf (runscript_pid, "%d", &pid) == 1)
329 if (kill (pid, SIGHUP) != 0) 334 if (kill (pid, SIGHUP) != 0)
330 eerror ("%s: failed to signal parent %d: %s", 335 eerror ("%s: failed to signal parent %d: %s",
331 applet, pid, strerror (errno)); 336 applet, pid, strerror (errno));
332 337
333 /* Remove the exclsive time test. This ensures that it's not 338 /* Remove the exclusive time test. This ensures that it's not
334 in control as well */ 339 in control as well */
335 l = strlen (RC_SVCDIR "exclusive") + 340 l = strlen (RC_SVCDIR "exclusive") +
336 strlen (svcname) + 341 strlen (svcname) +
337 strlen (runscript_pid) + 342 strlen (runscript_pid) +
338 4; 343 4;
339 mtime = rc_xmalloc (l); 344 mtime = xmalloc (l);
340 snprintf (mtime, l, RC_SVCDIR "exclusive/%s.%s", 345 snprintf (mtime, l, RC_SVCDIR "exclusive/%s.%s",
341 svcname, runscript_pid); 346 svcname, runscript_pid);
342 if (rc_exists (mtime) && unlink (mtime) != 0) 347 if (exists (mtime) && unlink (mtime) != 0)
343 eerror ("%s: unlink: %s", applet, strerror (errno)); 348 eerror ("%s: unlink: %s", applet, strerror (errno));
344 free (mtime); 349 free (mtime);
345 } 350 }
346 351
347 return (ok ? EXIT_SUCCESS : EXIT_FAILURE); 352 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
357 362
358 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0) 363 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
359 eerrorx ("%s: no option specified", applet); 364 eerrorx ("%s: no option specified", applet);
360 365
361 if (strcmp (applet, "get_options") == 0) { 366 if (strcmp (applet, "get_options") == 0) {
362 char *option = rc_get_service_option (service, argv[0]); 367 char *option = rc_service_value_get (service, argv[0]);
363 if (option) { 368 if (option) {
364 printf ("%s", option); 369 printf ("%s", option);
365 free (option); 370 free (option);
366 ok = true; 371 ok = true;
367 } 372 }
368 } else if (strcmp (applet, "save_options") == 0) 373 } else if (strcmp (applet, "save_options") == 0)
369 ok = rc_set_service_option (service, argv[0], argv[1]); 374 ok = rc_service_value_set (service, argv[0], argv[1]);
370 else 375 else
371 eerrorx ("%s: unknown applet", applet); 376 eerrorx ("%s: unknown applet", applet);
372 377
373 return (ok ? EXIT_SUCCESS : EXIT_FAILURE); 378 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
374} 379}
375 380
376#ifdef __linux__ 381#ifdef __linux__
377static char *proc_getent (const char *ent) 382static char *proc_getent (const char *ent)
378{ 383{
379 FILE *fp; 384 FILE *fp;
380 char buffer[RC_LINEBUFFER]; 385 char *buffer;
381 char *p; 386 char *p;
382 char *value = NULL; 387 char *value = NULL;
383 int i; 388 int i;
384 389
385 if (! rc_exists ("/proc/cmdline")) 390 if (! exists ("/proc/cmdline"))
386 return (NULL); 391 return (NULL);
387 392
388 if (! (fp = fopen ("/proc/cmdline", "r"))) { 393 if (! (fp = fopen ("/proc/cmdline", "r"))) {
389 eerror ("failed to open `/proc/cmdline': %s", strerror (errno)); 394 eerror ("failed to open `/proc/cmdline': %s", strerror (errno));
390 return (NULL); 395 return (NULL);
391 } 396 }
392 397
393 memset (buffer, 0, sizeof (buffer)); 398 buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
399 memset (buffer, 0, RC_LINEBUFFER);
394 if (fgets (buffer, RC_LINEBUFFER, fp) && 400 if (fgets (buffer, RC_LINEBUFFER, fp) &&
395 (p = strstr (buffer, ent))) 401 (p = strstr (buffer, ent)))
396 { 402 {
397 i = p - buffer; 403 i = p - buffer;
398 if (i == '\0' || buffer[i - 1] == ' ') { 404 if (i == '\0' || buffer[i - 1] == ' ') {
402 buffer[i] = 0; 408 buffer[i] = 0;
403 409
404 p += strlen (ent); 410 p += strlen (ent);
405 if (*p == '=') 411 if (*p == '=')
406 p++; 412 p++;
407 value = strdup (strsep (&p, " ")); 413 value = xstrdup (strsep (&p, " "));
408 } 414 }
409 } else 415 } else
410 errno = ENOENT; 416 errno = ENOENT;
417 free (buffer);
411 fclose (fp); 418 fclose (fp);
412 419
413 return (value); 420 return (value);
414} 421}
415#endif 422#endif
417static char read_key (bool block) 424static char read_key (bool block)
418{ 425{
419 struct termios termios; 426 struct termios termios;
420 char c = 0; 427 char c = 0;
421 int fd = fileno (stdin); 428 int fd = fileno (stdin);
422 429
423 if (! isatty (fd)) 430 if (! isatty (fd))
424 return (false); 431 return (false);
425 432
426 /* Now save our terminal settings. We need to restore them at exit as we 433 /* Now save our terminal settings. We need to restore them at exit as we
427 will be changing it for non-blocking reads for Interactive */ 434 will be changing it for non-blocking reads for Interactive */
428 if (! termios_orig) { 435 if (! termios_orig) {
429 termios_orig = rc_xmalloc (sizeof (struct termios)); 436 termios_orig = xmalloc (sizeof (struct termios));
430 tcgetattr (fd, termios_orig); 437 tcgetattr (fd, termios_orig);
431 } 438 }
432 439
433 tcgetattr (fd, &termios); 440 tcgetattr (fd, &termios);
434 termios.c_lflag &= ~(ICANON | ECHO); 441 termios.c_lflag &= ~(ICANON | ECHO);
481 execl ("/sbin/halt", "/sbin/halt", "-f", (char *) NULL); 488 execl ("/sbin/halt", "/sbin/halt", "-f", (char *) NULL);
482 eerrorx ("%s: unable to exec `/sbin/halt': %s", applet, strerror (errno)); 489 eerrorx ("%s: unable to exec `/sbin/halt': %s", applet, strerror (errno));
483 } 490 }
484#endif 491#endif
485 492
486 newenv = rc_filter_env (); 493 newenv = env_filter ();
487 494
488 if (cont) { 495 if (cont) {
489 int status = 0; 496 int status = 0;
490#ifdef __linux__ 497#ifdef __linux__
491 char *tty = ttyname (fileno (stdout)); 498 char *tty = ttyname (fileno (stdout));
534 applet, strerror (errno)); 541 applet, strerror (errno));
535 exit (EXIT_SUCCESS); 542 exit (EXIT_SUCCESS);
536#endif 543#endif
537} 544}
538 545
539static void set_ksoftlevel (const char *runlevel) 546static void set_ksoftlevel (const char *level)
540{ 547{
541 FILE *fp; 548 FILE *fp;
542 549
543 if (! runlevel || 550 if (! level ||
544 strcmp (runlevel, getenv ("RC_BOOTLEVEL")) == 0 || 551 strcmp (level, getenv ("RC_BOOTLEVEL")) == 0 ||
545 strcmp (runlevel, RC_LEVEL_SINGLE) == 0 || 552 strcmp (level, RC_LEVEL_SINGLE) == 0 ||
546 strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) 553 strcmp (level, RC_LEVEL_SYSINIT) == 0)
547 { 554 {
548 if (rc_exists (RC_KSOFTLEVEL) && 555 if (exists (RC_KSOFTLEVEL) &&
549 unlink (RC_KSOFTLEVEL) != 0) 556 unlink (RC_KSOFTLEVEL) != 0)
550 eerror ("unlink `%s': %s", RC_KSOFTLEVEL, strerror (errno)); 557 eerror ("unlink `%s': %s", RC_KSOFTLEVEL, strerror (errno));
551 return; 558 return;
552 } 559 }
553 560
554 if (! (fp = fopen (RC_KSOFTLEVEL, "w"))) { 561 if (! (fp = fopen (RC_KSOFTLEVEL, "w"))) {
555 eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno)); 562 eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno));
556 return; 563 return;
557 } 564 }
558 565
559 fprintf (fp, "%s", runlevel); 566 fprintf (fp, "%s", level);
560 fclose (fp); 567 fclose (fp);
561} 568}
562 569
563static int get_ksoftlevel (char *buffer, int buffer_len) 570static int get_ksoftlevel (char *buffer, int buffer_len)
564{ 571{
565 FILE *fp; 572 FILE *fp;
566 int i = 0; 573 int i = 0;
567 574
568 if (! rc_exists (RC_KSOFTLEVEL)) 575 if (! exists (RC_KSOFTLEVEL))
569 return (0); 576 return (0);
570 577
571 if (! (fp = fopen (RC_KSOFTLEVEL, "r"))) { 578 if (! (fp = fopen (RC_KSOFTLEVEL, "r"))) {
572 eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno)); 579 eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno));
573 return (-1); 580 return (-1);
593{ 600{
594 pidlist_t *sp = service_pids; 601 pidlist_t *sp = service_pids;
595 if (sp) { 602 if (sp) {
596 while (sp->next) 603 while (sp->next)
597 sp = sp->next; 604 sp = sp->next;
598 sp->next = rc_xmalloc (sizeof (pidlist_t)); 605 sp->next = xmalloc (sizeof (pidlist_t));
599 sp = sp->next; 606 sp = sp->next;
600 } else 607 } else
601 sp = service_pids = rc_xmalloc (sizeof (pidlist_t)); 608 sp = service_pids = xmalloc (sizeof (pidlist_t));
602 memset (sp, 0, sizeof (pidlist_t)); 609 memset (sp, 0, sizeof (pidlist_t));
603 sp->pid = pid; 610 sp->pid = pid;
604} 611}
605 612
606static void remove_pid (pid_t pid) 613static void remove_pid (pid_t pid)
617 free (pl); 624 free (pl);
618 break; 625 break;
619 } 626 }
620 last = pl; 627 last = pl;
621 } 628 }
629}
630
631static int wait_pid (pid_t pid)
632{
633 int status = 0;
634 pid_t savedpid = pid;
635 int retval = -1;
636
637 errno = 0;
638 while ((pid = waitpid (savedpid, &status, 0)) > 0) {
639 if (pid == savedpid)
640 retval = WIFEXITED (status) ? WEXITSTATUS (status) : EXIT_FAILURE;
641 }
642
643 return (retval);
622} 644}
623 645
624static void handle_signal (int sig) 646static void handle_signal (int sig)
625{ 647{
626 int serrno = errno; 648 int serrno = errno;
662 signal (SIGCHLD, SIG_IGN); 684 signal (SIGCHLD, SIG_IGN);
663 for (pl = service_pids; pl; pl = pl->next) 685 for (pl = service_pids; pl; pl = pl->next)
664 kill (pl->pid, SIGTERM); 686 kill (pl->pid, SIGTERM);
665 687
666 /* Notify plugins we are aborting */ 688 /* Notify plugins we are aborting */
667 rc_plugin_run (rc_hook_abort, NULL); 689 rc_plugin_run (RC_HOOK_ABORT, NULL);
668 690
669 /* Only drop into single user mode if we're booting */ 691 /* Only drop into single user mode if we're booting */
670 if ((PREVLEVEL && 692 if ((PREVLEVEL &&
671 (strcmp (PREVLEVEL, "S") == 0 || 693 (strcmp (PREVLEVEL, "S") == 0 ||
672 strcmp (PREVLEVEL, "1") == 0)) || 694 strcmp (PREVLEVEL, "1") == 0)) ||
711 733
712#include "_usage.h" 734#include "_usage.h"
713#define getoptstring getoptstring_COMMON 735#define getoptstring getoptstring_COMMON
714static struct option longopts[] = { 736static struct option longopts[] = {
715 longopts_COMMON 737 longopts_COMMON
716 { NULL, 0, NULL, 0}
717}; 738};
718static const char * const longopts_help[] = { 739static const char * const longopts_help[] = {
719 longopts_help_COMMON 740 longopts_help_COMMON
720}; 741};
721#include "_usage.c" 742#include "_usage.c"
722 743
723int main (int argc, char **argv) 744int main (int argc, char **argv)
724{ 745{
725 char *runlevel = NULL;
726 const char *bootlevel = NULL; 746 const char *bootlevel = NULL;
727 char *newlevel = NULL; 747 char *newlevel = NULL;
728 char *service = NULL; 748 char *service = NULL;
729 char **deporder = NULL; 749 char **deporder = NULL;
730 char **tmplist; 750 char **tmplist;
734 bool interactive = false; 754 bool interactive = false;
735 int depoptions = RC_DEP_STRICT | RC_DEP_TRACE; 755 int depoptions = RC_DEP_STRICT | RC_DEP_TRACE;
736 char ksoftbuffer [PATH_MAX]; 756 char ksoftbuffer [PATH_MAX];
737 char pidstr[6]; 757 char pidstr[6];
738 int opt; 758 int opt;
759 DIR *dp;
760 struct dirent *d;
739 761
740 atexit (cleanup); 762 atexit (cleanup);
741 if (argv[0]) 763 if (argv[0])
742 applet = rc_xstrdup (basename (argv[0])); 764 applet = xstrdup (basename (argv[0]));
743 765
744 if (! applet) 766 if (! applet)
745 eerrorx ("arguments required"); 767 eerrorx ("arguments required");
746 768
747 /* These used to be programs in their own right, so we shouldn't 769 /* These used to be programs in their own right, so we shouldn't
819 signal (SIGTERM, handle_signal); 841 signal (SIGTERM, handle_signal);
820 signal (SIGUSR1, handle_signal); 842 signal (SIGUSR1, handle_signal);
821 843
822 /* Ensure our environment is pure 844 /* Ensure our environment is pure
823 Also, add our configuration to it */ 845 Also, add our configuration to it */
824 env = rc_filter_env (); 846 env = env_filter ();
825 tmplist = rc_make_env (); 847 tmplist = env_config ();
826 rc_strlist_join (&env, tmplist); 848 rc_strlist_join (&env, tmplist);
827 rc_strlist_free (tmplist); 849 rc_strlist_free (tmplist);
828 850
829 if (env) { 851 if (env) {
830 char *p; 852 char *p;
838 /* No clearenv present here then. 860 /* No clearenv present here then.
839 We could manipulate environ directly ourselves, but it seems that 861 We could manipulate environ directly ourselves, but it seems that
840 some kernels bitch about this according to the environ man pages 862 some kernels bitch about this according to the environ man pages
841 so we walk though environ and call unsetenv for each value. */ 863 so we walk though environ and call unsetenv for each value. */
842 while (environ[0]) { 864 while (environ[0]) {
843 tmp = rc_xstrdup (environ[0]); 865 tmp = xstrdup (environ[0]);
844 p = tmp; 866 p = tmp;
845 var = strsep (&p, "="); 867 var = strsep (&p, "=");
846 unsetenv (var); 868 unsetenv (var);
847 free (tmp); 869 free (tmp);
848 } 870 }
878 900
879 /* Export our PID */ 901 /* Export our PID */
880 snprintf (pidstr, sizeof (pidstr), "%d", getpid ()); 902 snprintf (pidstr, sizeof (pidstr), "%d", getpid ());
881 setenv ("RC_PID", pidstr, 1); 903 setenv ("RC_PID", pidstr, 1);
882 904
883 interactive = rc_exists (INTERACTIVE); 905 interactive = exists (INTERACTIVE);
884 rc_plugin_load (); 906 rc_plugin_load ();
885 907
886 /* Load current softlevel */ 908 /* Load current softlevel */
887 bootlevel = getenv ("RC_BOOTLEVEL"); 909 bootlevel = getenv ("RC_BOOTLEVEL");
888 runlevel = rc_get_runlevel (); 910 runlevel = rc_runlevel_get ();
889 911
890 /* Check we're in the runlevel requested, ie from 912 /* Check we're in the runlevel requested, ie from
891 rc single 913 rc single
892 rc shutdown 914 rc shutdown
893 rc reboot 915 rc reboot
905#endif 927#endif
906 928
907 /* exec init-early.sh if it exists 929 /* exec init-early.sh if it exists
908 * This should just setup the console to use the correct 930 * This should just setup the console to use the correct
909 * font. Maybe it should setup the keyboard too? */ 931 * font. Maybe it should setup the keyboard too? */
910 if (rc_exists (INITEARLYSH)) 932 if (exists (INITEARLYSH))
911 run_script (INITEARLYSH); 933 run_script (INITEARLYSH);
912 934
913 uname (&uts); 935 uname (&uts);
914 936
915 printf ("\n"); 937 printf ("\n");
916 printf (" %sGentoo/%s; %shttp://www.gentoo.org/%s" 938 printf (" %sGentoo/%s; %shttp://www.gentoo.org/%s"
917 "\n Copyright 1999-2007 Gentoo Foundation; " 939 "\n Copyright 1999-2007 Gentoo Foundation; "
918 "Distributed under the GPLv2\n\n", 940 "Distributed under the GPLv2\n\n",
919 ecolor (ecolor_good), uts.sysname, ecolor (ecolor_bracket), 941 ecolor (ECOLOR_GOOD), uts.sysname, ecolor (ECOLOR_BRACKET),
920 ecolor (ecolor_normal)); 942 ecolor (ECOLOR_NORMAL));
921 943
922 if (rc_env_bool ("RC_INTERACTIVE")) 944 if (rc_env_bool ("RC_INTERACTIVE"))
923 printf ("Press %sI%s to enter interactive boot mode\n\n", 945 printf ("Press %sI%s to enter interactive boot mode\n\n",
924 ecolor (ecolor_good), ecolor (ecolor_normal)); 946 ecolor (ECOLOR_GOOD), ecolor (ECOLOR_NORMAL));
925 947
926 setenv ("RC_SOFTLEVEL", newlevel, 1); 948 setenv ("RC_SOFTLEVEL", newlevel, 1);
927 rc_plugin_run (rc_hook_runlevel_start_in, newlevel); 949 rc_plugin_run (RC_HOOK_RUNLEVEL_START_IN, newlevel);
928 run_script (INITSH); 950 run_script (INITSH);
929 951
930#ifdef __linux__ 952#ifdef __linux__
931 /* If we requested a softlevel, save it now */ 953 /* If we requested a softlevel, save it now */
932 set_ksoftlevel (NULL); 954 set_ksoftlevel (NULL);
934 set_ksoftlevel (cmd); 956 set_ksoftlevel (cmd);
935 free (cmd); 957 free (cmd);
936 } 958 }
937 959
938#endif 960#endif
939 rc_plugin_run (rc_hook_runlevel_start_out, newlevel); 961 rc_plugin_run (RC_HOOK_RUNLEVEL_START_OUT, newlevel);
940 962
941 if (want_interactive ()) 963 if (want_interactive ())
942 mark_interactive (); 964 mark_interactive ();
943 965
944 exit (EXIT_SUCCESS); 966 exit (EXIT_SUCCESS);
1003 (strcmp (newlevel, RC_LEVEL_REBOOT) == 0 || 1025 (strcmp (newlevel, RC_LEVEL_REBOOT) == 0 ||
1004 strcmp (newlevel, RC_LEVEL_SHUTDOWN) == 0 || 1026 strcmp (newlevel, RC_LEVEL_SHUTDOWN) == 0 ||
1005 strcmp (newlevel, RC_LEVEL_SINGLE) == 0)) 1027 strcmp (newlevel, RC_LEVEL_SINGLE) == 0))
1006 { 1028 {
1007 going_down = true; 1029 going_down = true;
1008 rc_set_runlevel (newlevel); 1030 rc_runlevel_set (newlevel);
1009 setenv ("RC_SOFTLEVEL", newlevel, 1); 1031 setenv ("RC_SOFTLEVEL", newlevel, 1);
1010 rc_plugin_run (rc_hook_runlevel_stop_in, newlevel); 1032 rc_plugin_run (RC_HOOK_RUNLEVEL_STOP_IN, newlevel);
1011 } else { 1033 } else {
1012 rc_plugin_run (rc_hook_runlevel_stop_in, runlevel); 1034 rc_plugin_run (RC_HOOK_RUNLEVEL_STOP_IN, runlevel);
1013 } 1035 }
1014 1036
1015 /* Check if runlevel is valid if we're changing */ 1037 /* Check if runlevel is valid if we're changing */
1016 if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) { 1038 if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) {
1017 tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL); 1039 if (! rc_runlevel_exists (newlevel))
1018 if (! rc_is_dir (tmp))
1019 eerrorx ("%s: is not a valid runlevel", newlevel); 1040 eerrorx ("%s: is not a valid runlevel", newlevel);
1020 CHAR_FREE (tmp);
1021 } 1041 }
1022 1042
1023 /* Load our deptree now */ 1043 /* Load our deptree now */
1024 if ((deptree = rc_load_deptree ()) == NULL) 1044 if ((deptree = _rc_deptree_load ()) == NULL)
1025 eerrorx ("failed to load deptree"); 1045 eerrorx ("failed to load deptree");
1026 1046
1027 /* Clean the failed services state dir now */ 1047 /* Clean the failed services state dir now */
1028 if (rc_is_dir (RC_SVCDIR "/failed")) 1048 if ((dp = opendir (RC_SVCDIR "/failed"))) {
1029 rc_rm_dir (RC_SVCDIR "/failed", false); 1049 while ((d = readdir (dp))) {
1050 if (d->d_name[0] == '.' &&
1051 (d->d_name[1] == '\0' ||
1052 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
1053 continue;
1054
1055 asprintf (&tmp, RC_SVCDIR "/failed/%s", d->d_name);
1056 if (tmp) {
1057 if (unlink (tmp))
1058 eerror ("%s: unlink `%s': %s", applet, tmp,
1059 strerror (errno));
1060 free (tmp);
1061 }
1062 }
1063 closedir (dp);
1064 }
1030 1065
1031 mkdir (RC_STOPPING, 0755); 1066 mkdir (RC_STOPPING, 0755);
1032 1067
1033#ifdef __linux__ 1068#ifdef __linux__
1034 /* udev likes to start services before we're ready when it does 1069 /* udev likes to start services before we're ready when it does
1035 its coldplugging thing. runscript knows when we're not ready so it 1070 its coldplugging thing. runscript knows when we're not ready so it
1036 stores a list of coldplugged services in DEVBOOT for us to pick up 1071 stores a list of coldplugged services in DEVBOOT for us to pick up
1037 here when we are ready for them */ 1072 here when we are ready for them */
1038 if (rc_is_dir (DEVBOOT)) { 1073 if ((dp = opendir (DEVBOOT))) {
1039 start_services = rc_ls_dir (DEVBOOT, RC_LS_INITD); 1074 while ((d = readdir (dp))) {
1075 if (d->d_name[0] == '.' &&
1076 (d->d_name[1] == '\0' ||
1077 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
1078 continue;
1079
1080 if (rc_service_exists (d->d_name) &&
1081 rc_service_plugable (d->d_name))
1082 rc_service_mark (d->d_name, RC_SERVICE_COLDPLUGGED);
1083
1084 asprintf (&tmp, DEVBOOT "/%s", d->d_name);
1085 if (tmp) {
1086 if (unlink (tmp))
1087 eerror ("%s: unlink `%s': %s", applet, tmp,
1088 strerror (errno));
1089 free (tmp);
1090 }
1091 }
1092 closedir (dp);
1040 rc_rm_dir (DEVBOOT, true); 1093 rmdir (DEVBOOT);
1041
1042 STRLIST_FOREACH (start_services, service, i)
1043 if (rc_allow_plug (service))
1044 rc_mark_service (service, rc_service_coldplugged);
1045 /* We need to dump this list now.
1046 This may seem redunant, but only Linux needs this and saves on
1047 code bloat. */
1048 rc_strlist_free (start_services);
1049 start_services = NULL;
1050 } 1094 }
1051#else 1095#else
1052 /* BSD's on the other hand populate /dev automagically and use devd. 1096 /* BSD's on the other hand populate /dev automagically and use devd.
1053 The only downside of this approach and ours is that we have to hard code 1097 The only downside of this approach and ours is that we have to hard code
1054 the device node to the init script to simulate the coldplug into 1098 the device node to the init script to simulate the coldplug into
1058 strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) && 1102 strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) &&
1059 rc_env_bool ("RC_COLDPLUG")) 1103 rc_env_bool ("RC_COLDPLUG"))
1060 { 1104 {
1061#if defined(__DragonFly__) || defined(__FreeBSD__) 1105#if defined(__DragonFly__) || defined(__FreeBSD__)
1062 /* The net interfaces are easy - they're all in net /dev/net :) */ 1106 /* The net interfaces are easy - they're all in net /dev/net :) */
1063 start_services = rc_ls_dir ("/dev/net", 0); 1107 if ((dp = opendir ("/dev/net"))) {
1064 STRLIST_FOREACH (start_services, service, i) { 1108 while ((d = readdir (dp))) {
1065 j = (strlen ("net.") + strlen (service) + 1); 1109 i = (strlen ("net.") + strlen (d->d_name) + 1);
1066 tmp = rc_xmalloc (sizeof (char *) * j); 1110 tmp = xmalloc (sizeof (char) * i);
1067 snprintf (tmp, j, "net.%s", service); 1111 snprintf (tmp, i, "net.%s", d->d_name);
1068 if (rc_service_exists (tmp) && rc_allow_plug (tmp)) 1112 if (rc_service_exists (tmp) &&
1069 rc_mark_service (tmp, rc_service_coldplugged); 1113 rc_service_plugable (tmp))
1114 rc_service_mark (tmp, RC_SERVICE_COLDPLUGGED);
1070 CHAR_FREE (tmp); 1115 CHAR_FREE (tmp);
1071 } 1116 }
1072 rc_strlist_free (start_services); 1117 closedir (dp);
1118 }
1073#endif 1119#endif
1074 1120
1075 /* The mice are a little more tricky. 1121 /* The mice are a little more tricky.
1076 If we coldplug anything else, we'll probably do it here. */ 1122 If we coldplug anything else, we'll probably do it here. */
1077 start_services = rc_ls_dir ("/dev", 0); 1123 if ((dp == opendir ("/dev"))) {
1078 STRLIST_FOREACH (start_services, service, i) { 1124 while ((d = readdir (dp))) {
1079 if (strncmp (service, "psm", 3) == 0 || 1125 if (strncmp (d->d_name, "psm", 3) == 0 ||
1080 strncmp (service, "ums", 3) == 0) 1126 strncmp (d->d_name, "ums", 3) == 0)
1081 { 1127 {
1082 char *p = service + 3; 1128 char *p = d->d_name + 3;
1083 if (p && isdigit (*p)) { 1129 if (p && isdigit (*p)) {
1084 j = (strlen ("moused.") + strlen (service) + 1); 1130 i = (strlen ("moused.") + strlen (d->d_name) + 1);
1085 tmp = rc_xmalloc (sizeof (char *) * j); 1131 tmp = xmalloc (sizeof (char) * i);
1086 snprintf (tmp, j, "moused.%s", service); 1132 snprintf (tmp, i, "moused.%s", d->d_name);
1087 if (rc_service_exists (tmp) && rc_allow_plug (tmp)) 1133 if (rc_service_exists (tmp) && rc_service_plugable (tmp))
1088 rc_mark_service (tmp, rc_service_coldplugged); 1134 rc_service_mark (tmp, RC_SERVICE_COLDPLUGGED);
1089 CHAR_FREE (tmp); 1135 CHAR_FREE (tmp);
1136 }
1090 } 1137 }
1091 } 1138 }
1139 closedir (dp);
1092 } 1140 }
1093 rc_strlist_free (start_services);
1094 start_services = NULL;
1095 } 1141 }
1096#endif 1142#endif
1097 1143
1098 /* Build a list of all services to stop and then work out the 1144 /* Build a list of all services to stop and then work out the
1099 correct order for stopping them */ 1145 correct order for stopping them */
1100 stop_services = rc_ls_dir (RC_SVCDIR_STARTING, RC_LS_INITD); 1146 stop_services = rc_services_in_state (RC_SERVICE_STARTING);
1101 1147
1102 tmplist = rc_ls_dir (RC_SVCDIR_INACTIVE, RC_LS_INITD); 1148 tmplist = rc_services_in_state (RC_SERVICE_INACTIVE);
1103 rc_strlist_join (&stop_services, tmplist); 1149 rc_strlist_join (&stop_services, tmplist);
1104 rc_strlist_free (tmplist); 1150 rc_strlist_free (tmplist);
1105 1151
1106 tmplist = rc_ls_dir (RC_SVCDIR_STARTED, RC_LS_INITD); 1152 tmplist = rc_services_in_state (RC_SERVICE_STARTED);
1107 rc_strlist_join (&stop_services, tmplist); 1153 rc_strlist_join (&stop_services, tmplist);
1108 rc_strlist_free (tmplist); 1154 rc_strlist_free (tmplist);
1109 1155
1110 types = NULL; 1156 types = NULL;
1111 rc_strlist_add (&types, "ineed"); 1157 rc_strlist_add (&types, "ineed");
1112 rc_strlist_add (&types, "iuse"); 1158 rc_strlist_add (&types, "iuse");
1113 rc_strlist_add (&types, "iafter"); 1159 rc_strlist_add (&types, "iafter");
1114 1160
1115 deporder = rc_get_depends (deptree, types, stop_services, 1161 deporder = rc_deptree_depends (deptree, types, stop_services,
1116 runlevel, depoptions | RC_DEP_STOP); 1162 runlevel, depoptions | RC_DEP_STOP);
1117 1163
1118 rc_strlist_free (stop_services); 1164 rc_strlist_free (stop_services);
1119 rc_strlist_free (types); 1165 rc_strlist_free (types);
1120 types = NULL; 1166 types = NULL;
1121 stop_services = deporder; 1167 stop_services = deporder;
1122 deporder = NULL; 1168 deporder = NULL;
1123 rc_strlist_reverse (stop_services); 1169 rc_strlist_reverse (stop_services);
1124 1170
1125 /* Load our list of coldplugged services */ 1171 /* Load our list of coldplugged services */
1126 coldplugged_services = rc_ls_dir (RC_SVCDIR_COLDPLUGGED, RC_LS_INITD); 1172 coldplugged_services = rc_services_in_state (RC_SERVICE_COLDPLUGGED);
1127 1173
1128 /* Load our start services now. 1174 /* Load our start services now.
1129 We have different rules dependent on runlevel. */ 1175 We have different rules dependent on runlevel. */
1130 if (newlevel && strcmp (newlevel, bootlevel) == 0) { 1176 if (newlevel && strcmp (newlevel, bootlevel) == 0) {
1131 if (coldplugged_services) { 1177 if (coldplugged_services) {
1134 printf (" %s", service); 1180 printf (" %s", service);
1135 rc_strlist_add (&start_services, service); 1181 rc_strlist_add (&start_services, service);
1136 } 1182 }
1137 printf ("\n"); 1183 printf ("\n");
1138 } 1184 }
1139 tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel ? newlevel : runlevel, 1185 tmplist = rc_services_in_runlevel (newlevel ? newlevel : runlevel);
1140 (char *) NULL);
1141 tmplist = rc_ls_dir (tmp, RC_LS_INITD);
1142 rc_strlist_join (&start_services, tmplist); 1186 rc_strlist_join (&start_services, tmplist);
1143 rc_strlist_free (tmplist); 1187 rc_strlist_free (tmplist);
1144 CHAR_FREE (tmp);
1145 } else { 1188 } else {
1146 /* Store our list of coldplugged services */ 1189 /* Store our list of coldplugged services */
1147 tmplist = rc_ls_dir (RC_SVCDIR_COLDPLUGGED, RC_LS_INITD); 1190 tmplist = rc_services_in_state (RC_SERVICE_COLDPLUGGED);
1148 rc_strlist_join (&coldplugged_services, tmplist); 1191 rc_strlist_join (&coldplugged_services, tmplist);
1149 rc_strlist_free (tmplist); 1192 rc_strlist_free (tmplist);
1150 if (strcmp (newlevel ? newlevel : runlevel, RC_LEVEL_SINGLE) != 0 && 1193 if (strcmp (newlevel ? newlevel : runlevel, RC_LEVEL_SINGLE) != 0 &&
1151 strcmp (newlevel ? newlevel : runlevel, RC_LEVEL_SHUTDOWN) != 0 && 1194 strcmp (newlevel ? newlevel : runlevel, RC_LEVEL_SHUTDOWN) != 0 &&
1152 strcmp (newlevel ? newlevel : runlevel, RC_LEVEL_REBOOT) != 0) 1195 strcmp (newlevel ? newlevel : runlevel, RC_LEVEL_REBOOT) != 0)
1156 rc_strlist_join (&start_services, tmplist); 1199 rc_strlist_join (&start_services, tmplist);
1157 rc_strlist_free (tmplist); 1200 rc_strlist_free (tmplist);
1158 tmplist = rc_services_in_runlevel (newlevel ? newlevel : runlevel); 1201 tmplist = rc_services_in_runlevel (newlevel ? newlevel : runlevel);
1159 rc_strlist_join (&start_services, tmplist); 1202 rc_strlist_join (&start_services, tmplist);
1160 rc_strlist_free (tmplist); 1203 rc_strlist_free (tmplist);
1161 1204
1162 STRLIST_FOREACH (coldplugged_services, service, i) 1205 STRLIST_FOREACH (coldplugged_services, service, i)
1163 rc_strlist_add (&start_services, service); 1206 rc_strlist_add (&start_services, service);
1164 1207
1165 } 1208 }
1166 } 1209 }
1167 1210
1168 /* Save out softlevel now */ 1211 /* Save out softlevel now */
1169 if (going_down) 1212 if (going_down)
1170 rc_set_runlevel (newlevel); 1213 rc_runlevel_set (newlevel);
1171 1214
1172 types = NULL; 1215 types = NULL;
1173 rc_strlist_add (&types, "needsme"); 1216 rc_strlist_add (&types, "needsme");
1174 /* Now stop the services that shouldn't be running */ 1217 /* Now stop the services that shouldn't be running */
1175 STRLIST_FOREACH (stop_services, service, i) { 1218 STRLIST_FOREACH (stop_services, service, i) {
1178 char **stopdeps = NULL; 1221 char **stopdeps = NULL;
1179 char *svc1 = NULL; 1222 char *svc1 = NULL;
1180 char *svc2 = NULL; 1223 char *svc2 = NULL;
1181 int k; 1224 int k;
1182 1225
1183 if (rc_service_state (service, rc_service_stopped)) 1226 if (rc_service_state (service) & RC_SERVICE_STOPPED)
1184 continue; 1227 continue;
1185 1228
1186 /* We always stop the service when in these runlevels */ 1229 /* We always stop the service when in these runlevels */
1187 if (going_down) { 1230 if (going_down) {
1188 pid_t pid = rc_stop_service (service); 1231 pid_t pid = rc_service_stop (service);
1189 if (pid > 0 && ! rc_env_bool ("RC_PARALLEL")) 1232 if (pid > 0 && ! rc_env_bool ("RC_PARALLEL"))
1190 rc_waitpid (pid); 1233 wait_pid (pid);
1191 continue; 1234 continue;
1192 } 1235 }
1193 1236
1194 /* If we're in the start list then don't bother stopping us */ 1237 /* If we're in the start list then don't bother stopping us */
1195 STRLIST_FOREACH (start_services, svc1, j) 1238 STRLIST_FOREACH (start_services, svc1, j)
1203 int len; 1246 int len;
1204 if (! newlevel) 1247 if (! newlevel)
1205 continue; 1248 continue;
1206 1249
1207 len = strlen (service) + strlen (runlevel) + 2; 1250 len = strlen (service) + strlen (runlevel) + 2;
1208 tmp = rc_xmalloc (sizeof (char *) * len); 1251 tmp = xmalloc (sizeof (char) * len);
1209 snprintf (tmp, len, "%s.%s", service, runlevel); 1252 snprintf (tmp, len, "%s.%s", service, runlevel);
1210 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL); 1253 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL);
1211 found = rc_exists (conf); 1254 found = exists (conf);
1212 CHAR_FREE (conf); 1255 CHAR_FREE (conf);
1213 CHAR_FREE (tmp); 1256 CHAR_FREE (tmp);
1214 if (! found) { 1257 if (! found) {
1215 len = strlen (service) + strlen (newlevel) + 2; 1258 len = strlen (service) + strlen (newlevel) + 2;
1216 tmp = rc_xmalloc (sizeof (char *) * len); 1259 tmp = xmalloc (sizeof (char) * len);
1217 snprintf (tmp, len, "%s.%s", service, newlevel); 1260 snprintf (tmp, len, "%s.%s", service, newlevel);
1218 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL); 1261 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL);
1219 found = rc_exists (conf); 1262 found = exists (conf);
1220 CHAR_FREE (conf); 1263 CHAR_FREE (conf);
1221 CHAR_FREE (tmp); 1264 CHAR_FREE (tmp);
1222 if (!found) 1265 if (!found)
1223 continue; 1266 continue;
1224 } 1267 }
1225 } else { 1268 } else {
1226 /* Allow coldplugged services not to be in the runlevels list */ 1269 /* Allow coldplugged services not to be in the runlevels list */
1227 if (rc_service_state (service, rc_service_coldplugged)) 1270 if (rc_service_state (service) & RC_SERVICE_COLDPLUGGED)
1228 continue; 1271 continue;
1229 } 1272 }
1230 1273
1231 /* We got this far! Or last check is to see if any any service that 1274 /* We got this far! Or last check is to see if any any service that
1232 going to be started depends on us */ 1275 going to be started depends on us */
1233 rc_strlist_add (&stopdeps, service); 1276 rc_strlist_add (&stopdeps, service);
1234 deporder = rc_get_depends (deptree, types, stopdeps, 1277 deporder = rc_deptree_depends (deptree, types, stopdeps,
1235 runlevel, RC_DEP_STRICT); 1278 runlevel, RC_DEP_STRICT);
1236 rc_strlist_free (stopdeps); 1279 rc_strlist_free (stopdeps);
1237 stopdeps = NULL; 1280 stopdeps = NULL;
1238 found = false; 1281 found = false;
1239 STRLIST_FOREACH (deporder, svc1, j) { 1282 STRLIST_FOREACH (deporder, svc1, j) {
1240 STRLIST_FOREACH (start_services, svc2, k) 1283 STRLIST_FOREACH (start_services, svc2, k)
1248 rc_strlist_free (deporder); 1291 rc_strlist_free (deporder);
1249 deporder = NULL; 1292 deporder = NULL;
1250 1293
1251 /* After all that we can finally stop the blighter! */ 1294 /* After all that we can finally stop the blighter! */
1252 if (! found) { 1295 if (! found) {
1253 pid_t pid = rc_stop_service (service); 1296 pid_t pid = rc_service_stop (service);
1254 if (pid > 0 && ! rc_env_bool ("RC_PARALLEL")) 1297 if (pid > 0 && ! rc_env_bool ("RC_PARALLEL"))
1255 rc_waitpid (pid); 1298 wait_pid (pid);
1256 } 1299 }
1257 } 1300 }
1258 rc_strlist_free (types); 1301 rc_strlist_free (types);
1259 types = NULL; 1302 types = NULL;
1260 1303
1261 /* Wait for our services to finish */ 1304 /* Wait for our services to finish */
1262 wait_for_services (); 1305 wait_for_services ();
1263 1306
1264 /* Notify the plugins we have finished */ 1307 /* Notify the plugins we have finished */
1265 rc_plugin_run (rc_hook_runlevel_stop_out, runlevel); 1308 rc_plugin_run (RC_HOOK_RUNLEVEL_STOP_OUT, runlevel);
1266 1309
1267 rmdir (RC_STOPPING); 1310 rmdir (RC_STOPPING);
1268 1311
1269 /* Store the new runlevel */ 1312 /* Store the new runlevel */
1270 if (newlevel) { 1313 if (newlevel) {
1271 rc_set_runlevel (newlevel); 1314 rc_runlevel_set (newlevel);
1315 free (runlevel);
1272 runlevel = newlevel; 1316 runlevel = xstrdup (newlevel);
1273 setenv ("RC_SOFTLEVEL", runlevel, 1); 1317 setenv ("RC_SOFTLEVEL", runlevel, 1);
1274 } 1318 }
1275 1319
1276 /* Run the halt script if needed */ 1320 /* Run the halt script if needed */
1277 if (strcmp (runlevel, RC_LEVEL_SHUTDOWN) == 0 || 1321 if (strcmp (runlevel, RC_LEVEL_SHUTDOWN) == 0 ||
1282 applet, HALTSH, strerror (errno)); 1326 applet, HALTSH, strerror (errno));
1283 } 1327 }
1284 1328
1285 /* Single user is done now */ 1329 /* Single user is done now */
1286 if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) { 1330 if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) {
1287 if (rc_exists (INTERACTIVE)) 1331 if (exists (INTERACTIVE))
1288 unlink (INTERACTIVE); 1332 unlink (INTERACTIVE);
1289 sulogin (false); 1333 sulogin (false);
1290 } 1334 }
1291 1335
1292 mkdir (RC_STARTING, 0755); 1336 mkdir (RC_STARTING, 0755);
1293 rc_plugin_run (rc_hook_runlevel_start_in, runlevel); 1337 rc_plugin_run (RC_HOOK_RUNLEVEL_START_IN, runlevel);
1294 1338
1295 /* Re-add our coldplugged services if they stopped */ 1339 /* Re-add our coldplugged services if they stopped */
1296 STRLIST_FOREACH (coldplugged_services, service, i) 1340 STRLIST_FOREACH (coldplugged_services, service, i)
1297 rc_mark_service (service, rc_service_coldplugged); 1341 rc_service_mark (service, RC_SERVICE_COLDPLUGGED);
1298 1342
1299 /* Order the services to start */ 1343 /* Order the services to start */
1300 rc_strlist_add (&types, "ineed"); 1344 rc_strlist_add (&types, "ineed");
1301 rc_strlist_add (&types, "iuse"); 1345 rc_strlist_add (&types, "iuse");
1302 rc_strlist_add (&types, "iafter"); 1346 rc_strlist_add (&types, "iafter");
1303 deporder = rc_get_depends (deptree, types, start_services, 1347 deporder = rc_deptree_depends (deptree, types, start_services,
1304 runlevel, depoptions | RC_DEP_START); 1348 runlevel, depoptions | RC_DEP_START);
1305 rc_strlist_free (types); 1349 rc_strlist_free (types);
1306 types = NULL; 1350 types = NULL;
1307 rc_strlist_free (start_services); 1351 rc_strlist_free (start_services);
1308 start_services = deporder; 1352 start_services = deporder;
1309 deporder = NULL; 1353 deporder = NULL;
1314 if ((service = proc_getent ("noinitd"))) { 1358 if ((service = proc_getent ("noinitd"))) {
1315 char *p = service; 1359 char *p = service;
1316 char *token; 1360 char *token;
1317 1361
1318 while ((token = strsep (&p, ","))) 1362 while ((token = strsep (&p, ",")))
1319 rc_mark_service (token, rc_service_started); 1363 rc_service_mark (token, RC_SERVICE_STARTED);
1320 free (service); 1364 free (service);
1321 } 1365 }
1322 } 1366 }
1323#endif 1367#endif
1324 1368
1325 1369
1326 STRLIST_FOREACH (start_services, service, i) { 1370 STRLIST_FOREACH (start_services, service, i) {
1327 if (rc_service_state (service, rc_service_stopped)) { 1371 if (rc_service_state (service) & RC_SERVICE_STOPPED) {
1328 pid_t pid; 1372 pid_t pid;
1329 1373
1330 if (! interactive) 1374 if (! interactive)
1331 interactive = want_interactive (); 1375 interactive = want_interactive ();
1332 1376
1347 default: goto interactive_option; 1391 default: goto interactive_option;
1348 } 1392 }
1349 } 1393 }
1350 1394
1351 /* Remember the pid if we're running in parallel */ 1395 /* Remember the pid if we're running in parallel */
1352 if ((pid = rc_start_service (service))) 1396 if ((pid = rc_service_start (service)))
1353 add_pid (pid); 1397 add_pid (pid);
1354 1398
1355 if (! rc_env_bool ("RC_PARALLEL")) { 1399 if (! rc_env_bool ("RC_PARALLEL")) {
1356 rc_waitpid (pid); 1400 wait_pid (pid);
1357 remove_pid (pid); 1401 remove_pid (pid);
1358 } 1402 }
1359 } 1403 }
1360 } 1404 }
1361 1405
1362 /* Wait for our services to finish */ 1406 /* Wait for our services to finish */
1363 wait_for_services (); 1407 wait_for_services ();
1364 1408
1365 rc_plugin_run (rc_hook_runlevel_start_out, runlevel); 1409 rc_plugin_run (RC_HOOK_RUNLEVEL_START_OUT, runlevel);
1366 1410
1367#ifdef __linux__ 1411#ifdef __linux__
1368 /* mark any services skipped as stopped */ 1412 /* mark any services skipped as stopped */
1369 if (PREVLEVEL && strcmp (PREVLEVEL, "N") == 0) { 1413 if (PREVLEVEL && strcmp (PREVLEVEL, "N") == 0) {
1370 if ((service = proc_getent ("noinitd"))) { 1414 if ((service = proc_getent ("noinitd"))) {
1371 char *p = service; 1415 char *p = service;
1372 char *token; 1416 char *token;
1373 1417
1374 while ((token = strsep (&p, ","))) 1418 while ((token = strsep (&p, ",")))
1375 rc_mark_service (token, rc_service_stopped); 1419 rc_service_mark (token, RC_SERVICE_STOPPED);
1376 free (service); 1420 free (service);
1377 } 1421 }
1378 } 1422 }
1379#endif 1423#endif
1380 1424
1381 /* Store our interactive status for boot */ 1425 /* Store our interactive status for boot */
1382 if (interactive && strcmp (runlevel, bootlevel) == 0) 1426 if (interactive && strcmp (runlevel, bootlevel) == 0)
1383 mark_interactive (); 1427 mark_interactive ();
1384 else { 1428 else {
1385 if (rc_exists (INTERACTIVE)) 1429 if (exists (INTERACTIVE))
1386 unlink (INTERACTIVE); 1430 unlink (INTERACTIVE);
1387 } 1431 }
1388 1432
1389 return (EXIT_SUCCESS); 1433 return (EXIT_SUCCESS);
1390} 1434}

Legend:
Removed from v.2936  
changed lines
  Added in v.3036

  ViewVC Help
Powered by ViewVC 1.1.20