[systemd-devel] [RFC 21/25] make sure basename that doesn't alter it's argument
Emil Renner Berthing
systemd at esmil.dk
Thu Sep 18 06:24:57 PDT 2014
---
src/core/execute.c | 6 +++---
src/core/load-fragment.c | 2 +-
src/core/manager.c | 2 +-
src/core/unit.c | 4 ++--
src/delta/delta.c | 14 +++++++-------
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 | 14 +++++++-------
src/shared/path-util.c | 8 ++++++++
src/shared/path-util.h | 5 +++++
src/shared/util.c | 4 ++--
src/shared/utmp-wtmp.c | 2 +-
src/systemctl/systemctl.c | 12 ++++++------
src/sysv-generator/sysv-generator.c | 4 ++--
src/test/test-install.c | 18 +++++++++---------
src/test/test-path-util.c | 8 ++++----
22 files changed, 68 insertions(+), 55 deletions(-)
diff --git a/src/core/execute.c b/src/core/execute.c
index e73eb8e..77724ce 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -912,7 +912,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 = basename(path);
+ p = path_basename(path);
if (isempty(p)) {
rename_process("(...)");
return;
@@ -1331,13 +1331,13 @@ static int exec_child(ExecCommand *command,
return err;
}
- err = setup_output(context, STDOUT_FILENO, socket_fd, basename(command->path), params->unit_id, params->apply_tty_stdin);
+ err = setup_output(context, STDOUT_FILENO, socket_fd, path_basename(command->path), params->unit_id, params->apply_tty_stdin);
if (err < 0) {
*error = EXIT_STDOUT;
return err;
}
- err = setup_output(context, STDERR_FILENO, socket_fd, basename(command->path), params->unit_id, params->apply_tty_stdin);
+ err = setup_output(context, STDERR_FILENO, socket_fd, path_basename(command->path), params->unit_id, params->apply_tty_stdin);
if (err < 0) {
*error = EXIT_STDERR;
return err;
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 0620882..61db112 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -3322,7 +3322,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 = basename(*filename);
+ name = path_basename(*filename);
if (unit_name_is_valid(name, TEMPLATE_VALID)) {
diff --git a/src/core/manager.c b/src/core/manager.c
index 88d660d..22a9c89 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -1217,7 +1217,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 = basename(path);
+ name = path_basename(path);
t = unit_name_to_type(name);
diff --git a/src/core/unit.c b/src/core/unit.c
index def5c36..c14859e 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -2170,7 +2170,7 @@ static const char *resolve_template(Unit *u, const char *name, const char*path,
assert(p);
if (!name)
- name = basename(path);
+ name = path_basename(path);
if (!unit_name_is_template(name)) {
*p = NULL;
@@ -2941,7 +2941,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, basename(u->fragment_path));
+ NULL, path_basename(u->fragment_path));
return u->unit_file_state;
}
diff --git a/src/delta/delta.c b/src/delta/delta.c
index 91f8592..cb049e8 100644
--- a/src/delta/delta.c
+++ b/src/delta/delta.c
@@ -282,8 +282,8 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
return -ENOMEM;
log_debug("Adding to drops: %s %s %s %s %s",
- unit, draw_special_char(DRAW_ARROW), basename(p), draw_special_char(DRAW_ARROW), p);
- k = hashmap_put(h, basename(p), p);
+ unit, draw_special_char(DRAW_ARROW), path_basename(p), draw_special_char(DRAW_ARROW), p);
+ k = hashmap_put(h, path_basename(p), p);
if (k < 0) {
free(p);
if (k != -EEXIST)
@@ -334,8 +334,8 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
if (!p)
return -ENOMEM;
- log_debug("Adding at top: %s %s %s", basename(p), draw_special_char(DRAW_ARROW), p);
- k = hashmap_put(top, basename(p), p);
+ log_debug("Adding at top: %s %s %s", path_basename(p), draw_special_char(DRAW_ARROW), p);
+ k = hashmap_put(top, path_basename(p), p);
if (k >= 0) {
p = strdup(p);
if (!p)
@@ -345,9 +345,9 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
return k;
}
- log_debug("Adding at bottom: %s %s %s", basename(p), draw_special_char(DRAW_ARROW), p);
- free(hashmap_remove(bottom, basename(p)));
- k = hashmap_put(bottom, basename(p), p);
+ log_debug("Adding at bottom: %s %s %s", path_basename(p), draw_special_char(DRAW_ARROW), p);
+ free(hashmap_remove(bottom, path_basename(p)));
+ k = hashmap_put(bottom, path_basename(p), p);
if (k < 0) {
free(p);
return k;
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 47206d3..3085c03 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -741,7 +741,7 @@ static int add_matches(sd_journal *j, char **args) {
if (executable_is_script(path, &interpreter) > 0) {
_cleanup_free_ char *comm;
- comm = strndup(basename(path), 15);
+ comm = strndup(path_basename(path), 15);
if (!comm)
return log_oom();
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index bf8b7b2..7b0ffb8 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -247,7 +247,7 @@ static int nftw_cb(
!endswith(fpath, ".map.gz"))
return 0;
- p = strdup(basename(fpath));
+ p = strdup(path_basename(fpath));
if (!p)
return log_oom();
diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c
index a9e14af..0a33eea 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 = basename(i->state_file);
+ i->id = path_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 9992195..5930689 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 = basename(s->state_file);
+ s->id = path_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 eeb58c9..f676d68 100644
--- a/src/login/logind-session.c
+++ b/src/login/logind-session.c
@@ -68,7 +68,7 @@ Session* session_new(Manager *m, const char *id) {
return NULL;
}
- s->id = basename(s->state_file);
+ s->id = path_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 5af89c9..45ed502 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -2938,7 +2938,7 @@ int main(int argc, char *argv[]) {
}
if (!arg_machine) {
- arg_machine = strdup(basename(arg_image ? arg_image : arg_directory));
+ arg_machine = strdup(path_basename(arg_image ? arg_image : arg_directory));
if (!arg_machine) {
log_oom();
goto finish;
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index c862ee9..afecd62 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -180,7 +180,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),
- basename(last));
+ path_basename(last));
if (!p1) {
p1 = strappend(prefix, draw_special_char(DRAW_TREE_VERTICAL));
@@ -204,7 +204,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),
- basename(last));
+ path_basename(last));
if (!p2) {
p2 = strappend(prefix, " ");
diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c
index e6ee97a..48b2796 100644
--- a/src/shared/conf-files.c
+++ b/src/shared/conf-files.c
@@ -73,7 +73,7 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
if (!p)
return -ENOMEM;
- r = hashmap_put(h, basename(p), p);
+ r = hashmap_put(h, path_basename(p), p);
if (r == -EEXIST) {
log_debug("Skipping overridden file: %s.", p);
free(p);
@@ -94,7 +94,7 @@ static int base_cmp(const void *a, const void *b) {
s1 = *(char * const *)a;
s2 = *(char * const *)b;
- return strcmp(basename(s1), basename(s2));
+ return strcmp(path_basename(s1), path_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 61e572b..5e3af30 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -309,7 +309,7 @@ static int remove_marked_symlinks_fd(
found =
set_get(remove_symlinks_to, dest) ||
- set_get(remove_symlinks_to, basename(dest));
+ set_get(remove_symlinks_to, path_basename(dest));
if (!found)
continue;
@@ -476,7 +476,7 @@ static int find_symlinks_fd(
if (path_is_absolute(name))
found_dest = path_equal(dest, name);
else
- found_dest = streq(basename(dest), name);
+ found_dest = streq(path_basename(dest), name);
if (found_path && found_dest) {
_cleanup_free_ char *t = NULL;
@@ -736,7 +736,7 @@ int unit_file_link(
char *fn;
struct stat st;
- fn = basename(*i);
+ fn = path_basename(*i);
if (!path_is_absolute(*i) ||
!unit_name_is_valid(fn, TEMPLATE_VALID)) {
@@ -875,7 +875,7 @@ static int install_info_add(
assert(name || path);
if (!name)
- name = basename(path);
+ name = path_basename(path);
if (!unit_name_is_valid(name, TEMPLATE_VALID))
return -EINVAL;
@@ -1457,7 +1457,7 @@ static int install_context_mark_for_removal(
char *unit_file;
if (i->path) {
- unit_file = basename(i->path);
+ unit_file = path_basename(i->path);
if (unit_name_is_instance(unit_file))
/* unit file named as instance exists, thus all symlinks
@@ -1671,7 +1671,7 @@ int unit_file_get_default(
else if (r < 0)
return r;
else
- n = strdup(basename(tmp));
+ n = strdup(path_basename(tmp));
if (!n)
return -ENOMEM;
@@ -2141,7 +2141,7 @@ int unit_file_get_list(
f->state = UNIT_FILE_STATIC;
found:
- r = hashmap_put(h, basename(f->path), f);
+ r = hashmap_put(h, path_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 67566bc..9fbe900 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -45,6 +45,14 @@ bool is_path(const char *p) {
return !!strchr(p, '/');
}
+#ifndef __GLIBC__
+char *path_basename(const char *path) {
+ char *res = strrchr(path, '/');
+
+ return res ? res+1 : (char *)path;
+}
+#endif
+
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 d85291b..ec2b3cf 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -36,6 +36,11 @@
#endif
bool is_path(const char *p) _pure_;
+#ifdef __GLIBC__
+#define path_basename basename
+#else
+char *path_basename(const char *path);
+#endif
char** path_split_and_make_absolute(const char *p);
int path_get_parent(const char *path, char **parent);
bool path_is_absolute(const char *p) _pure_;
diff --git a/src/shared/util.c b/src/shared/util.c
index 76899f5..94aa176 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6839,7 +6839,7 @@ char *tempfn_xxxxxx(const char *p) {
if (!t)
return NULL;
- fn = basename(p);
+ fn = path_basename(p);
k = fn - p;
strcpy(stpcpy(stpcpy(mempcpy(t, p, k), "."), fn), "XXXXXX");
@@ -6860,7 +6860,7 @@ char *tempfn_random(const char *p) {
if (!t)
return NULL;
- fn = basename(p);
+ fn = path_basename(p);
k = fn - p;
x = stpcpy(stpcpy(mempcpy(t, p, k), "."), fn);
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index 619d6d1..6dcc5e9 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -225,7 +225,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, basename(line), sizeof(store.ut_line));
+ strncpy(store.ut_line, path_basename(line), sizeof(store.ut_line));
return write_entry_both(&store);
}
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 9012128..d782532 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -1244,7 +1244,7 @@ static int compare_unit_file_list(const void *a, const void *b) {
return r;
}
- return strcasecmp(basename(u->path), basename(v->path));
+ return strcasecmp(path_basename(u->path), path_basename(v->path));
}
static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
@@ -1254,7 +1254,7 @@ static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
char **pattern;
STRV_FOREACH(pattern, patterns)
- if (fnmatch(*pattern, basename(u->path), FNM_NOESCAPE) == 0)
+ if (fnmatch(*pattern, path_basename(u->path), FNM_NOESCAPE) == 0)
return true;
return false;
}
@@ -1270,7 +1270,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
state_cols = strlen("STATE");
for (u = units; u < units + c; u++) {
- max_id_len = MAX(max_id_len, strlen(basename(u->path)));
+ max_id_len = MAX(max_id_len, strlen(path_basename(u->path)));
state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
}
@@ -1306,7 +1306,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
} else
on = off = "";
- id = basename(u->path);
+ id = path_basename(u->path);
e = arg_full ? NULL : ellipsize(id, id_cols, 33);
@@ -3300,7 +3300,7 @@ static void print_status_info(
last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
- printf("%s%s", basename(*dropin), last ? "\n" : ", ");
+ printf("%s%s", path_basename(*dropin), last ? "\n" : ", ");
}
}
@@ -5038,7 +5038,7 @@ static int enable_sysv_units(const char *verb, char **args) {
if (!isempty(arg_root))
argv[c++] = q = strappend("--root=", arg_root);
- argv[c++] = basename(p);
+ argv[c++] = path_basename(p);
argv[c++] =
streq(verb, "enable") ? "on" :
streq(verb, "disable") ? "off" : "--level=5";
diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c
index 43bcaa8..510e247 100644
--- a/src/sysv-generator/sysv-generator.c
+++ b/src/sysv-generator/sysv-generator.c
@@ -454,7 +454,7 @@ static int load_sysv(SysvStub *s) {
if (!n)
return -ENOMEM;
- r = sysv_translate_facility(n, basename(s->path), &m);
+ r = sysv_translate_facility(n, path_basename(s->path), &m);
if (r < 0)
return r;
@@ -516,7 +516,7 @@ static int load_sysv(SysvStub *s) {
if (!n)
return -ENOMEM;
- r = sysv_translate_facility(n, basename(s->path), &m);
+ r = sysv_translate_facility(n, path_basename(s->path), &m);
if (r < 0) {
log_error_unit(s->name,
"[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
diff --git a/src/test/test-install.c b/src/test/test-install.c
index b0f77a1..c4bd520 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, basename(p->path));
+ s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == UNIT_FILE_ENABLED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == UNIT_FILE_LINKED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == UNIT_FILE_LINKED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == UNIT_FILE_ENABLED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files2[0])) == _UNIT_FILE_STATE_INVALID);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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, basename(files[0])) == UNIT_FILE_ENABLED);
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_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 01afb3e..ab099ba 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -52,10 +52,10 @@ static void test_path(void) {
assert_se(is_path("a/b"));
assert_se(!is_path("."));
- 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.../"), ""));
+ assert_se(streq(path_basename("./aa/bb/../file.da."), "file.da."));
+ assert_se(streq(path_basename("/aa///.file"), ".file"));
+ assert_se(streq(path_basename("/aa///file..."), "file..."));
+ assert_se(streq(path_basename("file.../"), ""));
#define test_parent(x, y) { \
_cleanup_free_ char *z = NULL; \
--
2.1.0
More information about the systemd-devel
mailing list