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"
40 #define CONTROL_BACKLOG 5
43 unsigned long timeout;
45 } control_state = {.fd = -1};
48 TAILQ_ENTRY(ctl_conn) entry;
52 struct ctl_conn *control_connbyfd(int);
53 struct ctl_conn *control_connbypid(pid_t);
54 void control_close(int);
56 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 control_init(char *path)
61 struct sockaddr_un sun;
65 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
66 warn("%s: socket", __func__);
70 if (!mark_nonblock_cloexec(fd)) {
75 memset(&sun, 0, sizeof(sun));
76 sun.sun_family = AF_UNIX;
77 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
79 if (unlink(path) == -1)
80 if (errno != ENOENT) {
81 warn("%s: unlink %s", __func__, path);
86 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
87 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
88 warn("%s: bind: %s", __func__, path);
95 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
96 warn("%s: chmod", __func__);
106 control_listen(int fd)
108 control_state.fd = fd;
109 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
110 warn("%s: listen", __func__);
114 ev_add(control_state.fd, EV_READ, control_accept, NULL);
119 control_accept(int listenfd, int event, void *bula)
123 struct sockaddr_un sun;
126 ev_add(control_state.fd, EV_READ, control_accept, NULL);
127 if ((event & EV_TIMEOUT))
131 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
133 * Pause accept if we are out of file descriptors, or
134 * ev will haunt us here too.
136 if (errno == ENFILE || errno == EMFILE) {
137 struct timeval evtpause = { 1, 0 };
139 ev_del(control_state.fd);
140 control_state.timeout = ev_timer(&evtpause,
141 control_accept, NULL);
142 } else if (errno != EWOULDBLOCK && errno != EINTR &&
143 errno != ECONNABORTED)
144 message("%s: accept4: %s", __func__, strerror(errno));
148 if (!mark_nonblock_cloexec(connfd)) {
149 message("%s: mark_nonblock_cloexec: %s", __func__,
155 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
156 message("%s: calloc: %s", __func__, strerror(errno));
161 imsg_init(&c->iev.ibuf, connfd);
162 c->iev.handler = control_dispatch_imsg;
163 c->iev.events = EV_READ;
164 if (ev_add(connfd, c->iev.events, c->iev.handler, &c->iev) == -1) {
165 message("%s: ev_add: %s", __func__, strerror(errno));
171 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
175 control_connbyfd(int fd)
179 TAILQ_FOREACH(c, &ctl_conns, entry) {
180 if (c->iev.ibuf.fd == fd)
188 control_connbypid(pid_t pid)
192 TAILQ_FOREACH(c, &ctl_conns, entry) {
193 if (c->iev.ibuf.pid == pid)
201 control_close(int fd)
205 if ((c = control_connbyfd(fd)) == NULL) {
206 message("%s: fd %d: not found", __func__, fd);
210 msgbuf_clear(&c->iev.ibuf.w);
211 TAILQ_REMOVE(&ctl_conns, c, entry);
213 ev_del(c->iev.ibuf.fd);
214 close(c->iev.ibuf.fd);
216 /* Some file descriptors are available again. */
217 if (ev_timer_pending(control_state.timeout)) {
218 ev_timer_cancel(control_state.timeout);
219 control_state.timeout = 0;
220 ev_add(control_state.fd, EV_READ, control_accept, NULL);
227 control_dispatch_imsg(int fd, int event, void *bula)
233 if ((c = control_connbyfd(fd)) == NULL) {
234 message("%s: fd %d: not found", __func__, fd);
238 if (event & EV_READ) {
239 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
245 if (event & EV_WRITE) {
246 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
253 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
260 switch (imsg.hdr.type) {
261 case IMSG_CTL_OPEN_URL: {
262 static char uri[GEMINI_URL_LEN];
264 if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
266 memset(uri, 0, sizeof(uri));
267 memcpy(uri, imsg.data, sizeof(uri));
268 if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
271 ui_remotely_open_link(uri);
275 message("%s: error handling imsg %d", __func__,
282 imsg_event_add(&c->iev);