auth/gensec: add some useful debugging to gensec_update_send/gensec_update_done
[Samba.git] / ctdb / common / sock_io.c
blob3f7138f0b7f59ec2cdb7b2668d239389819b5018
1 /*
2 Generic Unix-domain Socket I/O
4 Copyright (C) Amitay Isaacs 2016
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/util/sys_rw.h"
28 #include "lib/util/debug.h"
29 #include "lib/util/blocking.h"
31 #include "common/logging.h"
32 #include "common/sock_io.h"
34 bool sock_clean(const char *sockpath)
36 int ret;
38 ret = unlink(sockpath);
39 if (ret == 0) {
40 D_WARNING("Removed stale socket %s\n", sockpath);
41 } else if (errno != ENOENT) {
42 D_ERR("Failed to remove stale socket %s\n", sockpath);
43 return false;
46 return true;
49 int sock_connect(const char *sockpath)
51 struct sockaddr_un addr;
52 size_t len;
53 int fd, ret;
55 if (sockpath == NULL) {
56 D_ERR("Invalid socket path\n");
57 return -1;
60 memset(&addr, 0, sizeof(addr));
61 addr.sun_family = AF_UNIX;
62 len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
63 if (len >= sizeof(addr.sun_path)) {
64 D_ERR("Socket path too long, len=%zu\n", strlen(sockpath));
65 return -1;
68 fd = socket(AF_UNIX, SOCK_STREAM, 0);
69 if (fd == -1) {
70 D_ERR("socket() failed, errno=%d\n", errno);
71 return -1;
74 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
75 if (ret == -1) {
76 D_ERR("connect() failed, errno=%d\n", errno);
77 close(fd);
78 return -1;
81 return fd;
84 struct sock_queue {
85 struct tevent_context *ev;
86 sock_queue_callback_fn_t callback;
87 void *private_data;
88 int fd;
90 struct tevent_immediate *im;
91 struct tevent_queue *queue;
92 struct tevent_fd *fde;
93 uint8_t *buf;
94 size_t buflen, begin, end;
97 static bool sock_queue_set_fd(struct sock_queue *queue, int fd);
98 static int sock_queue_destructor(struct sock_queue *queue);
99 static void sock_queue_handler(struct tevent_context *ev,
100 struct tevent_fd *fde, uint16_t flags,
101 void *private_data);
102 static void sock_queue_process(struct sock_queue *queue);
103 static void sock_queue_process_event(struct tevent_context *ev,
104 struct tevent_immediate *im,
105 void *private_data);
107 struct sock_queue *sock_queue_setup(TALLOC_CTX *mem_ctx,
108 struct tevent_context *ev,
109 int fd,
110 sock_queue_callback_fn_t callback,
111 void *private_data)
113 struct sock_queue *queue;
115 queue = talloc_zero(mem_ctx, struct sock_queue);
116 if (queue == NULL) {
117 return NULL;
120 queue->ev = ev;
121 queue->callback = callback;
122 queue->private_data = private_data;
124 queue->im = tevent_create_immediate(queue);
125 if (queue->im == NULL) {
126 talloc_free(queue);
127 return NULL;
130 queue->queue = tevent_queue_create(queue, "out-queue");
131 if (queue->queue == NULL) {
132 talloc_free(queue);
133 return NULL;
136 if (! sock_queue_set_fd(queue, fd)) {
137 talloc_free(queue);
138 return NULL;
141 talloc_set_destructor(queue, sock_queue_destructor);
143 return queue;
146 static bool sock_queue_set_fd(struct sock_queue *queue, int fd)
148 TALLOC_FREE(queue->fde);
149 queue->fd = fd;
151 if (fd != -1) {
152 int ret;
154 ret = set_blocking(fd, false);
155 if (ret != 0) {
156 return false;
159 queue->fde = tevent_add_fd(queue->ev, queue, fd,
160 TEVENT_FD_READ,
161 sock_queue_handler, queue);
162 if (queue->fde == NULL) {
163 return false;
165 tevent_fd_set_auto_close(queue->fde);
168 return true;
171 static int sock_queue_destructor(struct sock_queue *queue)
173 TALLOC_FREE(queue->fde);
174 queue->fd = -1;
176 return 0;
179 static void sock_queue_handler(struct tevent_context *ev,
180 struct tevent_fd *fde, uint16_t flags,
181 void *private_data)
183 struct sock_queue *queue = talloc_get_type_abort(
184 private_data, struct sock_queue);
185 int ret, num_ready;
186 ssize_t nread;
188 ret = ioctl(queue->fd, FIONREAD, &num_ready);
189 if (ret != 0) {
190 /* Ignore */
191 return;
194 if (num_ready == 0) {
195 /* descriptor has been closed */
196 goto fail;
199 if (num_ready > queue->buflen - queue->end) {
200 queue->buf = talloc_realloc_size(queue, queue->buf,
201 queue->end + num_ready);
202 if (queue->buf == NULL) {
203 goto fail;
205 queue->buflen = queue->end + num_ready;
208 nread = sys_read(queue->fd, queue->buf + queue->end, num_ready);
209 if (nread < 0) {
210 goto fail;
212 queue->end += nread;
214 sock_queue_process(queue);
215 return;
217 fail:
218 queue->callback(NULL, 0, queue->private_data);
221 static void sock_queue_process(struct sock_queue *queue)
223 uint32_t pkt_size;
225 if ((queue->end - queue->begin) < sizeof(uint32_t)) {
226 /* not enough data */
227 return;
230 pkt_size = *(uint32_t *)(queue->buf + queue->begin);
231 if (pkt_size == 0) {
232 D_ERR("Invalid packet of length 0\n");
233 queue->callback(NULL, 0, queue->private_data);
236 if ((queue->end - queue->begin) < pkt_size) {
237 /* not enough data */
238 return;
241 queue->callback(queue->buf + queue->begin, pkt_size,
242 queue->private_data);
243 queue->begin += pkt_size;
245 if (queue->begin < queue->end) {
246 /* more data to be processed */
247 tevent_schedule_immediate(queue->im, queue->ev,
248 sock_queue_process_event, queue);
249 } else {
250 TALLOC_FREE(queue->buf);
251 queue->buflen = 0;
252 queue->begin = 0;
253 queue->end = 0;
257 static void sock_queue_process_event(struct tevent_context *ev,
258 struct tevent_immediate *im,
259 void *private_data)
261 struct sock_queue *queue = talloc_get_type_abort(
262 private_data, struct sock_queue);
264 sock_queue_process(queue);
267 struct sock_queue_write_state {
268 uint8_t *pkt;
269 uint32_t pkt_size;
272 static void sock_queue_trigger(struct tevent_req *req, void *private_data);
274 int sock_queue_write(struct sock_queue *queue, uint8_t *buf, size_t buflen)
276 struct tevent_req *req;
277 struct sock_queue_write_state *state;
278 bool status;
280 if (buflen >= INT32_MAX) {
281 return -1;
284 req = tevent_req_create(queue, &state, struct sock_queue_write_state);
285 if (req == NULL) {
286 return -1;
289 state->pkt = buf;
290 state->pkt_size = (uint32_t)buflen;
292 status = tevent_queue_add_entry(queue->queue, queue->ev, req,
293 sock_queue_trigger, queue);
294 if (! status) {
295 talloc_free(req);
296 return -1;
299 return 0;
302 static void sock_queue_trigger(struct tevent_req *req, void *private_data)
304 struct sock_queue *queue = talloc_get_type_abort(
305 private_data, struct sock_queue);
306 struct sock_queue_write_state *state = tevent_req_data(
307 req, struct sock_queue_write_state);
308 size_t offset = 0;
310 do {
311 ssize_t nwritten;
313 nwritten = sys_write(queue->fd, state->pkt + offset,
314 state->pkt_size - offset);
315 if (nwritten < 0) {
316 queue->callback(NULL, 0, queue->private_data);
317 return;
319 offset += nwritten;
321 } while (offset < state->pkt_size);
323 tevent_req_done(req);
324 talloc_free(req);