vdagent: Allow disabling the server-side udscs support
[vd_agent.git] / src / udscs.c
blob334d54aad6e61bf5858d4bd31ebd1f6167d7f813
1 /* udscs.c Unix Domain Socket Client Server framework. A framework for quickly
2 creating select() based servers capable of handling multiple clients and
3 matching select() based clients using variable size messages.
5 Copyright 2010 Red Hat, Inc.
7 Red Hat Authors:
8 Hans de Goede <hdegoede@redhat.com>
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program 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
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <syslog.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include "udscs.h"
36 struct udscs_buf {
37 uint8_t *buf;
38 size_t pos;
39 size_t size;
41 struct udscs_buf *next;
44 struct udscs_connection {
45 int fd;
46 const char * const *type_to_string;
47 int no_types;
48 int debug;
49 void *user_data;
50 #ifndef UDSCS_NO_SERVER
51 struct ucred peer_cred;
52 #endif
54 /* Read stuff, single buffer, separate header and data buffer */
55 int header_read;
56 struct udscs_message_header header;
57 struct udscs_buf data;
59 /* Writes are stored in a linked list of buffers, with both the header
60 + data for a single message in 1 buffer. */
61 struct udscs_buf *write_buf;
63 /* Callbacks */
64 udscs_read_callback read_callback;
65 udscs_disconnect_callback disconnect_callback;
67 struct udscs_connection *next;
68 struct udscs_connection *prev;
71 struct udscs_connection *udscs_connect(const char *socketname,
72 udscs_read_callback read_callback,
73 udscs_disconnect_callback disconnect_callback,
74 const char * const type_to_string[], int no_types, int debug)
76 int c;
77 struct sockaddr_un address;
78 struct udscs_connection *conn;
80 conn = calloc(1, sizeof(*conn));
81 if (!conn)
82 return NULL;
84 conn->type_to_string = type_to_string;
85 conn->no_types = no_types;
86 conn->debug = debug;
88 conn->fd = socket(PF_UNIX, SOCK_STREAM, 0);
89 if (conn->fd == -1) {
90 syslog(LOG_ERR, "creating unix domain socket: %m");
91 free(conn);
92 return NULL;
95 address.sun_family = AF_UNIX;
96 snprintf(address.sun_path, sizeof(address.sun_path), "%s", socketname);
97 c = connect(conn->fd, (struct sockaddr *)&address, sizeof(address));
98 if (c != 0) {
99 if (conn->debug) {
100 syslog(LOG_DEBUG, "connect %s: %m", socketname);
102 free(conn);
103 return NULL;
106 conn->read_callback = read_callback;
107 conn->disconnect_callback = disconnect_callback;
109 if (conn->debug)
110 syslog(LOG_DEBUG, "%p connected to %s", conn, socketname);
112 return conn;
115 void udscs_destroy_connection(struct udscs_connection **connp)
117 struct udscs_buf *wbuf, *next_wbuf;
118 struct udscs_connection *conn = *connp;
120 if (!conn)
121 return;
123 if (conn->disconnect_callback)
124 conn->disconnect_callback(conn);
126 wbuf = conn->write_buf;
127 while (wbuf) {
128 next_wbuf = wbuf->next;
129 free(wbuf->buf);
130 free(wbuf);
131 wbuf = next_wbuf;
134 free(conn->data.buf);
136 if (conn->next)
137 conn->next->prev = conn->prev;
138 if (conn->prev)
139 conn->prev->next = conn->next;
141 close(conn->fd);
143 if (conn->debug)
144 syslog(LOG_DEBUG, "%p disconnected", conn);
146 free(conn);
147 *connp = NULL;
150 void udscs_set_user_data(struct udscs_connection *conn, void *data)
152 conn->user_data = data;
155 void *udscs_get_user_data(struct udscs_connection *conn)
157 if (!conn)
158 return NULL;
160 return conn->user_data;
163 int udscs_write(struct udscs_connection *conn, uint32_t type, uint32_t arg1,
164 uint32_t arg2, const uint8_t *data, uint32_t size)
166 struct udscs_buf *wbuf, *new_wbuf;
167 struct udscs_message_header header;
169 new_wbuf = malloc(sizeof(*new_wbuf));
170 if (!new_wbuf)
171 return -1;
173 new_wbuf->pos = 0;
174 new_wbuf->size = sizeof(header) + size;
175 new_wbuf->next = NULL;
176 new_wbuf->buf = malloc(new_wbuf->size);
177 if (!new_wbuf->buf) {
178 free(new_wbuf);
179 return -1;
182 header.type = type;
183 header.arg1 = arg1;
184 header.arg2 = arg2;
185 header.size = size;
187 memcpy(new_wbuf->buf, &header, sizeof(header));
188 memcpy(new_wbuf->buf + sizeof(header), data, size);
190 if (conn->debug) {
191 if (type < conn->no_types)
192 syslog(LOG_DEBUG, "%p sent %s, arg1: %u, arg2: %u, size %u",
193 conn, conn->type_to_string[type], arg1, arg2, size);
194 else
195 syslog(LOG_DEBUG,
196 "%p sent invalid message %u, arg1: %u, arg2: %u, size %u",
197 conn, type, arg1, arg2, size);
200 if (!conn->write_buf) {
201 conn->write_buf = new_wbuf;
202 return 0;
205 /* maybe we should limit the write_buf stack depth ? */
206 wbuf = conn->write_buf;
207 while (wbuf->next)
208 wbuf = wbuf->next;
210 wbuf->next = new_wbuf;
212 return 0;
215 /* A helper for udscs_do_read() */
216 static void udscs_read_complete(struct udscs_connection **connp)
218 struct udscs_connection *conn = *connp;
220 if (conn->debug) {
221 if (conn->header.type < conn->no_types)
222 syslog(LOG_DEBUG,
223 "%p received %s, arg1: %u, arg2: %u, size %u",
224 conn, conn->type_to_string[conn->header.type],
225 conn->header.arg1, conn->header.arg2, conn->header.size);
226 else
227 syslog(LOG_DEBUG,
228 "%p received invalid message %u, arg1: %u, arg2: %u, size %u",
229 conn, conn->header.type, conn->header.arg1, conn->header.arg2,
230 conn->header.size);
233 if (conn->read_callback) {
234 conn->read_callback(connp, &conn->header, conn->data.buf);
235 if (!*connp) /* Was the connection disconnected by the callback ? */
236 return;
239 conn->header_read = 0;
240 memset(&conn->data, 0, sizeof(conn->data));
243 /* A helper for udscs_client_handle_fds() */
244 static void udscs_do_read(struct udscs_connection **connp)
246 ssize_t n;
247 size_t to_read;
248 uint8_t *dest;
249 struct udscs_connection *conn = *connp;
251 if (conn->header_read < sizeof(conn->header)) {
252 to_read = sizeof(conn->header) - conn->header_read;
253 dest = (uint8_t *)&conn->header + conn->header_read;
254 } else {
255 to_read = conn->data.size - conn->data.pos;
256 dest = conn->data.buf + conn->data.pos;
259 n = read(conn->fd, dest, to_read);
260 if (n < 0) {
261 if (errno == EINTR)
262 return;
263 syslog(LOG_ERR, "reading unix domain socket: %m, disconnecting %p",
264 conn);
266 if (n <= 0) {
267 udscs_destroy_connection(connp);
268 return;
271 if (conn->header_read < sizeof(conn->header)) {
272 conn->header_read += n;
273 if (conn->header_read == sizeof(conn->header)) {
274 if (conn->header.size == 0) {
275 udscs_read_complete(connp);
276 return;
278 conn->data.pos = 0;
279 conn->data.size = conn->header.size;
280 conn->data.buf = malloc(conn->data.size);
281 if (!conn->data.buf) {
282 syslog(LOG_ERR, "out of memory, disconnecting %p", conn);
283 udscs_destroy_connection(connp);
284 return;
287 } else {
288 conn->data.pos += n;
289 if (conn->data.pos == conn->data.size)
290 udscs_read_complete(connp);
294 /* A helper for udscs_client_handle_fds() */
295 static void udscs_do_write(struct udscs_connection **connp)
297 ssize_t n;
298 size_t to_write;
299 struct udscs_connection *conn = *connp;
301 struct udscs_buf* wbuf = conn->write_buf;
302 if (!wbuf) {
303 syslog(LOG_ERR,
304 "%p do_write called on a connection without a write buf ?!",
305 conn);
306 return;
309 to_write = wbuf->size - wbuf->pos;
310 n = write(conn->fd, wbuf->buf + wbuf->pos, to_write);
311 if (n < 0) {
312 if (errno == EINTR)
313 return;
314 syslog(LOG_ERR, "writing to unix domain socket: %m, disconnecting %p",
315 conn);
316 udscs_destroy_connection(connp);
317 return;
320 wbuf->pos += n;
321 if (wbuf->pos == wbuf->size) {
322 conn->write_buf = wbuf->next;
323 free(wbuf->buf);
324 free(wbuf);
328 void udscs_client_handle_fds(struct udscs_connection **connp, fd_set *readfds,
329 fd_set *writefds)
331 if (!*connp)
332 return;
334 if (FD_ISSET((*connp)->fd, readfds))
335 udscs_do_read(connp);
337 if (*connp && FD_ISSET((*connp)->fd, writefds))
338 udscs_do_write(connp);
341 int udscs_client_fill_fds(struct udscs_connection *conn, fd_set *readfds,
342 fd_set *writefds)
344 if (!conn)
345 return -1;
347 FD_SET(conn->fd, readfds);
348 if (conn->write_buf)
349 FD_SET(conn->fd, writefds);
351 return conn->fd + 1;
355 #ifndef UDSCS_NO_SERVER
357 /* ---------- Server-side implementation ---------- */
359 struct udscs_server {
360 int fd;
361 const char * const *type_to_string;
362 int no_types;
363 int debug;
364 struct udscs_connection connections_head;
365 udscs_connect_callback connect_callback;
366 udscs_read_callback read_callback;
367 udscs_disconnect_callback disconnect_callback;
370 struct udscs_server *udscs_create_server(const char *socketname,
371 udscs_connect_callback connect_callback,
372 udscs_read_callback read_callback,
373 udscs_disconnect_callback disconnect_callback,
374 const char * const type_to_string[], int no_types, int debug)
376 int c;
377 struct sockaddr_un address;
378 struct udscs_server *server;
380 server = calloc(1, sizeof(*server));
381 if (!server)
382 return NULL;
384 server->type_to_string = type_to_string;
385 server->no_types = no_types;
386 server->debug = debug;
388 server->fd = socket(PF_UNIX, SOCK_STREAM, 0);
389 if (server->fd == -1) {
390 syslog(LOG_ERR, "creating unix domain socket: %m");
391 free(server);
392 return NULL;
395 address.sun_family = AF_UNIX;
396 snprintf(address.sun_path, sizeof(address.sun_path), "%s", socketname);
397 c = bind(server->fd, (struct sockaddr *)&address, sizeof(address));
398 if (c != 0) {
399 syslog(LOG_ERR, "bind %s: %m", socketname);
400 free(server);
401 return NULL;
404 c = listen(server->fd, 5);
405 if (c != 0) {
406 syslog(LOG_ERR, "listen: %m");
407 free(server);
408 return NULL;
411 server->connect_callback = connect_callback;
412 server->read_callback = read_callback;
413 server->disconnect_callback = disconnect_callback;
415 return server;
418 void udscs_destroy_server(struct udscs_server *server)
420 struct udscs_connection *conn, *next_conn;
422 if (!server)
423 return;
425 conn = server->connections_head.next;
426 while (conn) {
427 next_conn = conn->next;
428 udscs_destroy_connection(&conn);
429 conn = next_conn;
431 close(server->fd);
432 free(server);
435 struct ucred udscs_get_peer_cred(struct udscs_connection *conn)
437 return conn->peer_cred;
440 static void udscs_server_accept(struct udscs_server *server) {
441 struct udscs_connection *new_conn, *conn;
442 struct sockaddr_un address;
443 socklen_t length = sizeof(address);
444 int r, fd;
446 fd = accept(server->fd, (struct sockaddr *)&address, &length);
447 if (fd == -1) {
448 if (errno == EINTR)
449 return;
450 syslog(LOG_ERR, "accept: %m");
451 return;
454 new_conn = calloc(1, sizeof(*conn));
455 if (!new_conn) {
456 syslog(LOG_ERR, "out of memory, disconnecting new client");
457 close(fd);
458 return;
461 new_conn->fd = fd;
462 new_conn->type_to_string = server->type_to_string;
463 new_conn->no_types = server->no_types;
464 new_conn->debug = server->debug;
465 new_conn->read_callback = server->read_callback;
466 new_conn->disconnect_callback = server->disconnect_callback;
468 length = sizeof(new_conn->peer_cred);
469 r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &new_conn->peer_cred, &length);
470 if (r != 0) {
471 syslog(LOG_ERR, "Could not get peercred, disconnecting new client");
472 close(fd);
473 free(new_conn);
474 return;
477 conn = &server->connections_head;
478 while (conn->next)
479 conn = conn->next;
481 new_conn->prev = conn;
482 conn->next = new_conn;
484 if (server->debug)
485 syslog(LOG_DEBUG, "new client accepted: %p, pid: %d",
486 new_conn, (int)new_conn->peer_cred.pid);
488 if (server->connect_callback)
489 server->connect_callback(new_conn);
492 int udscs_server_fill_fds(struct udscs_server *server, fd_set *readfds,
493 fd_set *writefds)
495 struct udscs_connection *conn;
496 int nfds = server->fd + 1;
498 if (!server)
499 return -1;
501 FD_SET(server->fd, readfds);
503 conn = server->connections_head.next;
504 while (conn) {
505 int conn_nfds = udscs_client_fill_fds(conn, readfds, writefds);
506 if (conn_nfds > nfds)
507 nfds = conn_nfds;
509 conn = conn->next;
512 return nfds;
515 void udscs_server_handle_fds(struct udscs_server *server, fd_set *readfds,
516 fd_set *writefds)
518 struct udscs_connection *conn, *next_conn;
520 if (!server)
521 return;
523 if (FD_ISSET(server->fd, readfds))
524 udscs_server_accept(server);
526 conn = server->connections_head.next;
527 while (conn) {
528 /* conn maybe destroyed by udscs_client_handle_fds (when disconnected),
529 so get the next connection first. */
530 next_conn = conn->next;
531 udscs_client_handle_fds(&conn, readfds, writefds);
532 conn = next_conn;
536 int udscs_server_write_all(struct udscs_server *server,
537 uint32_t type, uint32_t arg1, uint32_t arg2,
538 const uint8_t *data, uint32_t size)
540 struct udscs_connection *conn;
542 conn = server->connections_head.next;
543 while (conn) {
544 if (udscs_write(conn, type, arg1, arg2, data, size))
545 return -1;
546 conn = conn->next;
549 return 0;
552 int udscs_server_for_all_clients(struct udscs_server *server,
553 udscs_for_all_clients_callback func, void *priv)
555 int r = 0;
556 struct udscs_connection *conn, *next_conn;
558 if (!server)
559 return 0;
561 conn = server->connections_head.next;
562 while (conn) {
563 /* Get next conn as func may destroy the current conn */
564 next_conn = conn->next;
565 r += func(&conn, priv);
566 conn = next_conn;
568 return r;
571 #endif