mkfifoat: new module
authorEric Blake <ebb9@byu.net>
Mon, 7 Sep 2009 11:38:18 +0000 (05:38 -0600)
committerEric Blake <ebb9@byu.net>
Tue, 8 Sep 2009 12:24:44 +0000 (06:24 -0600)
* modules/mkfifoat: New file.
* lib/mkfifoat.c: Likewise.
* m4/mkfifoat.m4 (gl_FUNC_MKFIFOAT): Likewise.
* m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Add witnesses.
* modules/sys_stat (Makefile.am): Use them.
* lib/sys_stat.in.h (mkfifoat, mknodat): Declare them.
* MODULES.html.sh (File system functions): Mention module.
* doc/posix-functions/mkfifoat.texi (mkfifoat): Likewise.
* doc/posix-functions/mknodat.texi (mknodat): Likewise.
* modules/mkfifoat-tests: New test.
* tests/test-mkfifoat.c: Likewise.

Signed-off-by: Eric Blake <ebb9@byu.net>
12 files changed:
ChangeLog
MODULES.html.sh
doc/posix-functions/mkfifoat.texi
doc/posix-functions/mknodat.texi
lib/mkfifoat.c [new file with mode: 0644]
lib/sys_stat.in.h
m4/mkfifoat.m4 [new file with mode: 0644]
m4/sys_stat_h.m4
modules/mkfifoat [new file with mode: 0644]
modules/mkfifoat-tests [new file with mode: 0644]
modules/sys_stat
tests/test-mkfifoat.c [new file with mode: 0644]

index d0e0396..629a558 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2009-09-08  Eric Blake  <ebb9@byu.net>
 
+       mkfifoat: new module
+       * modules/mkfifoat: New file.
+       * lib/mkfifoat.c: Likewise.
+       * m4/mkfifoat.m4 (gl_FUNC_MKFIFOAT): Likewise.
+       * m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Add witnesses.
+       * modules/sys_stat (Makefile.am): Use them.
+       * lib/sys_stat.in.h (mkfifoat, mknodat): Declare them.
+       * MODULES.html.sh (File system functions): Mention module.
+       * doc/posix-functions/mkfifoat.texi (mkfifoat): Likewise.
+       * doc/posix-functions/mknodat.texi (mknodat): Likewise.
+       * modules/mkfifoat-tests: New test.
+       * tests/test-mkfifoat.c: Likewise.
+
        strchrnul: relax license
        * modules/strchrnul (License): Derived from glibc, so LGPLv2+ is
        okay.
index abfd93b..76741b3 100755 (executable)
@@ -2465,6 +2465,7 @@ func_all_modules ()
   func_module lchmod
   func_module lchown
   func_module mkancesdirs
+  func_module mkfifoat
   func_module mkdir-p
   func_module modechange
   func_module mountlist
index 087b4d7..c3e39ca 100644 (file)
@@ -4,16 +4,21 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/mkfifoat.html}
 
-Gnulib module: ---
+Gnulib module: mkfifoat
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+glibc 2.3.6, MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX
+5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw,
+Interix 3.5, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on some platforms:
-glibc 2.3.6, MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX
-5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw, Interix 3.5, BeOS.
+The gnulib replacement function always fails with @samp{ENOSYS} on
+some platforms:
+mingw.
 @end itemize
index aec66a6..d1af058 100644 (file)
@@ -4,16 +4,21 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/mknodat.html}
 
-Gnulib module: ---
+Gnulib module: mkfifoat
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+glibc 2.3.6, MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX
+5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw,
+Interix 3.5, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on some platforms:
-glibc 2.3.6, MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX
-5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw, Interix 3.5, BeOS.
+The gnulib replacement function always fails with @samp{ENOSYS} on
+some platforms:
+mingw.
 @end itemize
diff --git a/lib/mkfifoat.c b/lib/mkfifoat.c
new file mode 100644 (file)
index 0000000..a230b00
--- /dev/null
@@ -0,0 +1,101 @@
+/* Create a named fifo relative to an open directory.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This program 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 3 of the License, or
+   (at your option) any later version.
+
+   This program 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.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* written by Eric Blake */
+
+#include <config.h>
+
+#include <sys/stat.h>
+
+#include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
+#include "openat.h"
+#include "openat-priv.h"
+#include "save-cwd.h"
+
+#ifndef HAVE_MKFIFO
+# define HAVE_MKFIFO 0
+#endif
+#ifndef HAVE_MKNOD
+# define HAVE_MKNOD 0
+#endif
+
+/* For now, all known systems either have both mkfifo and mknod, or
+   neither.  If this is not true, we can implement the portable
+   aspects of one using the other (POSIX only requires mknod to create
+   fifos; all other uses of mknod are for root users and outside the
+   realm of POSIX).  */
+#if HAVE_MKNOD != HAVE_MKFIFO
+# error Please report this message and your system to bug-gnulib@gnu.org.
+#endif
+
+#if !HAVE_MKFIFO
+/* Mingw lacks mkfifo and mknod, so this wrapper is trivial.  */
+
+# include <errno.h>
+
+int
+mkfifoat (int fd _UNUSED_PARAMETER_, char const *path _UNUSED_PARAMETER_,
+         mode_t mode _UNUSED_PARAMETER_)
+{
+  errno = ENOSYS;
+  return -1;
+}
+
+int
+mknodat (int fd _UNUSED_PARAMETER_, char const *path _UNUSED_PARAMETER_,
+        mode_t mode _UNUSED_PARAMETER_, dev_t dev _UNUSED_PARAMETER_)
+{
+  errno = ENOSYS;
+  return -1;
+}
+
+#else /* HAVE_MKFIFO */
+
+/* Create a named fifo FILE relative to directory FD, with access
+   permissions in MODE.  If possible, do it without changing the
+   working directory.  Otherwise, resort to using save_cwd/fchdir,
+   then mkfifo/restore_cwd.  If either the save_cwd or the restore_cwd
+   fails, then give a diagnostic and exit nonzero.  */
+
+# define AT_FUNC_NAME mkfifoat
+# define AT_FUNC_F1 mkfifo
+# define AT_FUNC_POST_FILE_PARAM_DECLS , mode_t mode
+# define AT_FUNC_POST_FILE_ARGS        , mode
+# include "at-func.c"
+# undef AT_FUNC_NAME
+# undef AT_FUNC_F1
+# undef AT_FUNC_POST_FILE_PARAM_DECLS
+# undef AT_FUNC_POST_FILE_ARGS
+
+/* Create a file system node FILE relative to directory FD, with
+   access permissions and file type in MODE, and device type in DEV.
+   Usually, non-root applications can only create named fifos, with
+   DEV set to 0.  If possible, create the node without changing the
+   working directory.  Otherwise, resort to using save_cwd/fchdir,
+   then mknod/restore_cwd.  If either the save_cwd or the restore_cwd
+   fails, then give a diagnostic and exit nonzero.  */
+
+# define AT_FUNC_NAME mknodat
+# define AT_FUNC_F1 mknod
+# define AT_FUNC_POST_FILE_PARAM_DECLS , mode_t mode, dev_t dev
+# define AT_FUNC_POST_FILE_ARGS        , mode, dev
+# include "at-func.c"
+# undef AT_FUNC_NAME
+# undef AT_FUNC_F1
+# undef AT_FUNC_POST_FILE_PARAM_DECLS
+# undef AT_FUNC_POST_FILE_ARGS
+
+#endif /* HAVE_MKFIFO */
index 0c45229..b0ea92d 100644 (file)
@@ -345,6 +345,29 @@ extern int mkdirat (int fd, char const *file, mode_t mode);
      mkdirat (d, n, m))
 #endif
 
+#if @GNULIB_MKFIFOAT@
+# if !@HAVE_MKFIFOAT@
+int mkfifoat (int fd, char const *file, mode_t mode);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef mkfifoat
+# define mkfifoat(d,n,m)                                    \
+    (GL_LINK_WARNING ("mkfifoat is not portable - " \
+                      "use gnulib module mkfifoat for portability"), \
+     mkfifoat (d, n, m))
+#endif
+
+#if @GNULIB_MKNODAT@
+# if !@HAVE_MKNODAT@
+int mknodat (int fd, char const *file, mode_t mode, dev_t dev);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef mknodat
+# define mknodat(f,n,m,d)                           \
+    (GL_LINK_WARNING ("mknodat is not portable - " \
+                      "use gnulib module mkfifoat for portability"), \
+     mknodat (f, n, m, d))
+#endif
 
 #if @REPLACE_FCHDIR@
 # define fstat rpl_fstat
diff --git a/m4/mkfifoat.m4 b/m4/mkfifoat.m4
new file mode 100644 (file)
index 0000000..99e2336
--- /dev/null
@@ -0,0 +1,23 @@
+# serial 1
+# See if we need to provide mkfifoat/mknodat replacement.
+
+dnl Copyright (C) 2009 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+# Written by Eric Blake.
+
+AC_DEFUN([gl_FUNC_MKFIFOAT],
+[
+  AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
+  AC_REQUIRE([gl_FUNC_OPENAT])
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_CHECK_FUNCS_ONCE([mkfifo mknod mkfifoat mknodat])
+  if test $ac_cv_func_mkfifoat = no; then
+    # No known system has mkfifoat but not mknodat
+    HAVE_MKFIFOAT=0
+    HAVE_MKNODAT=0
+    AC_LIBOBJ([mkfifoat])
+  fi
+])
index d29f695..8b62902 100644 (file)
@@ -1,11 +1,11 @@
-# sys_stat_h.m4 serial 13   -*- Autoconf -*-
+# sys_stat_h.m4 serial 14   -*- Autoconf -*-
 dnl Copyright (C) 2006-2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
 dnl From Eric Blake.
-dnl Test whether <sys/stat.h> contains lstat and mkdir or must be substituted.
+dnl Provide a GNU-like <sys/stat.h>.
 
 AC_DEFUN([gl_HEADER_SYS_STAT_H],
 [
@@ -49,17 +49,21 @@ AC_DEFUN([gl_SYS_STAT_MODULE_INDICATOR],
 AC_DEFUN([gl_SYS_STAT_H_DEFAULTS],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) dnl for REPLACE_FCHDIR
-  GNULIB_FCHMODAT=0; AC_SUBST([GNULIB_FCHMODAT])
-  GNULIB_FSTATAT=0;  AC_SUBST([GNULIB_FSTATAT])
-  GNULIB_LCHMOD=0;   AC_SUBST([GNULIB_LCHMOD])
-  GNULIB_LSTAT=0;    AC_SUBST([GNULIB_LSTAT])
-  GNULIB_MKDIRAT=0;  AC_SUBST([GNULIB_MKDIRAT])
+  GNULIB_FCHMODAT=0;  AC_SUBST([GNULIB_FCHMODAT])
+  GNULIB_FSTATAT=0;   AC_SUBST([GNULIB_FSTATAT])
+  GNULIB_LCHMOD=0;    AC_SUBST([GNULIB_LCHMOD])
+  GNULIB_LSTAT=0;     AC_SUBST([GNULIB_LSTAT])
+  GNULIB_MKDIRAT=0;   AC_SUBST([GNULIB_MKDIRAT])
+  GNULIB_MKFIFOAT=0;  AC_SUBST([GNULIB_MKFIFOAT])
+  GNULIB_MKNODAT=0;   AC_SUBST([GNULIB_MKNODAT])
   dnl Assume proper GNU behavior unless another module says otherwise.
-  HAVE_FCHMODAT=1;   AC_SUBST([HAVE_FCHMODAT])
-  HAVE_FSTATAT=1;    AC_SUBST([HAVE_FSTATAT])
-  HAVE_LCHMOD=1;     AC_SUBST([HAVE_LCHMOD])
-  HAVE_MKDIRAT=1;    AC_SUBST([HAVE_MKDIRAT])
-  REPLACE_FSTATAT=0; AC_SUBST([REPLACE_FSTATAT])
-  REPLACE_LSTAT=0;   AC_SUBST([REPLACE_LSTAT])
-  REPLACE_MKDIR=0;   AC_SUBST([REPLACE_MKDIR])
+  HAVE_FCHMODAT=1;    AC_SUBST([HAVE_FCHMODAT])
+  HAVE_FSTATAT=1;     AC_SUBST([HAVE_FSTATAT])
+  HAVE_LCHMOD=1;      AC_SUBST([HAVE_LCHMOD])
+  HAVE_MKDIRAT=1;     AC_SUBST([HAVE_MKDIRAT])
+  HAVE_MKFIFOAT=1;    AC_SUBST([HAVE_MKFIFOAT])
+  HAVE_MKNODAT=1;     AC_SUBST([HAVE_MKNODAT])
+  REPLACE_FSTATAT=0;  AC_SUBST([REPLACE_FSTATAT])
+  REPLACE_LSTAT=0;    AC_SUBST([REPLACE_LSTAT])
+  REPLACE_MKDIR=0;    AC_SUBST([REPLACE_MKDIR])
 ])
diff --git a/modules/mkfifoat b/modules/mkfifoat
new file mode 100644 (file)
index 0000000..db2f0da
--- /dev/null
@@ -0,0 +1,29 @@
+Description:
+mkfifoat() and mknodat(): create named FIFOs relative to a directory
+
+Files:
+lib/mkfifoat.c
+m4/mkfifoat.m4
+
+Depends-on:
+extensions
+fcntl-h
+openat
+sys_stat
+
+configure.ac:
+gl_FUNC_MKFIFOAT
+gl_UNISTD_MODULE_INDICATOR([mkfifoat])
+gl_UNISTD_MODULE_INDICATOR([mknodat])
+
+Makefile.am:
+
+Include:
+<fcntl.h>
+<sys/stat.h>
+
+License:
+GPL
+
+Maintainer:
+Jim Meyering, Eric Blake
diff --git a/modules/mkfifoat-tests b/modules/mkfifoat-tests
new file mode 100644 (file)
index 0000000..9ff5091
--- /dev/null
@@ -0,0 +1,11 @@
+Files:
+tests/test-mkfifoat.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-mkfifoat
+check_PROGRAMS += test-mkfifoat
+test_mkfifoat_LDADD = $(LDADD) @LIBINTL@
index 35bafcc..93bc70a 100644 (file)
@@ -31,15 +31,19 @@ sys/stat.h: sys_stat.in.h
              -e 's|@''GNULIB_LCHMOD''@|$(GNULIB_LCHMOD)|g' \
              -e 's|@''GNULIB_LSTAT''@|$(GNULIB_LSTAT)|g' \
              -e 's|@''GNULIB_MKDIRAT''@|$(GNULIB_MKDIRAT)|g' \
+             -e 's|@''GNULIB_MKFIFOAT''@|$(GNULIB_MKFIFOAT)|g' \
+             -e 's|@''GNULIB_MKNODAT''@|$(GNULIB_MKNODAT)|g' \
              -e 's|@''HAVE_FCHMODAT''@|$(HAVE_FCHMODAT)|g' \
              -e 's|@''HAVE_FSTATAT''@|$(HAVE_FSTATAT)|g' \
              -e 's|@''HAVE_LCHMOD''@|$(HAVE_LCHMOD)|g' \
              -e 's|@''HAVE_LSTAT''@|$(HAVE_LSTAT)|g' \
              -e 's|@''HAVE_MKDIRAT''@|$(HAVE_MKDIRAT)|g' \
-             -e 's|@''REPLACE_LSTAT''@|$(REPLACE_LSTAT)|g' \
-             -e 's|@''REPLACE_MKDIR''@|$(REPLACE_MKDIR)|g' \
+             -e 's|@''HAVE_MKFIFOAT''@|$(HAVE_MKFIFOAT)|g' \
+             -e 's|@''HAVE_MKNODAT''@|$(HAVE_MKNODAT)|g' \
              -e 's|@''REPLACE_FCHDIR''@|$(REPLACE_FCHDIR)|g' \
              -e 's|@''REPLACE_FSTATAT''@|$(REPLACE_FSTATAT)|g' \
+             -e 's|@''REPLACE_LSTAT''@|$(REPLACE_LSTAT)|g' \
+             -e 's|@''REPLACE_MKDIR''@|$(REPLACE_MKDIR)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              < $(srcdir)/sys_stat.in.h; \
        } > $@-t && \
diff --git a/tests/test-mkfifoat.c b/tests/test-mkfifoat.c
new file mode 100644 (file)
index 0000000..3853dfe
--- /dev/null
@@ -0,0 +1,124 @@
+/* Tests of mkfifoat and mknodat.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This program 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 3 of the License, or
+   (at your option) any later version.
+
+   This program 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.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
+
+#include <config.h>
+
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define ASSERT(expr) \
+  do                                                                         \
+    {                                                                        \
+      if (!(expr))                                                           \
+       {                                                                    \
+         fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
+         fflush (stderr);                                                   \
+         abort ();                                                          \
+       }                                                                    \
+    }                                                                        \
+  while (0)
+
+typedef int (*test_func) (int, char const *, mode_t);
+
+/* Wrapper to make testing mknodat easier.  */
+static int
+test_mknodat (int fd, char const *name, mode_t mode)
+{
+  /* This is the only portable use of mknodat, per POSIX.  */
+  return mknodat (fd, name, mode | S_IFIFO, 0);
+}
+
+int
+main ()
+{
+  int i;
+  test_func funcs[2] = { mkfifoat, test_mknodat };
+  const char *fifo = "test-mkfifoat.fifo";
+
+  /* Create handle for future use.  */
+  int dfd = openat (AT_FDCWD, ".", O_RDONLY);
+  ASSERT (0 <= dfd);
+
+#if !HAVE_MKFIFO
+  fputs ("skipping test: no support for named fifos\n", stderr);
+  return 77;
+#endif
+
+  /* Clean up anything from previous incomplete test.  */
+  remove (fifo);
+
+  /* Test both functions.  */
+  for (i = 0; i < 2; i++)
+    {
+      struct stat st;
+      test_func func = funcs[i];
+
+      /* Sanity checks of failures.  */
+      errno = 0;
+      ASSERT (func (AT_FDCWD, "", 0600) == -1);
+      ASSERT (errno == ENOENT);
+      errno = 0;
+      ASSERT (func (dfd, "", S_IRUSR | S_IWUSR) == -1);
+      ASSERT (errno == ENOENT);
+      errno = 0;
+      ASSERT (func (AT_FDCWD, ".", 0600) == -1);
+      /* POSIX requires EEXIST, but Solaris gives EINVAL.  */
+      ASSERT (errno == EEXIST || errno == EINVAL);
+      errno = 0;
+      ASSERT (func (dfd, ".", 0600) == -1);
+      ASSERT (errno == EEXIST || errno == EINVAL);
+
+      /* Create fifo while cwd is '.', then stat it from '..'.  */
+      ASSERT (func (AT_FDCWD, fifo, 0600) == 0);
+      errno = 0;
+      ASSERT (func (dfd, fifo, 0600) == -1);
+      ASSERT (errno == EEXIST);
+      ASSERT (chdir ("..") == 0);
+      errno = 0;
+      ASSERT (fstatat (AT_FDCWD, fifo, &st, 0) == -1);
+      ASSERT (errno == ENOENT);
+      memset (&st, 0, sizeof st);
+      ASSERT (fstatat (dfd, fifo, &st, 0) == 0);
+      ASSERT (S_ISFIFO (st.st_mode));
+      ASSERT (unlinkat (dfd, fifo, 0) == 0);
+
+      /* Create fifo while cwd is '..', then stat it from '.'.  */
+      ASSERT (func (dfd, fifo, 0600) == 0);
+      ASSERT (fchdir (dfd) == 0);
+      errno = 0;
+      ASSERT (func (AT_FDCWD, fifo, 0600) == -1);
+      ASSERT (errno == EEXIST);
+      memset (&st, 0, sizeof st);
+      ASSERT (fstatat (AT_FDCWD, fifo, &st, AT_SYMLINK_NOFOLLOW) == 0);
+      ASSERT (S_ISFIFO (st.st_mode));
+      memset (&st, 0, sizeof st);
+      ASSERT (fstatat (dfd, fifo, &st, AT_SYMLINK_NOFOLLOW) == 0);
+      ASSERT (S_ISFIFO (st.st_mode));
+      ASSERT (unlink (fifo) == 0);
+    }
+
+  ASSERT (close (dfd) == 0);
+
+  return 0;
+}