Cause cli_close to return an NTSTATUS.
[Samba/ekacnet.git] / lib / tsocket / tsocket_helpers.c
blobb2edf43d97fff481947c4f659154c0f76da59813
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tevent
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "replace.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "tsocket.h"
28 #include "tsocket_internal.h"
30 int tsocket_error_from_errno(int ret,
31 int sys_errno,
32 bool *retry)
34 *retry = false;
36 if (ret >= 0) {
37 return 0;
40 if (ret != -1) {
41 return EIO;
44 if (sys_errno == 0) {
45 return EIO;
48 if (sys_errno == EINTR) {
49 *retry = true;
50 return sys_errno;
53 if (sys_errno == EINPROGRESS) {
54 *retry = true;
55 return sys_errno;
58 if (sys_errno == EAGAIN) {
59 *retry = true;
60 return sys_errno;
63 #ifdef EWOULDBLOCK
64 if (sys_errno == EWOULDBLOCK) {
65 *retry = true;
66 return sys_errno;
68 #endif
70 return sys_errno;
73 int tsocket_simple_int_recv(struct tevent_req *req, int *perrno)
75 enum tevent_req_state state;
76 uint64_t error;
78 if (!tevent_req_is_error(req, &state, &error)) {
79 return 0;
82 switch (state) {
83 case TEVENT_REQ_NO_MEMORY:
84 *perrno = ENOMEM;
85 return -1;
86 case TEVENT_REQ_TIMED_OUT:
87 *perrno = ETIMEDOUT;
88 return -1;
89 case TEVENT_REQ_USER_ERROR:
90 *perrno = (int)error;
91 return -1;
92 default:
93 *perrno = EIO;
94 return -1;
97 *perrno = EIO;
98 return -1;
101 int tsocket_common_prepare_fd(int fd, bool high_fd)
103 int i;
104 int sys_errno = 0;
105 int fds[3];
106 int num_fds = 0;
108 int result, flags;
110 if (fd == -1) {
111 return -1;
114 /* first make a fd >= 3 */
115 if (high_fd) {
116 while (fd < 3) {
117 fds[num_fds++] = fd;
118 fd = dup(fd);
119 if (fd == -1) {
120 sys_errno = errno;
121 break;
124 for (i=0; i<num_fds; i++) {
125 close(fds[i]);
127 if (fd == -1) {
128 errno = sys_errno;
129 return fd;
133 /* fd should be nonblocking. */
135 #ifdef O_NONBLOCK
136 #define FLAG_TO_SET O_NONBLOCK
137 #else
138 #ifdef SYSV
139 #define FLAG_TO_SET O_NDELAY
140 #else /* BSD */
141 #define FLAG_TO_SET FNDELAY
142 #endif
143 #endif
145 if ((flags = fcntl(fd, F_GETFL)) == -1) {
146 goto fail;
149 flags |= FLAG_TO_SET;
150 if (fcntl(fd, F_SETFL, flags) == -1) {
151 goto fail;
154 #undef FLAG_TO_SET
156 /* fd should be closed on exec() */
157 #ifdef FD_CLOEXEC
158 result = flags = fcntl(fd, F_GETFD, 0);
159 if (flags >= 0) {
160 flags |= FD_CLOEXEC;
161 result = fcntl(fd, F_SETFD, flags);
163 if (result < 0) {
164 goto fail;
166 #endif
167 return fd;
169 fail:
170 if (fd != -1) {
171 sys_errno = errno;
172 close(fd);
173 errno = sys_errno;
175 return -1;