chiark / gitweb /
conf_files_list(): split out conf_files_list_strv()
[elogind.git] / src / shared / util.c
index 7f41fc4f5ece08f14e66aa463510029947ffd24b..2bfa2cb4e64a67cc684482a87038b0a4dab76922 100644 (file)
@@ -6,16 +6,16 @@
   Copyright 2010 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
+  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
-  General Public License for more details.
+  Lesser General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
@@ -56,6 +56,7 @@
 #include <glob.h>
 #include <grp.h>
 #include <sys/mman.h>
+#include <sys/statvfs.h>
 
 #include "macro.h"
 #include "util.h"
@@ -1690,7 +1691,8 @@ char *cescape(const char *s) {
 
         /* Does C style string escaping. */
 
-        if (!(r = new(char, strlen(s)*4 + 1)))
+        r = new(char, strlen(s)*4 + 1);
+        if (!r)
                 return NULL;
 
         for (f = s, t = r; *f; f++)
@@ -4285,6 +4287,15 @@ bool tty_is_vc(const char *tty) {
         return vtnr_from_tty(tty) >= 0;
 }
 
+bool tty_is_console(const char *tty) {
+        assert(tty);
+
+        if (startswith(tty, "/dev/"))
+                tty += 5;
+
+        return streq(tty, "console");
+}
+
 int vtnr_from_tty(const char *tty) {
         int i, r;
 
@@ -4318,8 +4329,10 @@ bool tty_is_vc_resolve(const char *tty) {
         if (startswith(tty, "/dev/"))
                 tty += 5;
 
-        /* Resolve where /dev/console is pointing to */
-        if (streq(tty, "console"))
+        /* Resolve where /dev/console is pointing to, if /sys is
+         * actually ours (i.e. not read-only-mounted which is a sign
+         * for container setups) */
+        if (streq(tty, "console") && path_is_read_only_fs("/sys") <= 0)
                 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
                         /* If multiple log outputs are configured the
                          * last one is what /dev/console points to */
@@ -4339,7 +4352,7 @@ bool tty_is_vc_resolve(const char *tty) {
 const char *default_term_for_tty(const char *tty) {
         assert(tty);
 
-        return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt100";
+        return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt102";
 }
 
 bool dirent_is_file(const struct dirent *de) {
@@ -4864,7 +4877,7 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) {
 
         for (;;) {
                 int k;
-                char *p, *f;
+                char *p;
 
                 k = readdir_r(dir, &buffer, &de);
                 if (k != 0) {
@@ -4883,17 +4896,10 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) {
                         goto finish;
                 }
 
-                f = canonicalize_file_name(p);
-                if (!f) {
-                        log_error("Failed to canonicalize file name '%s': %m", p);
+                if (hashmap_put(h, file_name_from_path(p), p) <= 0) {
+                        log_debug("Skip overridden file: %s.", p);
                         free(p);
-                        continue;
                 }
-                free(p);
-
-                log_debug("found: %s\n", f);
-                if (hashmap_put(h, file_name_from_path(f), f) <= 0)
-                        free(f);
         }
 
 finish:
@@ -4909,29 +4915,13 @@ static int base_cmp(const void *a, const void *b) {
         return strcmp(file_name_from_path(s1), file_name_from_path(s2));
 }
 
-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) {
         Hashmap *fh = NULL;
-        char **dirs = NULL;
         char **files = NULL;
-        char **p;
-        va_list ap;
+        const char **p;
         int r = 0;
 
-        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;
-        }
-        if (!strv_uniq(dirs)) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        assert(dirs);
 
         fh = hashmap_new(string_hash_func, string_compare_func);
         if (!fh) {
@@ -4953,16 +4943,40 @@ int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) {
                 r = -ENOMEM;
                 goto finish;
         }
-
         qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
 
 finish:
-        strv_free(dirs);
         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;
@@ -4988,7 +5002,6 @@ int hwclock_is_localtime(void) {
                 if (!b)
                         return -EIO;
 
-
                 truncate_nl(line);
                 local = streq(line, "LOCAL");
 
@@ -5123,7 +5136,7 @@ int hwclock_get_time(struct tm *tm) {
         if (ioctl(fd, RTC_RD_TIME, tm) < 0)
                 err = -errno;
 
-        /* We don't now daylight saving, so we reset this in order not
+        /* We don't know daylight saving, so we reset this in order not
          * to confused mktime(). */
         tm->tm_isdst = -1;
 
@@ -6036,7 +6049,7 @@ int fd_inc_rcvbuf(int fd, size_t n) {
         return 1;
 }
 
-int fork_agent(pid_t *pid, const char *path, ...) {
+int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
         pid_t parent_pid, agent_pid;
         int fd;
         bool stdout_is_tty, stderr_is_tty;
@@ -6073,7 +6086,7 @@ int fork_agent(pid_t *pid, const char *path, ...) {
                 _exit(EXIT_SUCCESS);
 
         /* Don't leak fds to the agent */
-        close_all_fds(NULL, 0);
+        close_all_fds(except, n_except);
 
         stdout_is_tty = isatty(STDOUT_FILENO);
         stderr_is_tty = isatty(STDERR_FILENO);
@@ -6120,3 +6133,100 @@ int fork_agent(pid_t *pid, const char *path, ...) {
         execv(path, l);
         _exit(EXIT_FAILURE);
 }
+
+int setrlimit_closest(int resource, const struct rlimit *rlim) {
+        struct rlimit highest, fixed;
+
+        assert(rlim);
+
+        if (setrlimit(resource, rlim) >= 0)
+                return 0;
+
+        if (errno != EPERM)
+                return -errno;
+
+        /* So we failed to set the desired setrlimit, then let's try
+         * to get as close as we can */
+        assert_se(getrlimit(resource, &highest) == 0);
+
+        fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
+        fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
+
+        if (setrlimit(resource, &fixed) < 0)
+                return -errno;
+
+        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;
+        FILE *f;
+        bool done = false;
+        size_t l;
+
+        assert(field);
+        assert(_value);
+
+        if (pid == 0)
+                pid = getpid();
+
+        snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
+        char_array_0(path);
+
+        f = fopen(path, "re");
+        if (!f)
+                return -errno;
+
+        l = strlen(field);
+        r = 0;
+
+        do {
+                char line[LINE_MAX];
+                unsigned i;
+
+                for (i = 0; i < sizeof(line)-1; i++) {
+                        int c;
+
+                        c = getc(f);
+                        if (_unlikely_(c == EOF)) {
+                                done = true;
+                                break;
+                        } else if (c == 0)
+                                break;
+
+                        line[i] = c;
+                }
+                line[i] = 0;
+
+                if (memcmp(line, field, l) == 0 && line[l] == '=') {
+                        value = strdup(line + l + 1);
+                        if (!value) {
+                                r = -ENOMEM;
+                                break;
+                        }
+
+                        r = 1;
+                        break;
+                }
+
+        } while (!done);
+
+        fclose(f);
+
+        if (r >= 0)
+                *_value = value;
+
+        return r;
+}