[systemd-commits] 2 commits - man/systemctl.xml src/core src/delta src/journal src/locale src/login src/nspawn src/shared src/systemctl src/test
Zbigniew JÄdrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Fri Dec 6 18:30:27 PST 2013
man/systemctl.xml | 38 +++++++++------
src/core/execute.c | 7 +-
src/core/load-fragment.c | 2
src/core/manager.c | 2
src/core/service.c | 4 -
src/core/unit.c | 4 -
src/delta/delta.c | 8 +--
src/journal/journalctl.c | 2
src/locale/localectl.c | 2
src/login/logind-inhibit.c | 2
src/login/logind-seat.c | 2
src/login/logind-session.c | 2
src/nspawn/nspawn.c | 2
src/shared/cgroup-show.c | 4 -
src/shared/conf-files.c | 4 -
src/shared/install.c | 16 +++---
src/shared/path-util.c | 12 ----
src/shared/path-util.h | 1
src/shared/util.c | 7 +-
src/shared/utmp-wtmp.c | 2
src/systemctl/systemctl.c | 114 ++++++++++++++++++++++++++++++---------------
src/test/test-install.c | 18 +++----
src/test/test-path-util.c | 8 +--
23 files changed, 152 insertions(+), 111 deletions(-)
New commits:
commit d8fba7c6ccea5e60f31f329f481fb2cdf6907ce9
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Dec 6 21:29:55 2013 -0500
systemctl: allow globbing in list-<whatever> commands
It is nicer to say 'systemctl list-units ssh\*' then to use grep,
because colouring is preserved and it is easier to match just against
the unit name.
diff --git a/man/systemctl.xml b/man/systemctl.xml
index a84a983..ce28602 100644
--- a/man/systemctl.xml
+++ b/man/systemctl.xml
@@ -526,22 +526,26 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
<variablelist>
<varlistentry>
- <term><command>list-units</command></term>
+ <term><command>list-units <optional><replaceable>PATTERN...</replaceable></optional></command></term>
<listitem>
<para>List known units (subject to limitations specified
- with <option>-t</option>).</para>
+ with <option>-t</option>). If one or more
+ <replaceable>PATTERN</replaceable>s are specified, only
+ units matching one of them are shown.</para>
<para>This is the default command.</para>
</listitem>
</varlistentry>
<varlistentry>
- <term><command>list-sockets</command></term>
+ <term><command>list-sockets <optional><replaceable>PATTERN...</replaceable></optional></command></term>
<listitem>
- <para>List socket units ordered by the listening address. Produces output
- similar to
+ <para>List socket units ordered by the listening address.
+ If one or more <replaceable>PATTERN</replaceable>s are
+ specified, only socket units matching one of them are
+ shown. Produces output similar to
<programlisting>
LISTEN UNIT ACTIVATES
/dev/initctl systemd-initctl.socket systemd-initctl.service
@@ -561,10 +565,13 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
</varlistentry>
<varlistentry>
- <term><command>list-timers</command></term>
+ <term><command>list-timers <optional><replaceable>PATTERN...</replaceable></optional></command></term>
<listitem>
- <para>List timer units ordered by the time they elapse next.</para>
+ <para>List timer units ordered by the time they elapse
+ next. If one or more <replaceable>PATTERN</replaceable>s
+ are specified, only units matching one of them are shown.
+ </para>
<para>See also the options <option>--all</option> and
<option>--failed</option>.</para>
@@ -814,10 +821,13 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
<variablelist>
<varlistentry>
- <term><command>list-unit-files</command></term>
+ <term><command>list-unit-files <optional><replaceable>PATTERN...</replaceable></optional></command></term>
<listitem>
- <para>List installed unit files.</para>
+ <para>List installed unit files. If one or more
+ <replaceable>PATTERN</replaceable>s are specified, only
+ units whose filename (just the last component of the path)
+ matches one of them are shown.</para>
</listitem>
</varlistentry>
@@ -1013,10 +1023,12 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
<variablelist>
<varlistentry>
- <term><command>list-jobs</command></term>
+ <term><command>list-jobs <optional><replaceable>PATTERN...</replaceable></optional></command></term>
<listitem>
- <para>List jobs that are in progress.</para>
+ <para>List jobs that are in progress. If one or more
+ <replaceable>PATTERN</replaceable>s are specified, only
+ jobs for units matching one of them are shown.</para>
</listitem>
</varlistentry>
<varlistentry>
@@ -1036,7 +1048,7 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
<variablelist>
<varlistentry>
- <term><command>snapshot [<replaceable>NAME</replaceable>]</command></term>
+ <term><command>snapshot <optional><replaceable>NAME</replaceable></optional></command></term>
<listitem>
<para>Create a snapshot. If a snapshot name is specified,
@@ -1281,7 +1293,7 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
</listitem>
</varlistentry>
<varlistentry>
- <term><command>switch-root <replaceable>ROOT</replaceable> [<replaceable>INIT</replaceable>]</command></term>
+ <term><command>switch-root <replaceable>ROOT</replaceable> <optional><replaceable>INIT</replaceable></optional></command></term>
<listitem>
<para>Switches to a different root directory and executes a
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index c6ebf9b..3248f51 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -37,6 +37,7 @@
#include <sys/stat.h>
#include <stddef.h>
#include <sys/prctl.h>
+#include <fnmatch.h>
#include "sd-daemon.h"
#include "sd-shutdown.h"
@@ -140,6 +141,12 @@ static int halt_now(enum action a);
static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet);
+static char** strv_skip_first(char **strv) {
+ if (strv_length(strv) > 0)
+ return strv + 1;
+ return NULL;
+}
+
static void pager_open_if_enabled(void) {
if (arg_no_pager)
@@ -280,7 +287,7 @@ static int compare_unit_info(const void *a, const void *b) {
return strcasecmp(u->id, v->id);
}
-static bool output_show_unit(const UnitInfo *u) {
+static bool output_show_unit(const UnitInfo *u, char **patterns) {
const char *dot;
if (!strv_isempty(arg_states))
@@ -289,13 +296,22 @@ static bool output_show_unit(const UnitInfo *u) {
strv_contains(arg_states, u->sub_state) ||
strv_contains(arg_states, u->active_state);
+ if (!strv_isempty(patterns)) {
+ char **pattern;
+
+ STRV_FOREACH(pattern, patterns)
+ if (fnmatch(*pattern, u->id, FNM_NOESCAPE) == 0)
+ return true;
+ return false;
+ }
+
return (!arg_types || ((dot = strrchr(u->id, '.')) &&
strv_find(arg_types, dot+1))) &&
(arg_all || !(streq(u->active_state, "inactive")
|| u->following[0]) || u->job_id > 0);
}
-static void output_units_list(const UnitInfo *unit_infos, unsigned c) {
+static void output_units_list(const UnitInfo *unit_infos, unsigned c, char** patterns) {
unsigned id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len;
const UnitInfo *u;
unsigned n_shown = 0;
@@ -309,7 +325,7 @@ static void output_units_list(const UnitInfo *unit_infos, unsigned c) {
desc_len = 0;
for (u = unit_infos; u < unit_infos + c; u++) {
- if (!output_show_unit(u))
+ if (!output_show_unit(u, patterns))
continue;
max_id_len = MAX(max_id_len, strlen(u->id));
@@ -358,7 +374,7 @@ static void output_units_list(const UnitInfo *unit_infos, unsigned c) {
const char *on_loaded, *off_loaded, *on = "";
const char *on_active, *off_active, *off = "";
- if (!output_show_unit(u))
+ if (!output_show_unit(u, patterns))
continue;
if (!n_shown && !arg_no_legend) {
@@ -504,7 +520,7 @@ static int list_units(sd_bus *bus, char **args) {
return r;
qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info);
- output_units_list(unit_infos, r);
+ output_units_list(unit_infos, r, strv_skip_first(args));
return 0;
}
@@ -693,7 +709,7 @@ static int list_sockets(sd_bus *bus, char **args) {
_cleanup_strv_free_ char **listening = NULL, **triggered = NULL;
int i, c;
- if (!output_show_unit(u))
+ if (!output_show_unit(u, strv_skip_first(args)))
continue;
if (!endswith(u->id, ".socket"))
@@ -910,7 +926,7 @@ static int list_timers(sd_bus *bus, char **args) {
dual_timestamp next;
usec_t m;
- if (!output_show_unit(u))
+ if (!output_show_unit(u, strv_skip_first(args)))
continue;
if (!endswith(u->id, ".timer"))
@@ -983,13 +999,22 @@ static int compare_unit_file_list(const void *a, const void *b) {
return strcasecmp(basename(u->path), basename(v->path));
}
-static bool output_show_unit_file(const UnitFileList *u) {
+static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
const char *dot;
+ if (!strv_isempty(patterns)) {
+ char **pattern;
+
+ STRV_FOREACH(pattern, patterns)
+ if (fnmatch(*pattern, basename(u->path), FNM_NOESCAPE) == 0)
+ return true;
+ return false;
+ }
+
return !arg_types || ((dot = strrchr(u->path, '.')) && strv_find(arg_types, dot+1));
}
-static void output_unit_file_list(const UnitFileList *units, unsigned c) {
+static void output_unit_file_list(const UnitFileList *units, unsigned c, char **patterns) {
unsigned max_id_len, id_cols, state_cols, n_shown = 0;
const UnitFileList *u;
@@ -997,7 +1022,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
state_cols = sizeof("STATE")-1;
for (u = units; u < units + c; u++) {
- if (!output_show_unit_file(u))
+ if (!output_show_unit_file(u, patterns))
continue;
max_id_len = MAX(max_id_len, strlen(basename(u->path)));
@@ -1024,7 +1049,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
const char *on, *off;
const char *id;
- if (!output_show_unit_file(u))
+ if (!output_show_unit_file(u, patterns))
continue;
n_shown++;
@@ -1137,7 +1162,7 @@ static int list_unit_files(sd_bus *bus, char **args) {
if (c > 0) {
qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
- output_unit_file_list(units, c);
+ output_unit_file_list(units, c, strv_skip_first(args));
}
return 0;
@@ -1511,7 +1536,7 @@ struct job_info {
const char *name, *type, *state;
};
-static void output_jobs_list(const struct job_info* jobs, unsigned n) {
+static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipped) {
unsigned id_len, unit_len, type_len, state_len;
const struct job_info *j;
const char *on, *off;
@@ -1523,7 +1548,7 @@ static void output_jobs_list(const struct job_info* jobs, unsigned n) {
on = ansi_highlight_green();
off = ansi_highlight_off();
- printf("%sNo jobs running.%s\n", on, off);
+ printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off);
return;
}
@@ -1581,6 +1606,19 @@ static void output_jobs_list(const struct job_info* jobs, unsigned n) {
}
}
+static bool output_show_job(struct job_info *job, char **patterns) {
+ if (!strv_isempty(patterns)) {
+ char **pattern;
+
+ STRV_FOREACH(pattern, patterns)
+ if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0)
+ return true;
+ return false;
+ }
+
+ return true;
+}
+
static int list_jobs(sd_bus *bus, char **args) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
@@ -1590,6 +1628,7 @@ static int list_jobs(sd_bus *bus, char **args) {
unsigned c = 0;
uint32_t id;
int r;
+ bool skipped = false;
r = sd_bus_call_method(
bus,
@@ -1610,16 +1649,17 @@ static int list_jobs(sd_bus *bus, char **args) {
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) {
+ struct job_info job = { id, name, type, state };
+
+ if (!output_show_job(&job, strv_skip_first(args))) {
+ skipped = true;
+ continue;
+ }
if (!GREEDY_REALLOC(jobs, size, c + 1))
return log_oom();
- jobs[c++] = (struct job_info) {
- id,
- name,
- type,
- state
- };
+ jobs[c++] = job;
}
if (r < 0)
return bus_log_parse_error(r);
@@ -1628,7 +1668,7 @@ static int list_jobs(sd_bus *bus, char **args) {
if (r < 0)
return bus_log_parse_error(r);
- output_jobs_list(jobs, c);
+ output_jobs_list(jobs, c, skipped);
return r;
}
@@ -3661,7 +3701,7 @@ static int show_all(
for (u = unit_infos; u < unit_infos + c; u++) {
_cleanup_free_ char *p = NULL;
- if (!output_show_unit(u))
+ if (!output_show_unit(u, NULL))
continue;
p = unit_dbus_path_from_name(u->id);
@@ -4770,9 +4810,9 @@ static int systemctl_help(void) {
" -o --output=STRING Change journal output mode (short, short-monotonic,\n"
" verbose, export, json, json-pretty, json-sse, cat)\n\n"
"Unit Commands:\n"
- " list-units List loaded units\n"
- " list-sockets List loaded sockets ordered by address\n"
- " list-timers List loaded timers ordered by next elapse\n"
+ " list-units [PATTERN...] List loaded units\n"
+ " list-sockets [PATTERN...] List loaded sockets ordered by address\n"
+ " list-timers [PATTERN...] List loaded timers ordered by next elapse\n"
" start [NAME...] Start (activate) one or more units\n"
" stop [NAME...] Stop (deactivate) one or more units\n"
" reload [NAME...] Reload one or more units\n"
@@ -4799,7 +4839,7 @@ static int systemctl_help(void) {
" 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"
+ " list-unit-files [PATTERN...] List installed unit files\n"
" enable [NAME...] Enable one or more unit files\n"
" disable [NAME...] Disable one or more unit files\n"
" reenable [NAME...] Reenable one or more unit files\n"
@@ -4813,7 +4853,7 @@ static int systemctl_help(void) {
" get-default Get the name of the default target\n"
" set-default NAME Set the default target\n\n"
"Job Commands:\n"
- " list-jobs List jobs\n"
+ " list-jobs [PATTERN...] List jobs\n"
" cancel [JOB...] Cancel all, one, or more jobs\n\n"
"Snapshot Commands:\n"
" snapshot [NAME] Create a snapshot\n"
@@ -5749,11 +5789,11 @@ static int systemctl_main(sd_bus *bus, int argc, char *argv[], int bus_error) {
const int argc;
int (* const dispatch)(sd_bus *bus, char **args);
} verbs[] = {
- { "list-units", LESS, 1, list_units },
- { "list-unit-files", EQUAL, 1, list_unit_files },
- { "list-sockets", LESS, 1, list_sockets },
- { "list-timers", LESS, 1, list_timers },
- { "list-jobs", EQUAL, 1, list_jobs },
+ { "list-units", MORE, 0, list_units },
+ { "list-unit-files", MORE, 1, list_unit_files },
+ { "list-sockets", MORE, 1, list_sockets },
+ { "list-timers", MORE, 1, list_timers },
+ { "list-jobs", MORE, 1, list_jobs },
{ "clear-jobs", EQUAL, 1, daemon_reload },
{ "cancel", MORE, 2, cancel_job },
{ "start", MORE, 2, start_unit },
commit 2b6bf07dd23bb467099d213c97b3875c5e453491
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Fri Dec 6 21:29:55 2013 -0500
Get rid of our reimplementation of basename
The only problem is that libgen.h #defines basename to point to it's
own broken implementation instead of the GNU one. This can be fixed
by #undefining basename.
diff --git a/src/core/execute.c b/src/core/execute.c
index a0d63a4..55b8bb5 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -41,6 +41,7 @@
#include <linux/seccomp-bpf.h>
#include <glob.h>
#include <libgen.h>
+#undef basename
#ifdef HAVE_PAM
#include <security/pam_appl.h>
@@ -908,7 +909,7 @@ static void rename_process_from_path(const char *path) {
/* This resulting string must fit in 10 chars (i.e. the length
* of "/sbin/init") to look pretty in /bin/ps */
- p = path_get_file_name(path);
+ p = basename(path);
if (isempty(p)) {
rename_process("(...)");
return;
@@ -1197,13 +1198,13 @@ int exec_spawn(ExecCommand *command,
goto fail_child;
}
- err = setup_output(context, STDOUT_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+ err = setup_output(context, STDOUT_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
if (err < 0) {
r = EXIT_STDOUT;
goto fail_child;
}
- err = setup_output(context, STDERR_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+ err = setup_output(context, STDERR_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
if (err < 0) {
r = EXIT_STDERR;
goto fail_child;
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index d43d1b4..99cf20d 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -2431,7 +2431,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
/* Add the file name we are currently looking at to
* the names of this unit, but only if it is a valid
* unit name. */
- name = path_get_file_name(*filename);
+ name = basename(*filename);
if (unit_name_is_valid(name, true)) {
diff --git a/src/core/manager.c b/src/core/manager.c
index 0a3e840..f06df54 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -1078,7 +1078,7 @@ int manager_load_unit_prepare(
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
if (!name)
- name = path_get_file_name(path);
+ name = basename(path);
t = unit_name_to_type(name);
diff --git a/src/core/service.c b/src/core/service.c
index 530a305..702443d 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -790,7 +790,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
goto finish;
}
- r = sysv_translate_facility(n, path_get_file_name(path), &m);
+ r = sysv_translate_facility(n, basename(path), &m);
free(n);
if (r < 0)
@@ -842,7 +842,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
goto finish;
}
- r = sysv_translate_facility(n, path_get_file_name(path), &m);
+ r = sysv_translate_facility(n, basename(path), &m);
if (r < 0) {
log_error_unit(u->id,
"[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
diff --git a/src/core/unit.c b/src/core/unit.c
index 81d2162..6458c7f 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -1848,7 +1848,7 @@ static const char *resolve_template(Unit *u, const char *name, const char*path,
assert(p);
if (!name)
- name = path_get_file_name(path);
+ name = basename(path);
if (!unit_name_is_template(name)) {
*p = NULL;
@@ -2615,7 +2615,7 @@ UnitFileState unit_get_unit_file_state(Unit *u) {
if (u->unit_file_state < 0 && u->fragment_path)
u->unit_file_state = unit_file_get_state(
u->manager->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
- NULL, path_get_file_name(u->fragment_path));
+ NULL, basename(u->fragment_path));
return u->unit_file_state;
}
diff --git a/src/delta/delta.c b/src/delta/delta.c
index 4aaf1df..0785e7b 100644
--- a/src/delta/delta.c
+++ b/src/delta/delta.c
@@ -251,7 +251,7 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
if (!p)
return -ENOMEM;
- k = hashmap_put(h, path_get_file_name(p), p);
+ k = hashmap_put(h, basename(p), p);
if (k < 0) {
free(p);
if (k != -EEXIST)
@@ -303,7 +303,7 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
path_kill_slashes(p);
- k = hashmap_put(top, path_get_file_name(p), p);
+ k = hashmap_put(top, basename(p), p);
if (k >= 0) {
p = strdup(p);
if (!p)
@@ -313,8 +313,8 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
return k;
}
- free(hashmap_remove(bottom, path_get_file_name(p)));
- k = hashmap_put(bottom, path_get_file_name(p), p);
+ free(hashmap_remove(bottom, basename(p)));
+ k = hashmap_put(bottom, basename(p), p);
if (k < 0) {
free(p);
return k;
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index c44be74..f74dde9 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -658,7 +658,7 @@ static int add_matches(sd_journal *j, char **args) {
if (executable_is_script(path, &interpreter) > 0) {
_cleanup_free_ char *comm;
- comm = strndup(path_get_file_name(path), 15);
+ comm = strndup(basename(path), 15);
if (!comm)
return log_oom();
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index 76a53f6..b0abe7b 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -415,7 +415,7 @@ static int nftw_cb(
!endswith(fpath, ".map.gz"))
return 0;
- p = strdup(path_get_file_name(fpath));
+ p = strdup(basename(fpath));
if (!p)
return log_oom();
diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c
index 35e1abd..ec6a722 100644
--- a/src/login/logind-inhibit.c
+++ b/src/login/logind-inhibit.c
@@ -45,7 +45,7 @@ Inhibitor* inhibitor_new(Manager *m, const char* id) {
return NULL;
}
- i->id = path_get_file_name(i->state_file);
+ i->id = basename(i->state_file);
if (hashmap_put(m->inhibitors, i->id, i) < 0) {
free(i->state_file);
diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c
index b1a5ec3..c738604 100644
--- a/src/login/logind-seat.c
+++ b/src/login/logind-seat.c
@@ -51,7 +51,7 @@ Seat *seat_new(Manager *m, const char *id) {
return NULL;
}
- s->id = path_get_file_name(s->state_file);
+ s->id = basename(s->state_file);
s->manager = m;
if (hashmap_put(m->seats, s->id, s) < 0) {
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
index beaa601..10ea526 100644
--- a/src/login/logind-session.c
+++ b/src/login/logind-session.c
@@ -79,7 +79,7 @@ Session* session_new(Manager *m, const char *id) {
return NULL;
}
- s->id = path_get_file_name(s->state_file);
+ s->id = basename(s->state_file);
if (hashmap_put(m->sessions, s->id, s) < 0) {
hashmap_free(s->devices);
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index eef0e9a..583912f 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -1096,7 +1096,7 @@ int main(int argc, char *argv[]) {
path_kill_slashes(arg_directory);
if (!arg_machine) {
- arg_machine = strdup(path_get_file_name(arg_directory));
+ arg_machine = strdup(basename(arg_directory));
if (!arg_machine) {
log_oom();
goto finish;
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index ce02b76..ee14cee 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -178,7 +178,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
if (last) {
printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_BRANCH),
- path_get_file_name(last));
+ basename(last));
if (!p1) {
p1 = strappend(prefix, draw_special_char(DRAW_TREE_VERT));
@@ -202,7 +202,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
if (last) {
printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT),
- path_get_file_name(last));
+ basename(last));
if (!p2) {
p2 = strappend(prefix, " ");
diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c
index ed4070c..92204a6 100644
--- a/src/shared/conf-files.c
+++ b/src/shared/conf-files.c
@@ -71,7 +71,7 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
if (!p)
return -ENOMEM;
- r = hashmap_put(h, path_get_file_name(p), p);
+ r = hashmap_put(h, basename(p), p);
if (r == -EEXIST) {
log_debug("Skipping overridden file: %s.", p);
free(p);
@@ -92,7 +92,7 @@ static int base_cmp(const void *a, const void *b) {
s1 = *(char * const *)a;
s2 = *(char * const *)b;
- return strcmp(path_get_file_name(s1), path_get_file_name(s2));
+ return strcmp(basename(s1), basename(s2));
}
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
diff --git a/src/shared/install.c b/src/shared/install.c
index 100ed69..17e8a75 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -276,10 +276,10 @@ static int remove_marked_symlinks_fd(
found =
set_get(remove_symlinks_to, dest) ||
- set_get(remove_symlinks_to, path_get_file_name(dest));
+ set_get(remove_symlinks_to, basename(dest));
if (unit_name_is_instance(p))
- found = found && strv_contains(files, path_get_file_name(p));
+ found = found && strv_contains(files, basename(p));
if (found) {
@@ -451,7 +451,7 @@ static int find_symlinks_fd(
if (path_is_absolute(name))
found_dest = path_equal(dest, name);
else
- found_dest = streq(path_get_file_name(dest), name);
+ found_dest = streq(basename(dest), name);
if (found_path && found_dest) {
_cleanup_free_ char *t = NULL;
@@ -718,7 +718,7 @@ int unit_file_link(
char *fn;
struct stat st;
- fn = path_get_file_name(*i);
+ fn = basename(*i);
if (!path_is_absolute(*i) ||
!unit_name_is_valid(fn, true)) {
@@ -861,7 +861,7 @@ static int install_info_add(
assert(name || path);
if (!name)
- name = path_get_file_name(path);
+ name = basename(path);
if (!unit_name_is_valid(name, true))
return -EINVAL;
@@ -1429,7 +1429,7 @@ static int install_context_mark_for_removal(
char *unit_file;
if (i->path) {
- unit_file = path_get_file_name(i->path);
+ unit_file = basename(i->path);
if (unit_name_is_instance(unit_file))
/* unit file named as instance exists, thus all symlinks
@@ -1647,7 +1647,7 @@ int unit_file_get_default(
else if (r < 0)
return r;
else
- n = strdup(path_get_file_name(tmp));
+ n = strdup(basename(tmp));
if (!n)
return -ENOMEM;
@@ -2007,7 +2007,7 @@ int unit_file_get_list(
f->state = UNIT_FILE_STATIC;
found:
- r = hashmap_put(h, path_get_file_name(f->path), f);
+ r = hashmap_put(h, basename(f->path), f);
if (r < 0)
return r;
f = NULL; /* prevent cleanup */
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 6c4efbf..3843861 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -45,18 +45,6 @@ bool is_path(const char *p) {
return !!strchr(p, '/');
}
-char *path_get_file_name(const char *p) {
- char *r;
-
- assert(p);
-
- r = strrchr(p, '/');
- if (r)
- return r + 1;
-
- return (char*) p;
-}
-
int path_get_parent(const char *path, char **_r) {
const char *e, *a = NULL, *b = NULL, *p;
char *r;
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index 42b4189..1098f65 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -34,7 +34,6 @@
bool is_path(const char *p) _pure_;
char** path_split_and_make_absolute(const char *p);
-char* path_get_file_name(const char *p) _pure_;
int path_get_parent(const char *path, char **parent);
bool path_is_absolute(const char *p) _pure_;
char* path_make_absolute(const char *p, const char *prefix);
diff --git a/src/shared/util.c b/src/shared/util.c
index c712931..9c07392 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -59,6 +59,7 @@
#include <langinfo.h>
#include <locale.h>
#include <libgen.h>
+#undef basename
#include "macro.h"
#include "util.h"
@@ -3961,8 +3962,8 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
if (!t)
return -ENOMEM;
- fn = path_get_file_name(path);
- k = fn-path;
+ fn = basename(path);
+ k = fn - path;
memcpy(t, path, k);
t[k] = '.';
stpcpy(stpcpy(t+k+1, fn), "XXXXXX");
@@ -4147,7 +4148,7 @@ int symlink_atomic(const char *from, const char *to) {
if (!t)
return -ENOMEM;
- fn = path_get_file_name(to);
+ fn = basename(to);
k = fn-to;
memcpy(t, to, k);
t[k] = '.';
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index 5d88405..4a3a3f4 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -216,7 +216,7 @@ int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line
strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
if (line)
- strncpy(store.ut_line, path_get_file_name(line), sizeof(store.ut_line));
+ strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
return write_entry_both(&store);
}
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 6b6cb3d..c6ebf9b 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -980,7 +980,7 @@ static int compare_unit_file_list(const void *a, const void *b) {
return r;
}
- return strcasecmp(path_get_file_name(u->path), path_get_file_name(v->path));
+ return strcasecmp(basename(u->path), basename(v->path));
}
static bool output_show_unit_file(const UnitFileList *u) {
@@ -1000,7 +1000,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
if (!output_show_unit_file(u))
continue;
- max_id_len = MAX(max_id_len, strlen(path_get_file_name(u->path)));
+ max_id_len = MAX(max_id_len, strlen(basename(u->path)));
state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
}
@@ -1041,7 +1041,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
} else
on = off = "";
- id = path_get_file_name(u->path);
+ id = basename(u->path);
e = arg_full ? NULL : ellipsize(id, id_cols, 33);
@@ -2688,7 +2688,7 @@ static void print_status_info(
last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
- printf("%s%s", path_get_file_name(*dropin), last ? "\n" : ", ");
+ printf("%s%s", basename(*dropin), last ? "\n" : ", ");
}
}
@@ -4404,7 +4404,7 @@ static int enable_sysv_units(const char *verb, char **args) {
if (!isempty(arg_root))
argv[c++] = q = strappend("--root=", arg_root);
- argv[c++] = path_get_file_name(p);
+ argv[c++] = basename(p);
argv[c++] =
streq(verb, "enable") ? "on" :
streq(verb, "disable") ? "off" : "--level=5";
diff --git a/src/test/test-install.c b/src/test/test-install.c
index 2c1b9ef..2087d52 100644
--- a/src/test/test-install.c
+++ b/src/test/test-install.c
@@ -58,7 +58,7 @@ int main(int argc, char* argv[]) {
HASHMAP_FOREACH(p, h, i) {
UnitFileState s;
- s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(p->path));
+ s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(p->path));
assert_se(p->state == s);
@@ -176,7 +176,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_ENABLED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_ENABLED);
log_error("disable files2");
changes = NULL;
@@ -188,7 +188,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
log_error("link files2");
changes = NULL;
@@ -200,7 +200,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_LINKED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_LINKED);
log_error("disable files2");
changes = NULL;
@@ -212,7 +212,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
log_error("link files2");
changes = NULL;
@@ -224,7 +224,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_LINKED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_LINKED);
log_error("reenable files2");
changes = NULL;
@@ -236,7 +236,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_ENABLED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == UNIT_FILE_ENABLED);
log_error("disable files2");
changes = NULL;
@@ -248,7 +248,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
log_error("preset files");
changes = NULL;
n_changes = 0;
@@ -259,7 +259,7 @@ int main(int argc, char* argv[]) {
dump_changes(changes, n_changes);
unit_file_changes_free(changes, n_changes);
- assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files[0])) == UNIT_FILE_ENABLED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, basename(files[0])) == UNIT_FILE_ENABLED);
return 0;
}
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index ed3b315..bec2a83 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -50,10 +50,10 @@ static void test_path(void) {
assert_se(is_path("a/b"));
assert_se(!is_path("."));
- assert_se(streq(path_get_file_name("./aa/bb/../file.da."), "file.da."));
- assert_se(streq(path_get_file_name("/aa///.file"), ".file"));
- assert_se(streq(path_get_file_name("/aa///file..."), "file..."));
- assert_se(streq(path_get_file_name("file.../"), ""));
+ assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
+ assert_se(streq(basename("/aa///.file"), ".file"));
+ assert_se(streq(basename("/aa///file..."), "file..."));
+ assert_se(streq(basename("file.../"), ""));
#define test_parent(x, y) { \
char _cleanup_free_ *z = NULL; \
More information about the systemd-commits
mailing list