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

Contents of /trunk/src/rc-update.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2569 - (show annotations) (download) (as text)
Tue Apr 10 11:24:58 2007 UTC (11 years, 8 months ago) by uberlord
File MIME type: text/x-csrc
File size: 4072 byte(s)
Fix indentation
1 /*
2 rc-update
3 Manage init scripts and runlevels
4 Copyright 2007 Gentoo Foundation
5 Released under the GPLv2
6 */
7
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "einfo.h"
17 #include "rc.h"
18 #include "rc-misc.h"
19 #include "strlist.h"
20
21 static char *applet = NULL;
22
23 static bool add (const char *runlevel, const char *service)
24 {
25 bool retval = true;
26
27 if (! rc_runlevel_exists (runlevel))
28 {
29 ewarn ("runlevel `%s' does not exist", runlevel);
30 return (false);
31 }
32 if (rc_service_in_runlevel (service, runlevel))
33 {
34 ewarn ("%s already installed in runlevel `%s'; skipping",
35 service, runlevel);
36 return (false);
37 }
38
39 if (rc_service_add (runlevel, service))
40 einfo ("%s added to runlevel %s", service, runlevel);
41 else
42 {
43 eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
44 applet, service, runlevel, strerror (errno));
45 retval = false;
46 }
47
48 return (retval);
49 }
50
51 int main (int argc, char **argv)
52 {
53 int i;
54 int j;
55 char *service;
56 char **runlevels = NULL;
57 char *runlevel;
58
59 applet = argv[0];
60 if (argc < 2 ||
61 strcmp (argv[1], "show") == 0 ||
62 strcmp (argv[1], "-s") == 0)
63 {
64 bool verbose = false;
65 char **services = rc_services_in_runlevel (NULL);
66
67 for (i = 2; i < argc; i++)
68 {
69 if (strcmp (argv[i], "--verbose") == 0 ||
70 strcmp (argv[i], "-v") == 0)
71 verbose = true;
72 else
73 runlevels = rc_strlist_add (runlevels, argv[i]);
74 }
75
76 if (! runlevels)
77 runlevels = rc_get_runlevels ();
78
79 STRLIST_FOREACH (services, service, i)
80 {
81 char **in = NULL;
82 bool inone = false;
83
84 STRLIST_FOREACH (runlevels, runlevel, j)
85 {
86 if (rc_service_in_runlevel (service, runlevel))
87 {
88 in = rc_strlist_add (in, runlevel);
89 inone = true;
90 }
91 else
92 {
93 char buffer[PATH_MAX];
94 memset (buffer, ' ', strlen (runlevel));
95 buffer[strlen (runlevel)] = 0;
96 in = rc_strlist_add (in, buffer);
97 }
98 }
99
100 if (! inone && ! verbose)
101 continue;
102
103 printf (" %20s |", service);
104 STRLIST_FOREACH (in, runlevel, j)
105 printf (" %s", runlevel);
106 printf ("\n");
107 }
108
109 return (EXIT_SUCCESS);
110 }
111
112 if (geteuid () != 0)
113 eerrorx ("%s: must be root to add or delete services from runlevels",
114 applet);
115
116 if (! (service = argv[2]))
117 eerrorx ("%s: no service specified", applet);
118
119 if (strcmp (argv[1], "add") == 0 ||
120 strcmp (argv[1], "-a") == 0)
121 {
122 if (! service)
123 eerrorx ("%s: no service specified", applet);
124 if (! rc_service_exists (service))
125 eerrorx ("%s: service `%s' does not exist", applet, service);
126
127 if (argc < 4)
128 add (rc_get_runlevel (), service);
129
130 for (i = 3; i < argc; i++)
131 add (argv[i], service);
132
133 return (EXIT_SUCCESS);
134 }
135
136 if (strcmp (argv[1], "delete") == 0 ||
137 strcmp (argv[1], "del") == 0 ||
138 strcmp (argv[1], "-d") == 0)
139 {
140 for (i = 3; i < argc; i++)
141 runlevels = rc_strlist_add (runlevels, argv[i]);
142
143 if (! runlevels)
144 runlevels = rc_strlist_add (runlevels, rc_get_runlevel ());
145
146 STRLIST_FOREACH (runlevels, runlevel, i)
147 {
148 if (rc_service_in_runlevel (service, runlevel))
149 {
150 if (rc_service_delete (runlevel, service))
151 einfo ("%s removed from runlevel %s", service, runlevel);
152 else
153 eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
154 applet, service, runlevel, strerror (errno));
155 }
156 }
157
158 return (EXIT_SUCCESS);
159 }
160
161 eerrorx ("%s: unknown command `%s'", applet, argv[1]);
162 }

  ViewVC Help
Powered by ViewVC 1.1.20