diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/creat-safer.c | 33 | ||||
-rw-r--r-- | lib/dup-safer.c | 46 | ||||
-rw-r--r-- | lib/fcntl--.h | 28 | ||||
-rw-r--r-- | lib/fcntl-safer.h | 24 | ||||
-rw-r--r-- | lib/fd-safer.c | 59 | ||||
-rw-r--r-- | lib/open-safer.c | 51 | ||||
-rw-r--r-- | lib/pipe-safer.c | 50 | ||||
-rw-r--r-- | lib/unistd--.h | 28 | ||||
-rw-r--r-- | lib/unistd-safer.h | 23 |
9 files changed, 342 insertions, 0 deletions
diff --git a/lib/creat-safer.c b/lib/creat-safer.c new file mode 100644 index 00000000..4588de39 --- /dev/null +++ b/lib/creat-safer.c @@ -0,0 +1,33 @@ +/* Invoke creat, but avoid some glitches. + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Jim Meyering. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "fcntl-safer.h" + +#include <fcntl.h> +#include "unistd-safer.h" + +int +creat_safer (char const *file, mode_t mode) +{ + return fd_safer (creat (file, mode)); +} diff --git a/lib/dup-safer.c b/lib/dup-safer.c new file mode 100644 index 00000000..8cbee700 --- /dev/null +++ b/lib/dup-safer.c @@ -0,0 +1,46 @@ +/* Invoke dup, but avoid some glitches. + Copyright (C) 2001, 2004, 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "unistd-safer.h" + +#include <fcntl.h> + +#include <unistd.h> +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif + +/* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or + STDERR_FILENO. */ + +int +dup_safer (int fd) +{ +#ifdef F_DUPFD + return fcntl (fd, F_DUPFD, STDERR_FILENO + 1); +#else + /* fd_safer calls us back, but eventually the recursion unwinds and + does the right thing. */ + return fd_safer (dup (fd)); +#endif +} diff --git a/lib/fcntl--.h b/lib/fcntl--.h new file mode 100644 index 00000000..51b869e6 --- /dev/null +++ b/lib/fcntl--.h @@ -0,0 +1,28 @@ +/* Like fcntl.h, but redefine some names to avoid glitches. + + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +#include <fcntl.h> +#include "fcntl-safer.h" + +#undef open +#define open open_safer + +#undef creat +#define creat creat_safer diff --git a/lib/fcntl-safer.h b/lib/fcntl-safer.h new file mode 100644 index 00000000..cab6aab1 --- /dev/null +++ b/lib/fcntl-safer.h @@ -0,0 +1,24 @@ +/* Invoke fcntl-like functions, but avoid some glitches. + + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +#include <sys/types.h> + +int open_safer (char const *, int, ...); +int creat_safer (char const *, mode_t); diff --git a/lib/fd-safer.c b/lib/fd-safer.c new file mode 100644 index 00000000..5933bcbd --- /dev/null +++ b/lib/fd-safer.c @@ -0,0 +1,59 @@ +/* Return a safer copy of a file descriptor. + + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "unistd-safer.h" + +#include <errno.h> + +#include <unistd.h> +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif + +/* Return FD, unless FD would be a copy of standard input, output, or + error; in that case, return a duplicate of FD, closing FD. On + failure to duplicate, close FD, set errno, and return -1. Preserve + errno if FD is negative, so that the caller can always inspect + errno when the returned value is negative. + + This function is usefully wrapped around functions that return file + descriptors, e.g., fd_safer (open ("file", O_RDONLY)). */ + +int +fd_safer (int fd) +{ + if (STDIN_FILENO <= fd && fd <= STDERR_FILENO) + { + int f = dup_safer (fd); + int e = errno; + close (fd); + errno = e; + fd = f; + } + + return fd; +} diff --git a/lib/open-safer.c b/lib/open-safer.c new file mode 100644 index 00000000..d3ba894a --- /dev/null +++ b/lib/open-safer.c @@ -0,0 +1,51 @@ +/* Invoke open, but avoid some glitches. + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "fcntl-safer.h" + +#include <fcntl.h> +#include <stdarg.h> +#include "unistd-safer.h" + +int +open_safer (char const *file, int flags, ...) +{ + mode_t mode = 0; + + if (flags & O_CREAT) + { + va_list ap; + va_start (ap, flags); + + /* Assume mode_t promotes to int if and only if it is smaller. + This assumption isn't guaranteed by the C standard, but we + don't know of any real-world counterexamples. */ + mode = (sizeof (mode_t) < sizeof (int) + ? va_arg (ap, int) + : va_arg (ap, mode_t)); + + va_end (ap); + } + + return fd_safer (open (file, flags, mode)); +} diff --git a/lib/pipe-safer.c b/lib/pipe-safer.c new file mode 100644 index 00000000..fb02d721 --- /dev/null +++ b/lib/pipe-safer.c @@ -0,0 +1,50 @@ +/* Invoke pipe, but avoid some glitches. + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Jim Meyering. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "unistd-safer.h" + +#include <unistd.h> + +/* Like pipe, but ensure that neither of the file descriptors is + STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO. */ + +int +pipe_safer (int fd[2]) +{ + int fail = pipe (fd); + if (fail) + return fail; + + { + int i; + for (i = 0; i < 2; i++) + { + int f = fd_safer (fd[i]); + if (f < 0) + return -1; + fd[i] = f; + } + } + + return 0; +} diff --git a/lib/unistd--.h b/lib/unistd--.h new file mode 100644 index 00000000..1fe6ce8b --- /dev/null +++ b/lib/unistd--.h @@ -0,0 +1,28 @@ +/* Like unistd.h, but redefine some names to avoid glitches. + + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +#include <unistd.h> +#include "unistd-safer.h" + +#undef dup +#define dup dup_safer + +#undef pipe +#define pipe pipe_safer diff --git a/lib/unistd-safer.h b/lib/unistd-safer.h new file mode 100644 index 00000000..f95999d3 --- /dev/null +++ b/lib/unistd-safer.h @@ -0,0 +1,23 @@ +/* Invoke unistd-like functions, but avoid some glitches. + + Copyright (C) 2001, 2003, 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Paul Eggert. */ + +int dup_safer (int); +int fd_safer (int); +int pipe_safer (int[2]); |