2 * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/socket.h>
21 #include <sys/types.h>
48 #define nitems(x) (sizeof(x)/sizeof(x[0]))
51 #define FORM_URLENCODED "application/x-www-form-urlencoded"
53 #define ICON_REPEAT_ALL "🔁"
54 #define ICON_REPEAT_ONE "🔂"
58 #define ICON_PAUSE "⏸"
59 #define ICON_TOGGLE "⏯"
62 static struct clthead clients;
63 static struct imsgbuf imsgbuf;
64 static struct playlist playlist_tmp;
65 static struct player_status player_status;
66 static uint64_t position, duration;
68 static void client_ev(int, int, void *);
70 const char *head = "<!doctype html>"
73 "<meta name='viewport' content='width=device-width, initial-scale=1'/>"
74 "<title>Amused Web</title>"
75 "<link rel='stylesheet' href='/style.css?v=0'>"
80 const char *css = "*{box-sizing:border-box}"
88 " flex-direction: column;"
90 "button{cursor:pointer}"
99 ".playlist-wrapper{min-height:80vh}"
106 " font-family: monospace;"
111 " background: transparent;"
112 " transition: background-color .25s ease-in-out;"
114 ".playlist button::before{"
117 " display: inline-block;"
119 ".playlist button:hover{"
120 " background-color: #dfdddd;"
122 ".playlist #current button{"
123 " font-weight: bold;"
125 ".playlist #current button::before{"
127 " font-weight: bold;"
135 " background-color: white;"
136 " background: #3d3d3d;"
138 " border-radius: 10px 10px 0 0;"
140 " text-align: center;"
150 " font-size: 0.9rem;"
154 " padding: 5px 20px;"
163 "const playlist=document.querySelector('.playlist');"
165 " if (e) {e.preventDefault()}"
166 " let cur = document.querySelector('#current');"
167 " if (cur) {cur.scrollIntoView(); window.scrollBy(0, -100);}"
169 "function b(x){return x=='on'};"
171 " const l=document.createElement('li');"
172 " if(c){l.id='current'};"
173 " const b=document.createElement('button');"
174 " b.type='submit'; b.name='jump'; b.value=p;"
177 " playlist.appendChild(l);"
180 " const [, type, payload] = t.split(/^(.):(.*)$/);"
182 " let s=payload.split(' ');"
183 " pos=s[0], dur=s[1];"
184 " } else if (type=='S') {"
185 " const btn=document.querySelector('#toggle');"
186 " if (payload=='playing') {"
187 " btn.innerHTML='"ICON_PAUSE"';"
188 " btn.value='pause';"
190 " btn.innerHTML='"ICON_PLAY"';"
193 " } else if (type=='r') {"
194 " const btn=document.querySelector('#rone');"
195 " btn.className=b(payload)?'mode-active':'';"
196 " } else if (type=='R') {"
197 " const btn=document.querySelector('#rall');"
198 " btn.className=b(payload)?'mode-active':'';"
199 " } else if (type=='c') {"
201 " } else if (type=='x') {"
202 " playlist.innerHTML='';"
203 " } else if (type=='X') {"
204 " dofilt();" /* done with the list */
205 " } else if (type=='A') {"
207 " } else if (type=='a') {"
208 " c(payload, false);"
209 " } else if (type=='C') {"
210 " const t=document.querySelector('.controls>p>a');"
211 " t.innerText = payload.replace(/.*\\//, '');"
214 " console.log('unknown:',t);"
218 " ws = new WebSocket((location.protocol=='http:'?'ws://':'wss://')"
219 " + location.host + '/ws');"
220 " ws.addEventListener('open', () => console.log('ws: connected'));"
221 " ws.addEventListener('close', () => {"
222 " alert('Websocket closed. The interface won\\'t update itself.'"
223 " + ' Please refresh the page');"
225 " ws.addEventListener('message', e => d(e.data))"
229 "document.querySelector('.controls a').addEventListener('click',cur);"
230 "document.querySelectorAll('form').forEach(f => {"
231 " f.action='/a/'+f.getAttribute('action');"
232 " f.addEventListener('submit', e => {"
233 " e.preventDefault();"
234 " const fd = new FormData(f);"
235 " if (e.submitter && e.submitter.value && e.submitter.value != '')"
236 " fd.append(e.submitter.name, e.submitter.value);"
239 " body: new URLSearchParams(fd)"
241 " .catch(x => console.log('failed to submit form:', x));"
244 "const sb = document.createElement('section');"
245 "sb.className = 'searchbox';"
246 "const filter = document.createElement('input');"
247 "filter.type = 'search';"
248 "filter.setAttribute('aria-label', 'Filter Playlist');"
249 "filter.placeholder = 'Filter Playlist';"
251 "document.querySelector('main').prepend(sb);"
252 "function dofilt() {"
253 " let t = filter.value.toLowerCase();"
254 " document.querySelectorAll('.playlist li').forEach(e => {"
255 " if (e.querySelector('button').value.toLowerCase().indexOf(t) == -1)"
256 " e.setAttribute('hidden', 'true');"
258 " e.removeAttribute('hidden');"
261 "function dbc(fn, wait) {"
263 " return function() {"
264 " let later = () => {tout = null; fn()};"
265 " clearTimeout(tout);"
267 " tout = setTimeout(later, wait);"
270 "filter.addEventListener('input', dbc(dofilt, 400));"
273 const char *foot = "<script src='/app.js?v=0'></script></body></html>";
276 dial(const char *sock)
278 struct sockaddr_un sa;
282 memset(&sa, 0, sizeof(sa));
283 sa.sun_family = AF_UNIX;
284 len = strlcpy(sa.sun_path, sock, sizeof(sa.sun_path));
285 if (len >= sizeof(sa.sun_path))
286 err(1, "path too long: %s", sock);
288 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
290 if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
291 err(1, "failed to connect to %s", sock);
297 * Adapted from usr.sbin/httpd/httpd.c' url_decode.
300 url_decode(char *url)
310 /* Encoding character is followed by two hex chars */
311 if (!isxdigit((unsigned char)p[1]) ||
312 !isxdigit((unsigned char)p[2]) ||
313 (p[1] == '0' && p[2] == '0'))
320 * We don't have to validate "hex" because it is
321 * guaranteed to include two hex chars followed
324 x = strtoul(hex, NULL, 16);
344 dispatch_event(const char *msg)
351 TAILQ_FOREACH(clt, &clients, clients) {
352 if (!clt->ws || clt->done || clt->err)
355 if (ws_compose(clt, WST_TEXT, msg, len) == -1)
358 ev_add(clt->bio.fd, POLLIN|POLLOUT, client_ev, clt);
365 dispatch_event_status(void)
368 char buf[PATH_MAX + 2];
371 switch (player_status.status) {
372 case STATE_STOPPED: status = "stopped"; break;
373 case STATE_PLAYING: status = "playing"; break;
374 case STATE_PAUSED: status = "paused"; break;
375 default: status = "unknown";
378 r = snprintf(buf, sizeof(buf), "S:%s", status);
379 if (r < 0 || (size_t)r >= sizeof(buf)) {
380 log_warn("snprintf");
385 r = snprintf(buf, sizeof(buf), "r:%s",
386 player_status.mode.repeat_one == MODE_ON ? "on" : "off");
387 if (r < 0 || (size_t)r >= sizeof(buf)) {
388 log_warn("snprintf");
393 r = snprintf(buf, sizeof(buf), "R:%s",
394 player_status.mode.repeat_all == MODE_ON ? "on" : "off");
395 if (r < 0 || (size_t)r >= sizeof(buf)) {
396 log_warn("snprintf");
401 r = snprintf(buf, sizeof(buf), "c:%s",
402 player_status.mode.consume == MODE_ON ? "on" : "off");
403 if (r < 0 || (size_t)r >= sizeof(buf)) {
404 log_warn("snprintf");
409 r = snprintf(buf, sizeof(buf), "C:%s", player_status.path);
410 if (r < 0 || (size_t)r >= sizeof(buf)) {
411 log_warn("snprintf");
420 dispatch_event_track(struct player_status *ps)
422 char p[PATH_MAX + 2];
425 r = snprintf(p, sizeof(p), "%c:%s",
426 ps->status == STATE_PLAYING ? 'A' : 'a', ps->path);
427 if (r < 0 || (size_t)r >= sizeof(p))
430 return dispatch_event(p);
434 imsg_dispatch(int fd, int ev, void *d)
437 static int off_found;
441 struct player_status ps;
442 struct player_event event;
448 if (ev & (POLLIN|POLLHUP)) {
449 if ((n = imsg_read(&imsgbuf)) == -1 && errno != EAGAIN)
452 fatalx("pipe closed");
455 if ((n = msgbuf_write(&imsgbuf.w)) == -1 && errno != EAGAIN)
456 fatal("msgbuf_write");
458 fatalx("pipe closed");
462 if ((n = imsg_get(&imsgbuf, &imsg)) == -1)
467 datalen = IMSG_DATA_SIZE(imsg);
469 switch (imsg.hdr.type) {
471 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
472 (datalen = ibuf_size(&ibuf)) == 0 ||
473 (msg = ibuf_data(&ibuf)) == NULL ||
474 msg[datalen - 1] != '\0')
475 fatalx("malformed error message");
476 log_warnx("error: %s", msg);
480 playlist_free(&playlist_tmp);
481 imsg_compose(&imsgbuf, IMSG_CTL_SHOW, 0, 0, -1,
485 case IMSG_CTL_MONITOR:
486 if (imsg_get_data(&imsg, &event, sizeof(event)) == -1)
487 fatalx("corrupted IMSG_CTL_MONITOR");
488 switch (event.event) {
493 imsg_compose(&imsgbuf, IMSG_CTL_STATUS, 0, 0,
500 case IMSG_CTL_COMMIT:
501 imsg_compose(&imsgbuf, IMSG_CTL_SHOW, 0, 0, -1,
503 imsg_compose(&imsgbuf, IMSG_CTL_STATUS, 0, 0,
508 position = event.position;
509 duration = event.duration;
510 r = snprintf(seekmsg, sizeof(seekmsg),
511 "s:%lld %lld", (long long)position,
512 (long long)duration);
513 if (r < 0 || (size_t)r >= sizeof(seekmsg)) {
514 log_warn("snprintf failed");
517 dispatch_event(seekmsg);
521 log_debug("ignoring event %d", event.event);
527 if (imsg_get_len(&imsg) == 0) {
528 if (playlist_tmp.len == 0) {
529 dispatch_event("x:");
531 } else if (playlist_tmp.len == off)
533 dispatch_event("X:");
534 playlist_swap(&playlist_tmp, off);
535 memset(&playlist_tmp, 0, sizeof(playlist_tmp));
540 if (imsg_get_data(&imsg, &ps, sizeof(ps)) == -1)
541 fatalx("corrupted IMSG_CTL_SHOW");
542 if (ps.path[sizeof(ps.path) - 1] != '\0')
543 fatalx("corrupted IMSG_CTL_SHOW");
544 if (playlist_tmp.len == 0)
545 dispatch_event("x:");
546 dispatch_event_track(&ps);
547 playlist_push(&playlist_tmp, ps.path);
548 if (ps.status == STATE_PLAYING)
554 case IMSG_CTL_STATUS:
555 if (imsg_get_data(&imsg, &player_status,
556 sizeof(player_status)) == -1)
557 fatalx("corrupted IMSG_CTL_STATUS");
558 if (player_status.path[sizeof(player_status.path) - 1]
560 fatalx("corrupted IMSG_CTL_STATUS");
561 dispatch_event_status();
567 if (imsgbuf.w.queued)
569 ev_add(fd, ev, imsg_dispatch, NULL);
573 route_notfound(struct client *clt)
575 if (http_reply(clt, 404, "Not Found", "text/plain") == -1 ||
576 http_writes(clt, "Page not found\n") == -1)
581 render_playlist(struct client *clt)
587 http_writes(clt, "<section class='playlist-wrapper'>");
588 http_writes(clt, "<form action=jump method=post"
589 " enctype='"FORM_URLENCODED"'>");
590 http_writes(clt, "<ul class=playlist>");
592 for (i = 0; i < playlist.len; ++i) {
593 current = play_off == i;
595 path = playlist.songs[i];
597 http_fmt(clt, "<li%s>", current ? " id=current" : "");
598 http_writes(clt, "<button type=submit name=jump value=\"");
599 http_htmlescape(clt, path);
600 http_writes(clt, "\">");
601 http_htmlescape(clt, path);
602 http_writes(clt, "</button></li>");
605 http_writes(clt, "</ul>");
606 http_writes(clt, "</form>");
607 http_writes(clt, "</section>");
611 render_controls(struct client *clt)
613 const char *oc, *ac, *p;
616 ac = player_status.mode.repeat_all ? " class='mode-active'" : "";
617 oc = player_status.mode.repeat_one ? " class='mode-active'" : "";
618 playing = player_status.status == STATE_PLAYING;
620 if ((p = strrchr(player_status.path, '/')) != NULL)
623 p = player_status.path;
625 if (http_writes(clt, "<section class=controls>") == -1 ||
626 http_writes(clt, "<p><a href='#current'>") == -1 ||
627 http_htmlescape(clt, p) == -1 ||
628 http_writes(clt, "</a></p>") == -1 ||
629 http_writes(clt, "<form action=ctrls method=post"
630 " enctype='"FORM_URLENCODED"'>") == -1 ||
631 http_writes(clt, "<button type=submit name=ctl value=prev>"
632 ICON_PREV"</button>") == -1 ||
633 http_fmt(clt, "<button id='toggle' type=submit name=ctl value=%s>"
634 "%s</button>", playing ? "pause" : "play",
635 playing ? ICON_PAUSE : ICON_PLAY) == -1 ||
636 http_writes(clt, "<button type=submit name=ctl value=next>"
637 ICON_NEXT"</button>") == -1 ||
638 http_writes(clt, "</form>") == -1 ||
639 http_writes(clt, "<form action=mode method=post"
640 " enctype='"FORM_URLENCODED"'>") == -1 ||
641 http_fmt(clt, "<button%s id=rall type=submit name=mode value=all>"
642 ICON_REPEAT_ALL"</button>", ac) == -1 ||
643 http_fmt(clt, "<button%s id=rone type=submit name=mode value=one>"
644 ICON_REPEAT_ONE"</button>", oc) == -1 ||
645 http_writes(clt, "</form>") == -1 ||
646 http_writes(clt, "</section>") == -1)
651 route_home(struct client *clt)
653 if (http_reply(clt, 200, "OK", "text/html;charset=UTF-8") == -1)
656 if (http_write(clt, head, strlen(head)) == -1)
659 if (http_writes(clt, "<main>") == -1)
662 render_controls(clt);
663 render_playlist(clt);
665 if (http_writes(clt, "</main>") == -1)
668 http_write(clt, foot, strlen(foot));
672 route_jump(struct client *clt)
678 http_postdata(clt, &form, NULL);
679 while ((field = strsep(&form, "&")) != NULL) {
680 if (url_decode(field) == -1)
683 if (strncmp(field, "jump=", 5) != 0)
688 memset(&path, 0, sizeof(path));
689 if (strlcpy(path, field, sizeof(path)) >= sizeof(path))
692 imsg_compose(&imsgbuf, IMSG_CTL_JUMP, 0, 0, -1,
694 ev_add(imsgbuf.w.fd, POLLIN|POLLOUT, imsg_dispatch, NULL);
701 if (!strncmp(clt->req.path, "/a/", 2))
702 http_reply(clt, 200, "OK", "text/plain");
704 http_reply(clt, 302, "See Other", "/");
708 http_reply(clt, 400, "Bad Request", "text/plain");
709 http_writes(clt, "Bad Request.\n");
713 route_controls(struct client *clt)
718 http_postdata(clt, &form, NULL);
719 while ((field = strsep(&form, "&")) != NULL) {
720 if (url_decode(field) == -1)
723 if (strncmp(field, "ctl=", 4) != 0)
728 if (!strcmp(field, "play"))
730 else if (!strcmp(field, "pause"))
731 cmd = IMSG_CTL_PAUSE;
732 else if (!strcmp(field, "next"))
734 else if (!strcmp(field, "prev"))
739 imsg_compose(&imsgbuf, cmd, 0, 0, -1, NULL, 0);
740 imsg_flush(&imsgbuf);
747 if (!strncmp(clt->req.path, "/a/", 2))
748 http_reply(clt, 200, "OK", "text/plain");
750 http_reply(clt, 302, "See Other", "/");
754 http_reply(clt, 400, "Bad Request", "text/plain");
755 http_writes(clt, "Bad Request.\n");
759 route_mode(struct client *clt)
763 struct player_mode pm;
765 pm.repeat_one = pm.repeat_all = pm.consume = MODE_UNDEF;
767 http_postdata(clt, &form, NULL);
768 while ((field = strsep(&form, "&")) != NULL) {
769 if (url_decode(field) == -1)
772 if (strncmp(field, "mode=", 5) != 0)
777 if (!strcmp(field, "all"))
778 pm.repeat_all = MODE_TOGGLE;
779 else if (!strcmp(field, "one"))
780 pm.repeat_one = MODE_TOGGLE;
784 imsg_compose(&imsgbuf, IMSG_CTL_MODE, 0, 0, -1,
786 ev_add(imsgbuf.w.fd, POLLIN|POLLOUT, imsg_dispatch, NULL);
793 if (!strncmp(clt->req.path, "/a/", 2))
794 http_reply(clt, 200, "OK", "text/plain");
796 http_reply(clt, 302, "See Other", "/");
800 http_reply(clt, 400, "Bad Request", "text/plain");
801 http_writes(clt, "Bad Request.\n");
805 route_handle_ws(struct client *clt)
807 struct buffer *rbuf = &clt->bio.rbuf;
811 if (ws_read(clt, &type, &len) == -1) {
812 if (errno != EAGAIN) {
821 ws_compose(clt, WST_PONG, rbuf->buf, len);
824 /* log_info("<<< %.*s", (int)len, rbuf->buf); */
827 /* TODO send a close too (ack) */
831 log_info("got unexpected ws frame type 0x%02x", type);
835 buf_drain(rbuf, len);
839 route_init_ws(struct client *clt)
841 if (!(clt->req.flags & (R_CONNUPGR|R_UPGRADEWS|R_WSVERSION)) ||
842 clt->req.secret == NULL) {
843 http_reply(clt, 400, "Bad Request", "text/plain");
844 http_writes(clt, "Invalid websocket handshake.\r\n");
850 clt->route = route_handle_ws;
851 http_reply(clt, 101, "Switching Protocols", NULL);
855 route_assets(struct client *clt)
857 if (!strcmp(clt->req.path, "/style.css")) {
858 http_reply(clt, 200, "OK", "text/css");
859 http_write(clt, css, strlen(css));
863 if (!strcmp(clt->req.path, "/app.js")) {
864 http_reply(clt, 200, "OK", "application/javascript");
865 http_write(clt, js, strlen(js));
873 route_dispatch(struct client *clt)
875 static const struct route {
880 { METHOD_GET, "/", &route_home },
882 { METHOD_POST, "/jump", &route_jump },
883 { METHOD_POST, "/ctrls", &route_controls },
884 { METHOD_POST, "/mode", &route_mode },
886 { METHOD_POST, "/a/jump", &route_jump },
887 { METHOD_POST, "/a/ctrls", &route_controls },
888 { METHOD_POST, "/a/mode", &route_mode },
890 { METHOD_GET, "/ws", &route_init_ws },
892 { METHOD_GET, "/style.css", &route_assets },
893 { METHOD_GET, "/app.js", &route_assets },
895 { METHOD_GET, "*", &route_notfound },
896 { METHOD_POST, "*", &route_notfound },
898 struct request *req = &clt->req;
901 if ((req->method != METHOD_GET && req->method != METHOD_POST) ||
902 (req->ctype != NULL && strcmp(req->ctype, FORM_URLENCODED) != 0) ||
904 http_reply(clt, 400, "Bad Request", NULL);
908 for (i = 0; i < nitems(routes); ++i) {
909 if (req->method != routes[i].method ||
910 fnmatch(routes[i].path, req->path, 0) != 0)
912 clt->done = 1; /* assume with one round is done */
913 clt->route = routes[i].route;
922 client_ev(int fd, int ev, void *d)
924 struct client *clt = d;
926 if (ev & (POLLIN|POLLHUP)) {
927 if (bufio_read(&clt->bio) == -1 && errno != EAGAIN) {
928 log_warn("bufio_read");
934 if (bufio_write(&clt->bio) == -1 && errno != EAGAIN) {
935 log_warn("bufio_write");
940 if (clt->route == NULL) {
941 if (http_parse(clt) == -1) {
944 log_warnx("HTTP parse request failed");
947 if (clt->req.method == METHOD_POST &&
948 http_read(clt) == -1) {
951 log_warnx("failed to read POST data");
958 if (!clt->done && !clt->err)
962 ev = bufio_pollev(&clt->bio);
963 if (ev == POLLIN && (clt->done || clt->err)) {
964 goto err; /* done with this client */
967 ev_add(fd, ev, client_ev, clt);
972 TAILQ_REMOVE(&clients, clt, clients);
977 web_accept(int psock, int ev, void *d)
982 if ((sock = accept(psock, NULL, NULL)) == -1) {
986 if ((clt = calloc(1, sizeof(*clt))) == NULL ||
987 http_init(clt, sock) == -1) {
988 log_warn("failed to initialize client");
994 TAILQ_INSERT_TAIL(&clients, clt, clients);
996 client_ev(sock, POLLIN, clt);
1003 fprintf(stderr, "usage: %s [-v] [-s sock] [[host] port]\n",
1009 main(int argc, char **argv)
1011 struct addrinfo hints, *res, *res0;
1012 const char *cause = NULL;
1013 const char *host = NULL;
1014 const char *port = "9090";
1016 size_t nsock, error, save_errno;
1017 int ch, v, amused_sock, fd;
1020 TAILQ_INIT(&clients);
1021 setlocale(LC_ALL, NULL);
1023 log_init(1, LOG_DAEMON);
1025 if (pledge("stdio rpath unix inet dns", NULL) == -1)
1028 while ((ch = getopt(argc, argv, "s:v")) != -1) {
1052 log_setverbose(verbose);
1057 if ((tmpdir = getenv("TMPDIR")) == NULL)
1060 xasprintf(&sock, "%s/amused-%d", tmpdir, getuid());
1063 signal(SIGPIPE, SIG_IGN);
1065 if (ev_init() == -1)
1068 amused_sock = dial(sock);
1069 imsg_init(&imsgbuf, amused_sock);
1070 imsg_compose(&imsgbuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
1071 imsg_compose(&imsgbuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
1072 imsg_compose(&imsgbuf, IMSG_CTL_MONITOR, 0, 0, -1, NULL, 0);
1073 ev_add(amused_sock, POLLIN|POLLOUT, imsg_dispatch, NULL);
1075 memset(&hints, 0, sizeof(hints));
1076 hints.ai_family = AF_UNSPEC;
1077 hints.ai_socktype = SOCK_STREAM;
1078 hints.ai_flags = AI_PASSIVE;
1079 error = getaddrinfo(host, port, &hints, &res0);
1081 errx(1, "%s", gai_strerror(error));
1084 for (res = res0; res; res = res->ai_next) {
1085 fd = socket(res->ai_family, res->ai_socktype,
1093 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1094 &v, sizeof(v)) == -1)
1095 fatal("setsockopt(SO_REUSEADDR)");
1097 if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
1105 if (listen(fd, 5) == -1)
1108 if (ev_add(fd, POLLIN, web_accept, NULL) == -1)
1113 err(1, "%s", cause);
1116 if (pledge("stdio inet", NULL) == -1)
1119 log_info("listening on port %s", port);