Line data Source code
1 : /*
2 : SSSD
3 :
4 : io.c
5 :
6 : Authors:
7 : Lukas Slebodnik <lslebodn@redhat.com>
8 :
9 : Copyright (C) 2012 Red Hat
10 :
11 : This program is free software; you can redistribute it and/or modify
12 : it under the terms of the GNU General Public License as published by
13 : the Free Software Foundation; either version 3 of the License, or
14 : (at your option) any later version.
15 :
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 :
21 : You should have received a copy of the GNU General Public License
22 : along with this program. If not, see <http://www.gnu.org/licenses/>.
23 : */
24 :
25 : #include "config.h"
26 :
27 : #include <unistd.h>
28 : #include <fcntl.h>
29 : #include <errno.h>
30 :
31 : #include "util/io.h"
32 :
33 : /* CAUTION:
34 : * This file have to be minimalist and cannot include DEBUG macros
35 : * or header file util.h.
36 : */
37 :
38 136 : int sss_open_cloexec(const char *pathname, int flags, int *ret)
39 : {
40 : int fd;
41 : int oflags;
42 :
43 136 : oflags = flags;
44 : #ifdef O_CLOEXEC
45 136 : oflags |= O_CLOEXEC;
46 : #endif
47 :
48 136 : errno = 0;
49 136 : fd = open(pathname, oflags);
50 136 : if (fd == -1) {
51 131 : if (ret) {
52 131 : *ret = errno;
53 : }
54 131 : return -1;
55 : }
56 :
57 : #ifndef O_CLOEXEC
58 : int v;
59 :
60 : v = fcntl(fd, F_GETFD, 0);
61 : /* we ignore an error, it's not fatal and there is nothing we
62 : * can do about it anyways */
63 : (void)fcntl(fd, F_SETFD, v | FD_CLOEXEC);
64 : #endif
65 :
66 5 : return fd;
67 : }
68 :
69 15 : int sss_openat_cloexec(int dir_fd, const char *pathname, int flags, int *ret)
70 : {
71 : int fd;
72 : int oflags;
73 :
74 15 : oflags = flags;
75 : #ifdef O_CLOEXEC
76 15 : oflags |= O_CLOEXEC;
77 : #endif
78 :
79 15 : errno = 0;
80 15 : fd = openat(dir_fd, pathname, oflags);
81 15 : if (fd == -1) {
82 2 : if (ret) {
83 2 : *ret = errno;
84 : }
85 2 : return -1;
86 : }
87 :
88 : #ifndef O_CLOEXEC
89 : int v;
90 :
91 : v = fcntl(fd, F_GETFD, 0);
92 : /* we ignore an error, it's not fatal and there is nothing we
93 : * can do about it anyways */
94 : (void)fcntl(fd, F_SETFD, v | FD_CLOEXEC);
95 : #endif
96 :
97 13 : return fd;
98 : }
|