2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 d3a08f4d 2021-01-17 op * Permission to use, copy, modify, and distribute this software for any
5 d3a08f4d 2021-01-17 op * purpose with or without fee is hereby granted, provided that the above
6 d3a08f4d 2021-01-17 op * copyright notice and this permission notice appear in all copies.
8 d3a08f4d 2021-01-17 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d3a08f4d 2021-01-17 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d3a08f4d 2021-01-17 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d3a08f4d 2021-01-17 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d3a08f4d 2021-01-17 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d3a08f4d 2021-01-17 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d3a08f4d 2021-01-17 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d3a08f4d 2021-01-17 op #include <sys/mman.h>
18 d3a08f4d 2021-01-17 op #include <sys/stat.h>
20 d3a08f4d 2021-01-17 op #include <netdb.h>
22 d3a08f4d 2021-01-17 op #include <assert.h>
23 d3a08f4d 2021-01-17 op #include <err.h>
24 d3a08f4d 2021-01-17 op #include <errno.h>
25 d3a08f4d 2021-01-17 op #include <fcntl.h>
26 d3a08f4d 2021-01-17 op #include <string.h>
28 d3a08f4d 2021-01-17 op #include "gmid.h"
30 d3a08f4d 2021-01-17 op int connected_clients;
33 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
35 d3a08f4d 2021-01-17 op struct stat sb;
37 d3a08f4d 2021-01-17 op assert(path != NULL);
38 d3a08f4d 2021-01-17 op if ((*fd = openat(c->host->dirfd, *path ? path : ".",
39 f87bf597 2021-01-17 op O_RDONLY | O_NOFOLLOW)) == -1) {
40 d3a08f4d 2021-01-17 op return FILE_MISSING;
43 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
44 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
45 d3a08f4d 2021-01-17 op return FILE_MISSING;
48 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
49 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
51 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
52 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
54 d3a08f4d 2021-01-17 op return FILE_EXISTS;
58 d3a08f4d 2021-01-17 op open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
60 d3a08f4d 2021-01-17 op switch (check_path(c, fpath, &c->fd)) {
61 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
62 d3a08f4d 2021-01-17 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
63 d3a08f4d 2021-01-17 op return start_cgi(fpath, "", query, fds, c);
65 d3a08f4d 2021-01-17 op /* fallthrough */
67 d3a08f4d 2021-01-17 op case FILE_EXISTS:
68 d3a08f4d 2021-01-17 op if ((c->len = filesize(c->fd)) == -1) {
69 d3a08f4d 2021-01-17 op LOGE(c, "failed to get file size for %s", fpath);
70 d3a08f4d 2021-01-17 op goodbye(fds, c);
74 d3a08f4d 2021-01-17 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
75 d3a08f4d 2021-01-17 op c->fd, 0)) == MAP_FAILED) {
76 d3a08f4d 2021-01-17 op warn("mmap: %s", fpath);
77 d3a08f4d 2021-01-17 op goodbye(fds, c);
80 d3a08f4d 2021-01-17 op c->i = c->buf;
81 d3a08f4d 2021-01-17 op return start_reply(fds, c, SUCCESS, mime(fpath));
83 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
84 d3a08f4d 2021-01-17 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
87 d3a08f4d 2021-01-17 op send_dir(fpath, fds, c);
90 d3a08f4d 2021-01-17 op case FILE_MISSING:
91 d3a08f4d 2021-01-17 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
92 d3a08f4d 2021-01-17 op return check_for_cgi(fpath, query, fds, c);
94 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
96 d3a08f4d 2021-01-17 op goodbye(fds, c);
100 d3a08f4d 2021-01-17 op /* unreachable */
107 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
108 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
109 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
110 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
111 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
114 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
117 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
119 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
121 d3a08f4d 2021-01-17 op while (end > path) {
122 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
123 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
124 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
125 d3a08f4d 2021-01-17 op while (*end != '/')
129 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
130 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
131 d3a08f4d 2021-01-17 op return start_cgi(path, end+1, query, fds,c);
132 d3a08f4d 2021-01-17 op case FILE_MISSING:
143 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
145 d3a08f4d 2021-01-17 op goodbye(fds, c);
150 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
154 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
155 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
156 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
157 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
161 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
163 d3a08f4d 2021-01-17 op struct vhost *h;
164 d3a08f4d 2021-01-17 op const char *servname;
166 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
167 d3a08f4d 2021-01-17 op case 0: /* success */
168 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
170 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
171 d3a08f4d 2021-01-17 op fds->events = POLLIN;
173 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
174 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
177 d3a08f4d 2021-01-17 op /* unreachable */
181 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
182 d3a08f4d 2021-01-17 op if (servname == NULL)
183 d3a08f4d 2021-01-17 op goto hostnotfound;
185 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
186 d3a08f4d 2021-01-17 op if (!strcmp(h->domain, servname) || !strcmp(h->domain, "*"))
190 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
191 d3a08f4d 2021-01-17 op c->state = S_OPEN;
193 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
197 d3a08f4d 2021-01-17 op hostnotfound:
198 d3a08f4d 2021-01-17 op /* XXX: check the correct response */
199 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
201 d3a08f4d 2021-01-17 op goodbye(fds, c);
205 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
207 d3a08f4d 2021-01-17 op char buf[GEMINI_URL_LEN];
208 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
209 d3a08f4d 2021-01-17 op struct iri iri;
211 d3a08f4d 2021-01-17 op bzero(buf, sizeof(buf));
213 d3a08f4d 2021-01-17 op switch (tls_read(c->ctx, buf, sizeof(buf)-1)) {
215 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
216 d3a08f4d 2021-01-17 op goodbye(fds, c);
219 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
220 d3a08f4d 2021-01-17 op fds->events = POLLIN;
223 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
224 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
228 d3a08f4d 2021-01-17 op if (!trim_req_iri(buf) || !parse_iri(buf, &iri, &parse_err)) {
229 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, BAD_REQUEST, parse_err))
231 d3a08f4d 2021-01-17 op goodbye(fds, c);
235 d3a08f4d 2021-01-17 op if (strcmp(iri.schema, "gemini") || iri.port_no != conf.port) {
236 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
238 d3a08f4d 2021-01-17 op goodbye(fds, c);
242 d3a08f4d 2021-01-17 op LOGI(c, "GET %s%s%s",
243 d3a08f4d 2021-01-17 op *iri.path ? iri.path : "/",
244 d3a08f4d 2021-01-17 op *iri.query ? "?" : "",
245 d3a08f4d 2021-01-17 op *iri.query ? iri.query : "");
247 d3a08f4d 2021-01-17 op send_file(iri.path, iri.query, fds, c);
251 d3a08f4d 2021-01-17 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
253 d3a08f4d 2021-01-17 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
256 d3a08f4d 2021-01-17 op client->code = code;
257 d3a08f4d 2021-01-17 op client->meta = reason;
258 d3a08f4d 2021-01-17 op client->state = S_INITIALIZING;
260 d3a08f4d 2021-01-17 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
261 d3a08f4d 2021-01-17 op assert(len < (int)sizeof(buf));
263 d3a08f4d 2021-01-17 op switch (tls_write(client->ctx, buf, len)) {
264 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
265 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
267 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
268 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
276 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
277 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
279 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
280 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
283 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
284 d3a08f4d 2021-01-17 op addr, sizeof(addr),
286 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
290 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
291 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
292 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
293 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
295 d3a08f4d 2021-01-17 op ruser = NULL;
296 d3a08f4d 2021-01-17 op cissuer = NULL;
297 d3a08f4d 2021-01-17 op chash = NULL;
300 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
301 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
302 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
303 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
304 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
305 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
306 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
307 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
310 d3a08f4d 2021-01-17 op close(c->fd);
311 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
312 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
314 d3a08f4d 2021-01-17 op goodbye(fds, c);
317 d3a08f4d 2021-01-17 op c->child = 1;
318 d3a08f4d 2021-01-17 op c->state = S_SENDING;
319 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
320 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
324 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
325 d3a08f4d 2021-01-17 op err(1, "cannot talk to the executor process");
329 d3a08f4d 2021-01-17 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
331 d3a08f4d 2021-01-17 op ssize_t ret, len;
333 d3a08f4d 2021-01-17 op if (c->fd == -1) {
334 d3a08f4d 2021-01-17 op if (!open_file(path, query, fds, c))
336 d3a08f4d 2021-01-17 op c->state = S_SENDING;
339 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
341 d3a08f4d 2021-01-17 op while (len > 0) {
342 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
344 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
345 d3a08f4d 2021-01-17 op goodbye(fds, c);
348 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
349 d3a08f4d 2021-01-17 op fds->events = POLLIN;
352 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
353 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
363 d3a08f4d 2021-01-17 op goodbye(fds, c);
367 d3a08f4d 2021-01-17 op send_dir(char *path, struct pollfd *fds, struct client *client)
369 d3a08f4d 2021-01-17 op char fpath[PATHBUF];
372 d3a08f4d 2021-01-17 op bzero(fpath, PATHBUF);
374 d3a08f4d 2021-01-17 op if (path[0] != '.')
375 d3a08f4d 2021-01-17 op fpath[0] = '.';
377 d3a08f4d 2021-01-17 op /* this cannot fail since sizeof(fpath) > maxlen of path */
378 d3a08f4d 2021-01-17 op strlcat(fpath, path, PATHBUF);
379 d3a08f4d 2021-01-17 op len = strlen(fpath);
381 d3a08f4d 2021-01-17 op /* add a trailing / in case. */
382 d3a08f4d 2021-01-17 op if (fpath[len-1] != '/') {
383 d3a08f4d 2021-01-17 op fpath[len] = '/';
386 d3a08f4d 2021-01-17 op strlcat(fpath, "index.gmi", sizeof(fpath));
388 d3a08f4d 2021-01-17 op send_file(fpath, NULL, fds, client);
392 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
396 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
398 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
400 d3a08f4d 2021-01-17 op fds->events = POLLIN;
402 d3a08f4d 2021-01-17 op fd = fds->fd;
403 d3a08f4d 2021-01-17 op fds->fd = c->fd;
408 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
412 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
414 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
416 d3a08f4d 2021-01-17 op fd = fds->fd;
417 d3a08f4d 2021-01-17 op fds->fd = c->fd;
422 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
426 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
427 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
430 d3a08f4d 2021-01-17 op if (c->len == 0) {
431 d3a08f4d 2021-01-17 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
433 d3a08f4d 2021-01-17 op if (r == -1) {
434 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
435 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
444 d3a08f4d 2021-01-17 op while (c->len > 0) {
445 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
449 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
450 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
453 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
454 d3a08f4d 2021-01-17 op fds->events = POLLIN;
466 d3a08f4d 2021-01-17 op goodbye(fds, c);
470 d3a08f4d 2021-01-17 op goodbye(struct pollfd *pfd, struct client *c)
472 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
474 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
475 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
476 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
478 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
479 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
483 d3a08f4d 2021-01-17 op connected_clients--;
485 d3a08f4d 2021-01-17 op tls_free(c->ctx);
486 d3a08f4d 2021-01-17 op c->ctx = NULL;
488 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
489 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
491 d3a08f4d 2021-01-17 op if (c->fd != -1)
492 d3a08f4d 2021-01-17 op close(c->fd);
494 d3a08f4d 2021-01-17 op close(pfd->fd);
495 d3a08f4d 2021-01-17 op pfd->fd = -1;
499 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
502 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
503 d3a08f4d 2021-01-17 op socklen_t len;
505 d3a08f4d 2021-01-17 op len = sizeof(addr);
506 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
507 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
509 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
512 d3a08f4d 2021-01-17 op mark_nonblock(fd);
514 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
515 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
516 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
517 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
518 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
520 d3a08f4d 2021-01-17 op fds[i].fd = fd;
521 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
523 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
524 d3a08f4d 2021-01-17 op clients[i].fd = -1;
525 d3a08f4d 2021-01-17 op clients[i].child = 0;
526 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
527 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
528 d3a08f4d 2021-01-17 op clients[i].af = AF_INET;
529 d3a08f4d 2021-01-17 op clients[i].addr = addr;
531 d3a08f4d 2021-01-17 op connected_clients++;
540 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
542 d3a08f4d 2021-01-17 op switch (client->state) {
543 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
544 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
548 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
551 d3a08f4d 2021-01-17 op case S_INITIALIZING:
552 d3a08f4d 2021-01-17 op if (!start_reply(fds, client, client->code, client->meta))
555 d3a08f4d 2021-01-17 op if (client->code != SUCCESS) {
556 d3a08f4d 2021-01-17 op /* we don't need a body */
557 d3a08f4d 2021-01-17 op goodbye(fds, client);
561 d3a08f4d 2021-01-17 op client->state = S_SENDING;
563 d3a08f4d 2021-01-17 op /* fallthrough */
565 d3a08f4d 2021-01-17 op case S_SENDING:
566 d3a08f4d 2021-01-17 op if (client->child)
567 d3a08f4d 2021-01-17 op handle_cgi(fds, client);
569 d3a08f4d 2021-01-17 op send_file(NULL, NULL, fds, client);
572 d3a08f4d 2021-01-17 op case S_CLOSING:
573 d3a08f4d 2021-01-17 op goodbye(fds, client);
577 d3a08f4d 2021-01-17 op /* unreachable */
583 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
586 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
587 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
589 d3a08f4d 2021-01-17 op connected_clients = 0;
591 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
592 d3a08f4d 2021-01-17 op fds[i].fd = -1;
593 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
594 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
597 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
598 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
601 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
602 d3a08f4d 2021-01-17 op if (errno == EINTR) {
603 d3a08f4d 2021-01-17 op warnx("connected clients: %d",
604 d3a08f4d 2021-01-17 op connected_clients);
607 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
610 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
611 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
614 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
615 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
616 d3a08f4d 2021-01-17 op strerror(errno));
618 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
619 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
620 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
621 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
622 d3a08f4d 2021-01-17 op goodbye(&fds[i], &clients[i]);
627 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
628 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
629 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
630 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
632 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);