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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
24 #include <netinet/in.h>
37 #define CONTROL_BACKLOG 5
43 } control_state = {.fd = -1};
46 TAILQ_ENTRY(ctl_conn) entry;
50 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
52 struct ctl_conn *control_connbyfd(int);
53 struct ctl_conn *control_connbypid(pid_t);
54 void control_close(int);
57 control_init(const char *path)
59 struct sockaddr_un sun;
63 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
65 log_warn("%s: socket", __func__);
69 memset(&sun, 0, sizeof(sun));
70 sun.sun_family = AF_UNIX;
71 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
73 if (unlink(path) == -1)
74 if (errno != ENOENT) {
75 log_warn("%s: unlink %s", __func__, path);
80 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
81 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
82 log_warn("%s: bind: %s", __func__, path);
89 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
90 log_warn("%s: chmod", __func__);
100 control_listen(int fd)
102 if (control_state.fd != -1)
103 fatalx("%s: received unexpected controlsock", __func__);
105 control_state.fd = fd;
106 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
107 log_warn("%s: listen", __func__);
111 event_set(&control_state.ev, control_state.fd, EV_READ,
112 control_accept, NULL);
113 event_add(&control_state.ev, NULL);
114 evtimer_set(&control_state.evt, control_accept, NULL);
120 control_accept(int listenfd, short event, void *bula)
124 struct sockaddr_un sun;
127 event_add(&control_state.ev, NULL);
128 if ((event & EV_TIMEOUT))
132 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
133 SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
135 * Pause accept if we are out of file descriptors, or
136 * libevent will haunt us here too.
138 if (errno == ENFILE || errno == EMFILE) {
139 struct timeval evtpause = { 1, 0 };
141 event_del(&control_state.ev);
142 evtimer_add(&control_state.evt, &evtpause);
143 } else if (errno != EWOULDBLOCK && errno != EINTR &&
144 errno != ECONNABORTED)
145 log_warn("%s: accept4", __func__);
149 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
150 log_warn("%s: calloc", __func__);
155 imsg_init(&c->iev.ibuf, connfd);
156 c->iev.handler = control_dispatch_imsg;
157 c->iev.events = EV_READ;
158 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
159 c->iev.handler, &c->iev);
160 event_add(&c->iev.ev, NULL);
162 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
166 control_connbyfd(int fd)
170 TAILQ_FOREACH(c, &ctl_conns, entry) {
171 if (c->iev.ibuf.fd == fd)
179 control_connbypid(pid_t pid)
183 TAILQ_FOREACH(c, &ctl_conns, entry) {
184 if (c->iev.ibuf.pid == pid)
192 control_close(int fd)
196 if ((c = control_connbyfd(fd)) == NULL) {
197 log_warnx("%s: fd %d: not found", __func__, fd);
201 msgbuf_clear(&c->iev.ibuf.w);
202 TAILQ_REMOVE(&ctl_conns, c, entry);
204 event_del(&c->iev.ev);
205 close(c->iev.ibuf.fd);
207 /* Some file descriptors are available again. */
208 if (evtimer_pending(&control_state.evt, NULL)) {
209 evtimer_del(&control_state.evt);
210 event_add(&control_state.ev, NULL);
217 control_dispatch_imsg(int fd, short event, void *bula)
224 if ((c = control_connbyfd(fd)) == NULL) {
225 log_warnx("%s: fd %d: not found", __func__, fd);
229 if (event & EV_READ) {
230 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
236 if (event & EV_WRITE) {
237 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
244 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
251 switch (imsg.hdr.type) {
252 case IMSG_CTL_RELOAD:
253 listener_imsg_compose_main(imsg.hdr.type, 0, NULL, 0);
255 case IMSG_CTL_LOG_VERBOSE:
256 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
259 /* Forward to all other processes. */
260 listener_imsg_compose_main(imsg.hdr.type, imsg.hdr.pid,
261 imsg.data, IMSG_DATA_SIZE(imsg));
263 /* XXX: send to every client? */
265 memcpy(&verbose, imsg.data, sizeof(verbose));
266 log_setverbose(verbose);
269 log_debug("%s: error handling imsg %d", __func__,
276 imsg_event_add(&c->iev);
280 control_imsg_relay(struct imsg *imsg)
284 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
287 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
288 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));