From dce818b390a857a11f7dd634684500675cf79833 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Thu, 12 Apr 2012 17:15:18 +0200 Subject: move all tools to subdirs --- src/sysctl/sysctl.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 src/sysctl/sysctl.c (limited to 'src/sysctl/sysctl.c') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c new file mode 100644 index 0000000000..57fba25605 --- /dev/null +++ b/src/sysctl/sysctl.c @@ -0,0 +1,272 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "strv.h" +#include "util.h" +#include "strv.h" + +#define PROC_SYS_PREFIX "/proc/sys/" + +static char **arg_prefixes = NULL; + +static int apply_sysctl(const char *property, const char *value) { + char *p, *n; + int r = 0, k; + + log_debug("Setting '%s' to '%s'", property, value); + + p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)); + if (!p) { + log_error("Out of memory"); + return -ENOMEM; + } + + n = stpcpy(p, PROC_SYS_PREFIX); + strcpy(n, property); + + for (; *n; n++) + if (*n == '.') + *n = '/'; + + if (!strv_isempty(arg_prefixes)) { + char **i; + bool good = false; + + STRV_FOREACH(i, arg_prefixes) + if (path_startswith(p, *i)) { + good = true; + break; + } + + if (!good) { + log_debug("Skipping %s", p); + free(p); + return 0; + } + } + + k = write_one_line_file(p, value); + if (k < 0) { + + log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING, + "Failed to write '%s' to '%s': %s", value, p, strerror(-k)); + + if (k != -ENOENT && r == 0) + r = k; + } + + free(p); + + return r; +} + +static int apply_file(const char *path, bool ignore_enoent) { + FILE *f; + int r = 0; + + assert(path); + + if (!(f = fopen(path, "re"))) { + if (ignore_enoent && errno == ENOENT) + return 0; + + log_error("Failed to open file '%s', ignoring: %m", path); + return -errno; + } + + log_debug("apply: %s\n", path); + while (!feof(f)) { + char l[LINE_MAX], *p, *value; + int k; + + if (!fgets(l, sizeof(l), f)) { + if (feof(f)) + break; + + log_error("Failed to read file '%s', ignoring: %m", path); + r = -errno; + goto finish; + } + + p = strstrip(l); + + if (!*p) + continue; + + if (strchr(COMMENTS, *p)) + continue; + + if (!(value = strchr(p, '='))) { + log_error("Line is not an assignment in file '%s': %s", path, value); + + if (r == 0) + r = -EINVAL; + continue; + } + + *value = 0; + value++; + + if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0) + r = k; + } + +finish: + fclose(f); + + return r; +} + +static int help(void) { + + printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" + "Applies kernel sysctl settings.\n\n" + " -h --help Show this help\n" + " --prefix=PATH Only apply rules that apply to paths with the specified prefix\n", + program_invocation_short_name); + + return 0; +} + +static int parse_argv(int argc, char *argv[]) { + + enum { + ARG_PREFIX + }; + + static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "prefix", required_argument, NULL, ARG_PREFIX }, + { NULL, 0, NULL, 0 } + }; + + int c; + + assert(argc >= 0); + assert(argv); + + while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) { + + switch (c) { + + case 'h': + help(); + return 0; + + case ARG_PREFIX: { + char *p; + char **l; + + for (p = optarg; *p; p++) + if (*p == '.') + *p = '/'; + + l = strv_append(arg_prefixes, optarg); + if (!l) { + log_error("Out of memory"); + return -ENOMEM; + } + + strv_free(arg_prefixes); + arg_prefixes = l; + + break; + } + + case '?': + return -EINVAL; + + default: + log_error("Unknown option code %c", c); + return -EINVAL; + } + } + + return 1; +} + +int main(int argc, char *argv[]) { + int r = 0; + + r = parse_argv(argc, argv); + if (r <= 0) + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; + + log_set_target(LOG_TARGET_AUTO); + log_parse_environment(); + log_open(); + + umask(0022); + + if (argc > optind) { + int i; + + for (i = optind; i < argc; i++) { + int k; + + k = apply_file(argv[i], false); + if (k < 0 && r == 0) + r = k; + } + } else { + char **files, **f; + int k; + + r = conf_files_list(&files, ".conf", + "/etc/sysctl.d", + "/run/sysctl.d", + "/usr/local/lib/sysctl.d", + "/usr/lib/sysctl.d", +#ifdef HAVE_SPLIT_USR + "/lib/sysctl.d", +#endif + NULL); + if (r < 0) { + log_error("Failed to enumerate sysctl.d files: %s", strerror(-r)); + goto finish; + } + + STRV_FOREACH(f, files) { + k = apply_file(*f, true); + if (k < 0 && r == 0) + r = k; + } + + k = apply_file("/etc/sysctl.conf", true); + if (k < 0 && r == 0) + r = k; + + strv_free(files); + } +finish: + strv_free(arg_prefixes); + + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} -- cgit v1.2.3-54-g00ecf From 2c21044f05e32ec483b6ab13e175278779e9ebe3 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Mon, 7 May 2012 18:55:45 +0200 Subject: util: split-out conf-file.[ch] --- Makefile.am | 6 +- src/binfmt/binfmt.c | 1 + src/modules-load/modules-load.c | 1 + src/shared/conf-files.c | 151 ++++++++++++++++++++++++++++++++++++++++ src/shared/conf-files.h | 31 +++++++++ src/shared/install.c | 1 + src/shared/util.c | 114 ------------------------------ src/shared/util.h | 3 - src/sysctl/sysctl.c | 1 + src/tmpfiles/tmpfiles.c | 1 + src/udev/udev-rules.c | 1 + 11 files changed, 190 insertions(+), 121 deletions(-) create mode 100644 src/shared/conf-files.c create mode 100644 src/shared/conf-files.h (limited to 'src/sysctl/sysctl.c') diff --git a/Makefile.am b/Makefile.am index c85a401db4..2bc8e01c95 100644 --- a/Makefile.am +++ b/Makefile.am @@ -547,6 +547,8 @@ libsystemd_shared_la_SOURCES = \ src/shared/ioprio.h \ src/shared/socket-util.c \ src/shared/socket-util.h \ + src/shared/conf-files.c \ + src/shared/conf-files.h \ src/shared/cgroup-util.c \ src/shared/cgroup-util.h \ src/shared/cgroup-show.c \ @@ -564,10 +566,6 @@ libsystemd_shared_la_SOURCES = \ src/shared/spawn-polkit-agent.c \ src/shared/spawn-polkit-agent.h -libsystemd_shared_la_CFLAGS = \ - $(AM_CFLAGS) \ - $(DBUS_CFLAGS) - #------------------------------------------------------------------------------- noinst_LTLIBRARIES += \ libsystemd-dbus.la diff --git a/src/binfmt/binfmt.c b/src/binfmt/binfmt.c index 5bd763339e..0399ab75e0 100644 --- a/src/binfmt/binfmt.c +++ b/src/binfmt/binfmt.c @@ -31,6 +31,7 @@ #include "hashmap.h" #include "strv.h" #include "util.h" +#include "conf-files.h" static int delete_rule(const char *rule) { char *x, *fn = NULL, *e; diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index 0f2144c2ed..c78f7d8aa8 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -31,6 +31,7 @@ #include "log.h" #include "util.h" #include "strv.h" +#include "conf-files.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c new file mode 100644 index 0000000000..019fadcf5e --- /dev/null +++ b/src/shared/conf-files.c @@ -0,0 +1,151 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "macro.h" +#include "util.h" +#include "missing.h" +#include "log.h" +#include "strv.h" +#include "hashmap.h" +#include "conf-files.h" + +static int files_add(Hashmap *h, const char *path, const char *suffix) { + DIR *dir; + struct dirent buffer, *de; + int r = 0; + + dir = opendir(path); + if (!dir) { + if (errno == ENOENT) + return 0; + return -errno; + } + + for (;;) { + int k; + char *p; + + k = readdir_r(dir, &buffer, &de); + if (k != 0) { + r = -k; + goto finish; + } + + if (!de) + break; + + if (!dirent_is_file_with_suffix(de, suffix)) + continue; + + if (asprintf(&p, "%s/%s", path, de->d_name) < 0) { + r = -ENOMEM; + goto finish; + } + + if (hashmap_put(h, file_name_from_path(p), p) <= 0) { + log_debug("Skip overridden file: %s.", p); + free(p); + } + } + +finish: + closedir(dir); + return r; +} + +static int base_cmp(const void *a, const void *b) { + const char *s1, *s2; + + s1 = *(char * const *)a; + s2 = *(char * const *)b; + return strcmp(file_name_from_path(s1), file_name_from_path(s2)); +} + +int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs) { + Hashmap *fh = NULL; + char **files = NULL; + const char **p; + int r = 0; + + assert(dirs); + + fh = hashmap_new(string_hash_func, string_compare_func); + if (!fh) { + r = -ENOMEM; + goto finish; + } + + STRV_FOREACH(p, dirs) { + if (files_add(fh, *p, suffix) < 0) { + log_error("Failed to search for files."); + r = -EINVAL; + goto finish; + } + } + + files = hashmap_get_strv(fh); + if (files == NULL) { + log_error("Failed to compose list of files."); + r = -ENOMEM; + goto finish; + } + qsort(files, hashmap_size(fh), sizeof(char *), base_cmp); + +finish: + hashmap_free(fh); + *strv = files; + return r; +} + +int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) { + char **dirs = NULL; + va_list ap; + int r; + + va_start(ap, dir); + dirs = strv_new_ap(dir, ap); + va_end(ap); + if (!dirs) { + r = -ENOMEM; + goto finish; + } + + if (!strv_path_canonicalize(dirs)) { + r = -ENOMEM; + goto finish; + } + strv_uniq(dirs); + + r = conf_files_list_strv(strv, suffix, (const char **)dirs); + +finish: + strv_free(dirs); + return r; +} diff --git a/src/shared/conf-files.h b/src/shared/conf-files.h new file mode 100644 index 0000000000..f37ee1f3db --- /dev/null +++ b/src/shared/conf-files.h @@ -0,0 +1,31 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#ifndef fooconffileshfoo +#define fooconffileshfoo + +/*** + This file is part of systemd. + + Copyright 2010-2012 Lennart Poettering + Copyright 2010-2012 Kay Sievers + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include "macro.h" + +int conf_files_list(char ***strv, const char *suffix, const char *dir, ...); +int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs); + +#endif diff --git a/src/shared/install.c b/src/shared/install.c index a982c54142..a77dfc77e4 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -34,6 +34,7 @@ #include "unit-name.h" #include "install.h" #include "conf-parser.h" +#include "conf-files.h" typedef struct { char *name; diff --git a/src/shared/util.c b/src/shared/util.c index 2bfa2cb4e6..d6f5661c24 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -4863,120 +4863,6 @@ int vt_disallocate(const char *name) { return 0; } -static int files_add(Hashmap *h, const char *path, const char *suffix) { - DIR *dir; - struct dirent buffer, *de; - int r = 0; - - dir = opendir(path); - if (!dir) { - if (errno == ENOENT) - return 0; - return -errno; - } - - for (;;) { - int k; - char *p; - - k = readdir_r(dir, &buffer, &de); - if (k != 0) { - r = -k; - goto finish; - } - - if (!de) - break; - - if (!dirent_is_file_with_suffix(de, suffix)) - continue; - - if (asprintf(&p, "%s/%s", path, de->d_name) < 0) { - r = -ENOMEM; - goto finish; - } - - if (hashmap_put(h, file_name_from_path(p), p) <= 0) { - log_debug("Skip overridden file: %s.", p); - free(p); - } - } - -finish: - closedir(dir); - return r; -} - -static int base_cmp(const void *a, const void *b) { - const char *s1, *s2; - - s1 = *(char * const *)a; - s2 = *(char * const *)b; - return strcmp(file_name_from_path(s1), file_name_from_path(s2)); -} - -int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs) { - Hashmap *fh = NULL; - char **files = NULL; - const char **p; - int r = 0; - - assert(dirs); - - fh = hashmap_new(string_hash_func, string_compare_func); - if (!fh) { - r = -ENOMEM; - goto finish; - } - - STRV_FOREACH(p, dirs) { - if (files_add(fh, *p, suffix) < 0) { - log_error("Failed to search for files."); - r = -EINVAL; - goto finish; - } - } - - files = hashmap_get_strv(fh); - if (files == NULL) { - log_error("Failed to compose list of files."); - r = -ENOMEM; - goto finish; - } - qsort(files, hashmap_size(fh), sizeof(char *), base_cmp); - -finish: - hashmap_free(fh); - *strv = files; - return r; -} - -int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) { - char **dirs = NULL; - va_list ap; - int r; - - va_start(ap, dir); - dirs = strv_new_ap(dir, ap); - va_end(ap); - if (!dirs) { - r = -ENOMEM; - goto finish; - } - - if (!strv_path_canonicalize(dirs)) { - r = -ENOMEM; - goto finish; - } - strv_uniq(dirs); - - r = conf_files_list_strv(strv, suffix, (const char **)dirs); - -finish: - strv_free(dirs); - return r; -} - int hwclock_is_localtime(void) { FILE *f; bool local = false; diff --git a/src/shared/util.h b/src/shared/util.h index 7a2661a04f..b246996fcc 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -460,9 +460,6 @@ int symlink_or_copy_atomic(const char *from, const char *to); int fchmod_umask(int fd, mode_t mode); -int conf_files_list(char ***strv, const char *suffix, const char *dir, ...); -int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs); - int hwclock_is_localtime(void); int hwclock_apply_localtime_delta(int *min); int hwclock_reset_localtime_delta(void); diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 57fba25605..575095f812 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -31,6 +31,7 @@ #include "strv.h" #include "util.h" #include "strv.h" +#include "conf-files.h" #define PROC_SYS_PREFIX "/proc/sys/" diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index d8fcb40155..16666ce5a6 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -45,6 +45,7 @@ #include "strv.h" #include "label.h" #include "set.h" +#include "conf-files.h" /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates * them in the file system. This is intended to be used to create diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 22b91c6797..45a611474d 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -31,6 +31,7 @@ #include #include "udev.h" +#include "conf-files.h" #define PREALLOC_TOKEN 2048 #define PREALLOC_STRBUF 32 * 1024 -- cgit v1.2.3-54-g00ecf From 9eb977db5b89b44f254ab40c1876a76b7d7ea2d0 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Mon, 7 May 2012 21:36:12 +0200 Subject: util: split-out path-util.[ch] --- Makefile.am | 2 + src/cgls/cgls.c | 1 + src/cgtop/cgtop.c | 1 + src/core/automount.c | 1 + src/core/cgroup.c | 1 + src/core/condition.c | 1 + src/core/device.c | 1 + src/core/execute.c | 7 +- src/core/load-fragment.c | 3 +- src/core/manager.c | 3 +- src/core/mount-setup.c | 1 + src/core/mount.c | 1 + src/core/namespace.c | 1 + src/core/path.c | 1 + src/core/service.c | 5 +- src/core/socket.c | 1 + src/core/swap.c | 1 + src/core/umount.c | 1 + src/core/unit.c | 7 +- src/cryptsetup/cryptsetup.c | 1 + src/journal/sd-journal.c | 3 +- src/login/logind-dbus.c | 1 + src/login/logind-inhibit.c | 4 +- src/login/logind-seat.c | 3 +- src/login/logind-session.c | 3 +- src/login/sysfs-show.c | 1 + src/nspawn/nspawn.c | 3 +- src/remount-fs/remount-fs.c | 1 + src/shared/cgroup-show.c | 5 +- src/shared/cgroup-util.c | 3 +- src/shared/conf-files.c | 7 +- src/shared/conf-parser.c | 1 + src/shared/install.c | 11 +- src/shared/label.c | 1 + src/shared/path-lookup.c | 22 +- src/shared/path-util.c | 371 +++++++++++++++++++++ src/shared/path-util.h | 46 +++ src/shared/socket-util.c | 1 + src/shared/unit-name.c | 1 + src/shared/util.c | 350 +------------------ src/shared/util.h | 27 -- src/shared/utmp-wtmp.c | 3 +- src/sysctl/sysctl.c | 1 + src/systemctl/systemctl.c | 10 +- src/test/test-cgroup.c | 1 + src/test/test-install.c | 19 +- src/tmpfiles/tmpfiles.c | 1 + .../tty-ask-password-agent.c | 1 + src/udev/udev-rules.c | 3 +- 49 files changed, 521 insertions(+), 424 deletions(-) create mode 100644 src/shared/path-util.c create mode 100644 src/shared/path-util.h (limited to 'src/sysctl/sysctl.c') diff --git a/Makefile.am b/Makefile.am index a6c4988382..dbcfeb384f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -526,6 +526,8 @@ libsystemd_shared_la_SOURCES = \ src/shared/util.h \ src/shared/virt.c \ src/shared/virt.h \ + src/shared/path-util.c \ + src/shared/path-util.h \ src/shared/hashmap.c \ src/shared/hashmap.h \ src/shared/set.c \ diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 4bec6a827e..b2cd968e46 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -29,6 +29,7 @@ #include "cgroup-show.h" #include "cgroup-util.h" #include "log.h" +#include "path-util.h" #include "util.h" #include "pager.h" #include "build.h" diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 1fe247c667..cc7a99f3df 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -26,6 +26,7 @@ #include #include +#include "path-util.h" #include "util.h" #include "hashmap.h" #include "cgroup-util.h" diff --git a/src/core/automount.c b/src/core/automount.c index 11c217beb9..c31e3d8bac 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -39,6 +39,7 @@ #include "special.h" #include "label.h" #include "mkdir.h" +#include "path-util.h" static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = { [AUTOMOUNT_DEAD] = UNIT_INACTIVE, diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 713c1ca768..5513f6560c 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -31,6 +31,7 @@ #include "cgroup-util.h" #include "log.h" #include "strv.h" +#include "path-util.h" int cgroup_bonding_realize(CGroupBonding *b) { int r; diff --git a/src/core/condition.c b/src/core/condition.c index 5d44039e5d..e4080d569d 100644 --- a/src/core/condition.c +++ b/src/core/condition.c @@ -33,6 +33,7 @@ #include "util.h" #include "condition.h" #include "virt.h" +#include "path-util.h" Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) { Condition *c; diff --git a/src/core/device.c b/src/core/device.c index 88ce0cdd18..f2bb656397 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -30,6 +30,7 @@ #include "unit-name.h" #include "dbus-device.h" #include "def.h" +#include "path-util.h" static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = { [DEVICE_DEAD] = UNIT_INACTIVE, diff --git a/src/core/execute.c b/src/core/execute.c index 99a7881f1c..953cfa2baa 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -59,6 +59,7 @@ #include "utmp-wtmp.h" #include "def.h" #include "loopback-setup.h" +#include "path-util.h" /* This assumes there is a 'tty' group */ #define TTY_MODE 0620 @@ -929,7 +930,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 = file_name_from_path(path); + p = path_get_file_name(path); if (isempty(p)) { rename_process("(...)"); return; @@ -1152,14 +1153,14 @@ int exec_spawn(ExecCommand *command, } if (!keep_stdout) { - err = setup_output(context, socket_fd, file_name_from_path(command->path), apply_tty_stdin); + err = setup_output(context, socket_fd, path_get_file_name(command->path), apply_tty_stdin); if (err < 0) { r = EXIT_STDOUT; goto fail_child; } } - err = setup_error(context, socket_fd, file_name_from_path(command->path), apply_tty_stdin); + err = setup_error(context, socket_fd, path_get_file_name(command->path), 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 d24919f998..c2efec6657 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -44,6 +44,7 @@ #include "unit-name.h" #include "bus-errors.h" #include "utf8.h" +#include "path-util.h" #ifndef HAVE_SYSV_COMPAT int config_parse_warn_compat( @@ -2089,7 +2090,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 = file_name_from_path(*filename); + name = path_get_file_name(*filename); if (unit_name_is_valid(name, true)) { diff --git a/src/core/manager.c b/src/core/manager.c index 62222e6c7a..c6baf22ae5 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -65,6 +65,7 @@ #include "virt.h" #include "watchdog.h" #include "cgroup-util.h" +#include "path-util.h" /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */ #define GC_QUEUE_ENTRIES_MAX 16 @@ -785,7 +786,7 @@ int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DB } if (!name) - name = file_name_from_path(path); + name = path_get_file_name(path); t = unit_name_to_type(name); diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index 6d37bbde3c..56ce2ae71a 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -38,6 +38,7 @@ #include "set.h" #include "strv.h" #include "mkdir.h" +#include "path-util.h" #ifndef TTY_GID #define TTY_GID 5 diff --git a/src/core/mount.c b/src/core/mount.c index 8bdd462ea1..01e9d78ad5 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -32,6 +32,7 @@ #include "log.h" #include "strv.h" #include "mkdir.h" +#include "path-util.h" #include "mount-setup.h" #include "unit-name.h" #include "dbus-mount.h" diff --git a/src/core/namespace.c b/src/core/namespace.c index 7ac6b381ab..ba1edbe512 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -33,6 +33,7 @@ #include "strv.h" #include "util.h" +#include "path-util.h" #include "namespace.h" #include "missing.h" diff --git a/src/core/path.c b/src/core/path.c index 0f23f1494d..d6fedc736a 100644 --- a/src/core/path.c +++ b/src/core/path.c @@ -32,6 +32,7 @@ #include "dbus-path.h" #include "special.h" #include "bus-errors.h" +#include "path-util.h" static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = { [PATH_DEAD] = UNIT_INACTIVE, diff --git a/src/core/service.c b/src/core/service.c index 40f1682784..e9a7000bae 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -38,6 +38,7 @@ #include "bus-errors.h" #include "exit-status.h" #include "def.h" +#include "path-util.h" #include "util.h" #include "utf8.h" @@ -722,7 +723,7 @@ static int service_load_sysv_path(Service *s, const char *path) { goto finish; } - r = sysv_translate_facility(n, file_name_from_path(path), &m); + r = sysv_translate_facility(n, path_get_file_name(path), &m); free(n); if (r < 0) @@ -772,7 +773,7 @@ static int service_load_sysv_path(Service *s, const char *path) { goto finish; } - r = sysv_translate_facility(n, file_name_from_path(path), &m); + r = sysv_translate_facility(n, path_get_file_name(path), &m); if (r < 0) { log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r)); diff --git a/src/core/socket.c b/src/core/socket.c index 60ea3cb25c..37e85d5fbe 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -37,6 +37,7 @@ #include "load-fragment.h" #include "strv.h" #include "mkdir.h" +#include "path-util.h" #include "unit-name.h" #include "dbus-socket.h" #include "missing.h" diff --git a/src/core/swap.c b/src/core/swap.c index 3881d2831f..a7e2126edd 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -38,6 +38,7 @@ #include "bus-errors.h" #include "exit-status.h" #include "def.h" +#include "path-util.h" static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = { [SWAP_DEAD] = UNIT_INACTIVE, diff --git a/src/core/umount.c b/src/core/umount.c index 488e1e4a4d..71ad4b623d 100644 --- a/src/core/umount.c +++ b/src/core/umount.c @@ -32,6 +32,7 @@ #include "list.h" #include "mount-setup.h" #include "umount.h" +#include "path-util.h" #include "util.h" typedef struct MountPoint { diff --git a/src/core/unit.c b/src/core/unit.c index fea75e88fe..68948574ed 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -33,6 +33,7 @@ #include "unit.h" #include "macro.h" #include "strv.h" +#include "path-util.h" #include "load-fragment.h" #include "load-dropin.h" #include "log.h" @@ -1670,7 +1671,7 @@ static const char *resolve_template(Unit *u, const char *name, const char*path, assert(name || path); if (!name) - name = file_name_from_path(path); + name = path_get_file_name(path); if (!unit_name_is_template(name)) { *p = NULL; @@ -2179,7 +2180,7 @@ static char *specifier_cgroup_root(char specifier, void *data, void *userdata) { if (specifier == 'r') return strdup(u->manager->cgroup_hierarchy); - if (parent_of_path(u->manager->cgroup_hierarchy, &p) < 0) + if (path_get_parent(u->manager->cgroup_hierarchy, &p) < 0) return strdup(""); if (streq(p, "/")) { @@ -2681,7 +2682,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 == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, - NULL, file_name_from_path(u->fragment_path)); + NULL, path_get_file_name(u->fragment_path)); return u->unit_file_state; } diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 3ff0ddf2cc..6d4e965fe1 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -29,6 +29,7 @@ #include "log.h" #include "util.h" +#include "path-util.h" #include "strv.h" #include "ask-password-api.h" #include "def.h" diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index 9d381e044c..4f3f1b5ff3 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -30,6 +30,7 @@ #include "journal-file.h" #include "hashmap.h" #include "list.h" +#include "path-util.h" #include "lookup3.h" #include "compress.h" #include "journal-internal.h" @@ -765,7 +766,7 @@ _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) { bid, (unsigned long long) le64toh(o->entry.monotonic), (unsigned long long) le64toh(o->entry.realtime), (unsigned long long) le64toh(o->entry.xor_hash), - file_name_from_path(j->current_file->path)) < 0) + path_get_file_name(j->current_file->path)) < 0) return -ENOMEM; return 1; diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index a361b93dc7..9fc3a14317 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -28,6 +28,7 @@ #include "dbus-common.h" #include "strv.h" #include "mkdir.h" +#include "path-util.h" #include "polkit.h" #include "special.h" diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index e4eefd0def..b5b801e5e0 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -28,7 +28,7 @@ #include "util.h" #include "mkdir.h" - +#include "path-util.h" #include "logind-inhibit.h" Inhibitor* inhibitor_new(Manager *m, const char* id) { @@ -46,7 +46,7 @@ Inhibitor* inhibitor_new(Manager *m, const char* id) { return NULL; } - i->id = file_name_from_path(i->state_file); + i->id = path_get_file_name(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 1bae6f6e07..06debf887a 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -31,6 +31,7 @@ #include "logind-acl.h" #include "util.h" #include "mkdir.h" +#include "path-util.h" Seat *seat_new(Manager *m, const char *id) { Seat *s; @@ -48,7 +49,7 @@ Seat *seat_new(Manager *m, const char *id) { return NULL; } - s->id = file_name_from_path(s->state_file); + s->id = path_get_file_name(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 e297d3feeb..475ebcaa5e 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -28,6 +28,7 @@ #include "strv.h" #include "util.h" #include "mkdir.h" +#include "path-util.h" #include "cgroup-util.h" #include "logind-session.h" @@ -49,7 +50,7 @@ Session* session_new(Manager *m, User *u, const char *id) { return NULL; } - s->id = file_name_from_path(s->state_file); + s->id = path_get_file_name(s->state_file); if (hashmap_put(m->sessions, s->id, s) < 0) { free(s->state_file); diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c index b0edc33b80..bc1bbccff7 100644 --- a/src/login/sysfs-show.c +++ b/src/login/sysfs-show.c @@ -25,6 +25,7 @@ #include "util.h" #include "sysfs-show.h" +#include "path-util.h" static int show_sysfs_one( struct udev *udev, diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 2a1f37bffc..31e8b015df 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -50,6 +50,7 @@ #include "missing.h" #include "cgroup-util.h" #include "strv.h" +#include "path-util.h" #include "loopback-setup.h" static char *arg_directory = NULL; @@ -524,7 +525,7 @@ static int setup_hostname(void) { char *hn; int r = 0; - hn = file_name_from_path(arg_directory); + hn = path_get_file_name(arg_directory); if (hn) { hn = strdup(hn); if (!hn) diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c index 35b71548a3..ef68e506e5 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -29,6 +29,7 @@ #include "log.h" #include "util.h" +#include "path-util.h" #include "set.h" #include "mount-setup.h" #include "exit-status.h" diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index 19f87e4bc6..9003a1228e 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -26,6 +26,7 @@ #include "util.h" #include "macro.h" +#include "path-util.h" #include "cgroup-util.h" #include "cgroup-show.h" @@ -205,7 +206,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns } if (last) { - printf("%s\342\224\234 %s\n", prefix, file_name_from_path(last)); + printf("%s\342\224\234 %s\n", prefix, path_get_file_name(last)); if (!p1) { p1 = strappend(prefix, "\342\224\202 "); @@ -230,7 +231,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads); if (last) { - printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last)); + printf("%s\342\224\224 %s\n", prefix, path_get_file_name(last)); if (!p2) { p2 = strappend(prefix, " "); diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 1f310d31f0..8d3bdce0f5 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -34,6 +34,7 @@ #include "set.h" #include "macro.h" #include "util.h" +#include "path-util.h" #include "strv.h" int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { @@ -673,7 +674,7 @@ int cg_delete(const char *controller, const char *path) { assert(controller); assert(path); - if ((r = parent_of_path(path, &parent)) < 0) + if ((r = path_get_parent(path, &parent)) < 0) return r; r = cg_migrate_recursive(controller, path, parent, false, true); diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c index 019fadcf5e..487c9a5e68 100644 --- a/src/shared/conf-files.c +++ b/src/shared/conf-files.c @@ -33,6 +33,7 @@ #include "missing.h" #include "log.h" #include "strv.h" +#include "path-util.h" #include "hashmap.h" #include "conf-files.h" @@ -69,7 +70,7 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) { goto finish; } - if (hashmap_put(h, file_name_from_path(p), p) <= 0) { + if (hashmap_put(h, path_get_file_name(p), p) <= 0) { log_debug("Skip overridden file: %s.", p); free(p); } @@ -85,7 +86,7 @@ static int base_cmp(const void *a, const void *b) { s1 = *(char * const *)a; s2 = *(char * const *)b; - return strcmp(file_name_from_path(s1), file_name_from_path(s2)); + return strcmp(path_get_file_name(s1), path_get_file_name(s2)); } int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs) { @@ -137,7 +138,7 @@ int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) { goto finish; } - if (!strv_path_canonicalize(dirs)) { + if (!path_strv_canonicalize(dirs)) { r = -ENOMEM; goto finish; } diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 30980a3ea2..65035e4c6a 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -31,6 +31,7 @@ #include "strv.h" #include "log.h" #include "utf8.h" +#include "path-util.h" int config_item_table_lookup( void *table, diff --git a/src/shared/install.c b/src/shared/install.c index a77dfc77e4..fc1bf96207 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -29,6 +29,7 @@ #include "mkdir.h" #include "hashmap.h" #include "set.h" +#include "path-util.h" #include "path-lookup.h" #include "strv.h" #include "unit-name.h" @@ -283,7 +284,7 @@ static int remove_marked_symlinks_fd( found = set_get(remove_symlinks_to, dest) || - set_get(remove_symlinks_to, file_name_from_path(dest)); + set_get(remove_symlinks_to, path_get_file_name(dest)); if (found) { @@ -468,7 +469,7 @@ static int find_symlinks_fd( if (path_is_absolute(name)) found_dest = path_equal(dest, name); else - found_dest = streq(file_name_from_path(dest), name); + found_dest = streq(path_get_file_name(dest), name); free(dest); @@ -754,7 +755,7 @@ int unit_file_link( char *path, *fn; struct stat st; - fn = file_name_from_path(*i); + fn = path_get_file_name(*i); if (!path_is_absolute(*i) || !unit_name_is_valid_no_type(fn, true)) { @@ -917,7 +918,7 @@ static int install_info_add( assert(name || path); if (!name) - name = file_name_from_path(path); + name = path_get_file_name(path); if (!unit_name_is_valid_no_type(name, true)) return -EINVAL; @@ -1915,7 +1916,7 @@ int unit_file_get_list( continue; found: - r = hashmap_put(h, file_name_from_path(f->path), f); + r = hashmap_put(h, path_get_file_name(f->path), f); if (r < 0) { free(f->path); free(f); diff --git a/src/shared/label.c b/src/shared/label.c index bd38f0ba67..2d7d42a40e 100644 --- a/src/shared/label.c +++ b/src/shared/label.c @@ -28,6 +28,7 @@ #include "label.h" #include "util.h" +#include "path-util.h" #ifdef HAVE_SELINUX #include diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index 1d95f7d1f8..41ebc7f5b3 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -28,7 +28,7 @@ #include "util.h" #include "mkdir.h" #include "strv.h" - +#include "path-util.h" #include "path-lookup.h" int user_config_home(char **config_home) { @@ -165,7 +165,7 @@ static char** user_dirs(void) { strv_free(r); r = t; - if (!strv_path_make_absolute_cwd(r)) + if (!path_strv_make_absolute_cwd(r)) goto fail; finish: @@ -191,7 +191,7 @@ int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal /* First priority is whatever has been passed to us via env * vars */ if ((e = getenv("SYSTEMD_UNIT_PATH"))) - if (!(p->unit_path = split_path_and_make_absolute(e))) + if (!(p->unit_path = path_split_and_make_absolute(e))) return -ENOMEM; if (strv_isempty(p->unit_path)) { @@ -239,11 +239,11 @@ int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal } if (p->unit_path) - if (!strv_path_canonicalize(p->unit_path)) + if (!path_strv_canonicalize(p->unit_path)) return -ENOMEM; strv_uniq(p->unit_path); - strv_path_remove_empty(p->unit_path); + path_strv_remove_empty(p->unit_path); if (!strv_isempty(p->unit_path)) { @@ -262,7 +262,7 @@ int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal /* /etc/init.d/ compatibility does not matter to users */ if ((e = getenv("SYSTEMD_SYSVINIT_PATH"))) - if (!(p->sysvinit_path = split_path_and_make_absolute(e))) + if (!(p->sysvinit_path = path_split_and_make_absolute(e))) return -ENOMEM; if (strv_isempty(p->sysvinit_path)) { @@ -275,7 +275,7 @@ int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal } if ((e = getenv("SYSTEMD_SYSVRCND_PATH"))) - if (!(p->sysvrcnd_path = split_path_and_make_absolute(e))) + if (!(p->sysvrcnd_path = path_split_and_make_absolute(e))) return -ENOMEM; if (strv_isempty(p->sysvrcnd_path)) { @@ -288,18 +288,18 @@ int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal } if (p->sysvinit_path) - if (!strv_path_canonicalize(p->sysvinit_path)) + if (!path_strv_canonicalize(p->sysvinit_path)) return -ENOMEM; if (p->sysvrcnd_path) - if (!strv_path_canonicalize(p->sysvrcnd_path)) + if (!path_strv_canonicalize(p->sysvrcnd_path)) return -ENOMEM; strv_uniq(p->sysvinit_path); strv_uniq(p->sysvrcnd_path); - strv_path_remove_empty(p->sysvinit_path); - strv_path_remove_empty(p->sysvrcnd_path); + path_strv_remove_empty(p->sysvinit_path); + path_strv_remove_empty(p->sysvrcnd_path); if (!strv_isempty(p->sysvinit_path)) { diff --git a/src/shared/path-util.c b/src/shared/path-util.c new file mode 100644 index 0000000000..ccd7667608 --- /dev/null +++ b/src/shared/path-util.c @@ -0,0 +1,371 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010-2012 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "macro.h" +#include "util.h" +#include "log.h" +#include "strv.h" +#include "path-util.h" + +bool path_is_absolute(const char *p) { + return p[0] == '/'; +} + +bool is_path(const char *p) { + return !!strchr(p, '/'); +} + +char *path_get_file_name(const char *p) { + char *r; + + assert(p); + + if ((r = strrchr(p, '/'))) + 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; + bool slash = false; + + assert(path); + assert(_r); + + if (!*path) + return -EINVAL; + + for (e = path; *e; e++) { + + if (!slash && *e == '/') { + a = b; + b = e; + slash = true; + } else if (slash && *e != '/') + slash = false; + } + + if (*(e-1) == '/') + p = a; + else + p = b; + + if (!p) + return -EINVAL; + + if (p == path) + r = strdup("/"); + else + r = strndup(path, p-path); + + if (!r) + return -ENOMEM; + + *_r = r; + return 0; +} + +char **path_split_and_make_absolute(const char *p) { + char **l; + assert(p); + + if (!(l = strv_split(p, ":"))) + return NULL; + + if (!path_strv_make_absolute_cwd(l)) { + strv_free(l); + return NULL; + } + + return l; +} + +char *path_make_absolute(const char *p, const char *prefix) { + assert(p); + + /* Makes every item in the list an absolute path by prepending + * the prefix, if specified and necessary */ + + if (path_is_absolute(p) || !prefix) + return strdup(p); + + return join(prefix, "/", p, NULL); +} + +char *path_make_absolute_cwd(const char *p) { + char *cwd, *r; + + assert(p); + + /* Similar to path_make_absolute(), but prefixes with the + * current working directory. */ + + if (path_is_absolute(p)) + return strdup(p); + + if (!(cwd = get_current_dir_name())) + return NULL; + + r = path_make_absolute(p, cwd); + free(cwd); + + return r; +} + +char **path_strv_make_absolute_cwd(char **l) { + char **s; + + /* Goes through every item in the string list and makes it + * absolute. This works in place and won't rollback any + * changes on failure. */ + + STRV_FOREACH(s, l) { + char *t; + + if (!(t = path_make_absolute_cwd(*s))) + return NULL; + + free(*s); + *s = t; + } + + return l; +} + +char **path_strv_canonicalize(char **l) { + char **s; + unsigned k = 0; + bool enomem = false; + + if (strv_isempty(l)) + return l; + + /* Goes through every item in the string list and canonicalize + * the path. This works in place and won't rollback any + * changes on failure. */ + + STRV_FOREACH(s, l) { + char *t, *u; + + t = path_make_absolute_cwd(*s); + free(*s); + + if (!t) { + enomem = true; + continue; + } + + errno = 0; + u = canonicalize_file_name(t); + free(t); + + if (!u) { + if (errno == ENOMEM || !errno) + enomem = true; + + continue; + } + + l[k++] = u; + } + + l[k] = NULL; + + if (enomem) + return NULL; + + return l; +} + +char **path_strv_remove_empty(char **l) { + char **f, **t; + + if (!l) + return NULL; + + for (f = t = l; *f; f++) { + + if (dir_is_empty(*f) > 0) { + free(*f); + continue; + } + + *(t++) = *f; + } + + *t = NULL; + return l; +} + +char *path_kill_slashes(char *path) { + char *f, *t; + bool slash = false; + + /* Removes redundant inner and trailing slashes. Modifies the + * passed string in-place. + * + * ///foo///bar/ becomes /foo/bar + */ + + for (f = path, t = path; *f; f++) { + + if (*f == '/') { + slash = true; + continue; + } + + if (slash) { + slash = false; + *(t++) = '/'; + } + + *(t++) = *f; + } + + /* Special rule, if we are talking of the root directory, a + trailing slash is good */ + + if (t == path && slash) + *(t++) = '/'; + + *t = 0; + return path; +} + +bool path_startswith(const char *path, const char *prefix) { + assert(path); + assert(prefix); + + if ((path[0] == '/') != (prefix[0] == '/')) + return false; + + for (;;) { + size_t a, b; + + path += strspn(path, "/"); + prefix += strspn(prefix, "/"); + + if (*prefix == 0) + return true; + + if (*path == 0) + return false; + + a = strcspn(path, "/"); + b = strcspn(prefix, "/"); + + if (a != b) + return false; + + if (memcmp(path, prefix, a) != 0) + return false; + + path += a; + prefix += b; + } +} + +bool path_equal(const char *a, const char *b) { + assert(a); + assert(b); + + if ((a[0] == '/') != (b[0] == '/')) + return false; + + for (;;) { + size_t j, k; + + a += strspn(a, "/"); + b += strspn(b, "/"); + + if (*a == 0 && *b == 0) + return true; + + if (*a == 0 || *b == 0) + return false; + + j = strcspn(a, "/"); + k = strcspn(b, "/"); + + if (j != k) + return false; + + if (memcmp(a, b, j) != 0) + return false; + + a += j; + b += k; + } +} + +int path_is_mount_point(const char *t, bool allow_symlink) { + struct stat a, b; + char *parent; + int r; + + if (allow_symlink) + r = stat(t, &a); + else + r = lstat(t, &a); + + if (r < 0) { + if (errno == ENOENT) + return 0; + + return -errno; + } + + r = path_get_parent(t, &parent); + if (r < 0) + return r; + + r = lstat(parent, &b); + free(parent); + + if (r < 0) + return -errno; + + return a.st_dev != b.st_dev; +} + +int path_is_read_only_fs(const char *path) { + struct statvfs st; + + assert(path); + + if (statvfs(path, &st) < 0) + return -errno; + + return !!(st.f_flag & ST_RDONLY); +} diff --git a/src/shared/path-util.h b/src/shared/path-util.h new file mode 100644 index 0000000000..181b981224 --- /dev/null +++ b/src/shared/path-util.h @@ -0,0 +1,46 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#ifndef foopathutilhfoo +#define foopathutilhfoo + +/*** + This file is part of systemd. + + Copyright 2010-2012 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include "stdbool.h" + +bool is_path(const char *p); +char **path_split_and_make_absolute(const char *p); +char *path_get_file_name(const char *p); +int path_get_parent(const char *path, char **parent); +bool path_is_absolute(const char *p); +char *path_make_absolute(const char *p, const char *prefix); +char *path_make_absolute_cwd(const char *p); +int path_parent(const char *path, char **parent); +char *path_kill_slashes(char *path); +bool path_startswith(const char *path, const char *prefix); +bool path_equal(const char *a, const char *b); + +char **path_strv_make_absolute_cwd(char **l); +char **path_strv_canonicalize(char **l); +char **path_strv_remove_empty(char **l); + +int path_is_mount_point(const char *path, bool allow_symlink); +int path_is_read_only_fs(const char *path); + +#endif diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c index 471b6dc284..7e65f8a691 100644 --- a/src/shared/socket-util.c +++ b/src/shared/socket-util.c @@ -35,6 +35,7 @@ #include "macro.h" #include "util.h" #include "mkdir.h" +#include "path-util.h" #include "socket-util.h" #include "missing.h" diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c index 566cdc51cc..e0a18d1357 100644 --- a/src/shared/unit-name.c +++ b/src/shared/unit-name.c @@ -23,6 +23,7 @@ #include #include +#include "path-util.h" #include "util.h" #include "unit-name.h" diff --git a/src/shared/util.c b/src/shared/util.c index a055ce197d..8a0b2a1b46 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -55,7 +55,6 @@ #include #include #include -#include #include "macro.h" #include "util.h" @@ -64,6 +63,7 @@ #include "log.h" #include "strv.h" #include "label.h" +#include "path-util.h" #include "exit-status.h" #include "hashmap.h" @@ -486,21 +486,6 @@ char *split_quoted(const char *c, size_t *l, char **state) { return (char*) current; } -char **split_path_and_make_absolute(const char *p) { - char **l; - assert(p); - - if (!(l = strv_split(p, ":"))) - return NULL; - - if (!strv_path_make_absolute_cwd(l)) { - strv_free(l); - return NULL; - } - - return l; -} - int get_parent_of_pid(pid_t pid, pid_t *_ppid) { int r; FILE *f; @@ -1313,187 +1298,6 @@ int readlink_and_canonicalize(const char *p, char **r) { return 0; } -int parent_of_path(const char *path, char **_r) { - const char *e, *a = NULL, *b = NULL, *p; - char *r; - bool slash = false; - - assert(path); - assert(_r); - - if (!*path) - return -EINVAL; - - for (e = path; *e; e++) { - - if (!slash && *e == '/') { - a = b; - b = e; - slash = true; - } else if (slash && *e != '/') - slash = false; - } - - if (*(e-1) == '/') - p = a; - else - p = b; - - if (!p) - return -EINVAL; - - if (p == path) - r = strdup("/"); - else - r = strndup(path, p-path); - - if (!r) - return -ENOMEM; - - *_r = r; - return 0; -} - - -char *file_name_from_path(const char *p) { - char *r; - - assert(p); - - if ((r = strrchr(p, '/'))) - return r + 1; - - return (char*) p; -} - -bool path_is_absolute(const char *p) { - assert(p); - - return p[0] == '/'; -} - -bool is_path(const char *p) { - - return !!strchr(p, '/'); -} - -char *path_make_absolute(const char *p, const char *prefix) { - assert(p); - - /* Makes every item in the list an absolute path by prepending - * the prefix, if specified and necessary */ - - if (path_is_absolute(p) || !prefix) - return strdup(p); - - return join(prefix, "/", p, NULL); -} - -char *path_make_absolute_cwd(const char *p) { - char *cwd, *r; - - assert(p); - - /* Similar to path_make_absolute(), but prefixes with the - * current working directory. */ - - if (path_is_absolute(p)) - return strdup(p); - - if (!(cwd = get_current_dir_name())) - return NULL; - - r = path_make_absolute(p, cwd); - free(cwd); - - return r; -} - -char **strv_path_make_absolute_cwd(char **l) { - char **s; - - /* Goes through every item in the string list and makes it - * absolute. This works in place and won't rollback any - * changes on failure. */ - - STRV_FOREACH(s, l) { - char *t; - - if (!(t = path_make_absolute_cwd(*s))) - return NULL; - - free(*s); - *s = t; - } - - return l; -} - -char **strv_path_canonicalize(char **l) { - char **s; - unsigned k = 0; - bool enomem = false; - - if (strv_isempty(l)) - return l; - - /* Goes through every item in the string list and canonicalize - * the path. This works in place and won't rollback any - * changes on failure. */ - - STRV_FOREACH(s, l) { - char *t, *u; - - t = path_make_absolute_cwd(*s); - free(*s); - - if (!t) { - enomem = true; - continue; - } - - errno = 0; - u = canonicalize_file_name(t); - free(t); - - if (!u) { - if (errno == ENOMEM || !errno) - enomem = true; - - continue; - } - - l[k++] = u; - } - - l[k] = NULL; - - if (enomem) - return NULL; - - return l; -} - -char **strv_path_remove_empty(char **l) { - char **f, **t; - - if (!l) - return NULL; - - for (f = t = l; *f; f++) { - - if (dir_is_empty(*f) > 0) { - free(*f); - continue; - } - - *(t++) = *f; - } - - *t = NULL; - return l; -} - int reset_all_signal_handlers(void) { int sig; @@ -1972,107 +1776,6 @@ char *bus_path_unescape(const char *f) { return r; } -char *path_kill_slashes(char *path) { - char *f, *t; - bool slash = false; - - /* Removes redundant inner and trailing slashes. Modifies the - * passed string in-place. - * - * ///foo///bar/ becomes /foo/bar - */ - - for (f = path, t = path; *f; f++) { - - if (*f == '/') { - slash = true; - continue; - } - - if (slash) { - slash = false; - *(t++) = '/'; - } - - *(t++) = *f; - } - - /* Special rule, if we are talking of the root directory, a - trailing slash is good */ - - if (t == path && slash) - *(t++) = '/'; - - *t = 0; - return path; -} - -bool path_startswith(const char *path, const char *prefix) { - assert(path); - assert(prefix); - - if ((path[0] == '/') != (prefix[0] == '/')) - return false; - - for (;;) { - size_t a, b; - - path += strspn(path, "/"); - prefix += strspn(prefix, "/"); - - if (*prefix == 0) - return true; - - if (*path == 0) - return false; - - a = strcspn(path, "/"); - b = strcspn(prefix, "/"); - - if (a != b) - return false; - - if (memcmp(path, prefix, a) != 0) - return false; - - path += a; - prefix += b; - } -} - -bool path_equal(const char *a, const char *b) { - assert(a); - assert(b); - - if ((a[0] == '/') != (b[0] == '/')) - return false; - - for (;;) { - size_t j, k; - - a += strspn(a, "/"); - b += strspn(b, "/"); - - if (*a == 0 && *b == 0) - return true; - - if (*a == 0 || *b == 0) - return false; - - j = strcspn(a, "/"); - k = strcspn(b, "/"); - - if (j != k) - return false; - - if (memcmp(a, b, j) != 0) - return false; - - a += j; - b += k; - } -} - char *ascii_strlower(char *t) { char *p; @@ -2986,36 +2689,6 @@ ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { return n; } -int path_is_mount_point(const char *t, bool allow_symlink) { - struct stat a, b; - char *parent; - int r; - - if (allow_symlink) - r = stat(t, &a); - else - r = lstat(t, &a); - - if (r < 0) { - if (errno == ENOENT) - return 0; - - return -errno; - } - - r = parent_of_path(t, &parent); - if (r < 0) - return r; - - r = lstat(parent, &b); - free(parent); - - if (r < 0) - return -errno; - - return a.st_dev != b.st_dev; -} - int parse_usec(const char *t, usec_t *usec) { static const struct { const char *suffix; @@ -4746,7 +4419,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) { if (!t) return -ENOMEM; - fn = file_name_from_path(path); + fn = path_get_file_name(path); k = fn-path; memcpy(t, path, k); t[k] = '.'; @@ -4928,8 +4601,8 @@ int symlink_or_copy(const char *from, const char *to) { assert(from); assert(to); - if (parent_of_path(from, &pf) < 0 || - parent_of_path(to, &pt) < 0) { + if (path_get_parent(from, &pf) < 0 || + path_get_parent(to, &pt) < 0) { r = -ENOMEM; goto finish; } @@ -4976,7 +4649,7 @@ int symlink_or_copy_atomic(const char *from, const char *to) { if (!t) return -ENOMEM; - fn = file_name_from_path(to); + fn = path_get_file_name(to); k = fn-to; memcpy(t, to, k); t[k] = '.'; @@ -5218,7 +4891,7 @@ int in_search_path(const char *path, char **search) { char **i, *parent; int r; - r = parent_of_path(path, &parent); + r = path_get_parent(path, &parent); if (r < 0) return r; @@ -5857,17 +5530,6 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) { return 0; } -int path_is_read_only_fs(const char *path) { - struct statvfs st; - - assert(path); - - if (statvfs(path, &st) < 0) - return -errno; - - return !!(st.f_flag & ST_RDONLY); -} - int getenv_for_pid(pid_t pid, const char *field, char **_value) { char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL; int r; diff --git a/src/shared/util.h b/src/shared/util.h index cca6118363..59a69a898a 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -122,10 +122,6 @@ static inline const char *strna(const char *s) { return s ? s : "n/a"; } -static inline bool is_path_absolute(const char *p) { - return *p == '/'; -} - static inline bool isempty(const char *p) { return !p || !p[0]; } @@ -205,8 +201,6 @@ char *split_quoted(const char *c, size_t *l, char **state); #define FOREACH_WORD_QUOTED(word, length, s, state) \ for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state))) -char **split_path_and_make_absolute(const char *p); - pid_t get_parent_of_pid(pid_t pid, pid_t *ppid); int get_starttime_of_pid(pid_t pid, unsigned long long *st); @@ -229,17 +223,6 @@ int readlink_malloc(const char *p, char **r); int readlink_and_make_absolute(const char *p, char **r); int readlink_and_canonicalize(const char *p, char **r); -char *file_name_from_path(const char *p); -bool is_path(const char *p); - -bool path_is_absolute(const char *p); -char *path_make_absolute(const char *p, const char *prefix); -char *path_make_absolute_cwd(const char *p); - -char **strv_path_make_absolute_cwd(char **l); -char **strv_path_canonicalize(char **l); -char **strv_path_remove_empty(char **l); - int reset_all_signal_handlers(void); char *strstrip(char *s); @@ -248,8 +231,6 @@ char *truncate_nl(char *s); char *file_in_same_dir(const char *path, const char *filename); -int parent_of_path(const char *path, char **parent); - int rmdir_parents(const char *path, const char *stop); int get_process_comm(pid_t pid, char **name); @@ -273,11 +254,6 @@ char *xescape(const char *s, const char *bad); char *bus_path_escape(const char *s); char *bus_path_unescape(const char *s); -char *path_kill_slashes(char *path); - -bool path_startswith(const char *path, const char *prefix); -bool path_equal(const char *a, const char *b); - char *ascii_strlower(char *path); bool dirent_is_file(const struct dirent *de); @@ -351,9 +327,6 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path); ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll); ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll); -int path_is_mount_point(const char *path, bool allow_symlink); -int path_is_read_only_fs(const char *path); - bool is_device_path(const char *path); int dir_is_empty(const char *path); diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 6bba325d3e..046fb584fb 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -29,6 +29,7 @@ #include #include "macro.h" +#include "path-util.h" #include "utmp-wtmp.h" int utmp_get_runlevel(int *runlevel, int *previous) { @@ -224,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, file_name_from_path(line), sizeof(store.ut_line)); + strncpy(store.ut_line, path_get_file_name(line), sizeof(store.ut_line)); return write_entry_both(&store); } diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 575095f812..72a74f9e44 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -31,6 +31,7 @@ #include "strv.h" #include "util.h" #include "strv.h" +#include "path-util.h" #include "conf-files.h" #define PROC_SYS_PREFIX "/proc/sys/" diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index c08d80912a..acede4e765 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -45,6 +45,7 @@ #include "utmp-wtmp.h" #include "special.h" #include "initreq.h" +#include "path-util.h" #include "strv.h" #include "dbus-common.h" #include "cgroup-show.h" @@ -61,6 +62,7 @@ #include "spawn-polkit-agent.h" #include "install.h" #include "logs-show.h" +#include "path-util.h" static const char *arg_type = NULL; static char **arg_property = NULL; @@ -560,7 +562,7 @@ static int compare_unit_file_list(const void *a, const void *b) { return r; } - return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path)); + return strcasecmp(path_get_file_name(u->path), path_get_file_name(v->path)); } static bool output_show_unit_file(const UnitFileList *u) { @@ -579,7 +581,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(file_name_from_path(u->path))); + max_id_len = MAX(max_id_len, strlen(path_get_file_name(u->path))); state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); } @@ -616,7 +618,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) { } else on = off = ""; - id = file_name_from_path(u->path); + id = path_get_file_name(u->path); e = arg_full ? NULL : ellipsize(id, id_cols, 33); @@ -3721,7 +3723,7 @@ static int enable_sysv_units(char **args) { if (!isempty(arg_root)) argv[c++] = q = strappend("--root=", arg_root); - argv[c++] = file_name_from_path(p); + argv[c++] = path_get_file_name(p); argv[c++] = streq(verb, "enable") ? "on" : streq(verb, "disable") ? "off" : "--level=5"; diff --git a/src/test/test-cgroup.c b/src/test/test-cgroup.c index e742632032..6d64a4e47f 100644 --- a/src/test/test-cgroup.c +++ b/src/test/test-cgroup.c @@ -23,6 +23,7 @@ #include #include "cgroup-util.h" +#include "path-util.h" #include "util.h" #include "log.h" diff --git a/src/test/test-install.c b/src/test/test-install.c index 709974f9e5..2c1b9efcb8 100644 --- a/src/test/test-install.c +++ b/src/test/test-install.c @@ -25,6 +25,7 @@ #include #include "util.h" +#include "path-util.h" #include "install.h" static void dump_changes(UnitFileChange *c, unsigned n) { @@ -57,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, file_name_from_path(p->path)); + s = unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(p->path)); assert_se(p->state == s); @@ -175,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, file_name_from_path(files2[0])) == UNIT_FILE_ENABLED); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_ENABLED); log_error("disable files2"); changes = NULL; @@ -187,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, file_name_from_path(files2[0])) == _UNIT_FILE_STATE_INVALID); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID); log_error("link files2"); changes = NULL; @@ -199,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, file_name_from_path(files2[0])) == UNIT_FILE_LINKED); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_LINKED); log_error("disable files2"); changes = NULL; @@ -211,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, file_name_from_path(files2[0])) == _UNIT_FILE_STATE_INVALID); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID); log_error("link files2"); changes = NULL; @@ -223,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, file_name_from_path(files2[0])) == UNIT_FILE_LINKED); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_LINKED); log_error("reenable files2"); changes = NULL; @@ -235,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, file_name_from_path(files2[0])) == UNIT_FILE_ENABLED); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == UNIT_FILE_ENABLED); log_error("disable files2"); changes = NULL; @@ -247,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, file_name_from_path(files2[0])) == _UNIT_FILE_STATE_INVALID); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files2[0])) == _UNIT_FILE_STATE_INVALID); log_error("preset files"); changes = NULL; n_changes = 0; @@ -258,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, file_name_from_path(files[0])) == UNIT_FILE_ENABLED); + assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, NULL, path_get_file_name(files[0])) == UNIT_FILE_ENABLED); return 0; } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 16666ce5a6..235617853b 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -42,6 +42,7 @@ #include "log.h" #include "util.h" #include "mkdir.h" +#include "path-util.h" #include "strv.h" #include "label.h" #include "set.h" diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 9fbd7f5fb2..de843b437e 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -34,6 +34,7 @@ #include "util.h" #include "mkdir.h" +#include "path-util.h" #include "conf-parser.h" #include "utmp-wtmp.h" #include "socket-util.h" diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 45a611474d..930c4770fe 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -31,6 +31,7 @@ #include #include "udev.h" +#include "path-util.h" #include "conf-files.h" #define PREALLOC_TOKEN 2048 @@ -1764,7 +1765,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) log_error("failed to build config directory array"); return NULL; } - if (!strv_path_canonicalize(rules->dirs)) { + if (!path_strv_canonicalize(rules->dirs)) { log_error("failed to canonicalize config directories\n"); return NULL; } -- cgit v1.2.3-54-g00ecf From 669241a076108e0483d7d8475beaa506106d077e Mon Sep 17 00:00:00 2001 From: Shawn Landden Date: Tue, 24 Jul 2012 21:12:43 -0700 Subject: use "Out of memory." consistantly (or with "\n") glibc/glib both use "out of memory" consistantly so maybe we should consider that instead of this. Eliminates one string out of a number of binaries. Also fixes extra newline in udev/scsi_id --- src/cgtop/cgtop.c | 2 +- src/core/cgroup.c | 2 +- src/core/dbus.c | 16 ++++++------ src/core/manager.c | 6 ++--- src/cryptsetup/cryptsetup-generator.c | 6 ++--- src/cryptsetup/cryptsetup.c | 6 ++--- src/fsck/fsck.c | 2 +- src/fstab-generator/fstab-generator.c | 30 +++++++++++----------- src/getty-generator/getty-generator.c | 6 ++--- src/hostname/hostnamed.c | 2 +- src/journal/coredump.c | 2 +- src/journal/journalctl.c | 2 +- src/journal/journald.c | 10 ++++---- src/locale/localed.c | 6 ++--- src/login/logind-button.c | 2 +- src/login/logind-session.c | 8 +++--- src/login/logind-user.c | 4 +-- src/login/logind.c | 4 +-- src/login/multi-seat-x.c | 2 +- src/modules-load/modules-load.c | 4 +-- src/nspawn/nspawn.c | 22 ++++++++-------- src/rc-local-generator/rc-local-generator.c | 2 +- src/readahead/readahead-collect.c | 6 ++--- src/readahead/readahead-replay.c | 2 +- src/shared/ask-password-api.c | 2 +- src/shared/dbus-common.c | 2 +- src/shared/logs-show.c | 2 +- src/shared/util.c | 6 ++--- src/shutdownd/shutdownd.c | 2 +- src/sysctl/sysctl.c | 4 +-- src/systemctl/systemctl.c | 20 +++++++-------- src/timedate/timedated.c | 10 ++++---- src/tmpfiles/tmpfiles.c | 10 ++++---- .../tty-ask-password-agent.c | 4 +-- src/udev/collect/collect.c | 2 +- src/udev/scsi_id/scsi_id.c | 4 +-- 36 files changed, 111 insertions(+), 111 deletions(-) (limited to 'src/sysctl/sysctl.c') diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 5557094a4f..c3824c7979 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -608,7 +608,7 @@ int main(int argc, char *argv[]) { a = hashmap_new(string_hash_func, string_compare_func); b = hashmap_new(string_hash_func, string_compare_func); if (!a || !b) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/core/cgroup.c b/src/core/cgroup.c index aaea96b820..1322f63eee 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -330,7 +330,7 @@ int manager_setup_cgroup(Manager *m) { /* We need a new root cgroup */ m->cgroup_hierarchy = NULL; if (asprintf(&m->cgroup_hierarchy, "%s%s", streq(current, "/") ? "" : current, suffix) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/core/dbus.c b/src/core/dbus.c index 1bc83a2c2a..0c13517143 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -724,12 +724,12 @@ static int bus_setup_loop(Manager *m, DBusConnection *bus) { if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) || !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); return -ENOMEM; } if (set_put(m->bus_connections_for_dispatch, bus) < 0) { - log_error("Not enough memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -764,7 +764,7 @@ static void bus_new_connection( !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) { - log_error("Not enough memory."); + log_error("Out of memory."); return; } @@ -777,7 +777,7 @@ static int init_registered_system_bus(Manager *m) { char *id; if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -815,7 +815,7 @@ static int init_registered_api_bus(Manager *m) { !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -1090,7 +1090,7 @@ static int bus_init_private(Manager *m) { return 0; if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0) { - log_error("Not enough memory"); + log_error("Out of memory."); r = -ENOMEM; goto fail; } @@ -1110,7 +1110,7 @@ static int bus_init_private(Manager *m) { if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) || !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) || !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); r = -ENOMEM; goto fail; } @@ -1158,7 +1158,7 @@ int bus_init(Manager *m, bool try_bus_connect) { return 0; oom: - log_error("Not enough memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/core/manager.c b/src/core/manager.c index 48305e373a..42a9490242 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -1625,7 +1625,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) { } if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } @@ -2074,7 +2074,7 @@ static int create_generator_dir(Manager *m, char **generator, const char *name) p = strappend("/run/systemd/", name); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -2087,7 +2087,7 @@ static int create_generator_dir(Manager *m, char **generator, const char *name) } else { p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index d0984242c8..fb6b4d25c0 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -303,7 +303,7 @@ static int parse_proc_cmdline(void) { t = strv_append(arg_proc_cmdline_disks, word + 10); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -317,7 +317,7 @@ static int parse_proc_cmdline(void) { t = strv_append(arg_proc_cmdline_disks, word + 13); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -380,7 +380,7 @@ int main(int argc, char *argv[]) { device = strappend("UUID=", *i); if (!name || !device) { - log_error("Out of memory"); + log_error("Out of memory."); r = EXIT_FAILURE; free(name); free(device); diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index f570724b9d..9d4e77364d 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -343,7 +343,7 @@ int main(int argc, char *argv[]) { l = strcspn(opt_cipher, "-"); if (!(truncated_cipher = strndup(opt_cipher, l))) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } @@ -365,7 +365,7 @@ int main(int argc, char *argv[]) { char **p; if (asprintf(&text, "Please enter passphrase for disk %s!", name) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } @@ -383,7 +383,7 @@ int main(int argc, char *argv[]) { assert(strv_length(passwords) == 1); if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index 833f3f74a6..036d3c5b41 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -297,7 +297,7 @@ int main(int argc, char *argv[]) { } if (!(udev = udev_new())) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 3a59b85d66..89a4d13504 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -98,14 +98,14 @@ static int add_swap(const char *what, struct mntent *me) { name = unit_name_from_path(what, ".swap"); if (!name) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } unit = strjoin(arg_dest, "/", name, NULL); if (!unit) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -148,7 +148,7 @@ static int add_swap(const char *what, struct mntent *me) { if (!noauto) { lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL); if (!lnk) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -162,7 +162,7 @@ static int add_swap(const char *what, struct mntent *me) { r = device_name(what, &device); if (r < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -171,7 +171,7 @@ static int add_swap(const char *what, struct mntent *me) { free(lnk); lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL); if (!lnk) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -256,14 +256,14 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { name = unit_name_from_path(where, ".mount"); if (!name) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } unit = strjoin(arg_dest, "/", name, NULL); if (!unit) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -323,7 +323,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { if (!noauto) { lnk = strjoin(arg_dest, "/", post, nofail || automount ? ".wants/" : ".requires/", name, NULL); if (!lnk) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -340,7 +340,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { r = device_name(what, &device); if (r < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -349,7 +349,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { free(lnk); lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL); if (!lnk) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -367,14 +367,14 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { if (automount && !path_equal(where, "/")) { automount_name = unit_name_from_path(where, ".automount"); if (!name) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } automount_unit = strjoin(arg_dest, "/", automount_name, NULL); if (!automount_unit) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -410,7 +410,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { free(lnk); lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL); if (!lnk) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -459,14 +459,14 @@ static int parse_fstab(void) { what = fstab_node_to_udev_node(me->mnt_fsname); if (!what) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } where = strdup(me->mnt_dir); if (!where) { - log_error("Out of memory"); + log_error("Out of memory."); free(what); r = -ENOMEM; goto finish; diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c index b2e3eb6393..9e46a47a9a 100644 --- a/src/getty-generator/getty-generator.c +++ b/src/getty-generator/getty-generator.c @@ -42,7 +42,7 @@ static int add_symlink(const char *fservice, const char *tservice) { to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL); if (!from || !to) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -78,7 +78,7 @@ static int add_serial_getty(const char *tty) { n = unit_name_replace_instance("serial-getty@.service", tty); if (!n) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -160,7 +160,7 @@ int main(int argc, char *argv[]) { int k; if (asprintf(&p, "/sys/class/tty/%s", j) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = EXIT_FAILURE; goto finish; } diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 67c56f3313..7dab5f40df 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -532,7 +532,7 @@ static int connect_bus(DBusConnection **_bus) { if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL) || !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); r = -ENOMEM; goto fail; } diff --git a/src/journal/coredump.c b/src/journal/coredump.c index fcd0d1e625..cfd3a910d9 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -239,7 +239,7 @@ int main(int argc, char* argv[]) { p = malloc(9 + COREDUMP_MAX); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index e633dd34d7..a9cf9cd957 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -256,7 +256,7 @@ static int add_matches(sd_journal *j, char **args) { t = strappend("_EXE=", path); if (!t) { free(p); - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/journal/journald.c b/src/journal/journald.c index 36f9abe47e..ae1fbc4bd4 100644 --- a/src/journal/journald.c +++ b/src/journal/journald.c @@ -1270,7 +1270,7 @@ static void process_native_message( u = MAX((n+N_IOVEC_META_FIELDS+1) * 2U, 4U); c = realloc(iovec, u * sizeof(struct iovec)); if (!c) { - log_error("Out of memory"); + log_error("Out of memory."); break; } @@ -1357,7 +1357,7 @@ static void process_native_message( k = malloc((e - p) + 1 + l); if (!k) { - log_error("Out of memory"); + log_error("Out of memory."); break; } @@ -1450,7 +1450,7 @@ static void process_native_file( p = malloc(st.st_size); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return; } @@ -1543,7 +1543,7 @@ static int stdout_stream_line(StdoutStream *s, char *p) { else { s->identifier = strdup(p); if (!s->identifier) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } } @@ -1558,7 +1558,7 @@ static int stdout_stream_line(StdoutStream *s, char *p) { else { s->unit_id = strdup(p); if (!s->unit_id) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } } diff --git a/src/locale/localed.c b/src/locale/localed.c index 56fb339e19..b8007d72fd 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -425,7 +425,7 @@ static void push_data(DBusConnection *bus) { l_set = new0(char*, _PROP_MAX); l_unset = new0(char*, _PROP_MAX); if (!l_set || !l_unset) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } @@ -438,7 +438,7 @@ static void push_data(DBusConnection *bus) { char *s; if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); goto finish; } @@ -1344,7 +1344,7 @@ static int connect_bus(DBusConnection **_bus) { if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) || !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); r = -ENOMEM; goto fail; } diff --git a/src/login/logind-button.c b/src/login/logind-button.c index 8b59c2b0c3..62e0c3dbba 100644 --- a/src/login/logind-button.c +++ b/src/login/logind-button.c @@ -108,7 +108,7 @@ int button_open(Button *b) { p = strappend("/dev/input/", b->name); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 819596ddbe..a43ecad2b1 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -375,7 +375,7 @@ static int session_link_x11_socket(Session *s) { k = strspn(s->display+1, "0123456789"); f = new(char, sizeof("/tmp/.X11-unix/X") + k); if (!f) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -395,7 +395,7 @@ static int session_link_x11_socket(Session *s) { t = strappend(s->user->runtime_path, "/X11-display"); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); free(f); return -ENOMEM; } @@ -469,7 +469,7 @@ static int session_create_cgroup(Session *s) { if (!s->cgroup_path) { if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } } else @@ -670,7 +670,7 @@ static int session_unlink_x11_socket(Session *s) { t = strappend(s->user->runtime_path, "/X11-display"); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/login/logind-user.c b/src/login/logind-user.c index 0a3f22ce99..fca68159a7 100644 --- a/src/login/logind-user.c +++ b/src/login/logind-user.c @@ -260,7 +260,7 @@ static int user_mkdir_runtime_path(User *u) { if (!u->runtime_path) { if (asprintf(&p, "/run/user/%lu", (unsigned long) u->uid) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } } else @@ -287,7 +287,7 @@ static int user_create_cgroup(User *u) { if (!u->cgroup_path) { if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } } else diff --git a/src/login/logind.c b/src/login/logind.c index 0775583147..1cfb7fa9ac 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -1176,7 +1176,7 @@ static int manager_connect_bus(Manager *m) { !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) || !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/user", &bus_user_vtable, m) || !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); r = -ENOMEM; goto fail; } @@ -1611,7 +1611,7 @@ int main(int argc, char *argv[]) { m = manager_new(); if (!m) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/login/multi-seat-x.c b/src/login/multi-seat-x.c index 92014f5316..a1fab86f6a 100644 --- a/src/login/multi-seat-x.c +++ b/src/login/multi-seat-x.c @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { path = strappend("/run/systemd/multi-session-x/", seat); if (!path) { - log_error("Out of memory"); + log_error("Out of memory."); goto fail; } diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index d6bc16d340..e73ba7f241 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -50,14 +50,14 @@ static int add_modules(const char *p) { k = strv_split(p, ","); if (!k) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } t = strv_merge(arg_proc_cmdline_modules, k); strv_free(k); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 1d7511e2ab..355a103edf 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -289,7 +289,7 @@ static int mount_all(const char *dest) { int t; if (asprintf(&where, "%s/%s", dest, mount_table[k].where) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); if (r == 0) r = -ENOMEM; @@ -336,7 +336,7 @@ static int setup_timezone(const char *dest) { /* Fix the timezone, if possible */ if (asprintf(&where, "%s/etc/localtime", dest) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -346,7 +346,7 @@ static int setup_timezone(const char *dest) { free(where); if (asprintf(&where, "%s/etc/timezone", dest) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -368,7 +368,7 @@ static int setup_resolv_conf(const char *dest) { /* Fix resolv.conf, if possible */ if (asprintf(&where, "%s/etc/resolv.conf", dest) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -480,7 +480,7 @@ static int setup_dev_console(const char *dest, const char *console) { } if (asprintf(&to, "%s/dev/console", dest) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -535,13 +535,13 @@ static int setup_kmsg(const char *dest, int kmsg_socket) { * avoid any problems with containers deadlocking due to this * we simply make /dev/kmsg unavailable to the container. */ if (asprintf(&from, "%s/dev/kmsg", dest) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } if (asprintf(&to, "%s/proc/kmsg", dest) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -639,7 +639,7 @@ static int setup_journal(const char *directory) { p = strappend(directory, "/etc/machine-id"); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -670,7 +670,7 @@ static int setup_journal(const char *directory) { p = strappend("/var/log/journal/", l); q = strjoin(directory, "/var/log/journal/", l, NULL); if (!p || !q) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -1296,13 +1296,13 @@ int main(int argc, char *argv[]) { if ((asprintf((char**)(envp + 3), "HOME=%s", home ? home: "/root") < 0) || (asprintf((char**)(envp + 4), "USER=%s", arg_user ? arg_user : "root") < 0) || (asprintf((char**)(envp + 5), "LOGNAME=%s", arg_user ? arg_user : "root") < 0)) { - log_error("Out of memory"); + log_error("Out of memory."); goto child_fail; } if (arg_uuid) { if (asprintf((char**)(envp + 6), "container_uuid=%s", arg_uuid) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); goto child_fail; } } diff --git a/src/rc-local-generator/rc-local-generator.c b/src/rc-local-generator/rc-local-generator.c index f41a6bf265..9fafa29f22 100644 --- a/src/rc-local-generator/rc-local-generator.c +++ b/src/rc-local-generator/rc-local-generator.c @@ -48,7 +48,7 @@ static int add_symlink(const char *service, const char *where) { asprintf(&to, "%s/%s.wants/%s", arg_dest, where, service); if (!from || !to) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c index 8dacf6af5c..45d489a60e 100644 --- a/src/readahead/readahead-collect.c +++ b/src/readahead/readahead-collect.c @@ -242,7 +242,7 @@ static int collect(const char *root) { assert(root); if (asprintf(&pack_fn, "%s/.readahead", root) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -477,7 +477,7 @@ done: log_debug("On btrfs: %s", yes_no(on_btrfs)); if (asprintf(&pack_fn_new, "%s/.readahead.new", root) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -510,7 +510,7 @@ done: n = hashmap_size(files); if (!(ordered = new(struct item, n))) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/readahead/readahead-replay.c b/src/readahead/readahead-replay.c index 7bd079aae5..f90821e831 100644 --- a/src/readahead/readahead-replay.c +++ b/src/readahead/readahead-replay.c @@ -150,7 +150,7 @@ static int replay(const char *root) { block_bump_request_nr(root); if (asprintf(&pack_fn, "%s/.readahead", root) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 4333bfb564..005f40fef7 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -282,7 +282,7 @@ static int create_socket(char **name) { if (!(c = strdup(sa.un.sun_path))) { r = -ENOMEM; - log_error("Out of memory"); + log_error("Out of memory."); goto fail; } diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c index 7d57680cfc..ab1dc4d7e4 100644 --- a/src/shared/dbus-common.c +++ b/src/shared/dbus-common.c @@ -1161,7 +1161,7 @@ void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) { return; oom: - log_error("Out of memory"); + log_error("Out of memory."); if (pending) { dbus_pending_call_cancel(pending); diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index a68cd3de57..f90f5a1f0d 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -53,7 +53,7 @@ static int parse_field(const void *data, size_t length, const char *field, char memcpy(buf, (const char*) data + fl, nl); ((char*)buf)[nl] = 0; if (!buf) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/shared/util.c b/src/shared/util.c index 43ec62eac6..2e7ae63db2 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -909,7 +909,7 @@ int load_env_file( continue; if (!(u = normalize_env_assignment(p))) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -918,7 +918,7 @@ int load_env_file( free(u); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -4278,7 +4278,7 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) { continue; if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); continue; } diff --git a/src/shutdownd/shutdownd.c b/src/shutdownd/shutdownd.c index 6eb8ed9bf8..9dd1336133 100644 --- a/src/shutdownd/shutdownd.c +++ b/src/shutdownd/shutdownd.c @@ -213,7 +213,7 @@ static int update_schedule_file(struct sd_shutdown_command *c) { t = cescape(c->wall_message); if (!t) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 72a74f9e44..b463c7fac7 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -46,7 +46,7 @@ static int apply_sysctl(const char *property, const char *value) { p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -192,7 +192,7 @@ static int parse_argv(int argc, char *argv[]) { l = strv_append(arg_prefixes, optarg); if (!l) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index a13fc1a8f0..d493733761 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -658,7 +658,7 @@ static int list_unit_files(DBusConnection *bus, char **args) { h = hashmap_new(string_hash_func, string_compare_func); if (!h) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -673,7 +673,7 @@ static int list_unit_files(DBusConnection *bus, char **args) { units = new(UnitFileList, n_units); if (!units) { unit_file_list_free(h); - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -2637,7 +2637,7 @@ static void show_unit_help(UnitStatusInfo *i) { section = strndup(e + 1, *p + k - e - 2); if (!section) { free(page); - log_error("Out of memory"); + log_error("Out of memory."); return; } @@ -3283,7 +3283,7 @@ static int show(DBusConnection *bus, char **args) { p = unit_dbus_path_from_name(n ? n : *name); free(n); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -3299,7 +3299,7 @@ static int show(DBusConnection *bus, char **args) { char *p; if (asprintf(&p, "/org/freedesktop/systemd1/job/%u", id) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -3974,7 +3974,7 @@ static int enable_sysv_units(char **args) { asprintf(&p, "%s/%s", *k, name); if (!p) { - log_error("No memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -3995,7 +3995,7 @@ static int enable_sysv_units(char **args) { else asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name); if (!p) { - log_error("No memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -4024,7 +4024,7 @@ static int enable_sysv_units(char **args) { l = strv_join((char**)argv, " "); if (!l) { - log_error("No memory."); + log_error("Out of memory."); free(q); free(p); r = -ENOMEM; @@ -4184,7 +4184,7 @@ static int enable_unit(DBusConnection *bus, char **args) { "org.freedesktop.systemd1.Manager", method); if (!m) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -4339,7 +4339,7 @@ static int unit_is_enabled(DBusConnection *bus, char **args) { "org.freedesktop.systemd1.Manager", "GetUnitFileState"); if (!m) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 237eb24894..3fef9e8844 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -153,7 +153,7 @@ static void verify_timezone(void) { p = strappend("/usr/share/zoneinfo/", tz.zone); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return; } @@ -220,7 +220,7 @@ static int write_data_timezone(void) { p = strappend("/usr/share/zoneinfo/", tz.zone); if (!p) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -341,7 +341,7 @@ static char** get_ntp_services(void) { q = strv_append(r, l); if (!q) { - log_error("Out of memory"); + log_error("Out of memory."); break; } @@ -379,7 +379,7 @@ static int read_ntp(DBusConnection *bus) { "org.freedesktop.systemd1.Manager", "GetUnitFileState"); if (!m) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -943,7 +943,7 @@ static int connect_bus(DBusConnection **_bus) { if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) || !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { - log_error("Not enough memory"); + log_error("Out of memory."); r = -ENOMEM; goto fail; } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 45125b7b94..f8d89218c9 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -260,7 +260,7 @@ static int dir_cleanup( sub_path = NULL; if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -970,7 +970,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { i = new0(Item, 1); if (!i) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } @@ -999,7 +999,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) { i->argument = unquote(buffer+n, "\""); if (!i->argument) { - log_error("Out of memory"); + log_error("Out of memory."); return -ENOMEM; } } @@ -1302,7 +1302,7 @@ static char *resolve_fragment(const char *fragment, const char **search_paths) { STRV_FOREACH(p, search_paths) { resolved_path = strjoin(*p, "/", fragment, NULL); if (resolved_path == NULL) { - log_error("Out of memory"); + log_error("Out of memory."); return NULL; } @@ -1337,7 +1337,7 @@ int main(int argc, char *argv[]) { globs = hashmap_new(string_hash_func, string_compare_func); if (!items || !globs) { - log_error("Out of memory"); + log_error("Out of memory."); r = EXIT_FAILURE; goto finish; } diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 2ab3401e6f..403e8cd3c6 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -315,7 +315,7 @@ static int parse_password(const char *filename, char **wall) { *wall ? "\r\n\r\n" : "", message, pid) < 0) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } @@ -533,7 +533,7 @@ static int show_passwords(void) { continue; if (!(p = strappend("/run/systemd/ask-password/", de->d_name))) { - log_error("Out of memory"); + log_error("Out of memory."); r = -ENOMEM; goto finish; } diff --git a/src/udev/collect/collect.c b/src/udev/collect/collect.c index 80f464aff0..4162c436b0 100644 --- a/src/udev/collect/collect.c +++ b/src/udev/collect/collect.c @@ -141,7 +141,7 @@ static int checkout(int fd) len = bufsize >> 1; buf = calloc(1,bufsize + 1); if (!buf) { - fprintf(stderr, "Out of memory\n"); + fprintf(stderr, "Out of memory.\n"); return -1; } memset(buf, ' ', bufsize); diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c index 6fc41e94fb..0bb1bc495a 100644 --- a/src/udev/scsi_id/scsi_id.c +++ b/src/udev/scsi_id/scsi_id.c @@ -198,7 +198,7 @@ static int get_file_options(struct udev *udev, buffer = malloc(MAX_BUFFER_LEN); if (!buffer) { fclose(fd); - log_error("can't allocate memory\n"); + log_error("Out of memory."); return -1; } @@ -294,7 +294,7 @@ static int get_file_options(struct udev *udev, c = argc_count(buffer) + 2; *newargv = calloc(c, sizeof(**newargv)); if (!*newargv) { - log_error("can't allocate memory\n"); + log_error("Out of memory."); retval = -1; } else { *argc = c; -- cgit v1.2.3-54-g00ecf From 0d0f0c50d3a1d90f03972a6abb82e6413daaa583 Mon Sep 17 00:00:00 2001 From: Shawn Landden Date: Wed, 25 Jul 2012 14:55:59 -0700 Subject: log.h: new log_oom() -> int -ENOMEM, use it also a number of minor fixups and bug fixes: spelling, oom errors that didn't print errors, not properly forwarding error codes, few more consistency issues, et cetera --- src/cgtop/cgtop.c | 3 +- src/core/cgroup.c | 3 +- src/core/dbus.c | 41 ++++++---------- src/core/manager.c | 14 ++---- src/cryptsetup/cryptsetup-generator.c | 8 ++- src/cryptsetup/cryptsetup.c | 8 +-- src/fsck/fsck.c | 2 +- src/fstab-generator/fstab-generator.c | 57 ++++++++-------------- src/getty-generator/getty-generator.c | 11 ++--- src/hostname/hostnamed.c | 3 +- src/journal/cat.c | 6 +-- src/journal/coredump.c | 3 +- src/journal/journalctl.c | 3 +- src/journal/journald.c | 31 +++++------- src/locale/localed.c | 17 +++---- src/login/logind-button.c | 6 +-- src/login/logind-session.c | 21 +++----- src/login/logind-user.c | 12 ++--- src/login/logind.c | 18 +++---- src/login/multi-seat-x.c | 4 +- src/modules-load/modules-load.c | 12 ++--- src/nspawn/nspawn.c | 42 ++++++---------- src/rc-local-generator/rc-local-generator.c | 3 +- src/readahead/readahead-collect.c | 9 ++-- src/readahead/readahead-replay.c | 3 +- src/remount-fs/remount-fs.c | 2 +- src/shared/ask-password-api.c | 3 +- src/shared/dbus-common.c | 2 +- src/shared/log.h | 6 +++ src/shared/logs-show.c | 6 +-- src/shared/util.c | 8 ++- src/shutdownd/shutdownd.c | 6 +-- src/sysctl/sysctl.c | 12 ++--- .../system-update-generator.c | 6 +-- src/systemctl/systemctl.c | 40 ++++++--------- src/timedate/timedated.c | 19 +++----- src/tmpfiles/tmpfiles.c | 19 +++----- .../tty-ask-password-agent.c | 6 +-- src/udev/scsi_id/scsi_id.c | 12 ++--- src/vconsole/vconsole-setup.c | 6 +-- 40 files changed, 183 insertions(+), 310 deletions(-) (limited to 'src/sysctl/sysctl.c') diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index c3824c7979..f0ca83fd45 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -608,8 +608,7 @@ int main(int argc, char *argv[]) { a = hashmap_new(string_hash_func, string_compare_func); b = hashmap_new(string_hash_func, string_compare_func); if (!a || !b) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 1322f63eee..8ddb1118ed 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -330,8 +330,7 @@ int manager_setup_cgroup(Manager *m) { /* We need a new root cgroup */ m->cgroup_hierarchy = NULL; if (asprintf(&m->cgroup_hierarchy, "%s%s", streq(current, "/") ? "" : current, suffix) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } } diff --git a/src/core/dbus.c b/src/core/dbus.c index 0c13517143..9db172b6e6 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -723,15 +723,11 @@ static int bus_setup_loop(Manager *m, DBusConnection *bus) { dbus_connection_set_exit_on_disconnect(bus, FALSE); if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) || - !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) { - log_error("Out of memory."); - return -ENOMEM; - } + !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) + return log_oom(); - if (set_put(m->bus_connections_for_dispatch, bus) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (set_put(m->bus_connections_for_dispatch, bus) < 0) + return log_oom(); dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL); return 0; @@ -764,7 +760,7 @@ static void bus_new_connection( !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) { - log_error("Out of memory."); + log_oom(); return; } @@ -776,10 +772,8 @@ static void bus_new_connection( static int init_registered_system_bus(Manager *m) { char *id; - if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) + return log_oom(); if (m->running_as != MANAGER_SYSTEM) { DBusError error; @@ -814,10 +808,8 @@ static int init_registered_api_bus(Manager *m) { if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) || !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || - !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) { - log_error("Out of memory."); - return -ENOMEM; - } + !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) + return log_oom(); /* Get NameOwnerChange messages */ dbus_bus_add_match(m->api_bus, @@ -1090,8 +1082,7 @@ static int bus_init_private(Manager *m) { return 0; if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto fail; } @@ -1110,8 +1101,7 @@ static int bus_init_private(Manager *m) { if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) || !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) || !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto fail; } @@ -1158,8 +1148,7 @@ int bus_init(Manager *m, bool try_bus_connect) { return 0; oom: - log_error("Out of memory."); - return -ENOMEM; + return log_oom(); } static void shutdown_connection(Manager *m, DBusConnection *c) { @@ -1458,7 +1447,7 @@ void bus_broadcast_finished( message = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartupFinished"); if (!message) { - log_error("Out of memory."); + log_oom(); return; } @@ -1469,13 +1458,13 @@ void bus_broadcast_finished( DBUS_TYPE_UINT64, &userspace_usec, DBUS_TYPE_UINT64, &total_usec, DBUS_TYPE_INVALID)) { - log_error("Out of memory."); + log_oom(); goto finish; } if (bus_broadcast(m, message) < 0) { - log_error("Out of memory."); + log_oom(); goto finish; } diff --git a/src/core/manager.c b/src/core/manager.c index 42a9490242..bcaf913b5a 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -1625,7 +1625,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) { } if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -2073,10 +2073,8 @@ static int create_generator_dir(Manager *m, char **generator, const char *name) if (m->running_as == MANAGER_SYSTEM && getpid() == 1) { p = strappend("/run/systemd/", name); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); r = mkdir_p_label(p, 0755); if (r < 0) { @@ -2086,10 +2084,8 @@ static int create_generator_dir(Manager *m, char **generator, const char *name) } } else { p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); if (!mkdtemp(p)) { free(p); diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index fb6b4d25c0..c6bc65aecf 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -303,8 +303,7 @@ static int parse_proc_cmdline(void) { t = strv_append(arg_proc_cmdline_disks, word + 10); if (!t) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } strv_free(arg_proc_cmdline_disks); @@ -317,8 +316,7 @@ static int parse_proc_cmdline(void) { t = strv_append(arg_proc_cmdline_disks, word + 13); if (!t) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } strv_free(arg_proc_cmdline_disks); @@ -380,7 +378,7 @@ int main(int argc, char *argv[]) { device = strappend("UUID=", *i); if (!name || !device) { - log_error("Out of memory."); + log_oom(); r = EXIT_FAILURE; free(name); free(device); diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 9d4e77364d..cc30e50003 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -343,7 +343,7 @@ int main(int argc, char *argv[]) { l = strcspn(opt_cipher, "-"); if (!(truncated_cipher = strndup(opt_cipher, l))) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -365,7 +365,7 @@ int main(int argc, char *argv[]) { char **p; if (asprintf(&text, "Please enter passphrase for disk %s!", name) < 0) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -383,7 +383,7 @@ int main(int argc, char *argv[]) { assert(strv_length(passwords) == 1); if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -416,7 +416,7 @@ int main(int argc, char *argv[]) { /* Pad password if necessary */ if (!(c = new(char, opt_key_size))) { - log_error("Out of memory."); + log_oom(); goto finish; } diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index 036d3c5b41..058f34d64f 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -297,7 +297,7 @@ int main(int argc, char *argv[]) { } if (!(udev = udev_new())) { - log_error("Out of memory."); + log_oom(); goto finish; } diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 89a4d13504..251a346c4d 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -47,7 +47,7 @@ static int device_name(const char *path, char **unit) { p = unit_name_from_path(path, ".device"); if (!p) - return -ENOMEM; + return log_oom(); *unit = p; return 1; @@ -98,15 +98,13 @@ static int add_swap(const char *what, struct mntent *me) { name = unit_name_from_path(what, ".swap"); if (!name) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } unit = strjoin(arg_dest, "/", name, NULL); if (!unit) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -148,8 +146,7 @@ static int add_swap(const char *what, struct mntent *me) { if (!noauto) { lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL); if (!lnk) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -161,18 +158,14 @@ static int add_swap(const char *what, struct mntent *me) { } r = device_name(what, &device); - if (r < 0) { - log_error("Out of memory."); - r = -ENOMEM; + if (r < 0) goto finish; - } if (r > 0) { free(lnk); lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL); if (!lnk) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -255,16 +248,14 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { } name = unit_name_from_path(where, ".mount"); - if (!name) { - log_error("Out of memory."); - r = -ENOMEM; + if (!name) { + r = log_oom(); goto finish; } unit = strjoin(arg_dest, "/", name, NULL); if (!unit) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -323,8 +314,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { if (!noauto) { lnk = strjoin(arg_dest, "/", post, nofail || automount ? ".wants/" : ".requires/", name, NULL); if (!lnk) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -339,24 +329,20 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { !path_equal(where, "/")) { r = device_name(what, &device); - if (r < 0) { - log_error("Out of memory."); - r = -ENOMEM; + if (r < 0) goto finish; - } if (r > 0) { free(lnk); lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL); if (!lnk) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } mkdir_parents_label(lnk, 0755); if (symlink(unit, lnk) < 0) { - log_error("Failed to creat symlink: %m"); + log_error("Failed to create symlink: %m"); r = -errno; goto finish; } @@ -367,15 +353,13 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { if (automount && !path_equal(where, "/")) { automount_name = unit_name_from_path(where, ".automount"); if (!name) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } automount_unit = strjoin(arg_dest, "/", automount_name, NULL); if (!automount_unit) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -410,8 +394,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) { free(lnk); lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL); if (!lnk) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -459,16 +442,14 @@ static int parse_fstab(void) { what = fstab_node_to_udev_node(me->mnt_fsname); if (!what) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } where = strdup(me->mnt_dir); if (!where) { - log_error("Out of memory."); + r = log_oom(); free(what); - r = -ENOMEM; goto finish; } @@ -513,7 +494,7 @@ static int parse_proc_cmdline(void) { word = strndup(w, l); if (!word) { - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c index 9e46a47a9a..1cef6aeae9 100644 --- a/src/getty-generator/getty-generator.c +++ b/src/getty-generator/getty-generator.c @@ -42,8 +42,7 @@ static int add_symlink(const char *fservice, const char *tservice) { to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL); if (!from || !to) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -77,10 +76,8 @@ static int add_serial_getty(const char *tty) { log_debug("Automatically adding serial getty for /dev/%s.", tty); n = unit_name_replace_instance("serial-getty@.service", tty); - if (!n) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!n) + return log_oom(); r = add_symlink("serial-getty@.service", n); free(n); @@ -160,7 +157,7 @@ int main(int argc, char *argv[]) { int k; if (asprintf(&p, "/sys/class/tty/%s", j) < 0) { - log_error("Out of memory."); + log_oom(); r = EXIT_FAILURE; goto finish; } diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 7dab5f40df..8f9d5a04f5 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -532,8 +532,7 @@ static int connect_bus(DBusConnection **_bus) { if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL) || !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto fail; } diff --git a/src/journal/cat.c b/src/journal/cat.c index cdd46bcf5b..523a7a2eda 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -91,10 +91,8 @@ static int parse_argv(int argc, char *argv[]) { arg_identifier = NULL; else { arg_identifier = strdup(optarg); - if (!arg_identifier) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!arg_identifier) + return log_oom(); } break; diff --git a/src/journal/coredump.c b/src/journal/coredump.c index cfd3a910d9..a507fc65f8 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -239,8 +239,7 @@ int main(int argc, char* argv[]) { p = malloc(9 + COREDUMP_MAX); if (!p) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index a9cf9cd957..c924afbccc 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -256,8 +256,7 @@ static int add_matches(sd_journal *j, char **args) { t = strappend("_EXE=", path); if (!t) { free(p); - log_error("Out of memory."); - return -ENOMEM; + return log_oom(); } r = sd_journal_add_match(j, t, 0); diff --git a/src/journal/journald.c b/src/journal/journald.c index ae1fbc4bd4..5602e362df 100644 --- a/src/journal/journald.c +++ b/src/journal/journald.c @@ -393,7 +393,7 @@ static void server_vacuum(Server *s) { if (s->system_journal) { if (asprintf(&p, "/var/log/journal/%s", ids) < 0) { - log_error("Out of memory."); + log_oom(); return; } @@ -405,7 +405,7 @@ static void server_vacuum(Server *s) { if (s->runtime_journal) { if (asprintf(&p, "/run/log/journal/%s", ids) < 0) { - log_error("Out of memory."); + log_oom(); return; } @@ -1270,7 +1270,7 @@ static void process_native_message( u = MAX((n+N_IOVEC_META_FIELDS+1) * 2U, 4U); c = realloc(iovec, u * sizeof(struct iovec)); if (!c) { - log_error("Out of memory."); + log_oom(); break; } @@ -1357,7 +1357,7 @@ static void process_native_message( k = malloc((e - p) + 1 + l); if (!k) { - log_error("Out of memory."); + log_oom(); break; } @@ -1450,7 +1450,7 @@ static void process_native_file( p = malloc(st.st_size); if (!p) { - log_error("Out of memory."); + log_oom(); return; } @@ -1542,10 +1542,8 @@ static int stdout_stream_line(StdoutStream *s, char *p) { s->identifier = NULL; else { s->identifier = strdup(p); - if (!s->identifier) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!s->identifier) + return log_oom(); } s->state = STDOUT_STREAM_UNIT_ID; @@ -1557,10 +1555,8 @@ static int stdout_stream_line(StdoutStream *s, char *p) { s->unit_id = NULL; else { s->unit_id = strdup(p); - if (!s->unit_id) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!s->unit_id) + return log_oom(); } } @@ -1761,9 +1757,8 @@ static int stdout_stream_new(Server *s) { stream = new0(StdoutStream, 1); if (!stream) { - log_error("Out of memory."); close_nointr_nofail(fd); - return -ENOMEM; + return log_oom(); } stream->fd = fd; @@ -2753,10 +2748,8 @@ static int server_init(Server *s) { server_parse_proc_cmdline(s); s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func); - if (!s->user_journals) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!s->user_journals) + return log_oom(); s->epoll_fd = epoll_create1(EPOLL_CLOEXEC); if (s->epoll_fd < 0) { diff --git a/src/locale/localed.c b/src/locale/localed.c index b8007d72fd..22950a60a9 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -425,7 +425,7 @@ static void push_data(DBusConnection *bus) { l_set = new0(char*, _PROP_MAX); l_unset = new0(char*, _PROP_MAX); if (!l_set || !l_unset) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -438,7 +438,7 @@ static void push_data(DBusConnection *bus) { char *s; if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -456,30 +456,30 @@ static void push_data(DBusConnection *bus) { dbus_message_iter_init_append(m, &iter); if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) { - log_error("Out of memory."); + log_oom(); goto finish; } STRV_FOREACH(t, l_unset) if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) { - log_error("Out of memory."); + log_oom(); goto finish; } if (!dbus_message_iter_close_container(&iter, &sub) || !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) { - log_error("Out of memory."); + log_oom(); goto finish; } STRV_FOREACH(t, l_set) if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) { - log_error("Out of memory."); + log_oom(); goto finish; } if (!dbus_message_iter_close_container(&iter, &sub)) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -1344,8 +1344,7 @@ static int connect_bus(DBusConnection **_bus) { if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) || !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto fail; } diff --git a/src/login/logind-button.c b/src/login/logind-button.c index 62e0c3dbba..d023294a59 100644 --- a/src/login/logind-button.c +++ b/src/login/logind-button.c @@ -107,10 +107,8 @@ int button_open(Button *b) { } p = strappend("/dev/input/", b->name); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); free(p); diff --git a/src/login/logind-session.c b/src/login/logind-session.c index a43ecad2b1..16d4955d5d 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -374,10 +374,8 @@ static int session_link_x11_socket(Session *s) { k = strspn(s->display+1, "0123456789"); f = new(char, sizeof("/tmp/.X11-unix/X") + k); - if (!f) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!f) + return log_oom(); c = stpcpy(f, "/tmp/.X11-unix/X"); memcpy(c, s->display+1, k); @@ -395,9 +393,8 @@ static int session_link_x11_socket(Session *s) { t = strappend(s->user->runtime_path, "/X11-display"); if (!t) { - log_error("Out of memory."); free(f); - return -ENOMEM; + return log_oom(); } if (link(f, t) < 0) { @@ -468,10 +465,8 @@ static int session_create_cgroup(Session *s) { assert(s->user->cgroup_path); if (!s->cgroup_path) { - if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) + return log_oom(); } else p = s->cgroup_path; @@ -669,10 +664,8 @@ static int session_unlink_x11_socket(Session *s) { s->user->display = NULL; t = strappend(s->user->runtime_path, "/X11-display"); - if (!t) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!t) + return log_oom(); r = unlink(t); free(t); diff --git a/src/login/logind-user.c b/src/login/logind-user.c index fca68159a7..aa9c3f1a31 100644 --- a/src/login/logind-user.c +++ b/src/login/logind-user.c @@ -259,10 +259,8 @@ static int user_mkdir_runtime_path(User *u) { } if (!u->runtime_path) { - if (asprintf(&p, "/run/user/%lu", (unsigned long) u->uid) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (asprintf(&p, "/run/user/%lu", (unsigned long) u->uid) < 0) + return log_oom(); } else p = u->runtime_path; @@ -286,10 +284,8 @@ static int user_create_cgroup(User *u) { assert(u); if (!u->cgroup_path) { - if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) + return log_oom(); } else p = u->cgroup_path; diff --git a/src/login/logind.c b/src/login/logind.c index 1cfb7fa9ac..bae9a95f38 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -1020,10 +1020,8 @@ int manager_get_session_by_cgroup(Manager *m, const char *cgroup, Session **sess } p = strdup(cgroup); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); for (;;) { char *e; @@ -1061,10 +1059,8 @@ int manager_get_user_by_cgroup(Manager *m, const char *cgroup, User **user) { } p = strdup(cgroup); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); for (;;) { char *e; @@ -1176,8 +1172,7 @@ static int manager_connect_bus(Manager *m) { !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) || !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/user", &bus_user_vtable, m) || !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto fail; } @@ -1611,8 +1606,7 @@ int main(int argc, char *argv[]) { m = manager_new(); if (!m) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/login/multi-seat-x.c b/src/login/multi-seat-x.c index a1fab86f6a..59f70882d4 100644 --- a/src/login/multi-seat-x.c +++ b/src/login/multi-seat-x.c @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) { device_node = strdup(dn); if (!device_node) { udev_device_unref(d); - log_error("Out of memory."); + log_oom(); goto fail; } } @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { path = strappend("/run/systemd/multi-session-x/", seat); if (!path) { - log_error("Out of memory."); + log_oom(); goto fail; } diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index e73ba7f241..6aeaf459af 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -49,17 +49,13 @@ static int add_modules(const char *p) { char **t, **k; k = strv_split(p, ","); - if (!k) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!k) + return log_oom(); t = strv_merge(arg_proc_cmdline_modules, k); strv_free(k); - if (!t) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!t) + return log_oom(); strv_free(arg_proc_cmdline_modules); arg_proc_cmdline_modules = t; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 355a103edf..b9fa02dc76 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -205,10 +205,8 @@ static int parse_argv(int argc, char *argv[]) { char *t; t = strndup(word, length); - if (!t) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!t) + return log_oom(); if (cap_from_name(t, &cap) < 0) { log_error("Failed to parse capability %s.", t); @@ -289,7 +287,7 @@ static int mount_all(const char *dest) { int t; if (asprintf(&where, "%s/%s", dest, mount_table[k].where) < 0) { - log_error("Out of memory."); + log_oom(); if (r == 0) r = -ENOMEM; @@ -335,20 +333,16 @@ static int setup_timezone(const char *dest) { assert(dest); /* Fix the timezone, if possible */ - if (asprintf(&where, "%s/etc/localtime", dest) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (asprintf(&where, "%s/etc/localtime", dest) < 0) + return log_oom(); if (mount("/etc/localtime", where, "bind", MS_BIND, NULL) >= 0) mount("/etc/localtime", where, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL); free(where); - if (asprintf(&where, "%s/etc/timezone", dest) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (asprintf(&where, "%s/etc/timezone", dest) < 0) + return log_oom(); if (mount("/etc/timezone", where, "bind", MS_BIND, NULL) >= 0) mount("/etc/timezone", where, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL); @@ -368,8 +362,7 @@ static int setup_resolv_conf(const char *dest) { /* Fix resolv.conf, if possible */ if (asprintf(&where, "%s/etc/resolv.conf", dest) < 0) { - log_error("Out of memory."); - return -ENOMEM; + return log_oom(); } if (mount("/etc/resolv.conf", where, "bind", MS_BIND, NULL) >= 0) @@ -480,8 +473,7 @@ static int setup_dev_console(const char *dest, const char *console) { } if (asprintf(&to, "%s/dev/console", dest) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -535,14 +527,12 @@ static int setup_kmsg(const char *dest, int kmsg_socket) { * avoid any problems with containers deadlocking due to this * we simply make /dev/kmsg unavailable to the container. */ if (asprintf(&from, "%s/dev/kmsg", dest) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } if (asprintf(&to, "%s/proc/kmsg", dest) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -639,8 +629,7 @@ static int setup_journal(const char *directory) { p = strappend(directory, "/etc/machine-id"); if (!p) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -670,8 +659,7 @@ static int setup_journal(const char *directory) { p = strappend("/var/log/journal/", l); q = strjoin(directory, "/var/log/journal/", l, NULL); if (!p || !q) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -1296,13 +1284,13 @@ int main(int argc, char *argv[]) { if ((asprintf((char**)(envp + 3), "HOME=%s", home ? home: "/root") < 0) || (asprintf((char**)(envp + 4), "USER=%s", arg_user ? arg_user : "root") < 0) || (asprintf((char**)(envp + 5), "LOGNAME=%s", arg_user ? arg_user : "root") < 0)) { - log_error("Out of memory."); + log_oom(); goto child_fail; } if (arg_uuid) { if (asprintf((char**)(envp + 6), "container_uuid=%s", arg_uuid) < 0) { - log_error("Out of memory."); + log_oom(); goto child_fail; } } diff --git a/src/rc-local-generator/rc-local-generator.c b/src/rc-local-generator/rc-local-generator.c index 9fafa29f22..c219e77047 100644 --- a/src/rc-local-generator/rc-local-generator.c +++ b/src/rc-local-generator/rc-local-generator.c @@ -48,8 +48,7 @@ static int add_symlink(const char *service, const char *where) { asprintf(&to, "%s/%s.wants/%s", arg_dest, where, service); if (!from || !to) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c index 45d489a60e..4fcf64c24d 100644 --- a/src/readahead/readahead-collect.c +++ b/src/readahead/readahead-collect.c @@ -242,8 +242,7 @@ static int collect(const char *root) { assert(root); if (asprintf(&pack_fn, "%s/.readahead", root) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -477,8 +476,7 @@ done: log_debug("On btrfs: %s", yes_no(on_btrfs)); if (asprintf(&pack_fn_new, "%s/.readahead.new", root) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -510,8 +508,7 @@ done: n = hashmap_size(files); if (!(ordered = new(struct item, n))) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/readahead/readahead-replay.c b/src/readahead/readahead-replay.c index f90821e831..a1ac6b0c91 100644 --- a/src/readahead/readahead-replay.c +++ b/src/readahead/readahead-replay.c @@ -150,8 +150,7 @@ static int replay(const char *root) { block_bump_request_nr(root); if (asprintf(&pack_fn, "%s/.readahead", root) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c index 8b3aaeb32c..636c46f0f3 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -114,7 +114,7 @@ int main(int argc, char *argv[]) { s = strdup(me->mnt_dir); if (!s) { - log_error("Out of memory."); + log_oom(); ret = EXIT_FAILURE; continue; } diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 005f40fef7..8a0fb89a84 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -281,8 +281,7 @@ static int create_socket(char **name) { } if (!(c = strdup(sa.un.sun_path))) { - r = -ENOMEM; - log_error("Out of memory."); + r = log_oom(); goto fail; } diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c index ab1dc4d7e4..5d64568c34 100644 --- a/src/shared/dbus-common.c +++ b/src/shared/dbus-common.c @@ -1161,7 +1161,7 @@ void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) { return; oom: - log_error("Out of memory."); + log_oom(); if (pending) { dbus_pending_call_cancel(pending); diff --git a/src/shared/log.h b/src/shared/log.h index c986b2579d..7bdb3e0865 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "macro.h" @@ -102,6 +103,11 @@ int log_dump_internal( #define log_warning(...) log_meta(LOG_WARNING, __FILE__, __LINE__, __func__, __VA_ARGS__) #define log_error(...) log_meta(LOG_ERR, __FILE__, __LINE__, __func__, __VA_ARGS__) +static inline int log_oom(void) { + log_error("Out of memory."); + return -ENOMEM; +} + /* This modifies the buffer passed! */ #define log_dump(level, buffer) log_dump_internal(level, __FILE__, __LINE__, __func__, buffer) diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index f90f5a1f0d..edb5a9cafb 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -52,10 +52,8 @@ static int parse_field(const void *data, size_t length, const char *field, char buf = malloc(nl+1); memcpy(buf, (const char*) data + fl, nl); ((char*)buf)[nl] = 0; - if (!buf) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!buf) + return log_oom(); free(*target); *target = buf; diff --git a/src/shared/util.c b/src/shared/util.c index 2e7ae63db2..5f360085a2 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -909,8 +909,7 @@ int load_env_file( continue; if (!(u = normalize_env_assignment(p))) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -918,8 +917,7 @@ int load_env_file( free(u); if (!t) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -4278,7 +4276,7 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) { continue; if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) { - log_error("Out of memory."); + log_oom(); continue; } diff --git a/src/shutdownd/shutdownd.c b/src/shutdownd/shutdownd.c index 9dd1336133..d426d9833d 100644 --- a/src/shutdownd/shutdownd.c +++ b/src/shutdownd/shutdownd.c @@ -212,10 +212,8 @@ static int update_schedule_file(struct sd_shutdown_command *c) { } t = cescape(c->wall_message); - if (!t) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!t) + return log_oom(); r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path); if (r < 0) { diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index b463c7fac7..3bfc454c04 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -45,10 +45,8 @@ static int apply_sysctl(const char *property, const char *value) { log_debug("Setting '%s' to '%s'", property, value); p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); n = stpcpy(p, PROC_SYS_PREFIX); strcpy(n, property); @@ -191,10 +189,8 @@ static int parse_argv(int argc, char *argv[]) { *p = '/'; l = strv_append(arg_prefixes, optarg); - if (!l) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!l) + return log_oom(); strv_free(arg_prefixes); arg_prefixes = l; diff --git a/src/system-update-generator/system-update-generator.c b/src/system-update-generator/system-update-generator.c index abda5a0190..6660192f5e 100644 --- a/src/system-update-generator/system-update-generator.c +++ b/src/system-update-generator/system-update-generator.c @@ -47,10 +47,8 @@ static int generate_symlink(void) { } p = strappend(arg_dest, "/default.target"); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); if (symlink(SYSTEM_DATA_UNIT_PATH "/system-update.target", p) < 0) { free(p); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index d493733761..ef8ab2dc14 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -657,10 +657,8 @@ static int list_unit_files(DBusConnection *bus, char **args) { Iterator i; h = hashmap_new(string_hash_func, string_compare_func); - if (!h) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!h) + return log_oom(); r = unit_file_get_list(arg_scope, arg_root, h); if (r < 0) { @@ -673,8 +671,7 @@ static int list_unit_files(DBusConnection *bus, char **args) { units = new(UnitFileList, n_units); if (!units) { unit_file_list_free(h); - log_error("Out of memory."); - return -ENOMEM; + return log_oom(); } HASHMAP_FOREACH(u, h, i) { @@ -2630,14 +2627,14 @@ static void show_unit_help(UnitStatusInfo *i) { if (e) { page = strndup((*p) + 4, e - *p - 4); if (!page) { - log_error("Out of memory."); + log_oom(); return; } section = strndup(e + 1, *p + k - e - 2); if (!section) { free(page); - log_error("Out of memory."); + log_oom(); return; } @@ -3282,10 +3279,8 @@ static int show(DBusConnection *bus, char **args) { n = unit_name_mangle(*name); p = unit_dbus_path_from_name(n ? n : *name); free(n); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); r = show_one(args[0], bus, p, show_properties, &new_line); free(p); @@ -3298,10 +3293,8 @@ static int show(DBusConnection *bus, char **args) { /* Interpret as job id */ char *p; - if (asprintf(&p, "/org/freedesktop/systemd1/job/%u", id) < 0) { - log_error("Out of memory."); - return -ENOMEM; - } + if (asprintf(&p, "/org/freedesktop/systemd1/job/%u", id) < 0) + return log_oom(); r = show_one(args[0], bus, p, show_properties, &new_line); free(p); @@ -3974,8 +3967,7 @@ static int enable_sysv_units(char **args) { asprintf(&p, "%s/%s", *k, name); if (!p) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -3995,8 +3987,7 @@ static int enable_sysv_units(char **args) { else asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name); if (!p) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -4024,10 +4015,9 @@ static int enable_sysv_units(char **args) { l = strv_join((char**)argv, " "); if (!l) { - log_error("Out of memory."); free(q); free(p); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -4184,8 +4174,7 @@ static int enable_unit(DBusConnection *bus, char **args) { "org.freedesktop.systemd1.Manager", method); if (!m) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -4339,8 +4328,7 @@ static int unit_is_enabled(DBusConnection *bus, char **args) { "org.freedesktop.systemd1.Manager", "GetUnitFileState"); if (!m) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 3fef9e8844..09fd808332 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -153,7 +153,7 @@ static void verify_timezone(void) { p = strappend("/usr/share/zoneinfo/", tz.zone); if (!p) { - log_error("Out of memory."); + log_oom(); return; } @@ -219,10 +219,8 @@ static int write_data_timezone(void) { } p = strappend("/usr/share/zoneinfo/", tz.zone); - if (!p) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!p) + return log_oom(); r = symlink_or_copy_atomic(p, "/etc/localtime"); free(p); @@ -341,7 +339,7 @@ static char** get_ntp_services(void) { q = strv_append(r, l); if (!q) { - log_error("Out of memory."); + log_oom(); break; } @@ -379,16 +377,14 @@ static int read_ntp(DBusConnection *bus) { "org.freedesktop.systemd1.Manager", "GetUnitFileState"); if (!m) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } if (!dbus_message_append_args(m, DBUS_TYPE_STRING, i, DBUS_TYPE_INVALID)) { - log_error("Could not append arguments to message."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -943,8 +939,7 @@ static int connect_bus(DBusConnection **_bus) { if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) || !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto fail; } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index f8d89218c9..e70332ca06 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -260,8 +260,7 @@ static int dir_cleanup( sub_path = NULL; if (asprintf(&sub_path, "%s/%s", p, dent->d_name) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -969,10 +968,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { assert(buffer); i = new0(Item, 1); - if (!i) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!i) + return log_oom(); if (sscanf(buffer, "%c " @@ -998,10 +995,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { n += strspn(buffer+n, WHITESPACE); if (buffer[n] != 0 && (buffer[n] != '-' || buffer[n+1] != 0)) { i->argument = unquote(buffer+n, "\""); - if (!i->argument) { - log_error("Out of memory."); - return -ENOMEM; - } + if (!i->argument) + return log_oom(); } } @@ -1302,7 +1297,7 @@ static char *resolve_fragment(const char *fragment, const char **search_paths) { STRV_FOREACH(p, search_paths) { resolved_path = strjoin(*p, "/", fragment, NULL); if (resolved_path == NULL) { - log_error("Out of memory."); + log_oom(); return NULL; } @@ -1337,7 +1332,7 @@ int main(int argc, char *argv[]) { globs = hashmap_new(string_hash_func, string_compare_func); if (!items || !globs) { - log_error("Out of memory."); + log_oom(); r = EXIT_FAILURE; goto finish; } diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 403e8cd3c6..052c10e7d5 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -315,8 +315,7 @@ static int parse_password(const char *filename, char **wall) { *wall ? "\r\n\r\n" : "", message, pid) < 0) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -533,8 +532,7 @@ static int show_passwords(void) { continue; if (!(p = strappend("/run/systemd/ask-password/", de->d_name))) { - log_error("Out of memory."); - r = -ENOMEM; + r = log_oom(); goto finish; } diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c index 0bb1bc495a..b1e089cb7c 100644 --- a/src/udev/scsi_id/scsi_id.c +++ b/src/udev/scsi_id/scsi_id.c @@ -198,8 +198,7 @@ static int get_file_options(struct udev *udev, buffer = malloc(MAX_BUFFER_LEN); if (!buffer) { fclose(fd); - log_error("Out of memory."); - return -1; + return log_oom(); } *newargv = NULL; @@ -231,7 +230,7 @@ static int get_file_options(struct udev *udev, if (str1 && strcasecmp(str1, "VENDOR") == 0) { str1 = get_value(&buf); if (!str1) { - retval = -1; + retval = log_oom(); break; } vendor_in = str1; @@ -240,7 +239,7 @@ static int get_file_options(struct udev *udev, if (str1 && strcasecmp(str1, "MODEL") == 0) { str1 = get_value(&buf); if (!str1) { - retval = -1; + retval = log_oom(); break; } model_in = str1; @@ -251,7 +250,7 @@ static int get_file_options(struct udev *udev, if (str1 && strcasecmp(str1, "OPTIONS") == 0) { str1 = get_value(&buf); if (!str1) { - retval = -1; + retval = log_oom(); break; } options_in = str1; @@ -294,8 +293,7 @@ static int get_file_options(struct udev *udev, c = argc_count(buffer) + 2; *newargv = calloc(c, sizeof(**newargv)); if (!*newargv) { - log_error("Out of memory."); - retval = -1; + retval = log_oom(); } else { *argc = c; c = 0; diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c index 7679d446e3..62d9c8d7b5 100644 --- a/src/vconsole/vconsole-setup.c +++ b/src/vconsole/vconsole-setup.c @@ -274,7 +274,7 @@ int main(int argc, char **argv) { t = strdup("/etc/sysconfig/console/default.kmap"); if (!t) { - log_error("Out of memory."); + log_oom(); goto finish; } @@ -415,7 +415,7 @@ int main(int argc, char **argv) { free(vc_keytable); if (!vc_keymap) { - log_error("Out of memory."); + log_oom(); goto finish; } } @@ -425,7 +425,7 @@ int main(int argc, char **argv) { t = strdup("/etc/sysconfig/console/default.kmap"); if (!t) { - log_error("Out of memory."); + log_oom(); goto finish; } -- cgit v1.2.3-54-g00ecf From 86fc77c47f2d22cd01d0871866869cb194af0884 Mon Sep 17 00:00:00 2001 From: Michal Sekletar Date: Fri, 3 Aug 2012 16:20:31 +0200 Subject: sysctl: apply configuration at once https://bugzilla.redhat.com/show_bug.cgi?id=767795 [ Simplified by iterating the config files in the backwards order - no need for hashmap_update(). Other minor cleanups. -- michich ] --- src/sysctl/sysctl.c | 92 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 16 deletions(-) (limited to 'src/sysctl/sysctl.c') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 3bfc454c04..793796d363 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -31,12 +31,14 @@ #include "strv.h" #include "util.h" #include "strv.h" +#include "hashmap.h" #include "path-util.h" #include "conf-files.h" #define PROC_SYS_PREFIX "/proc/sys/" -static char **arg_prefixes = NULL; +static char **arg_prefixes; +static Hashmap *sysctl_options; static int apply_sysctl(const char *property, const char *value) { char *p, *n; @@ -87,13 +89,29 @@ static int apply_sysctl(const char *property, const char *value) { return r; } -static int apply_file(const char *path, bool ignore_enoent) { +static int apply_all(void) { + int r = 0; + char *property, *value; + Iterator i; + + HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) { + int k; + + k = apply_sysctl(property, value); + if (k < 0 && r == 0) + r = k; + } + return r; +} + +static int parse_file(const char *path, bool ignore_enoent) { FILE *f; int r = 0; assert(path); - if (!(f = fopen(path, "re"))) { + f = fopen(path, "re"); + if (!f) { if (ignore_enoent && errno == ENOENT) return 0; @@ -101,10 +119,9 @@ static int apply_file(const char *path, bool ignore_enoent) { return -errno; } - log_debug("apply: %s\n", path); + log_debug("parse: %s\n", path); while (!feof(f)) { - char l[LINE_MAX], *p, *value; - int k; + char l[LINE_MAX], *p, *value, *new_value, *property; if (!fgets(l, sizeof(l), f)) { if (feof(f)) @@ -123,7 +140,8 @@ static int apply_file(const char *path, bool ignore_enoent) { if (strchr(COMMENTS, *p)) continue; - if (!(value = strchr(p, '='))) { + value = strchr(p, '='); + if (!value) { log_error("Line is not an assignment in file '%s': %s", path, value); if (r == 0) @@ -134,8 +152,31 @@ static int apply_file(const char *path, bool ignore_enoent) { *value = 0; value++; - if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0) - r = k; + property = strdup(strstrip(p)); + if (!property) { + r = log_oom(); + goto finish; + } + + new_value = strdup(strstrip(value)); + if (!new_value) { + free(property); + r = log_oom(); + goto finish; + } + + r = hashmap_put(sysctl_options, property, new_value); + if (r < 0) { + if (r == -EEXIST) + log_debug("Skipping previously assigned sysctl variable %s", property); + else + log_error("Failed to add sysctl variable %s to hashmap: %s", property, strerror(-r)); + + free(property); + free(new_value); + if (r != -EEXIST) + goto finish; + } } finish: @@ -212,6 +253,8 @@ static int parse_argv(int argc, char *argv[]) { int main(int argc, char *argv[]) { int r = 0; + char *property, *value; + Iterator it; r = parse_argv(argc, argv); if (r <= 0) @@ -223,13 +266,19 @@ int main(int argc, char *argv[]) { umask(0022); + sysctl_options = hashmap_new(string_hash_func, string_compare_func); + if (!sysctl_options) { + r = log_oom(); + goto finish; + } + if (argc > optind) { int i; for (i = optind; i < argc; i++) { int k; - k = apply_file(argv[i], false); + k = parse_file(argv[i], false); if (k < 0 && r == 0) r = k; } @@ -251,19 +300,30 @@ int main(int argc, char *argv[]) { goto finish; } - STRV_FOREACH(f, files) { - k = apply_file(*f, true); + /* We parse the files in decreasing order of precedence. + * parse_file() will skip keys that were already assigned. */ + + r = parse_file("/etc/sysctl.conf", true); + + f = files + strv_length(files) - 1; + STRV_FOREACH_BACKWARDS(f, files) { + k = parse_file(*f, true); if (k < 0 && r == 0) r = k; } - k = apply_file("/etc/sysctl.conf", true); - if (k < 0 && r == 0) - r = k; - strv_free(files); } + + r = apply_all(); finish: + HASHMAP_FOREACH_KEY(value, property, sysctl_options, it) { + hashmap_remove(sysctl_options, property); + free(property); + free(value); + } + hashmap_free(sysctl_options); + strv_free(arg_prefixes); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; -- cgit v1.2.3-54-g00ecf From 089d4a08d0cda5bae0bf9bb3273bfdb397200ee8 Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Fri, 21 Sep 2012 12:30:56 +0200 Subject: sysctl: fix error code handling After if (r <= 0) r can't be 0 so if (k < 0 && r == 0) never happens. --- src/sysctl/sysctl.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/sysctl/sysctl.c') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 793796d363..62d4c81c47 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -260,6 +260,8 @@ int main(int argc, char *argv[]) { if (r <= 0) return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; + r = 0; + log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); -- cgit v1.2.3-54-g00ecf From 0187f62bb5f785c22952e79c55ef0fdb87f1ad65 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 21 Sep 2012 17:01:39 +0200 Subject: sysctl: always return the last error we encountered --- src/sysctl/sysctl.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/sysctl/sysctl.c') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 62d4c81c47..e77f496089 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -252,7 +252,7 @@ static int parse_argv(int argc, char *argv[]) { } int main(int argc, char *argv[]) { - int r = 0; + int r = 0, k; char *property, *value; Iterator it; @@ -260,8 +260,6 @@ int main(int argc, char *argv[]) { if (r <= 0) return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; - r = 0; - log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); @@ -274,19 +272,18 @@ int main(int argc, char *argv[]) { goto finish; } + r = 0; + if (argc > optind) { int i; for (i = optind; i < argc; i++) { - int k; - k = parse_file(argv[i], false); - if (k < 0 && r == 0) + if (k < 0) r = k; } } else { char **files, **f; - int k; r = conf_files_list(&files, ".conf", "/etc/sysctl.d", @@ -310,14 +307,17 @@ int main(int argc, char *argv[]) { f = files + strv_length(files) - 1; STRV_FOREACH_BACKWARDS(f, files) { k = parse_file(*f, true); - if (k < 0 && r == 0) + if (k < 0) r = k; } strv_free(files); } - r = apply_all(); + k = apply_all(); + if (k < 0) + r = k; + finish: HASHMAP_FOREACH_KEY(value, property, sysctl_options, it) { hashmap_remove(sysctl_options, property); -- cgit v1.2.3-54-g00ecf From 1a3f40f912670d3dea3811a4560b368412090b81 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 6 Oct 2012 16:32:17 -0400 Subject: sysctl: avoiding exiting with error on -EEXIST If the final key in any sysctl.d file is a duplicate, systemd-sysctl will exit with an error (and no explaination why). Ignore this, as duplicate keys are to be expected when overriding settings in the directory hierarchy. --- src/sysctl/sysctl.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/sysctl/sysctl.c') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index e77f496089..a68d67fd4b 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -167,9 +167,13 @@ static int parse_file(const char *path, bool ignore_enoent) { r = hashmap_put(sysctl_options, property, new_value); if (r < 0) { - if (r == -EEXIST) + if (r == -EEXIST) { + /* ignore this "error" to avoid returning it + * for the function when this is the last key + * in the file being parsed. */ + r = 0; log_debug("Skipping previously assigned sysctl variable %s", property); - else + } else log_error("Failed to add sysctl variable %s to hashmap: %s", property, strerror(-r)); free(property); -- cgit v1.2.3-54-g00ecf From 91b32fa987a4a50faf3d8561b28b6c9d5150adef Mon Sep 17 00:00:00 2001 From: Michal Sekletar Date: Thu, 25 Oct 2012 16:16:19 +0200 Subject: sysctl: parse all keys in a config file https://bugzilla.redhat.com/show_bug.cgi?id=869779 --- src/sysctl/sysctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sysctl/sysctl.c') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index a68d67fd4b..035e0ec321 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -178,7 +178,7 @@ static int parse_file(const char *path, bool ignore_enoent) { free(property); free(new_value); - if (r != -EEXIST) + if (r != 0) goto finish; } } -- cgit v1.2.3-54-g00ecf