2 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
4 * Permission to use, copy, modify, and 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.
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.
19 #include <sys/types.h>
21 #include <sys/socket.h>
25 #include <netinet/in.h>
40 #define CONTROL_BACKLOG 5
48 } control_state = {.fd = -1};
51 TAILQ_ENTRY(ctl_conn) entry;
56 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
58 struct ctl_conn *control_connbyfd(int);
59 struct ctl_conn *control_connbypeer(uint32_t);
60 struct ctl_conn *control_connbypid(pid_t);
61 void control_close(int);
64 control_init(const char *path)
66 struct sockaddr_un sun;
70 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
72 log_warn("%s: socket", __func__);
76 memset(&sun, 0, sizeof(sun));
77 sun.sun_family = AF_UNIX;
78 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
80 if (unlink(path) == -1)
81 if (errno != ENOENT) {
82 log_warn("%s: unlink %s", __func__, path);
87 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
88 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
89 log_warn("%s: bind: %s", __func__, path);
96 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
97 log_warn("%s: chmod", __func__);
107 control_listen(int fd)
109 if (control_state.fd != -1)
110 fatalx("%s: received unexpected controlsock", __func__);
112 control_state.fd = fd;
113 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
114 log_warn("%s: listen", __func__);
118 event_set(&control_state.ev, control_state.fd, EV_READ,
119 control_accept, NULL);
120 event_add(&control_state.ev, NULL);
121 evtimer_set(&control_state.evt, control_accept, NULL);
127 control_accept(int listenfd, short event, void *bula)
131 struct sockaddr_un sun;
134 event_add(&control_state.ev, NULL);
135 if ((event & EV_TIMEOUT))
139 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
141 * Pause accept if we are out of file descriptors, or
142 * libevent will haunt us here too.
144 if (errno == ENFILE || errno == EMFILE) {
145 struct timeval evtpause = { 1, 0 };
147 event_del(&control_state.ev);
148 evtimer_add(&control_state.evt, &evtpause);
149 } else if (errno != EWOULDBLOCK && errno != EINTR &&
150 errno != ECONNABORTED)
151 log_warn("%s: accept4", __func__);
155 if ((flags = fcntl(connfd, F_GETFL)) == -1) {
156 log_warn("%s: fcntl F_GETFL", __func__);
161 if (fcntl(connfd, F_SETFL, flags) == -1 ||
162 fcntl(connfd, F_SETFD, FD_CLOEXEC) == -1) {
163 log_warn("%s: can't set nonblock/cloexec", __func__);
168 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
169 log_warn("%s: calloc", __func__);
174 c->peerid = peeridcnt++;
175 imsg_init(&c->iev.ibuf, connfd);
176 c->iev.handler = control_dispatch_imsg;
177 c->iev.events = EV_READ;
178 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
179 c->iev.handler, &c->iev);
180 event_add(&c->iev.ev, NULL);
182 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
186 control_connbyfd(int fd)
190 TAILQ_FOREACH(c, &ctl_conns, entry) {
191 if (c->iev.ibuf.fd == fd)
199 control_connbypeer(uint32_t peerid)
203 TAILQ_FOREACH(c, &ctl_conns, entry) {
204 if (c->peerid == peerid)
212 control_connbypid(pid_t pid)
216 TAILQ_FOREACH(c, &ctl_conns, entry) {
217 if (c->iev.ibuf.pid == pid)
225 control_close(int fd)
229 if ((c = control_connbyfd(fd)) == NULL) {
230 log_warnx("%s: fd %d: not found", __func__, fd);
234 msgbuf_clear(&c->iev.ibuf.w);
235 TAILQ_REMOVE(&ctl_conns, c, entry);
237 event_del(&c->iev.ev);
238 close(c->iev.ibuf.fd);
240 /* Some file descriptors are available again. */
241 if (evtimer_pending(&control_state.evt, NULL)) {
242 evtimer_del(&control_state.evt);
243 event_add(&control_state.ev, NULL);
250 control_dispatch_imsg(int fd, short event, void *bula)
256 if ((c = control_connbyfd(fd)) == NULL) {
257 log_warnx("%s: fd %d: not found", __func__, fd);
261 if (event & EV_READ) {
262 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
268 if (event & EV_WRITE) {
269 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
276 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
283 switch (imsg.hdr.type) {
284 case IMSG_CTL_RELOAD:
285 if (main_reload() == -1)
286 log_warnx("configuration reload failed");
289 main_imsg_compose_listener(IMSG_CTL_DEBUG, -1,
292 case IMSG_CTL_LOG_VERBOSE:
293 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
296 /* Forward to all other processes. */
297 main_imsg_compose_listener(imsg.hdr.type, -1,
298 0, imsg.data, IMSG_DATA_SIZE(imsg));
300 memcpy(&verbose, imsg.data, sizeof(verbose));
301 log_setverbose(verbose);
304 log_debug("%s: error handling imsg %d", __func__,
311 imsg_event_add(&c->iev);
315 control_imsg_relay(struct imsg *imsg)
319 if ((c = control_connbypeer(imsg->hdr.peerid)) == NULL)
322 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
323 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));