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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3040 - (show annotations) (download) (as text)
Mon Oct 15 14:40:53 2007 UTC (6 years, 6 months ago) by uberlord
File MIME type: text/x-csrc
File size: 3525 byte(s)
const correctness
1 /*
2 rc-depend
3 rc service dependency and ordering
4 Copyright 2006-2007 Gentoo Foundation
5 Released under the GPLv2
6 */
7
8 #define APPLET "rc-depend"
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13 #include <getopt.h>
14 #include <errno.h>
15 #include <stdbool.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "builtins.h"
21 #include "einfo.h"
22 #include "rc.h"
23 #include "rc-misc.h"
24 #include "strlist.h"
25
26 rc_depinfo_t *_rc_deptree_load (void) {
27 if (rc_deptree_update_needed ()) {
28 int retval;
29
30 ebegin ("Caching service dependencies");
31 retval = rc_deptree_update ();
32 eend (retval ? 0 : -1, "Failed to update the dependency tree");
33 }
34
35 return (rc_deptree_load ());
36 }
37
38 static char *applet = NULL;
39
40 #include "_usage.h"
41 #define getoptstring "t:suT" getoptstring_COMMON
42 static struct option longopts[] = {
43 { "type", 1, NULL, 't'},
44 { "notrace", 0, NULL, 'T'},
45 { "strict", 0, NULL, 's'},
46 { "update", 0, NULL, 'u'},
47 longopts_COMMON
48 };
49 static const char * const longopts_help[] = {
50 "Type(s) of dependency to list",
51 "Don't trace service dependencies",
52 "Only use what is in the runlevels",
53 "Force an update of the dependency tree",
54 longopts_help_COMMON
55 };
56 #include "_usage.c"
57
58 int rc_depend (int argc, char **argv)
59 {
60 char **types = NULL;
61 char **services = NULL;
62 char **depends = NULL;
63 char **list;
64 rc_depinfo_t *deptree = NULL;
65 char *service;
66 int options = RC_DEP_TRACE;
67 bool first = true;
68 int i;
69 bool update = false;
70 char *runlevel = xstrdup( getenv ("RC_SOFTLEVEL"));
71 int opt;
72 char *token;
73
74 applet = argv[0];
75
76 while ((opt = getopt_long (argc, argv, getoptstring,
77 longopts, (int *) 0)) != -1)
78 {
79 switch (opt) {
80 case 's':
81 options |= RC_DEP_STRICT;
82 break;
83 case 't':
84 while ((token = strsep (&optarg, ",")))
85 rc_strlist_addu (&types, token);
86 break;
87 case 'u':
88 update = true;
89 break;
90 case 'T':
91 options &= RC_DEP_TRACE;
92 break;
93
94 case_RC_COMMON_GETOPT
95 }
96 }
97
98 if (update) {
99 bool u = false;
100 ebegin ("Caching service dependencies");
101 u = rc_deptree_update ();
102 eend (u ? 0 : -1, "%s: %s", applet, strerror (errno));
103 if (! u)
104 eerrorx ("Failed to update the dependency tree");
105 }
106
107 if (! (deptree = _rc_deptree_load ()))
108 eerrorx ("failed to load deptree");
109
110 if (! runlevel)
111 runlevel = rc_runlevel_get ();
112
113 while (optind < argc) {
114 list = NULL;
115 rc_strlist_add (&list, argv[optind]);
116 errno = 0;
117 depends = rc_deptree_depends (deptree, NULL, (const char **) list,
118 runlevel, 0);
119 if (! depends && errno == ENOENT)
120 eerror ("no dependency info for service `%s'", argv[optind]);
121 else
122 rc_strlist_add (&services, argv[optind]);
123
124 rc_strlist_free (depends);
125 rc_strlist_free (list);
126 optind++;
127 }
128
129 if (! services) {
130 rc_strlist_free (types);
131 rc_deptree_free (deptree);
132 free (runlevel);
133 if (update)
134 return (EXIT_SUCCESS);
135 eerrorx ("no services specified");
136 }
137
138 /* If we don't have any types, then supply some defaults */
139 if (! types) {
140 rc_strlist_add (&types, "ineed");
141 rc_strlist_add (&types, "iuse");
142 }
143
144 depends = rc_deptree_depends (deptree, (const char **) types,
145 (const char **) services, runlevel, options);
146
147 if (depends) {
148 STRLIST_FOREACH (depends, service, i) {
149 if (first)
150 first = false;
151 else
152 printf (" ");
153
154 if (service)
155 printf ("%s", service);
156
157 }
158 printf ("\n");
159 }
160
161 rc_strlist_free (types);
162 rc_strlist_free (services);
163 rc_strlist_free (depends);
164 rc_deptree_free (deptree);
165 free (runlevel);
166 return (EXIT_SUCCESS);
167 }

  ViewVC Help
Powered by ViewVC 1.1.20