dependency.c (1410B)
1 #include "config.h" 2 #include "service.h" 3 #include "util.h" 4 5 #include <fcntl.h> 6 #include <limits.h> 7 #include <stdio.h> 8 #include <string.h> 9 #include <unistd.h> 10 11 12 static bool circular_dependency(struct service* s, struct service* d) { 13 if (s == d) 14 return true; 15 16 for (int i = 0; i < s->parents_size; i++) { 17 if (circular_dependency(s->parents[i], d)) 18 return true; 19 } 20 21 return false; 22 } 23 24 void service_add_dependency(struct service* s, struct service* d) { 25 if (circular_dependency(s, d)) { 26 print_error("warning: detected circular dependency while adding %s to %s\n", d->name, s->name); 27 return; 28 } 29 30 s->children[s->children_size++] = d; 31 d->parents[s->parents_size++] = s; 32 } 33 34 void service_update_dependency(struct service* s) { 35 struct service* dep; 36 int depends_file; 37 char line[SV_NAME_MAX]; 38 39 if (s->log_service) { // aka keep first entry (the log service) if a log service is used 40 service_add_dependency(s, s->log_service); 41 } 42 43 if ((depends_file = openat(s->dir, "depends", O_RDONLY)) == -1) 44 return; 45 46 while (dgetline(depends_file, line, sizeof(line)) > 0) { 47 if (streq(s->name, line)) { 48 fprint(1, "warning: %s depends on itself\n", s->name); 49 continue; 50 } 51 52 if ((dep = service_get(line)) == NULL) { 53 fprint(1, "warning: %s depends on %s: dependency not found\n", s->name, line); 54 continue; 55 } 56 service_add_dependency(s, dep); 57 } 58 59 close(depends_file); 60 }