/src/samba/lib/tevent/tevent_util.c
Line | Count | Source |
1 | | /* |
2 | | Unix SMB/CIFS implementation. |
3 | | |
4 | | Copyright (C) Andrew Tridgell 2005 |
5 | | Copyright (C) Jelmer Vernooij 2005 |
6 | | |
7 | | ** NOTE! The following LGPL license applies to the tevent |
8 | | ** library. This does NOT imply that all of Samba is released |
9 | | ** under the LGPL |
10 | | |
11 | | This library is free software; you can redistribute it and/or |
12 | | modify it under the terms of the GNU Lesser General Public |
13 | | License as published by the Free Software Foundation; either |
14 | | version 3 of the License, or (at your option) any later version. |
15 | | |
16 | | This library 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 GNU |
19 | | Lesser General Public License for more details. |
20 | | |
21 | | You should have received a copy of the GNU Lesser General Public |
22 | | License along with this library; if not, see <http://www.gnu.org/licenses/>. |
23 | | */ |
24 | | |
25 | | #include "replace.h" |
26 | | #include "talloc.h" |
27 | | #include "tevent.h" |
28 | | #include "tevent_internal.h" |
29 | | #include "tevent_util.h" |
30 | | #include <fcntl.h> |
31 | | |
32 | | /** |
33 | | Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available, |
34 | | else |
35 | | if SYSV use O_NDELAY |
36 | | if BSD use FNDELAY |
37 | | **/ |
38 | | |
39 | | int ev_set_blocking(int fd, bool set) |
40 | 0 | { |
41 | 0 | int val; |
42 | 0 | #ifdef O_NONBLOCK |
43 | 0 | #define FLAG_TO_SET O_NONBLOCK |
44 | | #else |
45 | | #ifdef SYSV |
46 | | #define FLAG_TO_SET O_NDELAY |
47 | | #else /* BSD */ |
48 | | #define FLAG_TO_SET FNDELAY |
49 | | #endif |
50 | | #endif |
51 | |
|
52 | 0 | if((val = fcntl(fd, F_GETFL, 0)) == -1) |
53 | 0 | return -1; |
54 | 0 | if(set) /* Turn blocking on - ie. clear nonblock flag */ |
55 | 0 | val &= ~FLAG_TO_SET; |
56 | 0 | else |
57 | 0 | val |= FLAG_TO_SET; |
58 | 0 | return fcntl( fd, F_SETFL, val); |
59 | 0 | #undef FLAG_TO_SET |
60 | 0 | } |
61 | | |
62 | | bool ev_set_close_on_exec(int fd) |
63 | 0 | { |
64 | 0 | #ifdef FD_CLOEXEC |
65 | 0 | int val; |
66 | |
|
67 | 0 | val = fcntl(fd, F_GETFD, 0); |
68 | 0 | if (val >= 0) { |
69 | 0 | val |= FD_CLOEXEC; |
70 | 0 | val = fcntl(fd, F_SETFD, val); |
71 | 0 | if (val != -1) { |
72 | 0 | return true; |
73 | 0 | } |
74 | 0 | } |
75 | 0 | #endif |
76 | 0 | return false; |
77 | 0 | } |