]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/ctl-server.c
Add support to read /etc/os-release for system information.
[thirdparty/lldpd.git] / src / ctl-server.c
1 /*
2 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "lldpd.h"
18
19 #include <unistd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24
25 static void
26 ctl_callback(struct lldpd *cfg, struct lldpd_callback *callback)
27 {
28 char *buffer;
29 int n;
30
31 if ((buffer = (char *)malloc(MAX_HMSGSIZE)) ==
32 NULL) {
33 LLOG_WARN("failed to alloc reception buffer");
34 return;
35 }
36 if ((n = recv(callback->fd, buffer,
37 MAX_HMSGSIZE, 0)) == -1) {
38 LLOG_WARN("error while receiving message");
39 free(buffer);
40 return;
41 }
42 if (n > 0)
43 client_handle_client(cfg, callback, buffer, n);
44 else {
45 close(callback->fd);
46 lldpd_callback_del(cfg, callback->fd, ctl_callback);
47 }
48 free(buffer);
49 }
50
51 void
52 ctl_accept(struct lldpd *cfg, struct lldpd_callback *callback)
53 {
54 int s;
55 if ((s = accept(callback->fd, NULL, NULL)) == -1) {
56 LLOG_WARN("unable to accept connection from socket");
57 return;
58 }
59 if (lldpd_callback_add(cfg, s, ctl_callback, NULL) != 0) {
60 LLOG_WARN("unable to add callback for new client");
61 close(s);
62 }
63 return;
64 }