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 @@ | |||
1 | /* Invoke creat, but avoid some glitches. | ||
2 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
17 | |||
18 | /* Written by Jim Meyering. */ | ||
19 | |||
20 | #ifdef HAVE_CONFIG_H | ||
21 | # include <config.h> | ||
22 | #endif | ||
23 | |||
24 | #include "fcntl-safer.h" | ||
25 | |||
26 | #include <fcntl.h> | ||
27 | #include "unistd-safer.h" | ||
28 | |||
29 | int | ||
30 | creat_safer (char const *file, mode_t mode) | ||
31 | { | ||
32 | return fd_safer (creat (file, mode)); | ||
33 | } | ||
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 @@ | |||
1 | /* Invoke dup, but avoid some glitches. | ||
2 | Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
17 | |||
18 | /* Written by Paul Eggert. */ | ||
19 | |||
20 | #ifdef HAVE_CONFIG_H | ||
21 | # include <config.h> | ||
22 | #endif | ||
23 | |||
24 | #include "unistd-safer.h" | ||
25 | |||
26 | #include <fcntl.h> | ||
27 | |||
28 | #include <unistd.h> | ||
29 | #ifndef STDERR_FILENO | ||
30 | # define STDERR_FILENO 2 | ||
31 | #endif | ||
32 | |||
33 | /* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or | ||
34 | STDERR_FILENO. */ | ||
35 | |||
36 | int | ||
37 | dup_safer (int fd) | ||
38 | { | ||
39 | #ifdef F_DUPFD | ||
40 | return fcntl (fd, F_DUPFD, STDERR_FILENO + 1); | ||
41 | #else | ||
42 | /* fd_safer calls us back, but eventually the recursion unwinds and | ||
43 | does the right thing. */ | ||
44 | return fd_safer (dup (fd)); | ||
45 | #endif | ||
46 | } | ||
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 @@ | |||
1 | /* Like fcntl.h, but redefine some names to avoid glitches. | ||
2 | |||
3 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | |||
19 | /* Written by Paul Eggert. */ | ||
20 | |||
21 | #include <fcntl.h> | ||
22 | #include "fcntl-safer.h" | ||
23 | |||
24 | #undef open | ||
25 | #define open open_safer | ||
26 | |||
27 | #undef creat | ||
28 | #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 @@ | |||
1 | /* Invoke fcntl-like functions, but avoid some glitches. | ||
2 | |||
3 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | |||
19 | /* Written by Paul Eggert. */ | ||
20 | |||
21 | #include <sys/types.h> | ||
22 | |||
23 | int open_safer (char const *, int, ...); | ||
24 | 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 @@ | |||
1 | /* Return a safer copy of a file descriptor. | ||
2 | |||
3 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | |||
19 | /* Written by Paul Eggert. */ | ||
20 | |||
21 | #ifdef HAVE_CONFIG_H | ||
22 | # include <config.h> | ||
23 | #endif | ||
24 | |||
25 | #include "unistd-safer.h" | ||
26 | |||
27 | #include <errno.h> | ||
28 | |||
29 | #include <unistd.h> | ||
30 | #ifndef STDIN_FILENO | ||
31 | # define STDIN_FILENO 0 | ||
32 | #endif | ||
33 | #ifndef STDERR_FILENO | ||
34 | # define STDERR_FILENO 2 | ||
35 | #endif | ||
36 | |||
37 | /* Return FD, unless FD would be a copy of standard input, output, or | ||
38 | error; in that case, return a duplicate of FD, closing FD. On | ||
39 | failure to duplicate, close FD, set errno, and return -1. Preserve | ||
40 | errno if FD is negative, so that the caller can always inspect | ||
41 | errno when the returned value is negative. | ||
42 | |||
43 | This function is usefully wrapped around functions that return file | ||
44 | descriptors, e.g., fd_safer (open ("file", O_RDONLY)). */ | ||
45 | |||
46 | int | ||
47 | fd_safer (int fd) | ||
48 | { | ||
49 | if (STDIN_FILENO <= fd && fd <= STDERR_FILENO) | ||
50 | { | ||
51 | int f = dup_safer (fd); | ||
52 | int e = errno; | ||
53 | close (fd); | ||
54 | errno = e; | ||
55 | fd = f; | ||
56 | } | ||
57 | |||
58 | return fd; | ||
59 | } | ||
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 @@ | |||
1 | /* Invoke open, but avoid some glitches. | ||
2 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
17 | |||
18 | /* Written by Paul Eggert. */ | ||
19 | |||
20 | #ifdef HAVE_CONFIG_H | ||
21 | # include <config.h> | ||
22 | #endif | ||
23 | |||
24 | #include "fcntl-safer.h" | ||
25 | |||
26 | #include <fcntl.h> | ||
27 | #include <stdarg.h> | ||
28 | #include "unistd-safer.h" | ||
29 | |||
30 | int | ||
31 | open_safer (char const *file, int flags, ...) | ||
32 | { | ||
33 | mode_t mode = 0; | ||
34 | |||
35 | if (flags & O_CREAT) | ||
36 | { | ||
37 | va_list ap; | ||
38 | va_start (ap, flags); | ||
39 | |||
40 | /* Assume mode_t promotes to int if and only if it is smaller. | ||
41 | This assumption isn't guaranteed by the C standard, but we | ||
42 | don't know of any real-world counterexamples. */ | ||
43 | mode = (sizeof (mode_t) < sizeof (int) | ||
44 | ? va_arg (ap, int) | ||
45 | : va_arg (ap, mode_t)); | ||
46 | |||
47 | va_end (ap); | ||
48 | } | ||
49 | |||
50 | return fd_safer (open (file, flags, mode)); | ||
51 | } | ||
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 @@ | |||
1 | /* Invoke pipe, but avoid some glitches. | ||
2 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
17 | |||
18 | /* Written by Jim Meyering. */ | ||
19 | |||
20 | #ifdef HAVE_CONFIG_H | ||
21 | # include <config.h> | ||
22 | #endif | ||
23 | |||
24 | #include "unistd-safer.h" | ||
25 | |||
26 | #include <unistd.h> | ||
27 | |||
28 | /* Like pipe, but ensure that neither of the file descriptors is | ||
29 | STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO. */ | ||
30 | |||
31 | int | ||
32 | pipe_safer (int fd[2]) | ||
33 | { | ||
34 | int fail = pipe (fd); | ||
35 | if (fail) | ||
36 | return fail; | ||
37 | |||
38 | { | ||
39 | int i; | ||
40 | for (i = 0; i < 2; i++) | ||
41 | { | ||
42 | int f = fd_safer (fd[i]); | ||
43 | if (f < 0) | ||
44 | return -1; | ||
45 | fd[i] = f; | ||
46 | } | ||
47 | } | ||
48 | |||
49 | return 0; | ||
50 | } | ||
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 @@ | |||
1 | /* Like unistd.h, but redefine some names to avoid glitches. | ||
2 | |||
3 | Copyright (C) 2005 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | |||
19 | /* Written by Paul Eggert. */ | ||
20 | |||
21 | #include <unistd.h> | ||
22 | #include "unistd-safer.h" | ||
23 | |||
24 | #undef dup | ||
25 | #define dup dup_safer | ||
26 | |||
27 | #undef pipe | ||
28 | #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 @@ | |||
1 | /* Invoke unistd-like functions, but avoid some glitches. | ||
2 | |||
3 | Copyright (C) 2001, 2003, 2005 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | |||
19 | /* Written by Paul Eggert. */ | ||
20 | |||
21 | int dup_safer (int); | ||
22 | int fd_safer (int); | ||
23 | int pipe_safer (int[2]); | ||