From f4aa44fff84819c50f1761342017c36605a8fe99 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 7 Sep 2009 05:38:18 -0600 Subject: [PATCH] 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. Signed-off-by: Eric Blake --- ChangeLog | 13 ++++ MODULES.html.sh | 1 + doc/posix-functions/mkfifoat.texi | 13 ++-- doc/posix-functions/mknodat.texi | 13 ++-- lib/mkfifoat.c | 101 +++++++++++++++++++++++++++++++ lib/sys_stat.in.h | 23 +++++++ m4/mkfifoat.m4 | 23 +++++++ m4/sys_stat_h.m4 | 32 +++++----- modules/mkfifoat | 29 +++++++++ modules/mkfifoat-tests | 11 ++++ modules/sys_stat | 8 ++- tests/test-mkfifoat.c | 124 ++++++++++++++++++++++++++++++++++++++ 12 files changed, 367 insertions(+), 24 deletions(-) create mode 100644 lib/mkfifoat.c create mode 100644 m4/mkfifoat.m4 create mode 100644 modules/mkfifoat create mode 100644 modules/mkfifoat-tests create mode 100644 tests/test-mkfifoat.c diff --git a/ChangeLog b/ChangeLog index d0e039654..629a558a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2009-09-08 Eric Blake + 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. diff --git a/MODULES.html.sh b/MODULES.html.sh index abfd93b08..76741b3b6 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -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 diff --git a/doc/posix-functions/mkfifoat.texi b/doc/posix-functions/mkfifoat.texi index 087b4d778..c3e39cade 100644 --- a/doc/posix-functions/mkfifoat.texi +++ b/doc/posix-functions/mkfifoat.texi @@ -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 diff --git a/doc/posix-functions/mknodat.texi b/doc/posix-functions/mknodat.texi index aec66a602..d1af05861 100644 --- a/doc/posix-functions/mknodat.texi +++ b/doc/posix-functions/mknodat.texi @@ -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 index 000000000..a230b0048 --- /dev/null +++ b/lib/mkfifoat.c @@ -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 . */ + +/* written by Eric Blake */ + +#include + +#include + +#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 + +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 */ diff --git a/lib/sys_stat.in.h b/lib/sys_stat.in.h index 0c4522931..b0ea92d6e 100644 --- a/lib/sys_stat.in.h +++ b/lib/sys_stat.in.h @@ -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 index 000000000..99e2336f7 --- /dev/null +++ b/m4/mkfifoat.m4 @@ -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 +]) diff --git a/m4/sys_stat_h.m4 b/m4/sys_stat_h.m4 index d29f695db..8b62902c5 100644 --- a/m4/sys_stat_h.m4 +++ b/m4/sys_stat_h.m4 @@ -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 contains lstat and mkdir or must be substituted. +dnl Provide a GNU-like . 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 index 000000000..db2f0da84 --- /dev/null +++ b/modules/mkfifoat @@ -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: + + + +License: +GPL + +Maintainer: +Jim Meyering, Eric Blake diff --git a/modules/mkfifoat-tests b/modules/mkfifoat-tests new file mode 100644 index 000000000..9ff5091e4 --- /dev/null +++ b/modules/mkfifoat-tests @@ -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@ diff --git a/modules/sys_stat b/modules/sys_stat index 35bafcc1b..93bc70a45 100644 --- a/modules/sys_stat +++ b/modules/sys_stat @@ -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 index 000000000..3853dfecb --- /dev/null +++ b/tests/test-mkfifoat.c @@ -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 . */ + +/* Written by Eric Blake , 2009. */ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +#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; +} -- 2.11.0