summaryrefslogtreecommitdiff
path: root/m4
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2007-05-23 22:28:53 +0200
committerJim Meyering <jim@meyering.net>2007-05-23 22:30:15 +0200
commit873251291e04dec0f7ab0d5b7d65809981ed95b4 (patch)
tree799101161149a00354dc9a5214afec650798f0ba /m4
parentc1c80224d31fb1cfde26aa544790b307dccbb9a5 (diff)
downloadparted-873251291e04dec0f7ab0d5b7d65809981ed95b4.tar.gz
Don't fail all tests when "." lacks O_DIRECT support.
I often build tools on a tmpfs file system (it's faster), and found that parted tests always failed there. That's because it tries to open the "device" (a file) with O_DIRECT, and at least the linux tmpfs driver always fails with EINVAL in that case. So here's a patch that makes it work. Since the test may require writing in a directory like /tmp, to which others typically have write access, it is particularly careful about security (see the mkdtemp script below), in case "make check" is run by e.g., root. Don't fail all tests when "." lacks O_DIRECT support. Before, running "make check" on a file system that doesn't support O_DIRECT (e.g. tmpfs), would always fail. Now, it works, as long as the test machinery can find a writable directory in which open with O_DIRECT *does* work. * m4/o-direct.m4: New file. Find a directory/FS with O_DIRECT support. * configure.ac: Use the new macro. * libparted/tests/t1000-label.sh: New file. Wrap the binary, so it can take advantage of the code that finds O_DIRECT supporting FS. * tests/mkdtemp: New file. Required, since when running tests as root, we may have to create a temporary directory in a directory like /tmp that's writable by others. * tests/Makefile.am (EXTRA_DIST): Add mkdtemp. * tests/test-lib.sh: When creating test subdir, and setting up "trap", use the directory specified in $PARTED_USABLE_TEST_DIR. Don't set PATH here. Now, that's done via the generated, and always- sourced, init.sh. As a result, invoke parted via its full file name.
Diffstat (limited to 'm4')
-rw-r--r--m4/o-direct.m4235
1 files changed, 235 insertions, 0 deletions
diff --git a/m4/o-direct.m4 b/m4/o-direct.m4
new file mode 100644
index 0000000..ec84ef4
--- /dev/null
+++ b/m4/o-direct.m4
@@ -0,0 +1,235 @@
+#serial 1
+# Find a directory in which a disk-simulating file is usable by parted.
+# The problem is that on systems supporting O_DIRECT, open with O_DIRECT
+# fails for some file system types (e.g., tmpfs on linux-2.6.21).
+
+# Copyright (C) 2007 Free Software Foundation, Inc.
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# From Jim Meyering.
+
+# Set PARTED_USABLE_TEST_DIR to the name of the first usable directory
+# from the list below. If none is usable, set it to the empty string.
+# Consider $TMPDIR only if it specifies an absolute name, and that
+# name contains no shell meta-character. Likewise for $HOME.
+# The candidate directories:
+# . $HOME $TMPDIR /tmp /var/tmp /dev/shm
+AC_DEFUN([parted_FIND_USABLE_TEST_DIR],
+[
+ AC_CACHE_CHECK([for a usable (O_DIRECT-supporting) temporary dir],
+ [parted_cv_func_open_O_DIRECT_temp_dir],
+ [
+ # First of all, if there is no O_DIRECT definition, use ".",
+ # and skip the run-test.
+ AC_EGREP_CPP([frobnozzle], [
+#include <fcntl.h>
+#ifdef O_DIRECT
+frobnozzle
+#endif
+ ], pe_have_O_DIRECT=yes, pe_have_O_DIRECT=no)
+ if test $pe_have_O_DIRECT = no; then
+ # With no O_DIRECT definition, "." is fine.
+ pe_cand_dirs=.
+ else
+ pe_cand_dirs=.
+ for pe_dir in "$HOME" "$TMPDIR"; do
+ case $pe_dir in
+ /tmp) ;;
+ /var/tmp) ;;
+ /dev/shm) ;;
+ /*) case $pe_dir in
+ # Accept $HOME or $TMP only if the value is nice and boring.
+ *[^/a-zA-Z0-9_.-]*) ;;
+ *) pe_cand_dirs="$pe_cand_dirs $pe_dir";;
+ esac
+ esac
+ done
+
+ # This is the list of candidate directories.
+ pe_cand_dirs="$pe_cand_dirs /tmp /var/tmp /dev/shm"
+
+ PARTED_CANDIDATE_DIRS=$pe_cand_dirs
+ export PARTED_CANDIDATE_DIRS
+
+ AC_RUN_IFELSE(
+ [AC_LANG_SOURCE(
+ [[
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define LOGICAL_BLOCK_SIZE 4096
+static char g_buf[2 * LOGICAL_BLOCK_SIZE];
+
+static inline void *
+ptr_align (void const *ptr, size_t alignment)
+{
+ char const *p0 = ptr;
+ char const *p1 = p0 + alignment - 1;
+ return (void *) (p1 - (size_t) p1 % alignment);
+}
+
+static int
+create_input_file (char const *file, char const *buf, size_t n_bytes)
+{
+ int fd = open (file, O_CREAT | O_WRONLY, 0600);
+ if (fd < 0)
+ return 1;
+ if (write (fd, buf, n_bytes) != n_bytes)
+ {
+ close (fd);
+ return 1;
+ }
+ return !! close (fd);
+}
+
+static int
+try_o_direct (char const *file)
+{
+ char const *p = ptr_align (g_buf, LOGICAL_BLOCK_SIZE);
+ int fd;
+
+ if (!(p + LOGICAL_BLOCK_SIZE < g_buf + sizeof g_buf))
+ return 4;
+
+ fd = open (file, O_WRONLY | O_DIRECT);
+ if (fd < 0)
+ return 1;
+
+ if (write (fd, p, LOGICAL_BLOCK_SIZE) != LOGICAL_BLOCK_SIZE)
+ return 1;
+
+ return !! close (fd);
+}
+
+#undef stpcpy
+#define stpcpy(a, b) my_stpcpy (a, b)
+static char *
+my_stpcpy (char *dest, const char *src)
+{
+ char *d = dest;
+ const char *s = src;
+ do *d++ = *s; while (*s++ != '\0');
+ return d - 1;
+}
+
+/* The base name of the file we'll create in the mkdtemp-returned
+ temporary directory. */
+#define BASENAME "x"
+
+/* Return 0 upon failure, else the 1-based index of the first
+ useful directory name from PARTED_CANDIDATE_DIRS. */
+int
+main ()
+{
+ char const *env_dirs;
+ char *dirs;
+ char *dir;
+ unsigned int n;
+ int found = 0;
+ size_t dirs_len;
+
+ if ((env_dirs = getenv ("PARTED_CANDIDATE_DIRS")) == NULL)
+ return 0;
+
+ dirs_len = strlen (env_dirs);
+ if ((dirs = strndup (env_dirs, dirs_len)) == NULL)
+ return 0;
+ dir = dirs;
+
+ for (n = 1; ; n++)
+ {
+ size_t dirname_len;
+ char *space;
+
+ /* Skip any leading spaces. */
+ while (*dir == ' ')
+ ++dir;
+
+ space = strchr (dir, ' ');
+ if (space)
+ {
+ *space = '\0';
+ dirname_len = space - dir;
+ }
+ else
+ {
+ dirname_len = strlen (dir);
+ }
+
+ if (dirname_len != 0)
+ {
+ /* Create an mkdtemp template starting with dir. */
+ char *tmp;
+ char *endp;
+ char const *base = "partedOD.XXXXXX";
+ /* Allocate enough space not just for the dir name, but
+ also for the name of the file to create within it. */
+ char *template = malloc (dirname_len + 1 + strlen (base)
+ + 1 + strlen (BASENAME) + 1);
+ if (template != NULL
+ && (endp = stpcpy (stpcpy (stpcpy (template, dir), "/"), base))
+ && (tmp = mkdtemp (template)) != NULL)
+ {
+ /* Append "/BASENAME" to create the file name. */
+ stpcpy (stpcpy (endp, "/"), BASENAME);
+
+ if (create_input_file (tmp, g_buf, sizeof g_buf) == 0
+ && try_o_direct (tmp) == 0)
+ found = 1;
+
+ unlink (tmp); /* ignore failure */
+ *endp = '\0';
+ rmdir (tmp); /* ignore failure */
+ }
+ if (template)
+ free (template);
+ }
+
+ if (found)
+ break;
+
+ dir += dirname_len + 1;
+ if (dirs + dirs_len < dir)
+ {
+ n = 0;
+ break;
+ }
+ }
+ free (dirs);
+
+ return n;
+}
+ ]])],
+ # If the above program exits with status 0, then
+ # there it found no useful directory. Use ".".
+ [parted_cv_func_open_O_DIRECT_temp_dir=.],
+
+ # It found one. The exit status is an index into the list.
+ # We also run this code when the program fails to compile or
+ # to link, as will happen on systems without a mkdtemp function.
+ [pe_err=$?; set _ $pe_cand_dirs; shift
+ eval parted_cv_func_open_O_DIRECT_temp_dir='$'$pe_err],
+
+ # When cross-compiling, use ".".
+ [parted_cv_func_open_O_DIRECT_temp_dir=.]
+ )
+ fi
+ ])
+ PARTED_USABLE_TEST_DIR=$parted_cv_func_open_O_DIRECT_temp_dir
+ AC_SUBST([PARTED_USABLE_TEST_DIR])
+
+ # If the result is ".", don't cache it. The next user of
+ # the cache may well be running from a different file system.
+ dnl Here, I'm using "$as_unset", which is a non-published (i.e., internal)
+ dnl part of autoconf, but we don't expect its name to change any time soon.
+ dnl and by then, it'll probably be ok to use "unset" all by itself.
+ if test "$parted_cv_func_open_O_DIRECT_temp_dir" = .; then
+ $as_unset parted_cv_func_open_O_DIRECT_temp_dir
+ fi
+])