summaryrefslogtreecommitdiff
blob: c3a41c87356dcb5ab28493b176a61c757d6678ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * depend.c
 *
 * Dependancy engine for Gentoo style rc-scripts.
 *
 * Copyright (C) 2004,2005 Martin Schlemmer <azarah@nosferatu.za.org>
 *
 *
 *      This program is free software; you can redistribute it and/or modify it
 *      under the terms of the GNU General Public License as published by the
 *      Free Software Foundation version 2 of the License.
 *
 *      This program is distributed in the hope that it will be useful, but
 *      WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *      General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License along
 *      with this program; if not, write to the Free Software Foundation, Inc.,
 *      675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Header$
 */

#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include "rcscripts.h"

LIST_HEAD (service_info_list);

/* Names for service types (service_type_t) in depend.h.
 * Note that this should sync with service_type_t */
char *service_type_names[] = {
  "NEED",
  "NEED_ME",
  "USE",
  "USE_ME",
  "BEFORE",
  "AFTER",
  "BROKEN",
  "PROVIDE",
  NULL
};

static char *
service_is_recursive_dependency (char *servicename, char *dependency,
				 bool checkuse);
static int __service_resolve_dependency (char *servicename, char *dependency,
					 service_type_t type);

service_info_t *
service_get_info (char *servicename)
{
  service_info_t *info;

  if (!check_arg_str (servicename))
    return NULL;

  list_for_each_entry (info, &service_info_list, node)
    {
      if (NULL != info->name)
	if (0 == strcmp (info->name, servicename))
	  return info;
    }

  /* We use this to check if a service exists, so rather do not
   * add debugging, otherwise it is very noisy! */
  /* DBG_MSG("Invalid service name '%s'!\n", servicename); */

  return NULL;
}

int
service_add (char *servicename)
{
  service_info_t *info;
  service_info_t *sorted;
  int count;

  if (!check_arg_str (servicename))
    return -1;

  info = service_get_info (servicename);
  if (NULL == info)
    {
      DBG_MSG ("Adding service '%s'.\n", servicename);

      info = xmalloc (sizeof (service_info_t));
      if (NULL == info)
	return -1;

      info->name = xstrndup (servicename, strlen (servicename));
      if (NULL == info->name)
	{
	  free (info);
	  return -1;
	}

      for (count = 0; count < ALL_SERVICE_TYPE_T; count++)
	info->depend_info[count] = NULL;
      info->provide = NULL;

      /* We want to keep the list sorted */
      list_for_each_entry (sorted, &service_info_list, node)
	{
	  if (strcmp (sorted->name, servicename) > 0)
	    {
	      break;
	    }
	}

      list_add_tail (&info->node, &sorted->node);

      return 0;
    }
  else
    {
      DBG_MSG ("Tried to add duplicate service '%s'!\n", servicename);
    }

  return -1;
}

int
service_is_dependency (char *servicename, char *dependency,
		       service_type_t type)
{
  service_info_t *info;
  char *service;
  int count = 0;

  if ((!check_arg_str (servicename)) || (!check_arg_str (dependency)))
    return -1;

  info = service_get_info (servicename);
  if (NULL != info)
    {
      str_list_for_each_item (info->depend_info[type], service, count)
	{
	  if (0 == strcmp (dependency, service))
	    return 0;
	}
    }
  else
    {
      DBG_MSG ("Invalid service name '%s'!\n", servicename);
    }

  return -1;
}

char *
service_is_recursive_dependency (char *servicename, char *dependency,
				 bool checkuse)
{
  service_info_t *info;
  char *depend;
  int count = 0;

  if ((!check_arg_str (servicename)) || (!check_arg_str (dependency)))
    return NULL;

  info = service_get_info (dependency);
  if (NULL != info)
    {
      str_list_for_each_item (info->depend_info[NEED_ME], depend, count)
	{
	  if ((0 == service_is_dependency (servicename, depend, NEED))
	      || (0 == service_is_dependency (servicename, depend, USE)))
	    return depend;
	}
      if (checkuse)
	{
	  str_list_for_each_item (info->depend_info[USE_ME], depend, count)
	    {
	      if ((0 == service_is_dependency (servicename, depend, NEED))
		  || (0 == service_is_dependency (servicename, depend, USE)))
		return depend;
	    }
	}
    }
  else
    {
      DBG_MSG ("Invalid service name '%s'!\n", servicename);
    }

  return NULL;
}

int
service_add_dependency (char *servicename, char *dependency,
			service_type_t type)
{
  service_info_t *info;
  char *tmp_buf;

  if ((!check_arg_str (servicename)) || (!check_arg_str (dependency)))
    return -1;

  info = service_get_info (servicename);
  if (NULL != info)
    {
      /* Do not add duplicates */
      if (-1 == service_is_dependency (servicename, dependency, type))
	{
	  DBG_MSG ("Adding dependency '%s' of service '%s', type '%s'.\n",
		   dependency, servicename, service_type_names[type]);

	  tmp_buf = xstrndup (dependency, strlen (dependency));
	  if (NULL == tmp_buf)
	    return -1;

	  str_list_add_item_sorted (info->depend_info[type], tmp_buf, error);
	}
      else
	{
	  DBG_MSG ("Duplicate dependency '%s' for service '%s', type '%s'!\n",
		   dependency, servicename, service_type_names[type]);
	  /* Rather do not fail here, as we add a lot of doubles
	   * during resolving of dependencies */
	}

      return 0;
    }
  else
    {
      DBG_MSG ("Invalid service name '%s'!\n", servicename);
    }

error:
  return -1;
}

int
service_del_dependency (char *servicename, char *dependency,
			service_type_t type)
{
  service_info_t *info;

  if ((!check_arg_str (servicename)) || (!check_arg_str (dependency)))
    return -1;

  if (-1 == service_is_dependency (servicename, dependency, type))
    {
      DBG_MSG ("Tried to remove invalid dependency '%s'!\n", dependency);
      return -1;
    }

  info = service_get_info (servicename);
  if (NULL != info)
    {
      DBG_MSG ("Removing dependency '%s' of service '%s', type '%s'.\n",
	       dependency, servicename, service_type_names[type]);

      str_list_del_item (info->depend_info[type], dependency, error);
      return 0;
    }
  else
    {
      DBG_MSG ("Invalid service name '%s'!\n", servicename);
    }

error:
  return -1;
}

service_info_t *
service_get_virtual (char *virtual)
{
  service_info_t *info;

  if (!check_arg_str (virtual))
    return NULL;

  list_for_each_entry (info, &service_info_list, node)
    {
      if (NULL != info->provide)
	if (0 == strcmp (info->provide, virtual))
	  return info;
    }

  /* We use this to check if a virtual exists, so rather do not
   * add debugging, otherwise it is very noisy! */
  /* DBG_MSG("Invalid service name '%s'!\n", virtual); */

  return NULL;
}

int
service_add_virtual (char *servicename, char *virtual)
{
  service_info_t *info;

  if ((!check_arg_str (servicename)) || (!check_arg_str (virtual)))
    return -1;

  if (NULL != service_get_info (virtual))
    {
      EERROR
       (" Cannot add provide '%s', as a service with the same name exists!\n",
	virtual);
      /* Do not fail here as we do have a service that resolves
       * the virtual */
    }

  info = service_get_virtual (virtual);
  if (NULL != info)
    {
      /* We cannot have more than one service Providing a virtual */
      EWARN (" Service '%s' already provides '%s'!;\n", info->name, virtual);
      EWARN (" Not adding service '%s'...\n", servicename);
      /* Do not fail here as we do have a service that resolves
       * the virtual */
    }
  else
    {
      info = service_get_info (servicename);
      if (NULL != info)
	{
	  DBG_MSG ("Adding virtual '%s' of service '%s'.\n",
		   virtual, servicename);

	  info->provide = xstrndup (virtual, strlen (virtual));
	  if (NULL == info->provide)
	    return -1;
	}
      else
	{
	  DBG_MSG ("Invalid service name '%s'!\n", servicename);
	  return -1;
	}
    }

  return 0;
}

int
service_set_mtime (char *servicename, time_t mtime)
{
  service_info_t *info;

  if (!check_arg_str (servicename))
    return -1;

  info = service_get_info (servicename);
  if (NULL != info)
    {
      DBG_MSG ("Setting mtime '%li' of service '%s'.\n", mtime, servicename);

      info->mtime = mtime;

      return 0;
    }
  else
    {
      DBG_MSG ("Invalid service name '%s'!\n", servicename);
    }

  return -1;
}

int
__service_resolve_dependency (char *servicename, char *dependency,
			      service_type_t type)
{
  service_info_t *info;
  int retval;

  if ((!check_arg_str (servicename)) || (!check_arg_str (dependency)))
    return -1;

  info = service_get_info (servicename);
  if (NULL == info)
    {
      DBG_MSG ("Invalid service name passed!\n");
      return -1;
    }

  DBG_MSG ("Checking dependency '%s' of service '%s', type '%s'.\n",
	   dependency, servicename, service_type_names[type]);

  /* If there are no existing service 'dependency', try to resolve
   * possible virtual services */
  info = service_get_info (dependency);
  if (NULL == info)
    {
      info = service_get_virtual (dependency);
      if (NULL != info)
	{
	  DBG_MSG ("Virtual '%s' -> '%s' for service '%s', type '%s'.\n",
		   dependency, info->name, servicename,
		   service_type_names[type]);

	  retval = service_del_dependency (servicename, dependency, type);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to delete dependency!\n");
	      return -1;
	    }

	  /* Add the actual service name for the virtual */
	  dependency = info->name;
	  retval = service_add_dependency (servicename, dependency, type);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add dependency!\n");
	      return -1;
	    }
	}
    }

  /* Handle 'need', as it is the only dependency type that should
   * handle invalid database entries currently. */
  if (NULL == info)
    {
      if ((type == NEED) || (type == NEED_ME))
	{
	  EWARN (" Can't find service '%s' needed by '%s';  continuing...\n",
		 dependency, servicename);

	  retval = service_add_dependency (servicename, dependency, BROKEN);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add dependency!\n");
	      return -1;
	    }

	  /* Delete invalid entry */
	  goto remove;
	}

      /* For the rest, if the dependency is not 'net', just silently
       * die without error.  Should not be needed as we add a 'net'
       * service manually before we start, but you never know ... */
      if (0 != strcmp (dependency, "net"))
	{
	  /* Delete invalid entry */
	  goto remove;
	}
    }

  /* Ugly bug ... if a service depends on itself, it creates a
   * 'mini fork bomb' effect, and breaks things horribly ... */
  if (0 == strcmp (servicename, dependency))
    {
      /* Dont work too well with the '*' before and after */
      if ((type != BEFORE) && (type != AFTER))
	EWARN (" Service '%s' can't depend on itself;  continuing...\n",
	       servicename);

      /* Delete invalid entry */
      goto remove;
    }

  /* Currently only these depend/order types are supported */
  if ((type == NEED) || (type == USE) || (type == BEFORE) || (type == AFTER))
    {
      if (type == BEFORE)
	{
	  char *depend;
	  
	  /* NEED and USE override BEFORE
	   * ('servicename' BEFORE 'dependency') */
	  if ((0 == service_is_dependency (servicename, dependency, NEED))
	      || (0 == service_is_dependency (servicename, dependency, USE)))
	    {
	      /* Delete invalid entry */
	      goto remove;
	    }

	  depend = service_is_recursive_dependency (servicename, dependency,
						    TRUE);
	  if (NULL != depend)
	    {
	      EWARN (" Service '%s' should be BEFORE service '%s', but '%s'\n",
		     servicename, dependency, depend);
	      EWARN (" needed by '%s', depends in return on '%s'!\n",
		     servicename, dependency);

	      /* Delete invalid entry */
	      goto remove;
	    }
	}

      if (type == AFTER)
	{
	  char *depend;
	  
	  /* NEED and USE override AFTER
	   * ('servicename' AFTER 'dependency') */
	  if ((0 == service_is_dependency (dependency, servicename, NEED))
	      || (0 == service_is_dependency (dependency, servicename, USE)))
	    {
	      /* Delete invalid entry */
	      goto remove;
	    }

	  depend = service_is_recursive_dependency (dependency, servicename,
						    TRUE);
	  if (NULL != depend)
	    {
	      EWARN (" Service '%s' should be AFTER service '%s', but '%s'\n",
		     servicename, dependency, depend);
	      EWARN (" needed by '%s', depends in return on '%s'!\n",
		     dependency, servicename);

	      /* Delete invalid entry */
	      goto remove;
	    }
	}

      /* We do not want to add circular dependencies ... */
      if (0 == service_is_dependency (dependency, servicename, type))
	{
	  EWARN (" Services '%s' and '%s' have circular\n",
		 servicename, dependency);
	  EWARN (" dependency of type '%s';  continuing...\n",
		 service_type_names[type]);

	  /* For now remove this dependency */
	  goto remove;
	}

      /* Reverse mapping */
      if (type == NEED)
	{
	  retval = service_add_dependency (dependency, servicename, NEED_ME);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add dependency!\n");
	      return -1;
	    }
	}

      /* Reverse mapping */
      if (type == USE)
	{
	  retval = service_add_dependency (dependency, servicename, USE_ME);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add dependency!\n");
	      return -1;
	    }
	}

      /* Reverse mapping */
      if (type == BEFORE)
	{
	  retval = service_add_dependency (dependency, servicename, AFTER);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add dependency!\n");
	      return -1;
	    }
	}

      /* Reverse mapping */
      if (type == AFTER)
	{
	  retval = service_add_dependency (dependency, servicename, BEFORE);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add dependency!\n");
	      return -1;
	    }
	}
    }

  return 0;

remove:
  /* Delete invalid entry */
  DBG_MSG ("Removing invalid dependency '%s' of service '%s', type '%s'.\n",
	   dependency, servicename, service_type_names[type]);

  retval = service_del_dependency (servicename, dependency, type);
  if (-1 == retval)
    {
      DBG_MSG ("Failed to delete dependency!\n");
      return -1;
    }

  /* Here we should not die with error */
  return 0;
}

int
service_resolve_dependencies (void)
{
  service_info_t *info;
  char *service;
  char *next = NULL;
  int count;

  /* Add our 'net' service */
  if (NULL == service_get_info ("net"))
    {
      if (-1 == service_add ("net"))
	{
	  DBG_MSG ("Failed to add virtual!\n");
	  return -1;
	}
      service_set_mtime ("net", 0);
    }

  /* Calculate all virtuals */
  list_for_each_entry (info, &service_info_list, node)
    {
      str_list_for_each_item_safe (info->depend_info[PROVIDE], service, next,
				   count)
	{
	  if (-1 == service_add_virtual (info->name, service))
	    {
	      DBG_MSG ("Failed to add virtual!\n");
	      return -1;
	    }
	}
    }

  /* Now do NEED, USE, BEFORE and AFTER */
  list_for_each_entry (info, &service_info_list, node)
    {
      str_list_for_each_item_safe (info->depend_info[NEED], service, next, count)
	{
	  if (-1 == __service_resolve_dependency (info->name, service, NEED))
	    {
	      DBG_MSG ("Failed to resolve dependency!\n");
	      return -1;
	    }
	}
    }
  list_for_each_entry (info, &service_info_list, node)
    {
      str_list_for_each_item_safe (info->depend_info[USE], service, next, count)
	{
	  if (-1 == __service_resolve_dependency (info->name, service, USE))
	    {
	      DBG_MSG ("Failed to resolve dependency!\n");
	      return -1;
	    }
	}
    }
  list_for_each_entry (info, &service_info_list, node)
    {
      str_list_for_each_item_safe (info->depend_info[BEFORE], service, next,
				   count)
	{
	  if (-1 == __service_resolve_dependency (info->name, service, BEFORE))
	    {
	      DBG_MSG ("Failed to resolve dependency!\n");
	      return -1;
	    }
	}
    }
  list_for_each_entry (info, &service_info_list, node)
    {
      str_list_for_each_item_safe (info->depend_info[AFTER], service, next, count)
	{
	  if (-1 == __service_resolve_dependency (info->name, service, AFTER))
	    {
	      DBG_MSG ("Failed to resolve dependency!\n");
	      return -1;
	    }
	}
    }

  return 0;
}