chiark / gitweb /
path_lookup: moved _cleanup_lookup_paths_free_ from install.c to path-lookup.h
[elogind.git] / src / shared / path-util.c
index 68881357784eba69f4cd8d165b32f903f058323e..def7a7409a30ed85ef8ebf573b5df489977f81b8 100644 (file)
@@ -425,3 +425,82 @@ int path_is_os_tree(const char *path) {
 
         return r < 0 ? 0 : 1;
 }
+
+int find_binary(const char *name, char **filename) {
+        assert(name);
+        if (strchr(name, '/')) {
+                char *p;
+
+                if (path_is_absolute(name))
+                        p = strdup(name);
+                else
+                        p = path_make_absolute_cwd(name);
+                if (!p)
+                        return -ENOMEM;
+
+                *filename = p;
+                return 0;
+        } else {
+                const char *path;
+                char *state, *w;
+                size_t l;
+
+                /**
+                 * Plain getenv, not secure_getenv, because we want
+                 * to actually allow the user to pick the binary.
+                 */
+                path = getenv("PATH");
+                if (!path)
+                        path = DEFAULT_PATH;
+
+                FOREACH_WORD_SEPARATOR(w, l, path, ":", state) {
+                        char *p;
+
+                        if (asprintf(&p, "%.*s/%s", (int) l, w, name) < 0)
+                                return -ENOMEM;
+
+                        if (access(p, X_OK) < 0) {
+                                free(p);
+                                continue;
+                        }
+
+                        path_kill_slashes(p);
+                        *filename = p;
+
+                        return 0;
+                }
+
+                return -ENOENT;
+        }
+}
+
+bool paths_check_timestamp(char **paths, usec_t *paths_ts_usec, bool update)
+{
+        unsigned int i;
+        bool changed = false;
+
+        if (paths == NULL)
+                goto out;
+
+        for (i = 0; paths[i]; i++) {
+                struct stat stats;
+
+                if (stat(paths[i], &stats) < 0)
+                        continue;
+
+                if (paths_ts_usec[i] == timespec_load(&stats.st_mtim))
+                        continue;
+
+                /* first check */
+                if (paths_ts_usec[i] != 0) {
+                        log_debug("reload - timestamp of '%s' changed\n", paths[i]);
+                        changed = true;
+                }
+
+                /* update timestamp */
+                if (update)
+                        paths_ts_usec[i] = timespec_load(&stats.st_mtim);
+        }
+out:
+        return changed;
+}