Blob


1 /* $OpenBSD: control.c,v 1.4 2021/08/01 09:07:03 florian Exp $ */
3 /*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include "compat.h"
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/uio.h>
25 #include <sys/un.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "control.h"
33 #include "minibuffer.h"
34 #include "telescope.h"
35 #include "utils.h"
36 #include "ui.h"
38 #define CONTROL_BACKLOG 5
40 struct {
41 struct event ev;
42 struct event evt;
43 int fd;
44 } control_state = {.fd = -1};
46 struct ctl_conn {
47 TAILQ_ENTRY(ctl_conn) entry;
48 struct imsgev iev;
49 };
51 struct ctl_conn *control_connbyfd(int);
52 struct ctl_conn *control_connbypid(pid_t);
53 void control_close(int);
55 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
57 int
58 control_init(char *path)
59 {
60 struct sockaddr_un sun;
61 int fd;
62 mode_t old_umask;
64 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
65 warn("%s: socket", __func__);
66 return (-1);
67 }
69 if (!mark_nonblock_cloexec(fd)) {
70 close(fd);
71 return (-1);
72 }
74 memset(&sun, 0, sizeof(sun));
75 sun.sun_family = AF_UNIX;
76 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
78 if (unlink(path) == -1)
79 if (errno != ENOENT) {
80 warn("%s: unlink %s", __func__, path);
81 close(fd);
82 return (-1);
83 }
85 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
86 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
87 warn("%s: bind: %s", __func__, path);
88 close(fd);
89 umask(old_umask);
90 return (-1);
91 }
92 umask(old_umask);
94 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
95 warn("%s: chmod", __func__);
96 close(fd);
97 (void)unlink(path);
98 return (-1);
99 }
101 return (fd);
104 int
105 control_listen(int fd)
107 control_state.fd = fd;
108 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
109 warn("%s: listen", __func__);
110 return (-1);
113 event_set(&control_state.ev, control_state.fd, EV_READ,
114 control_accept, NULL);
115 event_add(&control_state.ev, NULL);
116 evtimer_set(&control_state.evt, control_accept, NULL);
118 return (0);
121 void
122 control_accept(int listenfd, short event, void *bula)
124 int connfd;
125 socklen_t len;
126 struct sockaddr_un sun;
127 struct ctl_conn *c;
129 event_add(&control_state.ev, NULL);
130 if ((event & EV_TIMEOUT))
131 return;
133 len = sizeof(sun);
134 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
135 /*
136 * Pause accept if we are out of file descriptors, or
137 * libevent will haunt us here too.
138 */
139 if (errno == ENFILE || errno == EMFILE) {
140 struct timeval evtpause = { 1, 0 };
142 event_del(&control_state.ev);
143 evtimer_add(&control_state.evt, &evtpause);
144 } else if (errno != EWOULDBLOCK && errno != EINTR &&
145 errno != ECONNABORTED)
146 message("%s: accept4: %s", __func__, strerror(errno));
147 return;
150 if (!mark_nonblock_cloexec(connfd)) {
151 message("%s: mark_nonblock_cloexec: %s", __func__,
152 strerror(errno));
153 close(connfd);
154 return;
157 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
158 message("%s: calloc: %s", __func__, strerror(errno));
159 close(connfd);
160 return;
163 imsg_init(&c->iev.ibuf, connfd);
164 c->iev.handler = control_dispatch_imsg;
165 c->iev.events = EV_READ;
166 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
167 c->iev.handler, &c->iev);
168 event_add(&c->iev.ev, NULL);
170 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
173 struct ctl_conn *
174 control_connbyfd(int fd)
176 struct ctl_conn *c;
178 TAILQ_FOREACH(c, &ctl_conns, entry) {
179 if (c->iev.ibuf.fd == fd)
180 break;
183 return (c);
186 struct ctl_conn *
187 control_connbypid(pid_t pid)
189 struct ctl_conn *c;
191 TAILQ_FOREACH(c, &ctl_conns, entry) {
192 if (c->iev.ibuf.pid == pid)
193 break;
196 return (c);
199 void
200 control_close(int fd)
202 struct ctl_conn *c;
204 if ((c = control_connbyfd(fd)) == NULL) {
205 message("%s: fd %d: not found", __func__, fd);
206 return;
209 msgbuf_clear(&c->iev.ibuf.w);
210 TAILQ_REMOVE(&ctl_conns, c, entry);
212 event_del(&c->iev.ev);
213 close(c->iev.ibuf.fd);
215 /* Some file descriptors are available again. */
216 if (evtimer_pending(&control_state.evt, NULL)) {
217 evtimer_del(&control_state.evt);
218 event_add(&control_state.ev, NULL);
221 free(c);
224 void
225 control_dispatch_imsg(int fd, short event, void *bula)
227 struct ctl_conn *c;
228 struct imsg imsg;
229 ssize_t n;
231 if ((c = control_connbyfd(fd)) == NULL) {
232 message("%s: fd %d: not found", __func__, fd);
233 return;
236 if (event & EV_READ) {
237 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
238 n == 0) {
239 control_close(fd);
240 return;
243 if (event & EV_WRITE) {
244 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
245 control_close(fd);
246 return;
250 for (;;) {
251 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
252 control_close(fd);
253 return;
255 if (n == 0)
256 break;
258 switch (imsg.hdr.type) {
259 case IMSG_CTL_OPEN_URL: {
260 static char uri[GEMINI_URL_LEN];
262 if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
263 break;
264 memset(uri, 0, sizeof(uri));
265 memcpy(uri, imsg.data, sizeof(uri));
266 if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
267 break;
269 ui_remotely_open_link(uri);
270 break;
272 default:
273 message("%s: error handling imsg %d", __func__,
274 imsg.hdr.type);
275 break;
277 imsg_free(&imsg);
280 imsg_event_add(&c->iev);
283 int
284 control_imsg_relay(struct imsg *imsg)
286 struct ctl_conn *c;
288 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
289 return (0);
291 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
292 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));