/[baselayout]/trunk/src/awk/gendepends.awk
Gentoo

Contents of /trunk/src/awk/gendepends.awk

Parent Directory Parent Directory | Revision Log Revision Log


Revision 357 - (show annotations) (download)
Wed May 7 22:30:20 2003 UTC (15 years, 11 months ago) by azarah
File size: 8664 byte(s)
cleanup gendepends.awk

1 # Copyright 1999-2003 Gentoo Technologies, Inc.
2 # Distributed under the terms of the GNU General Public License v2
3 # Author: Martin Schlemmer <azarah@gentoo.org>
4 # $Header$
5
6 # bool check_service(name)
7 #
8 # Returns true if the service exists
9 #
10 function check_service(name, x)
11 {
12 for (x = 1; x <= RCNUMBER; x++) {
13 if (DEPTREE[x,NAME] == name)
14 return 1
15 else
16 continue
17 }
18
19 return 0
20 }
21
22 # int get_service_position(name)
23 #
24 # Return the index position in DEPTREE
25 #
26 function get_service_position(name, x)
27 {
28 for (x = 1; x <= RCNUMBER; x++) {
29 if (DEPTREE[x,NAME] == name)
30 return x
31 else
32 continue
33 }
34
35 return 0
36 }
37
38 # bool check_depend(service1, type, service2)
39 #
40 # Returns true if 'service1' need/use/is_before/is_after 'service2'
41 #
42 function check_depend(service1, type, service2, tmpsplit, x)
43 {
44 if (get_service_position(service1)) {
45 x = get_service_position(service1)
46
47 if ((x,type) in DEPTREE) {
48 split(DEPTREE[x,type], tmpsplit, " ")
49
50 for (x in tmpsplit) {
51 if (tmpsplit[x] == service2)
52 return 1
53 }
54 }
55 }
56
57 return 0
58 }
59
60 # bool add_deptree_item(rcnumber, type, item)
61 #
62 # Add an item(s) 'item' to the DEPTREE array at index [rcnumber,type]
63 #
64 function add_deptree_item(rcnumber, type, item)
65 {
66 if (DEPTREE[rcnumber,type] != "")
67 DEPTREE[rcnumber,type] = DEPTREE[rcnumber,type] " " item
68 else
69 DEPTREE[rcnumber,type] = item
70
71 return 1
72 }
73
74 # bool add_provide(service, provide)
75 #
76 # Add a name of a virtual service ('provide') that 'service' Provides
77 #
78 function add_provide(service, provide)
79 {
80 # We cannot have a service Provide a virtual service with the same name as
81 # an existing service ...
82 if (check_service(provide)) {
83 eerror(" Cannot add provide '" provide "', as a service with the same name exists!")
84 return 0
85 }
86
87 if (check_provide(provide)) {
88 # We cannot have more than one service Providing a virtual ...
89 ewarn(" Service '" get_provide(provide) "' already provide '" provide "'!;")
90 ewarn(" Not adding service '" service "'...")
91 } else {
92 # Sanity check
93 if (check_service(service)) {
94 PROVIDE_LIST[provide] = service
95 } else {
96 eerror(" Cannot add provide '" provide "', as service '" service "' do not exist!")
97 return 0
98 }
99 }
100
101 return 1
102 }
103
104 # string get_provide(provide)
105 #
106 # Return the name of the service that Provides 'provide'
107 #
108 function get_provide(provide)
109 {
110 if (provide in PROVIDE_LIST)
111 if (check_service(PROVIDE_LIST[provide]))
112 return PROVIDE_LIST[provide]
113
114 return ""
115 }
116
117 # bool check_provide(provide)
118 #
119 # Return true if any service Provides the virtual service with name 'provide'
120 #
121 function check_provide(provide)
122 {
123 if (provide in PROVIDE_LIST)
124 return 1
125
126 return 0
127 }
128
129 # void depend_dbadd(type, service, deplist)
130 #
131 # Add an DB entry(s) 'deplist' for service 'service' of type 'type'
132 #
133 function depend_dbadd(type, service, deplist, x, deparray)
134 {
135 if ((type == "") || (service == "") || (deplist == ""))
136 return
137
138 # If there are no existing service 'service', resolve possible
139 # provided services
140 if (!check_service(service)) {
141 if (check_provide(service))
142 service = get_provide(service)
143 else
144 return
145 }
146
147 split(deplist, deparray, " ")
148
149 for (x in deparray) {
150
151 # If there are no existing service 'deparray[x]', resolve possible
152 # provided services
153 if (!check_service(deparray[x])) {
154 if (check_provide(deparray[x]))
155 deparray[x] = get_provide(deparray[x])
156 }
157
158 # Handle 'need', as it is the only dependency type that
159 # should handle invalid database entries currently.
160 if (!check_service(deparray[x])) {
161
162 if ((type == NEED) && (deparray[x] != "net")) {
163
164 ewarn(" Can't find service '" deparray[x] "' needed by '" service "'; continuing...")
165
166 # service is broken due to missing 'need' dependencies
167 if (!isdir(SVCDIR "/broken/" service))
168 assert(mktree(SVCDIR "/broken/" service, 0755),
169 "mktree(" SVCDIR "/broken/" service ", 0755)")
170 if (!isfile(SVCDIR "/broken/" service "/" deparray[x]))
171 assert(dosystem("touch " SVCDIR "/broken/" service "/" deparray[x]),
172 "system(touch " SVCDIR "/broken/" service "/" deparray[x] ")")
173
174 continue
175 }
176 else if (deparray[x] != "net")
177 continue
178 }
179
180 # Ugly bug ... if a service depends on itself, it creates
181 # a 'mini fork bomb' effect, and breaks things...
182 if (deparray[x] == service) {
183
184 # Dont work too well with the '*' use and need
185 if ((type != BEFORE) && (type != AFTER))
186 ewarn(" Service '" deparray[x] "' can't depend on itself; continuing...")
187
188 continue
189 }
190
191 # Currently only these depend/order types are supported
192 if ((type == NEED) || (type == USE) || (type == BEFORE) || (type == AFTER)) {
193
194 if (type == BEFORE) {
195 # NEED and USE override BEFORE (service BEFORE deparray[x])
196 if (check_depend(service, NEED, deparray[x]) ||
197 check_depend(service, USE, deparray[x]))
198 continue
199
200 # Do not all circular ordering
201 if (check_depend(service, AFTER, deparray[x]))
202 continue
203 }
204
205 if (type == AFTER) {
206 # NEED and USE override AFTER (service AFTER deparray[x])
207 if (check_depend(deparray[x], NEED, service) ||
208 check_depend(deparray[x], USE, service))
209 continue
210
211 # Do not all circular ordering
212 if (check_depend(service, BEFORE, deparray[x]))
213 continue
214 }
215
216 # NEED override USE (service USE deparray[x])
217 if ((type == USE) && (check_depend(deparray[x], NEED, service))) {
218 ewarn(" Service '" deparray[x] "' NEED service '" service "', but service '" service "' wants")
219 ewarn(" to USE service '" deparray[x] "'!")
220 continue
221 }
222
223 # Ok, add our db entry ...
224 if (!isdir(SVCDIR "/" TYPENAMES[type] "/" deparray[x]))
225 assert(mktree(SVCDIR "/" TYPENAMES[type] "/" deparray[x], 0755),
226 "mktree(" SVCDIR "/" TYPENAMES[type] "/" deparray[x] ", 0755)")
227 if (!islink(SVCDIR "/" TYPENAMES[type] "/" deparray[x] "/" service))
228 assert(dosymlink("/etc/init.d/" service, SVCDIR "/" TYPENAMES[type] "/" deparray[x] "/" service),
229 "dosymlink(/etc/init.d/" service ", " SVCDIR "/" TYPENAMES[type] "/" deparray[x] "/" service")")
230 }
231 }
232 }
233
234 BEGIN {
235
236 extension("/lib/rcscripts/filefuncs.so", "dlload")
237
238 NAME = 1
239 NEED = 2
240 USE = 3
241 BEFORE = 4
242 AFTER = 5
243 PROVIDE = 6
244 RCNUMBER = 0
245
246 TYPENAMES[NEED] = "need"
247 TYPENAMES[USE] = "use"
248 TYPENAMES[BEFORE] = "before"
249 TYPENAMES[AFTER] = "after"
250 TYPENAMES[PROVIDE] = "provide"
251
252 if (!isdir(SVCDIR))
253 if (!mktree(SVCDIR, 0755)) {
254
255 eerror(" Could not create needed directories!")
256 exit 1
257 }
258
259 svcdirs = "softscripts snapshot options broken started"
260 svcdirs = svcdirs " " DEPTYPES " " ORDTYPES
261
262 split (svcdirs, svcdirnodes)
263
264 for (x in svcdirnodes) {
265
266 if (!isdir(SVCDIR "/" svcdirnodes[x])) {
267
268 if (!mktree(SVCDIR "/" svcdirnodes[x], 0755)) {
269
270 eerror(" Could not create needed directories!")
271 exit 1
272 }
273 }
274 }
275
276 # Cleanup and fix a problem with 'for x in foo/*' if foo/ is empty
277 system("rm -rf " SVCDIR "/{need,use,before,after,broken}/*")
278 }
279
280 {
281 #
282 # Build our DEPTREE array
283 #
284
285 if ($1 == "RCSCRIPT") {
286 RCNUMBER++
287
288 DEPTREE[RCNUMBER,NAME] = $2
289 }
290
291 if ($1 == "NEED") {
292 sub(/NEED[[:space:]]*/, "")
293
294 if ($0 != "")
295 add_deptree_item(RCNUMBER, NEED, $0)
296 }
297
298 if ($1 == "USE") {
299 sub(/USE[[:space:]]*/, "")
300
301 if ($0 != "")
302 add_deptree_item(RCNUMBER, USE, $0)
303 }
304
305 if ($1 == "BEFORE") {
306 sub(/BEFORE[[:space:]]*/, "")
307
308 if ($0 != "")
309 add_deptree_item(RCNUMBER, BEFORE, $0)
310 }
311
312 if ($1 == "AFTER") {
313 sub(/AFTER[[:space:]]*/, "")
314
315 if ($0 != "")
316 add_deptree_item(RCNUMBER, AFTER, $0)
317 }
318
319 if ($1 == "PROVIDE") {
320 sub(/PROVIDE[[:space:]]*/, "")
321
322 if ($0 != "")
323 add_deptree_item(RCNUMBER, PROVIDE, $0)
324 }
325 }
326
327 END {
328 # Calculate all the provides ...
329 for (x = 1;x <= RCNUMBER;x++) {
330
331 if ((x,PROVIDE) in DEPTREE)
332 add_provide(DEPTREE[x,NAME], DEPTREE[x,PROVIDE])
333 }
334
335 # Now do NEED and USE
336 for (x = 1; x <= RCNUMBER;x++) {
337
338 if ((x,NEED) in DEPTREE)
339 depend_dbadd(NEED, DEPTREE[x,NAME], DEPTREE[x,NEED])
340
341 if ((x,USE) in DEPTREE)
342 depend_dbadd(USE, DEPTREE[x,NAME], DEPTREE[x,USE])
343 }
344
345 # Now do BEFORE and AFTER (we do them in a seperate cycle to
346 # so that we can check for NEED or USE)
347 for (x = 1; x <= RCNUMBER;x++) {
348
349 if ((x,BEFORE) in DEPTREE) {
350
351 depend_dbadd(BEFORE, DEPTREE[x,NAME], DEPTREE[x,BEFORE])
352
353 split(DEPTREE[x,BEFORE], tmpsplit)
354
355 # Reverse mapping
356 for (y in tmpsplit)
357 depend_dbadd(AFTER, tmpsplit[y], DEPTREE[x,NAME])
358 }
359
360 if ((x,AFTER) in DEPTREE) {
361
362 depend_dbadd(AFTER, DEPTREE[x,NAME], DEPTREE[x,AFTER])
363
364 split(DEPTREE[x,AFTER], tmpsplit)
365
366 # Reverse mapping
367 for (y in tmpsplit)
368 depend_dbadd(BEFORE, tmpsplit[y], DEPTREE[x,NAME])
369 }
370 }
371 }
372
373
374 # vim:ts=4

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20