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

Diff of /trunk/src/rc.c

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

Revision 2923 Revision 2927
107 rc_free_deptree (deptree); 107 rc_free_deptree (deptree);
108 rc_strlist_free (types); 108 rc_strlist_free (types);
109 109
110 /* Clean runlevel start, stop markers */ 110 /* Clean runlevel start, stop markers */
111 if (! rc_in_plugin) { 111 if (! rc_in_plugin) {
112 if (rc_is_dir (RC_STARTING)) 112 if (rc_is_dir (RC_STARTING) == 0)
113 rc_rm_dir (RC_STARTING, true); 113 rc_rm_dir (RC_STARTING, true);
114 if (rc_is_dir (RC_STOPPING)) 114 if (rc_is_dir (RC_STOPPING) == 0)
115 rc_rm_dir (RC_STOPPING, true); 115 rc_rm_dir (RC_STOPPING, true);
116 } 116 }
117 } 117 }
118 118
119 free (applet); 119 free (applet);
261 return (retval); 261 return (retval);
262} 262}
263 263
264static int do_service (int argc, char **argv) 264static int do_service (int argc, char **argv)
265{ 265{
266 bool ok = false; 266 int ok = -1;
267 267
268 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0) 268 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
269 eerrorx ("%s: no service specified", applet); 269 eerrorx ("%s: no service specified", applet);
270 270
271 if (strcmp (applet, "service_started") == 0) 271 if (strcmp (applet, "service_started") == 0)
284 ok = rc_service_state (argv[0], rc_service_wasinactive); 284 ok = rc_service_state (argv[0], rc_service_wasinactive);
285 else if (strcmp (applet, "service_started_daemon") == 0) { 285 else if (strcmp (applet, "service_started_daemon") == 0) {
286 int idx = 0; 286 int idx = 0;
287 if (argc > 2) 287 if (argc > 2)
288 sscanf (argv[2], "%d", &idx); 288 sscanf (argv[2], "%d", &idx);
289 exit (rc_service_started_daemon (argv[0], argv[1], idx) 289 ok = rc_service_started_daemon (argv[0], argv[1], idx);
290 ? 0 : 1);
291 } else 290 } else
292 eerrorx ("%s: unknown applet", applet); 291 eerrorx ("%s: unknown applet", applet);
293 292
294 return (ok ? EXIT_SUCCESS : EXIT_FAILURE); 293 return (ok);
295} 294}
296 295
297static int do_mark_service (int argc, char **argv) 296static int do_mark_service (int argc, char **argv)
298{ 297{
299 bool ok = false; 298 int ok = -1;
300 char *svcname = getenv ("SVCNAME"); 299 char *svcname = getenv ("SVCNAME");
301 300
302 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0) 301 if (argc < 1 || ! argv[0] || strlen (argv[0]) == 0)
303 eerrorx ("%s: no service specified", applet); 302 eerrorx ("%s: no service specified", applet);
304 303
317 else 316 else
318 eerrorx ("%s: unknown applet", applet); 317 eerrorx ("%s: unknown applet", applet);
319 318
320 /* If we're marking ourselves then we need to inform our parent runscript 319 /* 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 */ 320 process so they do not mark us based on our exit code */
322 if (ok && svcname && strcmp (svcname, argv[0]) == 0) { 321 if (ok == 0 && svcname && strcmp (svcname, argv[0]) == 0) {
323 char *runscript_pid = getenv ("RC_RUNSCRIPT_PID"); 322 char *runscript_pid = getenv ("RC_RUNSCRIPT_PID");
324 char *mtime; 323 char *mtime;
325 pid_t pid = 0; 324 pid_t pid = 0;
326 int l; 325 int l;
327 326
337 strlen (runscript_pid) + 336 strlen (runscript_pid) +
338 4; 337 4;
339 mtime = rc_xmalloc (l); 338 mtime = rc_xmalloc (l);
340 snprintf (mtime, l, RC_SVCDIR "exclusive/%s.%s", 339 snprintf (mtime, l, RC_SVCDIR "exclusive/%s.%s",
341 svcname, runscript_pid); 340 svcname, runscript_pid);
342 if (rc_exists (mtime) && unlink (mtime) != 0) 341 if (rc_exists (mtime) == 0 && unlink (mtime) != 0)
343 eerror ("%s: unlink: %s", applet, strerror (errno)); 342 eerror ("%s: unlink: %s", applet, strerror (errno));
344 free (mtime); 343 free (mtime);
345 } 344 }
346 345
347 return (ok ? EXIT_SUCCESS : EXIT_FAILURE); 346 return (ok);
348} 347}
349 348
350static int do_options (int argc, char **argv) 349static int do_options (int argc, char **argv)
351{ 350{
352 bool ok = false; 351 int ok = -1;
353 char *service = getenv ("SVCNAME"); 352 char *service = getenv ("SVCNAME");
354 353
355 if (! service) 354 if (! service)
356 eerrorx ("%s: no service specified", applet); 355 eerrorx ("%s: no service specified", applet);
357 356
361 if (strcmp (applet, "get_options") == 0) { 360 if (strcmp (applet, "get_options") == 0) {
362 char *option = rc_get_service_option (service, argv[0]); 361 char *option = rc_get_service_option (service, argv[0]);
363 if (option) { 362 if (option) {
364 printf ("%s", option); 363 printf ("%s", option);
365 free (option); 364 free (option);
366 ok = true; 365 ok = 0;
367 } 366 }
368 } else if (strcmp (applet, "save_options") == 0) 367 } else if (strcmp (applet, "save_options") == 0)
369 ok = rc_set_service_option (service, argv[0], argv[1]); 368 ok = rc_set_service_option (service, argv[0], argv[1]);
370 else 369 else
371 eerrorx ("%s: unknown applet", applet); 370 eerrorx ("%s: unknown applet", applet);
372 371
373 return (ok ? EXIT_SUCCESS : EXIT_FAILURE); 372 return (ok);
374} 373}
375 374
376#ifdef __linux__ 375#ifdef __linux__
377static char *proc_getent (const char *ent) 376static char *proc_getent (const char *ent)
378{ 377{
380 char buffer[RC_LINEBUFFER]; 379 char buffer[RC_LINEBUFFER];
381 char *p; 380 char *p;
382 char *value = NULL; 381 char *value = NULL;
383 int i; 382 int i;
384 383
385 if (! rc_exists ("/proc/cmdline")) 384 if (rc_exists ("/proc/cmdline") != 0)
386 return (NULL); 385 return (NULL);
387 386
388 if (! (fp = fopen ("/proc/cmdline", "r"))) { 387 if (! (fp = fopen ("/proc/cmdline", "r"))) {
389 eerror ("failed to open `/proc/cmdline': %s", strerror (errno)); 388 eerror ("failed to open `/proc/cmdline': %s", strerror (errno));
390 return (NULL); 389 return (NULL);
543 if (! runlevel || 542 if (! runlevel ||
544 strcmp (runlevel, getenv ("RC_BOOTLEVEL")) == 0 || 543 strcmp (runlevel, getenv ("RC_BOOTLEVEL")) == 0 ||
545 strcmp (runlevel, RC_LEVEL_SINGLE) == 0 || 544 strcmp (runlevel, RC_LEVEL_SINGLE) == 0 ||
546 strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) 545 strcmp (runlevel, RC_LEVEL_SYSINIT) == 0)
547 { 546 {
548 if (rc_exists (RC_KSOFTLEVEL) && 547 if (rc_exists (RC_KSOFTLEVEL) == 0 &&
549 unlink (RC_KSOFTLEVEL) != 0) 548 unlink (RC_KSOFTLEVEL) != 0)
550 eerror ("unlink `%s': %s", RC_KSOFTLEVEL, strerror (errno)); 549 eerror ("unlink `%s': %s", RC_KSOFTLEVEL, strerror (errno));
551 return; 550 return;
552 } 551 }
553 552
563static int get_ksoftlevel (char *buffer, int buffer_len) 562static int get_ksoftlevel (char *buffer, int buffer_len)
564{ 563{
565 FILE *fp; 564 FILE *fp;
566 int i = 0; 565 int i = 0;
567 566
568 if (! rc_exists (RC_KSOFTLEVEL)) 567 if (rc_exists (RC_KSOFTLEVEL) != 0)
569 return (0); 568 return (0);
570 569
571 if (! (fp = fopen (RC_KSOFTLEVEL, "r"))) { 570 if (! (fp = fopen (RC_KSOFTLEVEL, "r"))) {
572 eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno)); 571 eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno));
573 return (-1); 572 return (-1);
782 781
783 if (strncmp (applet, "mark_service_", strlen ("mark_service_")) == 0) 782 if (strncmp (applet, "mark_service_", strlen ("mark_service_")) == 0)
784 exit (do_mark_service (argc, argv)); 783 exit (do_mark_service (argc, argv));
785 784
786 if (strcmp (applet, "is_runlevel_start") == 0) 785 if (strcmp (applet, "is_runlevel_start") == 0)
787 exit (rc_runlevel_starting () ? 0 : 1); 786 exit (rc_runlevel_starting ());
788 else if (strcmp (applet, "is_runlevel_stop") == 0) 787 else if (strcmp (applet, "is_runlevel_stop") == 0)
789 exit (rc_runlevel_stopping () ? 0 : 1); 788 exit (rc_runlevel_stopping ());
790 789
791 if (strcmp (applet, "rc-abort") == 0) { 790 if (strcmp (applet, "rc-abort") == 0) {
792 char *p = getenv ("RC_PID"); 791 char *p = getenv ("RC_PID");
793 pid_t pid = 0; 792 pid_t pid = 0;
794 793
878 877
879 /* Export our PID */ 878 /* Export our PID */
880 snprintf (pidstr, sizeof (pidstr), "%d", getpid ()); 879 snprintf (pidstr, sizeof (pidstr), "%d", getpid ());
881 setenv ("RC_PID", pidstr, 1); 880 setenv ("RC_PID", pidstr, 1);
882 881
883 interactive = rc_exists (INTERACTIVE); 882 interactive = (rc_exists (INTERACTIVE) == 0);
884 rc_plugin_load (); 883 rc_plugin_load ();
885 884
886 /* Load current softlevel */ 885 /* Load current softlevel */
887 bootlevel = getenv ("RC_BOOTLEVEL"); 886 bootlevel = getenv ("RC_BOOTLEVEL");
888 runlevel = rc_get_runlevel (); 887 runlevel = rc_get_runlevel ();
905#endif 904#endif
906 905
907 /* exec init-early.sh if it exists 906 /* exec init-early.sh if it exists
908 * This should just setup the console to use the correct 907 * This should just setup the console to use the correct
909 * font. Maybe it should setup the keyboard too? */ 908 * font. Maybe it should setup the keyboard too? */
910 if (rc_exists (INITEARLYSH)) 909 if (rc_exists (INITEARLYSH) == 0)
911 run_script (INITEARLYSH); 910 run_script (INITEARLYSH);
912 911
913 uname (&uts); 912 uname (&uts);
914 913
915 printf ("\n"); 914 printf ("\n");
1013 } 1012 }
1014 1013
1015 /* Check if runlevel is valid if we're changing */ 1014 /* Check if runlevel is valid if we're changing */
1016 if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) { 1015 if (newlevel && strcmp (runlevel, newlevel) != 0 && ! going_down) {
1017 tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL); 1016 tmp = rc_strcatpaths (RC_RUNLEVELDIR, newlevel, (char *) NULL);
1018 if (! rc_is_dir (tmp)) 1017 if (rc_is_dir (tmp) != 0)
1019 eerrorx ("%s: is not a valid runlevel", newlevel); 1018 eerrorx ("%s: is not a valid runlevel", newlevel);
1020 CHAR_FREE (tmp); 1019 CHAR_FREE (tmp);
1021 } 1020 }
1022 1021
1023 /* Load our deptree now */ 1022 /* Load our deptree now */
1024 if ((deptree = rc_load_deptree ()) == NULL) 1023 if ((deptree = rc_load_deptree ()) == NULL)
1025 eerrorx ("failed to load deptree"); 1024 eerrorx ("failed to load deptree");
1026 1025
1027 /* Clean the failed services state dir now */ 1026 /* Clean the failed services state dir now */
1028 if (rc_is_dir (RC_SVCDIR "/failed")) 1027 if (rc_is_dir (RC_SVCDIR "/failed") == 0)
1029 rc_rm_dir (RC_SVCDIR "/failed", false); 1028 rc_rm_dir (RC_SVCDIR "/failed", false);
1030 1029
1031 mkdir (RC_STOPPING, 0755); 1030 mkdir (RC_STOPPING, 0755);
1032 1031
1033#ifdef __linux__ 1032#ifdef __linux__
1034 /* udev likes to start services before we're ready when it does 1033 /* 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 1034 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 1035 stores a list of coldplugged services in DEVBOOT for us to pick up
1037 here when we are ready for them */ 1036 here when we are ready for them */
1038 if (rc_is_dir (DEVBOOT)) { 1037 if (rc_is_dir (DEVBOOT) == 0) {
1039 start_services = rc_ls_dir (DEVBOOT, RC_LS_INITD); 1038 start_services = rc_ls_dir (DEVBOOT, RC_LS_INITD);
1040 rc_rm_dir (DEVBOOT, true); 1039 rc_rm_dir (DEVBOOT, true);
1041 1040
1042 STRLIST_FOREACH (start_services, service, i) 1041 STRLIST_FOREACH (start_services, service, i)
1043 if (rc_allow_plug (service)) 1042 if (rc_allow_plug (service) == 0)
1044 rc_mark_service (service, rc_service_coldplugged); 1043 rc_mark_service (service, rc_service_coldplugged);
1045 /* We need to dump this list now. 1044 /* We need to dump this list now.
1046 This may seem redunant, but only Linux needs this and saves on 1045 This may seem redunant, but only Linux needs this and saves on
1047 code bloat. */ 1046 code bloat. */
1048 rc_strlist_free (start_services); 1047 rc_strlist_free (start_services);
1063 start_services = rc_ls_dir ("/dev/net", 0); 1062 start_services = rc_ls_dir ("/dev/net", 0);
1064 STRLIST_FOREACH (start_services, service, i) { 1063 STRLIST_FOREACH (start_services, service, i) {
1065 j = (strlen ("net.") + strlen (service) + 1); 1064 j = (strlen ("net.") + strlen (service) + 1);
1066 tmp = rc_xmalloc (sizeof (char *) * j); 1065 tmp = rc_xmalloc (sizeof (char *) * j);
1067 snprintf (tmp, j, "net.%s", service); 1066 snprintf (tmp, j, "net.%s", service);
1068 if (rc_service_exists (tmp) && rc_allow_plug (tmp)) 1067 if (rc_service_exists (tmp) == 0 && rc_allow_plug (tmp) == 0)
1069 rc_mark_service (tmp, rc_service_coldplugged); 1068 rc_mark_service (tmp, rc_service_coldplugged);
1070 CHAR_FREE (tmp); 1069 CHAR_FREE (tmp);
1071 } 1070 }
1072 rc_strlist_free (start_services); 1071 rc_strlist_free (start_services);
1073#endif 1072#endif
1082 char *p = service + 3; 1081 char *p = service + 3;
1083 if (p && isdigit (*p)) { 1082 if (p && isdigit (*p)) {
1084 j = (strlen ("moused.") + strlen (service) + 1); 1083 j = (strlen ("moused.") + strlen (service) + 1);
1085 tmp = rc_xmalloc (sizeof (char *) * j); 1084 tmp = rc_xmalloc (sizeof (char *) * j);
1086 snprintf (tmp, j, "moused.%s", service); 1085 snprintf (tmp, j, "moused.%s", service);
1087 if (rc_service_exists (tmp) && rc_allow_plug (tmp)) 1086 if (rc_service_exists (tmp) == 0 && rc_allow_plug (tmp) == 0)
1088 rc_mark_service (tmp, rc_service_coldplugged); 1087 rc_mark_service (tmp, rc_service_coldplugged);
1089 CHAR_FREE (tmp); 1088 CHAR_FREE (tmp);
1090 } 1089 }
1091 } 1090 }
1092 } 1091 }
1178 char **stopdeps = NULL; 1177 char **stopdeps = NULL;
1179 char *svc1 = NULL; 1178 char *svc1 = NULL;
1180 char *svc2 = NULL; 1179 char *svc2 = NULL;
1181 int k; 1180 int k;
1182 1181
1183 if (rc_service_state (service, rc_service_stopped)) 1182 if (rc_service_state (service, rc_service_stopped) == 0)
1184 continue; 1183 continue;
1185 1184
1186 /* We always stop the service when in these runlevels */ 1185 /* We always stop the service when in these runlevels */
1187 if (going_down) { 1186 if (going_down) {
1188 pid_t pid = rc_stop_service (service); 1187 pid_t pid = rc_stop_service (service);
1206 1205
1207 len = strlen (service) + strlen (runlevel) + 2; 1206 len = strlen (service) + strlen (runlevel) + 2;
1208 tmp = rc_xmalloc (sizeof (char *) * len); 1207 tmp = rc_xmalloc (sizeof (char *) * len);
1209 snprintf (tmp, len, "%s.%s", service, runlevel); 1208 snprintf (tmp, len, "%s.%s", service, runlevel);
1210 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL); 1209 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL);
1211 found = rc_exists (conf); 1210 found = (rc_exists (conf) == 0);
1212 CHAR_FREE (conf); 1211 CHAR_FREE (conf);
1213 CHAR_FREE (tmp); 1212 CHAR_FREE (tmp);
1214 if (! found) { 1213 if (! found) {
1215 len = strlen (service) + strlen (newlevel) + 2; 1214 len = strlen (service) + strlen (newlevel) + 2;
1216 tmp = rc_xmalloc (sizeof (char *) * len); 1215 tmp = rc_xmalloc (sizeof (char *) * len);
1217 snprintf (tmp, len, "%s.%s", service, newlevel); 1216 snprintf (tmp, len, "%s.%s", service, newlevel);
1218 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL); 1217 conf = rc_strcatpaths (RC_CONFDIR, tmp, (char *) NULL);
1219 found = rc_exists (conf); 1218 found = (rc_exists (conf) == 0);
1220 CHAR_FREE (conf); 1219 CHAR_FREE (conf);
1221 CHAR_FREE (tmp); 1220 CHAR_FREE (tmp);
1222 if (!found) 1221 if (!found)
1223 continue; 1222 continue;
1224 } 1223 }
1225 } else { 1224 } else {
1226 /* Allow coldplugged services not to be in the runlevels list */ 1225 /* Allow coldplugged services not to be in the runlevels list */
1227 if (rc_service_state (service, rc_service_coldplugged)) 1226 if (rc_service_state (service, rc_service_coldplugged) == 0)
1228 continue; 1227 continue;
1229 } 1228 }
1230 1229
1231 /* We got this far! Or last check is to see if any any service that 1230 /* We got this far! Or last check is to see if any any service that
1232 going to be started depends on us */ 1231 going to be started depends on us */
1282 applet, HALTSH, strerror (errno)); 1281 applet, HALTSH, strerror (errno));
1283 } 1282 }
1284 1283
1285 /* Single user is done now */ 1284 /* Single user is done now */
1286 if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) { 1285 if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0) {
1287 if (rc_exists (INTERACTIVE)) 1286 if (rc_exists (INTERACTIVE) == 0)
1288 unlink (INTERACTIVE); 1287 unlink (INTERACTIVE);
1289 sulogin (false); 1288 sulogin (false);
1290 } 1289 }
1291 1290
1292 mkdir (RC_STARTING, 0755); 1291 mkdir (RC_STARTING, 0755);
1322 } 1321 }
1323#endif 1322#endif
1324 1323
1325 1324
1326 STRLIST_FOREACH (start_services, service, i) { 1325 STRLIST_FOREACH (start_services, service, i) {
1327 if (rc_service_state (service, rc_service_stopped)) { 1326 if (rc_service_state (service, rc_service_stopped) == 0) {
1328 pid_t pid; 1327 pid_t pid;
1329 1328
1330 if (! interactive) 1329 if (! interactive)
1331 interactive = want_interactive (); 1330 interactive = want_interactive ();
1332 1331
1380 1379
1381 /* Store our interactive status for boot */ 1380 /* Store our interactive status for boot */
1382 if (interactive && strcmp (runlevel, bootlevel) == 0) 1381 if (interactive && strcmp (runlevel, bootlevel) == 0)
1383 mark_interactive (); 1382 mark_interactive ();
1384 else { 1383 else {
1385 if (rc_exists (INTERACTIVE)) 1384 if (rc_exists (INTERACTIVE) == 0)
1386 unlink (INTERACTIVE); 1385 unlink (INTERACTIVE);
1387 } 1386 }
1388 1387
1389 return (EXIT_SUCCESS); 1388 return (EXIT_SUCCESS);
1390} 1389}

Legend:
Removed from v.2923  
changed lines
  Added in v.2927

  ViewVC Help
Powered by ViewVC 1.1.20