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 | SOCK_CLOEXEC | SOCK_NONBLOCK,
65 0)) == -1) {
66 warn("%s: socket", __func__);
67 return (-1);
68 }
70 memset(&sun, 0, sizeof(sun));
71 sun.sun_family = AF_UNIX;
72 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
74 if (unlink(path) == -1)
75 if (errno != ENOENT) {
76 warn("%s: unlink %s", __func__, path);
77 close(fd);
78 return (-1);
79 }
81 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
82 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
83 warn("%s: bind: %s", __func__, path);
84 close(fd);
85 umask(old_umask);
86 return (-1);
87 }
88 umask(old_umask);
90 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
91 warn("%s: chmod", __func__);
92 close(fd);
93 (void)unlink(path);
94 return (-1);
95 }
97 return (fd);
98 }
100 int
101 control_listen(int fd)
103 control_state.fd = fd;
104 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
105 warn("%s: listen", __func__);
106 return (-1);
109 event_set(&control_state.ev, control_state.fd, EV_READ,
110 control_accept, NULL);
111 event_add(&control_state.ev, NULL);
112 evtimer_set(&control_state.evt, control_accept, NULL);
114 return (0);
117 void
118 control_accept(int listenfd, short event, void *bula)
120 int connfd;
121 socklen_t len;
122 struct sockaddr_un sun;
123 struct ctl_conn *c;
125 event_add(&control_state.ev, NULL);
126 if ((event & EV_TIMEOUT))
127 return;
129 len = sizeof(sun);
130 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
131 SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
132 /*
133 * Pause accept if we are out of file descriptors, or
134 * libevent will haunt us here too.
135 */
136 if (errno == ENFILE || errno == EMFILE) {
137 struct timeval evtpause = { 1, 0 };
139 event_del(&control_state.ev);
140 evtimer_add(&control_state.evt, &evtpause);
141 } else if (errno != EWOULDBLOCK && errno != EINTR &&
142 errno != ECONNABORTED)
143 message("%s: accept4: %s", __func__, strerror(errno));
144 return;
147 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
148 message("%s: calloc: %s", __func__, strerror(errno));
149 close(connfd);
150 return;
153 imsg_init(&c->iev.ibuf, connfd);
154 c->iev.handler = control_dispatch_imsg;
155 c->iev.events = EV_READ;
156 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
157 c->iev.handler, &c->iev);
158 event_add(&c->iev.ev, NULL);
160 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
163 struct ctl_conn *
164 control_connbyfd(int fd)
166 struct ctl_conn *c;
168 TAILQ_FOREACH(c, &ctl_conns, entry) {
169 if (c->iev.ibuf.fd == fd)
170 break;
173 return (c);
176 struct ctl_conn *
177 control_connbypid(pid_t pid)
179 struct ctl_conn *c;
181 TAILQ_FOREACH(c, &ctl_conns, entry) {
182 if (c->iev.ibuf.pid == pid)
183 break;
186 return (c);
189 void
190 control_close(int fd)
192 struct ctl_conn *c;
194 if ((c = control_connbyfd(fd)) == NULL) {
195 message("%s: fd %d: not found", __func__, fd);
196 return;
199 msgbuf_clear(&c->iev.ibuf.w);
200 TAILQ_REMOVE(&ctl_conns, c, entry);
202 event_del(&c->iev.ev);
203 close(c->iev.ibuf.fd);
205 /* Some file descriptors are available again. */
206 if (evtimer_pending(&control_state.evt, NULL)) {
207 evtimer_del(&control_state.evt);
208 event_add(&control_state.ev, NULL);
211 free(c);
214 void
215 control_dispatch_imsg(int fd, short event, void *bula)
217 struct ctl_conn *c;
218 struct imsg imsg;
219 ssize_t n;
221 if ((c = control_connbyfd(fd)) == NULL) {
222 message("%s: fd %d: not found", __func__, fd);
223 return;
226 if (event & EV_READ) {
227 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
228 n == 0) {
229 control_close(fd);
230 return;
233 if (event & EV_WRITE) {
234 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
235 control_close(fd);
236 return;
240 for (;;) {
241 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
242 control_close(fd);
243 return;
245 if (n == 0)
246 break;
248 switch (imsg.hdr.type) {
249 case IMSG_CTL_OPEN_URL: {
250 static char uri[GEMINI_URL_LEN];
252 if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
253 break;
254 memset(uri, 0, sizeof(uri));
255 memcpy(uri, imsg.data, sizeof(uri));
256 if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
257 break;
259 ui_remotely_open_link(uri);
260 break;
262 default:
263 message("%s: error handling imsg %d", __func__,
264 imsg.hdr.type);
265 break;
267 imsg_free(&imsg);
270 imsg_event_add(&c->iev);
273 int
274 control_imsg_relay(struct imsg *imsg)
276 struct ctl_conn *c;
278 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
279 return (0);
281 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
282 -1, imsg->data, IMSG_DATA_SIZE(*imsg)));