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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2577 - (show annotations) (download) (as text)
Wed Apr 11 12:44:47 2007 UTC (7 years, 8 months ago) by uberlord
File MIME type: text/x-csrc
File size: 3433 byte(s)
Cuddle up to LKML C style
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 ewarn ("runlevel `%s' does not exist", runlevel);
29 return (false);
30 }
31 if (rc_service_in_runlevel (service, runlevel)) {
32 ewarn ("%s already installed in runlevel `%s'; skipping",
33 service, runlevel);
34 return (false);
35 }
36
37 if (rc_service_add (runlevel, service))
38 einfo ("%s added to runlevel %s", service, runlevel);
39 else {
40 eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
41 applet, service, runlevel, strerror (errno));
42 retval = false;
43 }
44
45 return (retval);
46 }
47
48 int main (int argc, char **argv)
49 {
50 int i;
51 int j;
52 char *service;
53 char **runlevels = NULL;
54 char *runlevel;
55
56 applet = argv[0];
57 if (argc < 2 ||
58 strcmp (argv[1], "show") == 0 ||
59 strcmp (argv[1], "-s") == 0)
60 {
61 bool verbose = false;
62 char **services = rc_services_in_runlevel (NULL);
63
64 for (i = 2; i < argc; i++) {
65 if (strcmp (argv[i], "--verbose") == 0 ||
66 strcmp (argv[i], "-v") == 0)
67 verbose = true;
68 else
69 runlevels = rc_strlist_add (runlevels, argv[i]);
70 }
71
72 if (! runlevels)
73 runlevels = rc_get_runlevels ();
74
75 STRLIST_FOREACH (services, service, i) {
76 char **in = NULL;
77 bool inone = false;
78
79 STRLIST_FOREACH (runlevels, runlevel, j) {
80 if (rc_service_in_runlevel (service, runlevel)) {
81 in = rc_strlist_add (in, runlevel);
82 inone = true;
83 } else {
84 char buffer[PATH_MAX];
85 memset (buffer, ' ', strlen (runlevel));
86 buffer[strlen (runlevel)] = 0;
87 in = rc_strlist_add (in, buffer);
88 }
89 }
90
91 if (! inone && ! verbose)
92 continue;
93
94 printf (" %20s |", service);
95 STRLIST_FOREACH (in, runlevel, j)
96 printf (" %s", runlevel);
97 printf ("\n");
98 }
99
100 return (EXIT_SUCCESS);
101 }
102
103 if (geteuid () != 0)
104 eerrorx ("%s: must be root to add or delete services from runlevels",
105 applet);
106
107 if (! (service = argv[2]))
108 eerrorx ("%s: no service specified", applet);
109
110 if (strcmp (argv[1], "add") == 0 ||
111 strcmp (argv[1], "-a") == 0)
112 {
113 if (! service)
114 eerrorx ("%s: no service specified", applet);
115 if (! rc_service_exists (service))
116 eerrorx ("%s: service `%s' does not exist", applet, service);
117
118 if (argc < 4)
119 add (rc_get_runlevel (), service);
120
121 for (i = 3; i < argc; i++)
122 add (argv[i], service);
123
124 return (EXIT_SUCCESS);
125 }
126
127 if (strcmp (argv[1], "delete") == 0 ||
128 strcmp (argv[1], "del") == 0 ||
129 strcmp (argv[1], "-d") == 0)
130 {
131 for (i = 3; i < argc; i++)
132 runlevels = rc_strlist_add (runlevels, argv[i]);
133
134 if (! runlevels)
135 runlevels = rc_strlist_add (runlevels, rc_get_runlevel ());
136
137 STRLIST_FOREACH (runlevels, runlevel, i) {
138 if (rc_service_in_runlevel (service, runlevel)) {
139 if (rc_service_delete (runlevel, service))
140 einfo ("%s removed from runlevel %s", service, runlevel);
141 else
142 eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
143 applet, service, runlevel, strerror (errno));
144 }
145 }
146
147 return (EXIT_SUCCESS);
148 }
149
150 eerrorx ("%s: unknown command `%s'", applet, argv[1]);
151 }

  ViewVC Help
Powered by ViewVC 1.1.20