X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fsystemctl%2Fsystemctl.c;h=e6bd855c15eb2b0501e193c93d73fe6bafe368ed;hp=2f43052f9af029187bcc7c0996008147bca653e4;hb=571bfc6c56962dfc926e9bbc1600c511fc8565d1;hpb=82da66fb750c91f06e713ff23a5e5c57ff05c2a8 diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 2f43052f9..e6bd855c1 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -72,6 +72,12 @@ static char **arg_types = NULL; static char **arg_load_states = NULL; static char **arg_properties = NULL; static bool arg_all = false; +static enum dependency { + DEPENDENCY_FORWARD, + DEPENDENCY_REVERSE, + DEPENDENCY_AFTER, + DEPENDENCY_BEFORE, +} arg_dependency = DEPENDENCY_FORWARD; static const char *arg_job_mode = "replace"; static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; static bool arg_no_block = false; @@ -126,6 +132,7 @@ static enum transport { static const char *arg_host = NULL; static unsigned arg_lines = 10; static OutputMode arg_output = OUTPUT_SHORT; +static bool arg_plain = false; static bool private_bus = false; @@ -353,7 +360,7 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) { id_len = max_id_len; for (u = unit_infos; u < unit_infos + c; u++) { - char _cleanup_free_ *e = NULL; + _cleanup_free_ char *e = NULL; const char *on_loaded, *off_loaded, *on = ""; const char *on_active, *off_active, *off = ""; @@ -495,7 +502,7 @@ static int get_triggered_units(DBusConnection *bus, const char* unit_path, { const char *interface = "org.freedesktop.systemd1.Unit", *triggers_property = "Triggers"; - DBusMessage _cleanup_dbus_message_unref_ *reply = NULL; + _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; DBusMessageIter iter, sub; int r; @@ -546,7 +553,7 @@ static int get_listening(DBusConnection *bus, const char* unit_path, { const char *interface = "org.freedesktop.systemd1.Socket", *listen_property = "Listen"; - DBusMessage _cleanup_dbus_message_unref_ *reply = NULL; + _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; DBusMessageIter iter, sub; int r; @@ -648,11 +655,12 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { } if (cs) { - printf("%-*s %-*.*s%-*s %s\n", - pathlen, "LISTEN", - typelen + arg_show_types, typelen + arg_show_types, "TYPE ", - socklen, "UNIT", - "ACTIVATES"); + if (!arg_no_legend) + printf("%-*s %-*.*s%-*s %s\n", + pathlen, "LISTEN", + typelen + arg_show_types, typelen + arg_show_types, "TYPE ", + socklen, "UNIT", + "ACTIVATES"); for (s = socket_infos; s < socket_infos + cs; s++) { char **a; @@ -671,15 +679,18 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { on = ansi_highlight(true); off = ansi_highlight(false); - printf("\n"); + if (!arg_no_legend) + printf("\n"); } else { on = ansi_highlight_red(true); off = ansi_highlight_red(false); } - printf("%s%u sockets listed.%s\n", on, cs, off); - if (!arg_all) - printf("Pass --all to see loaded but inactive sockets, too.\n"); + if (!arg_no_legend) { + printf("%s%u sockets listed.%s\n", on, cs, off); + if (!arg_all) + printf("Pass --all to see loaded but inactive sockets, too.\n"); + } return 0; } @@ -702,7 +713,7 @@ static int list_sockets(DBusConnection *bus, char **args) { for (u = unit_infos; u < unit_infos + cu; u++) { const char *dot; - char _cleanup_strv_free_ **listen = NULL, **triggered = NULL; + _cleanup_strv_free_ char **listen = NULL, **triggered = NULL; unsigned c = 0, i; if (!output_show_unit(u)) @@ -808,7 +819,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) { printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE"); for (u = units; u < units + c; u++) { - char _cleanup_free_ *e = NULL; + _cleanup_free_ char *e = NULL; const char *on, *off; const char *id; @@ -950,20 +961,22 @@ static int list_dependencies_print(const char *name, int level, unsigned int bra size_t len = 0; size_t max_len = MAX(columns(),20u); - for (i = level - 1; i >= 0; i--) { + if (!arg_plain) { + for (i = level - 1; i >= 0; i--) { + len += 2; + if(len > max_len - 3 && !arg_full) { + printf("%s...\n",max_len % 2 ? "" : " "); + return 0; + } + printf("%s", draw_special_char(branches & (1 << i) ? DRAW_TREE_VERT : DRAW_TREE_SPACE)); + } len += 2; if(len > max_len - 3 && !arg_full) { printf("%s...\n",max_len % 2 ? "" : " "); return 0; } - printf("%s", draw_special_char(branches & (1 << i) ? DRAW_TREE_VERT : DRAW_TREE_SPACE)); - } - len += 2; - if(len > max_len - 3 && !arg_full) { - printf("%s...\n",max_len % 2 ? "" : " "); - return 0; + printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH)); } - printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH)); if(arg_full){ printf("%s\n", name); @@ -979,12 +992,19 @@ static int list_dependencies_print(const char *name, int level, unsigned int bra } static int list_dependencies_get_dependencies(DBusConnection *bus, const char *name, char ***deps) { - static const char dependencies[] = - "Requires\0" - "RequiresOverridable\0" - "Requisite\0" - "RequisiteOverridable\0" - "Wants\0"; + static const char *dependencies[] = { + [DEPENDENCY_FORWARD] = "Requires\0" + "RequiresOverridable\0" + "Requisite\0" + "RequisiteOverridable\0" + "Wants\0", + [DEPENDENCY_REVERSE] = "RequiredBy\0" + "RequiredByOverridable\0" + "WantedBy\0" + "PartOf\0", + [DEPENDENCY_AFTER] = "After\0", + [DEPENDENCY_BEFORE] = "Before\0", + }; _cleanup_free_ char *path; const char *interface = "org.freedesktop.systemd1.Unit"; @@ -1049,7 +1069,8 @@ static int list_dependencies_get_dependencies(DBusConnection *bus, const char *n dbus_message_iter_recurse(&sub2, &sub3); dbus_message_iter_next(&sub); - if (!nulstr_contains(dependencies, prop)) + assert(arg_dependency < ELEMENTSOF(dependencies)); + if (!nulstr_contains(dependencies[arg_dependency], prop)) continue; if (dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_ARRAY) { @@ -1091,12 +1112,12 @@ static int list_dependencies_compare(const void *_a, const void *_b) { return strcasecmp(*a, *b); } -static int list_dependencies_one(DBusConnection *bus, const char *name, int level, char **units, unsigned int branches) { - char _cleanup_strv_free_ **deps = NULL, **u; +static int list_dependencies_one(DBusConnection *bus, const char *name, int level, char ***units, unsigned int branches) { + _cleanup_strv_free_ char **deps = NULL, **u; char **c; int r = 0; - u = strv_append(units, name); + u = strv_append(*units, name); if (!u) return log_oom(); @@ -1108,9 +1129,11 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, int leve STRV_FOREACH(c, deps) { if (strv_contains(u, *c)) { - r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); - if (r < 0) - return r; + if (!arg_plain) { + r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); + if (r < 0) + return r; + } continue; } @@ -1119,17 +1142,22 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, int leve return r; if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { - r = list_dependencies_one(bus, *c, level + 1, u, (branches << 1) | (c[1] == NULL ? 0 : 1)); + r = list_dependencies_one(bus, *c, level + 1, &u, (branches << 1) | (c[1] == NULL ? 0 : 1)); if(r < 0) return r; } } - + if (arg_plain) { + strv_free(*units); + *units = u; + u = NULL; + } return 0; } static int list_dependencies(DBusConnection *bus, char **args) { _cleanup_free_ char *unit = NULL; + _cleanup_strv_free_ char **units = NULL; const char *u; assert(bus); @@ -1146,7 +1174,7 @@ static int list_dependencies(DBusConnection *bus, char **args) { puts(u); - return list_dependencies_one(bus, u, 0, NULL, 0); + return list_dependencies_one(bus, u, 0, &units, 0); } struct job_info { @@ -1197,7 +1225,7 @@ static void list_jobs_print(struct job_info* jobs, size_t n) { l3, "STATE"); for (i = 0, j = jobs; i < n; i++, j++) { - char _cleanup_free_ *e = NULL; + _cleanup_free_ char *e = NULL; if (streq(j->state, "running")) { on = ansi_highlight(true); @@ -1271,8 +1299,7 @@ static int list_jobs(DBusConnection *bus, char **args) { goto finish; } - if (!greedy_realloc((void**) &jobs, &size, - sizeof(struct job_info) * (used + 1))) { + if (!GREEDY_REALLOC(jobs, size, used + 1)) { r = log_oom(); goto finish; } @@ -1439,7 +1466,7 @@ typedef struct WaitData { } WaitData; static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) { - DBusError _cleanup_dbus_error_free_ error; + _cleanup_dbus_error_free_ DBusError error; WaitData *d = data; dbus_error_init(&error); @@ -1675,7 +1702,7 @@ static void check_triggering_units( *load_state_property = "LoadState", *triggered_by_property = "TriggeredBy", *state; - char _cleanup_free_ *unit_path = NULL, *n = NULL; + _cleanup_free_ char *unit_path = NULL, *n = NULL; bool print_warning_label = true; int r; @@ -1843,9 +1870,8 @@ static int start_unit_one( if (!p) return log_oom(); - r = set_put(s, p); + r = set_consume(s, p); if (r < 0) { - free(p); log_error("Failed to add path to set."); return r; } @@ -1889,8 +1915,8 @@ static int start_unit(DBusConnection *bus, char **args) { int r, ret = 0; const char *method, *mode, *one_name; - Set _cleanup_set_free_free_ *s = NULL; - DBusError _cleanup_dbus_error_free_ error; + _cleanup_set_free_free_ Set *s = NULL; + _cleanup_dbus_error_free_ DBusError error; char **name; dbus_error_init(&error); @@ -2916,7 +2942,7 @@ static void show_unit_help(UnitStatusInfo *i) { if (startswith(*p, "man:")) { size_t k; char *e = NULL; - char _cleanup_free_ *page = NULL, *section = NULL; + _cleanup_free_ char *page = NULL, *section = NULL; const char *args[4] = { "man", NULL, NULL, NULL }; pid_t pid; @@ -3361,7 +3387,7 @@ static int print_property(const char *name, DBusMessageIter *iter) { if (exec_status_info_deserialize(&sub, &info) >= 0) { char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; - char _cleanup_free_ *t; + _cleanup_free_ char *t; t = strv_join(info.argv, " "); @@ -3401,7 +3427,7 @@ static int print_property(const char *name, DBusMessageIter *iter) { } static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) { - DBusMessage _cleanup_free_ *reply = NULL; + _cleanup_free_ DBusMessage *reply = NULL; const char *interface = ""; int r; DBusMessageIter iter, sub, sub2, sub3; @@ -3494,7 +3520,7 @@ static int show_one(const char *verb, DBusConnection *bus, const char *path, boo static int show_one_by_pid(const char *verb, DBusConnection *bus, uint32_t pid, bool *new_line) { _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; const char *path = NULL; - DBusError _cleanup_dbus_error_free_ error; + _cleanup_dbus_error_free_ DBusError error; int r; dbus_error_init(&error); @@ -3537,7 +3563,7 @@ static int show_all(const char* verb, DBusConnection *bus, bool show_properties, qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info); for (u = unit_infos; u < unit_infos + c; u++) { - char _cleanup_free_ *p = NULL; + _cleanup_free_ char *p = NULL; if (!output_show_unit(u)) continue; @@ -4009,11 +4035,11 @@ static int enable_sysv_units(char **args) { r = 0; for (f = 1; args[f]; f++) { const char *name; - char *p; + _cleanup_free_ char *p = NULL, *q = NULL; bool found_native = false, found_sysv; unsigned c = 1; const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL }; - char **k, *l, *q = NULL; + char **k, *l; int j; pid_t pid; siginfo_t status; @@ -4027,8 +4053,6 @@ static int enable_sysv_units(char **args) { continue; STRV_FOREACH(k, paths.unit_path) { - p = NULL; - if (!isempty(arg_root)) asprintf(&p, "%s/%s/%s", arg_root, *k, name); else @@ -4041,6 +4065,7 @@ static int enable_sysv_units(char **args) { found_native = access(p, F_OK) >= 0; free(p); + p = NULL; if (found_native) break; @@ -4049,7 +4074,6 @@ static int enable_sysv_units(char **args) { if (found_native) continue; - p = NULL; if (!isempty(arg_root)) asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name); else @@ -4062,10 +4086,8 @@ static int enable_sysv_units(char **args) { p[strlen(p) - sizeof(".service") + 1] = 0; found_sysv = access(p, F_OK) >= 0; - if (!found_sysv) { - free(p); + if (!found_sysv) continue; - } /* Mark this entry, so that we don't try enabling it as native unit */ args[f] = (char*) ""; @@ -4083,8 +4105,6 @@ static int enable_sysv_units(char **args) { l = strv_join((char**)argv, " "); if (!l) { - free(q); - free(p); r = log_oom(); goto finish; } @@ -4095,8 +4115,6 @@ static int enable_sysv_units(char **args) { pid = fork(); if (pid < 0) { log_error("Failed to fork: %m"); - free(p); - free(q); r = -errno; goto finish; } else if (pid == 0) { @@ -4106,9 +4124,6 @@ static int enable_sysv_units(char **args) { _exit(EXIT_FAILURE); } - free(p); - free(q); - j = wait_for_terminate(pid, &status); if (j < 0) { log_error("Failed to wait for child: %s", strerror(-r)); @@ -4192,10 +4207,10 @@ static int enable_unit(DBusConnection *bus, char **args) { UnitFileChange *changes = NULL; unsigned n_changes = 0, i; int carries_install_info = -1; - DBusMessage _cleanup_dbus_message_unref_ *m = NULL, *reply = NULL; + _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL; int r; - DBusError _cleanup_dbus_error_free_ error; - char _cleanup_strv_free_ **mangled_names = NULL; + _cleanup_dbus_error_free_ DBusError error; + _cleanup_strv_free_ char **mangled_names = NULL; dbus_error_init(&error); @@ -4391,9 +4406,9 @@ finish: } static int unit_is_enabled(DBusConnection *bus, char **args) { - DBusError _cleanup_dbus_error_free_ error; + _cleanup_dbus_error_free_ DBusError error; int r; - DBusMessage _cleanup_dbus_message_unref_ *reply = NULL; + _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; bool enabled; char **name; char *n; @@ -4488,7 +4503,10 @@ static int systemctl_help(void) { " --version Show package version\n" " -t --type=TYPE List only units of a particular type\n" " -p --property=NAME Show only properties by this name\n" - " -a --all Show all units/properties, including dead/empty ones\n" + " -a --all Show all loaded units/properties, including dead/empty\n" + " ones. To list all units installed on the system, use\n" + " the 'list-unit-files' command instead.\n" + " --reverse Show reverse dependencies with 'list-dependencies'\n" " --failed Show only failed units\n" " --full Don't ellipsize unit names on output\n" " --fail When queueing a new job, fail if conflicting jobs are\n" @@ -4554,7 +4572,8 @@ static int systemctl_help(void) { " unset-cgroup [NAME] [CGROUP...] Remove unit from a control group\n" " load [NAME...] Load one or more units\n" " list-dependencies [NAME] Recursively show units which are required\n" - " or wanted by this unit\n\n" + " or wanted by this unit or by which this\n" + " unit is required or wanted\n\n" "Unit File Commands:\n" " list-unit-files List installed unit files\n" " enable [NAME...] Enable one or more unit files\n" @@ -4690,6 +4709,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) { enum { ARG_FAIL = 0x100, + ARG_REVERSE, + ARG_AFTER, + ARG_BEFORE, ARG_SHOW_TYPES, ARG_IRREVERSIBLE, ARG_IGNORE_DEPENDENCIES, @@ -4708,7 +4730,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) { ARG_NO_ASK_PASSWORD, ARG_FAILED, ARG_RUNTIME, - ARG_FORCE + ARG_FORCE, + ARG_PLAIN }; static const struct option options[] = { @@ -4717,6 +4740,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) { { "type", required_argument, NULL, 't' }, { "property", required_argument, NULL, 'p' }, { "all", no_argument, NULL, 'a' }, + { "reverse", no_argument, NULL, ARG_REVERSE }, + { "after", no_argument, NULL, ARG_AFTER }, + { "before", no_argument, NULL, ARG_BEFORE }, { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, { "failed", no_argument, NULL, ARG_FAILED }, { "full", no_argument, NULL, ARG_FULL }, @@ -4743,6 +4769,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) { { "runtime", no_argument, NULL, ARG_RUNTIME }, { "lines", required_argument, NULL, 'n' }, { "output", required_argument, NULL, 'o' }, + { "plain", no_argument, NULL, ARG_PLAIN }, { NULL, 0, NULL, 0 } }; @@ -4769,7 +4796,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) { size_t size; FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) { - char _cleanup_free_ *type; + _cleanup_free_ char *type; type = strndup(word, size); if (!type) @@ -4839,6 +4866,18 @@ static int systemctl_parse_argv(int argc, char *argv[]) { arg_all = true; break; + case ARG_REVERSE: + arg_dependency = DEPENDENCY_REVERSE; + break; + + case ARG_AFTER: + arg_dependency = DEPENDENCY_AFTER; + break; + + case ARG_BEFORE: + arg_dependency = DEPENDENCY_BEFORE; + break; + case ARG_SHOW_TYPES: arg_show_types = true; break; @@ -4958,6 +4997,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) { arg_ignore_inhibitors = true; break; + case ARG_PLAIN: + arg_plain = true; + break; + case '?': return -EINVAL; @@ -5390,7 +5433,7 @@ static int parse_argv(int argc, char *argv[]) { return systemctl_parse_argv(argc, argv); } -static int action_to_runlevel(void) { +_pure_ static int action_to_runlevel(void) { static const char table[_ACTION_MAX] = { [ACTION_HALT] = '0', @@ -5409,8 +5452,8 @@ static int action_to_runlevel(void) { } static int talk_upstart(void) { - DBusMessage _cleanup_dbus_message_unref_ *m = NULL, *reply = NULL; - DBusError _cleanup_dbus_error_free_ error; + _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL; + _cleanup_dbus_error_free_ DBusError error; int previous, rl, r; char env1_buf[] = "RUNLEVEL=X", @@ -5499,7 +5542,7 @@ finish: static int talk_initctl(void) { struct init_request request = {}; int r; - int _cleanup_close_ fd = -1; + _cleanup_close_ int fd = -1; char rl; rl = action_to_runlevel(); @@ -5702,7 +5745,7 @@ static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError } static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) { - int _cleanup_close_ fd; + _cleanup_close_ int fd; struct sd_shutdown_command c = { .usec = t, .mode = mode, @@ -5844,7 +5887,7 @@ static int halt_main(DBusConnection *bus) { } if (arg_when > 0) { - char _cleanup_free_ *m; + _cleanup_free_ char *m; m = strv_join(arg_wall, " "); r = send_shutdownd(arg_when, @@ -5907,7 +5950,7 @@ static int runlevel_main(void) { int main(int argc, char*argv[]) { int r, retval = EXIT_FAILURE; DBusConnection *bus = NULL; - DBusError _cleanup_dbus_error_free_ error; + _cleanup_dbus_error_free_ DBusError error; dbus_error_init(&error);