1 /* $OpenBSD: control.c,v 1.4 2021/08/01 09:07:03 florian Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
21 #include <sys/types.h>
23 #include <sys/socket.h>
35 #include "minibuffer.h"
36 #include "telescope.h"
41 #define CONTROL_BACKLOG 5
44 unsigned long timeout;
46 } control_state = {.fd = -1};
49 TAILQ_ENTRY(ctl_conn) entry;
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);
60 control_init(char *path)
62 struct sockaddr_un sun;
66 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
67 warn("%s: socket", __func__);
71 if (!mark_nonblock_cloexec(fd)) {
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);
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);
96 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
97 warn("%s: chmod", __func__);
107 control_listen(int fd)
109 control_state.fd = fd;
110 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
111 warn("%s: listen", __func__);
115 ev_add(control_state.fd, EV_READ, control_accept, NULL);
120 control_accept(int listenfd, int event, void *bula)
124 struct sockaddr_un sun;
127 ev_add(control_state.fd, EV_READ, control_accept, NULL);
128 if ((event & EV_TIMEOUT))
132 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
134 * Pause accept if we are out of file descriptors, or
135 * ev will haunt us here too.
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));
149 if (!mark_nonblock_cloexec(connfd)) {
150 message("%s: mark_nonblock_cloexec: %s", __func__,
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));
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));
174 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
178 control_connbyfd(int fd)
182 TAILQ_FOREACH(c, &ctl_conns, entry) {
183 if (c->iev.ibuf.fd == fd)
191 control_connbypid(pid_t pid)
195 TAILQ_FOREACH(c, &ctl_conns, entry) {
196 if (c->iev.ibuf.pid == pid)
204 control_close(int fd)
208 if ((c = control_connbyfd(fd)) == NULL) {
209 message("%s: fd %d: not found", __func__, fd);
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);
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);
230 control_dispatch_imsg(int fd, int event, void *bula)
236 if ((c = control_connbyfd(fd)) == NULL) {
237 message("%s: fd %d: not found", __func__, fd);
241 if (event & EV_READ) {
242 if (imsgbuf_read(&c->iev.ibuf) == -1) {
247 if (event & EV_WRITE) {
248 if (imsgbuf_write(&c->iev.ibuf) == -1) {
255 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
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))
268 memset(uri, 0, sizeof(uri));
269 memcpy(uri, imsg.data, sizeof(uri));
270 if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
273 ui_remotely_open_link(uri);
277 message("%s: error handling imsg %d", __func__,
284 imsg_event_add(&c->iev);