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

Diff of /trunk/src/runscript.c

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

Revision 2578 Revision 2580
10#include <sys/signal.h> 10#include <sys/signal.h>
11#include <sys/stat.h> 11#include <sys/stat.h>
12#include <sys/wait.h> 12#include <sys/wait.h>
13#include <dlfcn.h> 13#include <dlfcn.h>
14#include <errno.h> 14#include <errno.h>
15#include <getopt.h>
15#include <limits.h> 16#include <limits.h>
16#include <stdio.h> 17#include <stdio.h>
17#include <stdlib.h> 18#include <stdlib.h>
18#include <string.h> 19#include <string.h>
19#include <unistd.h> 20#include <unistd.h>
813 int i; 814 int i;
814 bool deps = true; 815 bool deps = true;
815 bool doneone = false; 816 bool doneone = false;
816 char pid[16]; 817 char pid[16];
817 int retval; 818 int retval;
818 bool ifstarted = false; 819 char c;
820
821 static struct option longopts[] = {
822 { "debug", 0, NULL, 'd'},
823 { "help", 0, NULL, 'h'},
824 { "nocolor", 0, NULL, 'C'},
825 { "nocolour", 0, NULL, 'C'},
826 { "nodeps", 0, NULL, 'D'},
827 { "verbose", 0, NULL, 'v'},
828 { NULL, 0, NULL, 0}
829 };
819 830
820 applet = strdup (basename (service)); 831 applet = strdup (basename (service));
821 atexit (cleanup); 832 atexit (cleanup);
822 833
823 /* Show help if insufficient args */ 834 /* Show help if insufficient args */
912 /* Ok, we are ready to go, so setup selinux if applicable */ 923 /* Ok, we are ready to go, so setup selinux if applicable */
913 setup_selinux (argc, argv); 924 setup_selinux (argc, argv);
914#endif 925#endif
915 926
916 /* Right then, parse any options there may be */ 927 /* Right then, parse any options there may be */
917 for (i = 2; i < argc; i++) { 928 while ((c = getopt_long (argc, argv, "dhCDNqv",
918 if (strlen (argv[i]) < 2 || argv[i][0] != '-' || argv[i][1] != '-') 929 longopts, (int *) 0)) != -1)
919 continue; 930 switch (c) {
920 931 case 'd':
921 if (strcmp (argv[i], "--debug") == 0)
922 setenv ("RC_DEBUG", "yes", 1); 932 setenv ("RC_DEBUG", "yes", 1);
923 else if (strcmp (argv[i], "--help") == 0) { 933 break;
934 case 'h':
924 execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); 935 execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
925 eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", 936 eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
926 applet, strerror (errno)); 937 applet, strerror (errno));
927 } else if (strcmp (argv[i],"--ifstarted") == 0) 938 case 'C':
928 ifstarted = true;
929 else if (strcmp (argv[i], "--nocolour") == 0 ||
930 strcmp (argv[i], "--nocolor") == 0)
931 setenv ("RC_NOCOLOR", "yes", 1); 939 setenv ("RC_NOCOLOR", "yes", 1);
932 else if (strcmp (argv[i], "--nodeps") == 0) 940 break;
941 case 'D':
933 deps = false; 942 deps = false;
934 else if (strcmp (argv[i], "--quiet") == 0) 943 break;
944 case 'q':
935 setenv ("RC_QUIET", "yes", 1); 945 setenv ("RC_QUIET", "yes", 1);
936 else if (strcmp (argv[i], "--verbose") == 0) 946 break;
947 case 'v':
937 setenv ("RC_VERBOSE", "yes", 1); 948 setenv ("RC_VERBOSE", "yes", 1);
938 else if (strcmp (argv[i], "--version") == 0) 949 break;
939 printf ("version me\n"); 950 default:
940 else
941 eerror ("%s: unknown option `%s'", applet, argv[i]);
942 }
943
944 if (ifstarted && ! rc_service_state (applet, rc_service_started)) {
945 if (! rc_is_env("RC_QUIET", "yes"))
946 eerror ("ERROR: %s is not started", applet);
947 exit (EXIT_FAILURE); 951 exit (EXIT_FAILURE);
948 } 952 }
949 953
950 if (rc_is_env ("IN_HOTPLUG", "1")) { 954 if (rc_is_env ("IN_HOTPLUG", "1")) {
951 if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet)) 955 if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet))
952 eerrorx ("%s: not allowed to be hotplugged", applet); 956 eerrorx ("%s: not allowed to be hotplugged", applet);
953 } 957 }
962 /* Load our plugins */ 966 /* Load our plugins */
963 rc_plugin_load (); 967 rc_plugin_load ();
964 968
965 /* Now run each option */ 969 /* Now run each option */
966 retval = EXIT_SUCCESS; 970 retval = EXIT_SUCCESS;
967 for (i = 2; i < argc; i++) { 971 while (optind < argc) {
972 optarg = argv[optind++];
973
968 /* Abort on a sighup here */ 974 /* Abort on a sighup here */
969 if (sighup) 975 if (sighup)
970 exit (EXIT_FAILURE); 976 exit (EXIT_FAILURE);
971
972 if (strlen (argv[i]) < 2 ||
973 (argv[i][0] == '-' && argv[i][1] == '-'))
974 continue;
975 977
976 /* Export the command we're running. 978 /* Export the command we're running.
977 This is important as we stamp on the restart function now but 979 This is important as we stamp on the restart function now but
978 some start/stop routines still need to behave differently if 980 some start/stop routines still need to behave differently if
979 restarting. */ 981 restarting. */
980 unsetenv ("RC_CMD"); 982 unsetenv ("RC_CMD");
981 setenv ("RC_CMD", argv[i], 1); 983 setenv ("RC_CMD", optarg, 1);
982 984
983 doneone = true; 985 doneone = true;
984 if (strcmp (argv[i], "conditionalrestart") == 0 || 986 if (strcmp (optarg, "conditionalrestart") == 0 ||
985 strcmp (argv[i], "condrestart") == 0) 987 strcmp (optarg, "condrestart") == 0)
986 { 988 {
987 if (rc_service_state (service, rc_service_started)) 989 if (rc_service_state (service, rc_service_started))
988 svc_restart (service, deps); 990 svc_restart (service, deps);
989 } 991 }
990 else if (strcmp (argv[i], "restart") == 0) 992 else if (strcmp (optarg, "restart") == 0)
991 svc_restart (service, deps); 993 svc_restart (service, deps);
992 else if (strcmp (argv[i], "start") == 0) 994 else if (strcmp (optarg, "start") == 0)
993 svc_start (service, deps); 995 svc_start (service, deps);
994 else if (strcmp (argv[i], "status") == 0) { 996 else if (strcmp (optarg, "status") == 0) {
995 rc_service_state_t r = svc_status (service); 997 rc_service_state_t r = svc_status (service);
996 retval = (int) r; 998 retval = (int) r;
997 } else if (strcmp (argv[i], "stop") == 0) { 999 } else if (strcmp (optarg, "stop") == 0) {
998 if (in_background) 1000 if (in_background)
999 get_started_services (); 1001 get_started_services ();
1000 1002
1001 svc_stop (service, deps); 1003 svc_stop (service, deps);
1002 1004
1012 int j; 1014 int j;
1013 STRLIST_FOREACH (restart_services, svc, j) 1015 STRLIST_FOREACH (restart_services, svc, j)
1014 if (rc_service_state (svc, rc_service_stopped)) 1016 if (rc_service_state (svc, rc_service_stopped))
1015 rc_schedule_start_service (service, svc); 1017 rc_schedule_start_service (service, svc);
1016 } 1018 }
1017 } else if (strcmp (argv[i], "zap") == 0) { 1019 } else if (strcmp (optarg, "zap") == 0) {
1018 einfo ("Manually resetting %s to stopped state", applet); 1020 einfo ("Manually resetting %s to stopped state", applet);
1019 rc_mark_service (applet, rc_service_stopped); 1021 rc_mark_service (applet, rc_service_stopped);
1020 uncoldplug (applet); 1022 uncoldplug (applet);
1021 } else if (strcmp (argv[i], "help") == 0) { 1023 } else if (strcmp (optarg, "help") == 0) {
1022 execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL); 1024 execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL);
1023 eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", 1025 eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
1024 applet, strerror (errno)); 1026 applet, strerror (errno));
1025 }else 1027 }else
1026 svc_exec (service, argv[i], NULL); 1028 svc_exec (service, optarg, NULL);
1027 1029
1028 /* Flush our buffered output if any */ 1030 /* Flush our buffered output if any */
1029 eflush (); 1031 eflush ();
1030 1032
1031 /* We should ensure this list is empty after an action is done */ 1033 /* We should ensure this list is empty after an action is done */

Legend:
Removed from v.2578  
changed lines
  Added in v.2580

  ViewVC Help
Powered by ViewVC 1.1.20