2 fb1a36c0 2022-01-09 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/types.h>
20 fb1a36c0 2022-01-09 op #include <sys/stat.h>
21 fb1a36c0 2022-01-09 op #include <sys/socket.h>
22 fb1a36c0 2022-01-09 op #include <sys/uio.h>
23 fb1a36c0 2022-01-09 op #include <sys/un.h>
25 fb1a36c0 2022-01-09 op #include <netinet/in.h>
26 fb1a36c0 2022-01-09 op #include <net/if.h>
28 fb1a36c0 2022-01-09 op #include <errno.h>
29 b5c4cc83 2022-10-13 op #include <fcntl.h>
30 fb1a36c0 2022-01-09 op #include <stdint.h>
31 fb1a36c0 2022-01-09 op #include <stdlib.h>
32 fb1a36c0 2022-01-09 op #include <string.h>
33 fb1a36c0 2022-01-09 op #include <unistd.h>
35 fb1a36c0 2022-01-09 op #include "control.h"
36 fb1a36c0 2022-01-09 op #include "kamid.h"
37 fb1a36c0 2022-01-09 op #include "log.h"
38 fb1a36c0 2022-01-09 op #include "utils.h"
40 fb1a36c0 2022-01-09 op #define CONTROL_BACKLOG 5
42 5c69b561 2022-01-29 op uint32_t peeridcnt;
45 fb1a36c0 2022-01-09 op struct event ev;
46 fb1a36c0 2022-01-09 op struct event evt;
48 fb1a36c0 2022-01-09 op } control_state = {.fd = -1};
50 fb1a36c0 2022-01-09 op struct ctl_conn {
51 fb1a36c0 2022-01-09 op TAILQ_ENTRY(ctl_conn) entry;
52 5c69b561 2022-01-29 op uint32_t peerid;
53 fb1a36c0 2022-01-09 op struct imsgev iev;
56 fb1a36c0 2022-01-09 op TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
58 fb1a36c0 2022-01-09 op struct ctl_conn *control_connbyfd(int);
59 5c69b561 2022-01-29 op struct ctl_conn *control_connbypeer(uint32_t);
60 fb1a36c0 2022-01-09 op struct ctl_conn *control_connbypid(pid_t);
61 fb1a36c0 2022-01-09 op void control_close(int);
64 fb1a36c0 2022-01-09 op control_init(const char *path)
66 fb1a36c0 2022-01-09 op struct sockaddr_un sun;
68 fb1a36c0 2022-01-09 op mode_t old_umask;
70 fb1a36c0 2022-01-09 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
72 fb1a36c0 2022-01-09 op log_warn("%s: socket", __func__);
76 fb1a36c0 2022-01-09 op memset(&sun, 0, sizeof(sun));
77 fb1a36c0 2022-01-09 op sun.sun_family = AF_UNIX;
78 fb1a36c0 2022-01-09 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
80 fb1a36c0 2022-01-09 op if (unlink(path) == -1)
81 fb1a36c0 2022-01-09 op if (errno != ENOENT) {
82 fb1a36c0 2022-01-09 op log_warn("%s: unlink %s", __func__, path);
87 fb1a36c0 2022-01-09 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
88 fb1a36c0 2022-01-09 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
89 fb1a36c0 2022-01-09 op log_warn("%s: bind: %s", __func__, path);
91 fb1a36c0 2022-01-09 op umask(old_umask);
94 fb1a36c0 2022-01-09 op umask(old_umask);
96 fb1a36c0 2022-01-09 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
97 fb1a36c0 2022-01-09 op log_warn("%s: chmod", __func__);
99 fb1a36c0 2022-01-09 op (void)unlink(path);
107 fb1a36c0 2022-01-09 op control_listen(int fd)
109 fb1a36c0 2022-01-09 op if (control_state.fd != -1)
110 fb1a36c0 2022-01-09 op fatalx("%s: received unexpected controlsock", __func__);
112 fb1a36c0 2022-01-09 op control_state.fd = fd;
113 fb1a36c0 2022-01-09 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
114 fb1a36c0 2022-01-09 op log_warn("%s: listen", __func__);
118 fb1a36c0 2022-01-09 op event_set(&control_state.ev, control_state.fd, EV_READ,
119 fb1a36c0 2022-01-09 op control_accept, NULL);
120 fb1a36c0 2022-01-09 op event_add(&control_state.ev, NULL);
121 fb1a36c0 2022-01-09 op evtimer_set(&control_state.evt, control_accept, NULL);
127 fb1a36c0 2022-01-09 op control_accept(int listenfd, short event, void *bula)
129 b5c4cc83 2022-10-13 op int connfd, flags;
130 fb1a36c0 2022-01-09 op socklen_t len;
131 fb1a36c0 2022-01-09 op struct sockaddr_un sun;
132 fb1a36c0 2022-01-09 op struct ctl_conn *c;
134 fb1a36c0 2022-01-09 op event_add(&control_state.ev, NULL);
135 fb1a36c0 2022-01-09 op if ((event & EV_TIMEOUT))
138 fb1a36c0 2022-01-09 op len = sizeof(sun);
139 b5c4cc83 2022-10-13 op if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
141 fb1a36c0 2022-01-09 op * Pause accept if we are out of file descriptors, or
142 fb1a36c0 2022-01-09 op * libevent will haunt us here too.
144 fb1a36c0 2022-01-09 op if (errno == ENFILE || errno == EMFILE) {
145 fb1a36c0 2022-01-09 op struct timeval evtpause = { 1, 0 };
147 fb1a36c0 2022-01-09 op event_del(&control_state.ev);
148 fb1a36c0 2022-01-09 op evtimer_add(&control_state.evt, &evtpause);
149 fb1a36c0 2022-01-09 op } else if (errno != EWOULDBLOCK && errno != EINTR &&
150 fb1a36c0 2022-01-09 op errno != ECONNABORTED)
151 fb1a36c0 2022-01-09 op log_warn("%s: accept4", __func__);
155 b5c4cc83 2022-10-13 op if ((flags = fcntl(connfd, F_GETFL)) == -1) {
156 b5c4cc83 2022-10-13 op log_warn("%s: fcntl F_GETFL", __func__);
157 b5c4cc83 2022-10-13 op close(connfd);
160 b5c4cc83 2022-10-13 op flags |= O_NONBLOCK;
161 b5c4cc83 2022-10-13 op if (fcntl(connfd, F_SETFL, flags) == -1 ||
162 b5c4cc83 2022-10-13 op fcntl(connfd, F_SETFD, FD_CLOEXEC) == -1) {
163 b5c4cc83 2022-10-13 op log_warn("%s: can't set nonblock/cloexec", __func__);
164 b5c4cc83 2022-10-13 op close(connfd);
168 fb1a36c0 2022-01-09 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
169 fb1a36c0 2022-01-09 op log_warn("%s: calloc", __func__);
170 fb1a36c0 2022-01-09 op close(connfd);
174 5c69b561 2022-01-29 op c->peerid = peeridcnt++;
175 fb1a36c0 2022-01-09 op imsg_init(&c->iev.ibuf, connfd);
176 fb1a36c0 2022-01-09 op c->iev.handler = control_dispatch_imsg;
177 fb1a36c0 2022-01-09 op c->iev.events = EV_READ;
178 fb1a36c0 2022-01-09 op event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
179 fb1a36c0 2022-01-09 op c->iev.handler, &c->iev);
180 fb1a36c0 2022-01-09 op event_add(&c->iev.ev, NULL);
182 fb1a36c0 2022-01-09 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
185 fb1a36c0 2022-01-09 op struct ctl_conn *
186 fb1a36c0 2022-01-09 op control_connbyfd(int fd)
188 fb1a36c0 2022-01-09 op struct ctl_conn *c;
190 fb1a36c0 2022-01-09 op TAILQ_FOREACH(c, &ctl_conns, entry) {
191 fb1a36c0 2022-01-09 op if (c->iev.ibuf.fd == fd)
198 5c69b561 2022-01-29 op struct ctl_conn *
199 5c69b561 2022-01-29 op control_connbypeer(uint32_t peerid)
201 5c69b561 2022-01-29 op struct ctl_conn *c;
203 5c69b561 2022-01-29 op TAILQ_FOREACH(c, &ctl_conns, entry) {
204 5c69b561 2022-01-29 op if (c->peerid == peerid)
211 fb1a36c0 2022-01-09 op struct ctl_conn *
212 fb1a36c0 2022-01-09 op control_connbypid(pid_t pid)
214 fb1a36c0 2022-01-09 op struct ctl_conn *c;
216 fb1a36c0 2022-01-09 op TAILQ_FOREACH(c, &ctl_conns, entry) {
217 fb1a36c0 2022-01-09 op if (c->iev.ibuf.pid == pid)
225 fb1a36c0 2022-01-09 op control_close(int fd)
227 fb1a36c0 2022-01-09 op struct ctl_conn *c;
229 fb1a36c0 2022-01-09 op if ((c = control_connbyfd(fd)) == NULL) {
230 fb1a36c0 2022-01-09 op log_warnx("%s: fd %d: not found", __func__, fd);
234 fb1a36c0 2022-01-09 op msgbuf_clear(&c->iev.ibuf.w);
235 fb1a36c0 2022-01-09 op TAILQ_REMOVE(&ctl_conns, c, entry);
237 fb1a36c0 2022-01-09 op event_del(&c->iev.ev);
238 fb1a36c0 2022-01-09 op close(c->iev.ibuf.fd);
240 fb1a36c0 2022-01-09 op /* Some file descriptors are available again. */
241 fb1a36c0 2022-01-09 op if (evtimer_pending(&control_state.evt, NULL)) {
242 fb1a36c0 2022-01-09 op evtimer_del(&control_state.evt);
243 fb1a36c0 2022-01-09 op event_add(&control_state.ev, NULL);
250 fb1a36c0 2022-01-09 op control_dispatch_imsg(int fd, short event, void *bula)
252 fb1a36c0 2022-01-09 op struct ctl_conn *c;
253 fb1a36c0 2022-01-09 op struct imsg imsg;
256 fb1a36c0 2022-01-09 op if ((c = control_connbyfd(fd)) == NULL) {
257 fb1a36c0 2022-01-09 op log_warnx("%s: fd %d: not found", __func__, fd);
261 fb1a36c0 2022-01-09 op if (event & EV_READ) {
262 fb1a36c0 2022-01-09 op if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
264 fb1a36c0 2022-01-09 op control_close(fd);
268 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
269 fb1a36c0 2022-01-09 op if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
270 fb1a36c0 2022-01-09 op control_close(fd);
276 fb1a36c0 2022-01-09 op if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
277 fb1a36c0 2022-01-09 op control_close(fd);
283 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
284 fb1a36c0 2022-01-09 op case IMSG_CTL_RELOAD:
285 a80d2bca 2022-01-23 op if (main_reload() == -1)
286 a80d2bca 2022-01-23 op log_warnx("configuration reload failed");
288 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG:
289 5c69b561 2022-01-29 op main_imsg_compose_listener(IMSG_CTL_DEBUG, -1,
290 5c69b561 2022-01-29 op c->peerid, NULL, 0);
292 fb1a36c0 2022-01-09 op case IMSG_CTL_LOG_VERBOSE:
293 fb1a36c0 2022-01-09 op if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
296 fb1a36c0 2022-01-09 op /* Forward to all other processes. */
297 a80d2bca 2022-01-23 op main_imsg_compose_listener(imsg.hdr.type, -1,
298 632c1cc2 2022-01-29 op 0, imsg.data, IMSG_DATA_SIZE(imsg));
300 fb1a36c0 2022-01-09 op memcpy(&verbose, imsg.data, sizeof(verbose));
301 fb1a36c0 2022-01-09 op log_setverbose(verbose);
304 fb1a36c0 2022-01-09 op log_debug("%s: error handling imsg %d", __func__,
305 fb1a36c0 2022-01-09 op imsg.hdr.type);
308 fb1a36c0 2022-01-09 op imsg_free(&imsg);
311 fb1a36c0 2022-01-09 op imsg_event_add(&c->iev);
315 fb1a36c0 2022-01-09 op control_imsg_relay(struct imsg *imsg)
317 fb1a36c0 2022-01-09 op struct ctl_conn *c;
319 5c69b561 2022-01-29 op if ((c = control_connbypeer(imsg->hdr.peerid)) == NULL)
322 fb1a36c0 2022-01-09 op return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
323 fb1a36c0 2022-01-09 op -1, imsg->data, IMSG_DATA_SIZE(*imsg)));