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 "ev.h"
34 #include "imsgev.h"
35 #include "minibuffer.h"
36 #include "telescope.h"
37 #include "utils.h"
38 #include "ui.h"
39 #include "xwrapper.h"
41 #define CONTROL_BACKLOG 5
43 struct {
44 unsigned long timeout;
45 int fd;
46 } control_state = {.fd = -1};
48 struct ctl_conn {
49 TAILQ_ENTRY(ctl_conn) entry;
50 struct imsgev iev;
51 };
53 struct ctl_conn *control_connbyfd(int);
54 struct ctl_conn *control_connbypid(pid_t);
55 void control_close(int);
57 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 int
60 control_init(char *path)
61 {
62 struct sockaddr_un sun;
63 int fd;
64 mode_t old_umask;
66 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
67 warn("%s: socket", __func__);
68 return (-1);
69 }
71 if (!mark_nonblock_cloexec(fd)) {
72 close(fd);
73 return (-1);
74 }
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 warn("%s: unlink %s", __func__, path);
83 close(fd);
84 return (-1);
85 }
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 warn("%s: bind: %s", __func__, path);
90 close(fd);
91 umask(old_umask);
92 return (-1);
93 }
94 umask(old_umask);
96 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
97 warn("%s: chmod", __func__);
98 close(fd);
99 (void)unlink(path);
100 return (-1);
103 return (fd);
106 int
107 control_listen(int fd)
109 control_state.fd = fd;
110 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
111 warn("%s: listen", __func__);
112 return (-1);
115 ev_add(control_state.fd, EV_READ, control_accept, NULL);
116 return (0);
119 void
120 control_accept(int listenfd, int event, void *bula)
122 int connfd;
123 socklen_t len;
124 struct sockaddr_un sun;
125 struct ctl_conn *c;
127 ev_add(control_state.fd, EV_READ, control_accept, NULL);
128 if ((event & EV_TIMEOUT))
129 return;
131 len = sizeof(sun);
132 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
133 /*
134 * Pause accept if we are out of file descriptors, or
135 * ev will haunt us here too.
136 */
137 if (errno == ENFILE || errno == EMFILE) {
138 struct timeval evtpause = { 1, 0 };
140 ev_del(control_state.fd);
141 control_state.timeout = ev_timer(&evtpause,
142 control_accept, NULL);
143 } else if (errno != EWOULDBLOCK && errno != EINTR &&
144 errno != ECONNABORTED)
145 message("%s: accept4: %s", __func__, strerror(errno));
146 return;
149 if (!mark_nonblock_cloexec(connfd)) {
150 message("%s: mark_nonblock_cloexec: %s", __func__,
151 strerror(errno));
152 close(connfd);
153 return;
156 c = xcalloc(1, sizeof(struct ctl_conn));
158 if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) {
159 message("%s: ev_add: %s", __func__, strerror(errno));
160 close(connfd);
161 free(c);
162 return;
165 c->iev.handler = control_dispatch_imsg;
166 c->iev.events = EV_READ;
167 if (ev_add(connfd, c->iev.events, c->iev.handler, &c->iev) == -1) {
168 message("%s: ev_add: %s", __func__, strerror(errno));
169 close(connfd);
170 free(c);
171 return;
174 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
177 struct ctl_conn *
178 control_connbyfd(int fd)
180 struct ctl_conn *c;
182 TAILQ_FOREACH(c, &ctl_conns, entry) {
183 if (c->iev.ibuf.fd == fd)
184 break;
187 return (c);
190 struct ctl_conn *
191 control_connbypid(pid_t pid)
193 struct ctl_conn *c;
195 TAILQ_FOREACH(c, &ctl_conns, entry) {
196 if (c->iev.ibuf.pid == pid)
197 break;
200 return (c);
203 void
204 control_close(int fd)
206 struct ctl_conn *c;
208 if ((c = control_connbyfd(fd)) == NULL) {
209 message("%s: fd %d: not found", __func__, fd);
210 return;
213 ev_del(c->iev.ibuf.fd);
214 close(c->iev.ibuf.fd);
216 imsgbuf_clear(&c->iev.ibuf);
217 TAILQ_REMOVE(&ctl_conns, c, entry);
219 free(c);
221 /* Some file descriptors are available again. */
222 if (ev_timer_pending(control_state.timeout)) {
223 ev_timer_cancel(control_state.timeout);
224 control_state.timeout = 0;
225 ev_add(control_state.fd, EV_READ, control_accept, NULL);
229 void
230 control_dispatch_imsg(int fd, int event, void *bula)
232 struct ctl_conn *c;
233 struct imsg imsg;
234 ssize_t n;
236 if ((c = control_connbyfd(fd)) == NULL) {
237 message("%s: fd %d: not found", __func__, fd);
238 return;
241 if (event & EV_READ) {
242 if (imsgbuf_read(&c->iev.ibuf) == -1) {
243 control_close(fd);
244 return;
247 if (event & EV_WRITE) {
248 if (imsgbuf_write(&c->iev.ibuf) == -1) {
249 control_close(fd);
250 return;
254 for (;;) {
255 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
256 control_close(fd);
257 return;
259 if (n == 0)
260 break;
262 switch (imsg.hdr.type) {
263 case IMSG_CTL_OPEN_URL: {
264 static char uri[GEMINI_URL_LEN];
266 if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
267 break;
268 memset(uri, 0, sizeof(uri));
269 memcpy(uri, imsg.data, sizeof(uri));
270 if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
271 break;
273 ui_remotely_open_link(uri);
274 break;
276 default:
277 message("%s: error handling imsg %d", __func__,
278 imsg.hdr.type);
279 break;
281 imsg_free(&imsg);
284 imsg_event_add(&c->iev);