chiark / gitweb /
fsck: Search for fsck.type in PATH
authorMike Gilbert <floppym@gentoo.org>
Sat, 12 Apr 2014 20:07:45 +0000 (16:07 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 12 Apr 2014 21:23:13 +0000 (17:23 -0400)
Modifies find_binary() to accept NULL in the second argument.

fsck.type lookup logic moved to new fsck_exists() function, with a test.

src/fsck/fsck.c
src/shared/generator.c
src/shared/path-util.c
src/shared/path-util.h
src/test/test-path-util.c

index 18f2acaa490afd7bdda27938602763cef076f55a..5ed837dffbeb58a545e926afba4485b2c6963bef 100644 (file)
@@ -37,6 +37,7 @@
 #include "bus-errors.h"
 #include "fileio.h"
 #include "udev-util.h"
 #include "bus-errors.h"
 #include "fileio.h"
 #include "udev-util.h"
+#include "path-util.h"
 
 static bool arg_skip = false;
 static bool arg_force = false;
 
 static bool arg_skip = false;
 static bool arg_force = false;
@@ -285,14 +286,13 @@ int main(int argc, char *argv[]) {
 
         type = udev_device_get_property_value(udev_device, "ID_FS_TYPE");
         if (type) {
 
         type = udev_device_get_property_value(udev_device, "ID_FS_TYPE");
         if (type) {
-                const char *checker = strappenda("/sbin/fsck.", type);
-                r = access(checker, X_OK);
+                r = fsck_exists(type);
                 if (r < 0) {
                 if (r < 0) {
-                        if (errno == ENOENT) {
-                                log_info("%s doesn't exist, not checking file system.", checker);
+                        if (r == -ENOENT) {
+                                log_info("fsck.%s doesn't exist, not checking file system.", type);
                                 return EXIT_SUCCESS;
                         } else
                                 return EXIT_SUCCESS;
                         } else
-                                log_warning("%s cannot be used: %m", checker);
+                                log_warning("fsck.%s cannot be used: %s", type, strerror(-r));
                 }
         }
 
                 }
         }
 
index 61103031db23b31df7a5d7c79dc98d11608a3d4a..5ac7b5f02f379fae03a7ff5392b77f6d6ee6a8e1 100644 (file)
@@ -19,6 +19,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <string.h>
 #include <unistd.h>
 
 #include "util.h"
 #include <unistd.h>
 
 #include "util.h"
@@ -26,6 +27,7 @@
 #include "mkdir.h"
 #include "unit-name.h"
 #include "generator.h"
 #include "mkdir.h"
 #include "unit-name.h"
 #include "generator.h"
+#include "path-util.h"
 
 int generator_write_fsck_deps(
                 FILE *f,
 
 int generator_write_fsck_deps(
                 FILE *f,
@@ -45,16 +47,12 @@ int generator_write_fsck_deps(
         }
 
         if (!isempty(fstype) && !streq(fstype, "auto")) {
         }
 
         if (!isempty(fstype) && !streq(fstype, "auto")) {
-                const char *checker;
                 int r;
                 int r;
-
-                checker = strappenda("/sbin/fsck.", fstype);
-                r = access(checker, X_OK);
+                r = fsck_exists(fstype);
                 if (r < 0) {
                 if (r < 0) {
-                        log_warning("Checking was requested for %s, but %s cannot be used: %m", what, checker);
-
+                        log_warning("Checking was requested for %s, but fsck.%s cannot be used: %s", what, fstype, strerror(-r));
                         /* treat missing check as essentially OK */
                         /* treat missing check as essentially OK */
-                        return errno == ENOENT ? 0 : -errno;
+                        return r == -ENOENT ? 0 : r;
                 }
         }
 
                 }
         }
 
index bdc54a9aa5f9a23d0dbfd89bcc801eee14564df3..1ad1084b2d5f0653bb56b1e8dee818b39199fe88 100644 (file)
@@ -425,19 +425,20 @@ int path_is_os_tree(const char *path) {
 
 int find_binary(const char *name, char **filename) {
         assert(name);
 
 int find_binary(const char *name, char **filename) {
         assert(name);
-        assert(filename);
 
         if (strchr(name, '/')) {
 
         if (strchr(name, '/')) {
-                char *p;
+                if (filename) {
+                        char *p;
 
 
-                if (path_is_absolute(name))
-                        p = strdup(name);
-                else
-                        p = path_make_absolute_cwd(name);
-                if (!p)
-                        return -ENOMEM;
+                        if (path_is_absolute(name))
+                                p = strdup(name);
+                        else
+                                p = path_make_absolute_cwd(name);
+                        if (!p)
+                                return -ENOMEM;
+                        *filename = p;
+                }
 
 
-                *filename = p;
                 return 0;
         } else {
                 const char *path;
                 return 0;
         } else {
                 const char *path;
@@ -453,18 +454,19 @@ int find_binary(const char *name, char **filename) {
                         path = DEFAULT_PATH;
 
                 FOREACH_WORD_SEPARATOR(w, l, path, ":", state) {
                         path = DEFAULT_PATH;
 
                 FOREACH_WORD_SEPARATOR(w, l, path, ":", state) {
-                        char *p;
+                        _cleanup_free_ char *p = NULL;
 
                         if (asprintf(&p, "%.*s/%s", (int) l, w, name) < 0)
                                 return -ENOMEM;
 
 
                         if (asprintf(&p, "%.*s/%s", (int) l, w, name) < 0)
                                 return -ENOMEM;
 
-                        if (access(p, X_OK) < 0) {
-                                free(p);
+                        if (access(p, X_OK) < 0)
                                 continue;
                                 continue;
-                        }
 
 
-                        path_kill_slashes(p);
-                        *filename = p;
+                        if (filename) {
+                                path_kill_slashes(p);
+                                *filename = p;
+                                p = NULL;
+                        }
 
                         return 0;
                 }
 
                         return 0;
                 }
@@ -507,3 +509,10 @@ bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool upd
 
         return changed;
 }
 
         return changed;
 }
+
+int fsck_exists(const char *fstype) {
+        const char *checker;
+
+        checker = strappenda("fsck.", fstype);
+        return find_binary(checker, NULL);
+}
index 2b8ea0260e0c4a3296e3ace88dba2eb29bc54446..fdf1f6b000501fcd4850d9e42e25420d0c5a3ff1 100644 (file)
@@ -57,6 +57,8 @@ int find_binary(const char *name, char **filename);
 
 bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update);
 
 
 bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update);
 
+int fsck_exists(const char *fstype);
+
 /* Iterates through the path prefixes of the specified path, going up
  * the tree, to root. Also returns "" (and not "/"!) for the root
  * directory. Excludes the specified directory itself */
 /* Iterates through the path prefixes of the specified path, going up
  * the tree, to root. Also returns "" (and not "/"!) for the root
  * directory. Excludes the specified directory itself */
index bec2a836112693836f3851165d30171850f8901d..a2cf0af75fef675774444ced03a7f49c81acb0b2 100644 (file)
@@ -158,9 +158,20 @@ static void test_prefixes(void) {
         }
 }
 
         }
 }
 
+static void test_fsck_exists(void) {
+        /* Ensure we use a sane default for PATH. */
+        unsetenv("PATH");
+
+        /* fsck.minix is provided by util-linux and will probably exist. */
+        assert_se(fsck_exists("minix") == 0);
+
+        assert_se(fsck_exists("AbCdE") == -ENOENT);
+}
+
 int main(void) {
         test_path();
         test_find_binary();
         test_prefixes();
 int main(void) {
         test_path();
         test_find_binary();
         test_prefixes();
+        test_fsck_exists();
         return 0;
 }
         return 0;
 }