X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fservice.c;h=b18c950baaaeef015943a85f3f25ed16b70094c3;hb=43be53513acba2b205fca248ff46cfd3ed7cfda8;hp=cf5edd2b9b1c71931d81a094356680aa4bf272a6;hpb=a7d3cc26f9694053712458615006ea79cd7a7cb4;p=elogind.git diff --git a/src/service.c b/src/service.c index cf5edd2b9..b18c950ba 100644 --- a/src/service.c +++ b/src/service.c @@ -250,7 +250,7 @@ static char *sysv_translate_name(const char *name) { return r; } -static int sysv_translate_facility(const char *name, char **_r) { +static int sysv_translate_facility(const char *name, const char *filename, char **_r) { /* We silently ignore the $ prefix here. According to the LSB * spec it simply indicates whether something is a @@ -289,28 +289,39 @@ static int sysv_translate_facility(const char *name, char **_r) { unsigned i; char *r; + const char *n; - for (i = 0; i < ELEMENTSOF(table); i += 2) + assert(name); + assert(_r); - if (streq(table[i], name) || - (*name == '$' && streq(table[i], name+1))) { + n = *name == '$' ? name + 1 : name; - if (!table[i+1]) - return 0; + for (i = 0; i < ELEMENTSOF(table); i += 2) { - if (!(r = strdup(table[i+1]))) - return -ENOMEM; + if (!streq(table[i], n)) + continue; - goto finish; - } + if (!table[i+1]) + return 0; + + if (!(r = strdup(table[i+1]))) + return -ENOMEM; + + goto finish; + } /* If we don't know this name, fallback heuristics to figure * out whether something is a target or an service alias. */ if (*name == '$') - r = unit_name_build(name+1, NULL, ".target"); + /* Facilities starting with $ are most likely targets */ + r = unit_name_build(n, NULL, ".target"); + else if (filename && streq(name, filename)) + /* Names equalling the file name of the services are redundant */ + return 0; else - r = sysv_translate_name(name); + /* Everything else we assume to be normal service names */ + r = sysv_translate_name(n); if (!r) return -ENOMEM; @@ -432,6 +443,7 @@ static int service_load_sysv_path(Service *s, const char *path) { LSB, LSB_DESCRIPTION } state = NORMAL; + char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description; assert(s); assert(path); @@ -522,24 +534,27 @@ static int service_load_sysv_path(Service *s, const char *path) { s->sysv_runlevels = d; } - } else if (startswith_no_case(t, "description:") && - !u->meta.description) { + } else if (startswith_no_case(t, "description:")) { size_t k = strlen(t); char *d; + const char *j; if (t[k-1] == '\\') { state = DESCRIPTION; t[k-1] = 0; } - if (!(d = strappend("LSB: ", strstrip(t+12)))) { - r = -ENOMEM; - goto finish; - } + if ((j = strstrip(t+12)) && *j) { + if (!(d = strdup(j))) { + r = -ENOMEM; + goto finish; + } + } else + d = NULL; - free(u->meta.description); - u->meta.description = d; + free(chkconfig_description); + chkconfig_description = d; } else if (startswith_no_case(t, "pidfile:")) { @@ -568,21 +583,29 @@ static int service_load_sysv_path(Service *s, const char *path) { * continuation */ size_t k = strlen(t); - char *d; + char *j; if (t[k-1] == '\\') t[k-1] = 0; else state = NORMAL; - assert(u->meta.description); - if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) { - r = -ENOMEM; - goto finish; - } + if ((j = strstrip(t)) && *j) { + char *d = NULL; - free(u->meta.description); - u->meta.description = d; + if (chkconfig_description) + asprintf(&d, "%s %s", chkconfig_description, j); + else + d = strdup(j); + + if (!d) { + r = -ENOMEM; + goto finish; + } + + free(chkconfig_description); + chkconfig_description = d; + } } else if (state == LSB || state == LSB_DESCRIPTION) { @@ -600,12 +623,7 @@ static int service_load_sysv_path(Service *s, const char *path) { goto finish; } - if (streq(n, file_name_from_path(path))) { - free(n); - continue; - } - - r = sysv_translate_facility(n, &m); + r = sysv_translate_facility(n, file_name_from_path(path), &m); free(n); if (r < 0) @@ -650,12 +668,7 @@ static int service_load_sysv_path(Service *s, const char *path) { goto finish; } - if (streq(n, file_name_from_path(path))) { - free(n); - continue; - } - - r = sysv_translate_facility(n, &m); + r = sysv_translate_facility(n, file_name_from_path(path), &m); free(n); if (r < 0) @@ -688,35 +701,37 @@ static int service_load_sysv_path(Service *s, const char *path) { s->sysv_runlevels = d; } - } else if (startswith_no_case(t, "Description:") && - !u->meta.description) { - char *d; - - /* We use the long description only if - * no short description is set. */ + } else if (startswith_no_case(t, "Description:")) { + char *d, *j; state = LSB_DESCRIPTION; - if (!(d = strappend("LSB: ", strstrip(t+12)))) { - r = -ENOMEM; - goto finish; - } + if ((j = strstrip(t+12)) && *j) { + if (!(d = strdup(j))) { + r = -ENOMEM; + goto finish; + } + } else + d = NULL; - free(u->meta.description); - u->meta.description = d; + free(long_description); + long_description = d; } else if (startswith_no_case(t, "Short-Description:")) { - char *d; + char *d, *j; state = LSB; - if (!(d = strappend("LSB: ", strstrip(t+18)))) { - r = -ENOMEM; - goto finish; - } + if ((j = strstrip(t+18)) && *j) { + if (!(d = strdup(j))) { + r = -ENOMEM; + goto finish; + } + } else + d = NULL; - free(u->meta.description); - u->meta.description = d; + free(short_description); + short_description = d; } else if (startswith_no_case(t, "X-Interactive:")) { int b; @@ -734,16 +749,25 @@ static int service_load_sysv_path(Service *s, const char *path) { } else if (state == LSB_DESCRIPTION) { if (startswith(l, "#\t") || startswith(l, "# ")) { - char *d; + char *j; - assert(u->meta.description); - if (asprintf(&d, "%s %s", u->meta.description, t) < 0) { - r = -ENOMEM; - goto finish; + if ((j = strstrip(t)) && *j) { + char *d = NULL; + + if (long_description) + asprintf(&d, "%s %s", long_description, t); + else + d = strdup(j); + + if (!d) { + r = -ENOMEM; + goto finish; + } + + free(long_description); + long_description = d; } - free(u->meta.description); - u->meta.description = d; } else state = LSB; } @@ -775,6 +799,29 @@ static int service_load_sysv_path(Service *s, const char *path) { ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL; s->exec_context.kill_mode = KILL_PROCESS_GROUP; + /* We use the long description only if + * no short description is set. */ + + if (short_description) + description = short_description; + else if (chkconfig_description) + description = chkconfig_description; + else if (long_description) + description = long_description; + else + description = NULL; + + if (description) { + char *d; + + if (!(d = strappend("LSB: ", description))) { + r = -ENOMEM; + goto finish; + } + + u->meta.description = d; + } + u->meta.load_state = UNIT_LOADED; r = 0; @@ -783,6 +830,10 @@ finish: if (f) fclose(f); + free(short_description); + free(long_description); + free(chkconfig_description); + return r; } @@ -2745,6 +2796,7 @@ static int service_enumerate(Manager *m) { STRV_FOREACH(p, arch_daemons_split) { free(name); + name = NULL; if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */ continue;