1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 19:19:47 +01:00

Cleanup "service list" command

- Use newly introduced helper functions
 - Add command line switch to print services for specific target

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-03-25 03:00:10 +02:00
parent b9d829bc9a
commit 5f27cb4804

View file

@ -19,13 +19,39 @@
#include "service.h" #include "service.h"
#include "config.h" #include "config.h"
static void print_services(service_t *svc)
{
size_t i;
for (; svc != NULL; svc = svc->next) {
printf("Name: %s\n", svc->name);
printf("\tDescrption: %s\n", svc->desc);
printf("\tType: %s\n", svc_type_to_string(svc->type));
printf("\tTarget: %s\n", svc_target_to_string(svc->target));
if (svc->num_before) {
fputs("\tMust be run before:\n", stdout);
for (i = 0; i < svc->num_before; ++i)
printf("\t\t%s\n", svc->before[i]);
}
if (svc->num_after) {
fputs("\tMust be run after:\n", stdout);
for (i = 0; i < svc->num_after; ++i)
printf("\t\t%s\n", svc->after[i]);
}
}
}
static int cmd_list(int argc, char **argv) static int cmd_list(int argc, char **argv)
{ {
int i, ret = EXIT_SUCCESS; int i, ret = EXIT_SUCCESS;
service_list_t list; service_list_t list;
service_t *svc;
(void)argc; (void)argv; if (check_arguments(argv[0], argc, 1, 2))
return EXIT_FAILURE;
if (srvscan(SVCDIR, &list)) { if (srvscan(SVCDIR, &list)) {
fprintf(stderr, "Error while reading services from %s\n", fprintf(stderr, "Error while reading services from %s\n",
@ -33,55 +59,33 @@ static int cmd_list(int argc, char **argv)
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
} }
for (i = 0; i < TGT_MAX; ++i) { if (argc == 2) {
if (list.targets[i] == NULL) i = svc_type_from_string(argv[1]);
continue;
fputs("******** target: ", stdout); if (i == -1) {
fprintf(stderr, "Unknown target `%s'\n", argv[1]);
switch (i) { tell_read_help(argv[1]);
case TGT_BOOT: ret = EXIT_FAILURE;
fputs("boot", stdout); goto out;
break;
case TGT_SHUTDOWN:
fputs("shutdown", stdout);
break;
case TGT_REBOOT:
fputs("reboot", stdout);
break;
case TGT_CAD:
fputs("ctrl-alt-delete", stdout);
break;
} }
fputs(" ********\n", stdout); print_services(list.targets[i]);
} else {
for (svc = list.targets[i]; svc != NULL; svc = svc->next) { for (i = 0; i < TGT_MAX; ++i)
fprintf(stdout, "Name: %s\n", svc->name); print_services(list.targets[i]);
fprintf(stdout, "Descrption: %s\n", svc->desc);
fputs("Type: ", stdout);
switch (svc->type) {
case SVC_ONCE: fputs("once\n", stdout); break;
case SVC_WAIT: fputs("wait\n", stdout); break;
case SVC_RESPAWN: fputs("respawn\n", stdout); break;
}
fputc('\n', stdout);
}
fputc('\n', stdout);
} }
out:
del_srv_list(&list); del_srv_list(&list);
return ret; return ret;
} }
static command_t list = { static command_t list = {
.cmd = "list", .cmd = "list",
.usage = "", .usage = "[target]",
.s_desc = "print a list of currently enabled services", .s_desc = "print a list of currently enabled services",
.l_desc = "Print a list of currently enabled services.", .l_desc = "Print a list of currently enabled services. If an "
"optional target is specified, print services for this "
"target.",
.run_cmd = cmd_list, .run_cmd = cmd_list,
}; };