[BACK]Return to listen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/listen.c, Revision 1.13

1.13    ! deraadt     1: /*     $OpenBSD: listen.c,v 1.12 2011/04/19 00:02:29 ratchov Exp $     */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      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.
                      8:  *
                      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.
                     16:  */
                     17: #include <sys/types.h>
                     18: #include <sys/socket.h>
1.13    ! deraadt    19: #include <sys/signal.h>
1.2       ratchov    20: #include <sys/stat.h>
1.1       ratchov    21: #include <sys/un.h>
                     22: #include <err.h>
                     23: #include <errno.h>
                     24: #include <fcntl.h>
                     25: #include <poll.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29: #include <unistd.h>
                     30:
                     31: #include "conf.h"
1.10      ratchov    32: #include "listen.h"
1.1       ratchov    33: #include "sock.h"
                     34:
                     35: struct fileops listen_ops = {
                     36:        "listen",
                     37:        sizeof(struct listen),
                     38:        listen_close,
                     39:        NULL, /* read */
                     40:        NULL, /* write */
                     41:        NULL, /* start */
                     42:        NULL, /* stop */
                     43:        listen_nfds,
                     44:        listen_pollfd,
                     45:        listen_revents
                     46: };
                     47:
1.12      ratchov    48: void
                     49: listen_new_un(char *path)
1.1       ratchov    50: {
1.7       ratchov    51:        int sock, oldumask;
1.1       ratchov    52:        struct sockaddr_un sockname;
                     53:        struct listen *f;
                     54:
                     55:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                     56:        if (sock < 0) {
                     57:                perror("socket");
1.12      ratchov    58:                exit(1);
1.1       ratchov    59:        }
                     60:        if (unlink(path) < 0 && errno != ENOENT) {
                     61:                perror("unlink");
1.8       ratchov    62:                goto bad_close;
1.1       ratchov    63:        }
                     64:        sockname.sun_family = AF_UNIX;
                     65:        strlcpy(sockname.sun_path, path, sizeof(sockname.sun_path));
1.7       ratchov    66:        oldumask = umask(0111);
1.6       ratchov    67:        if (bind(sock, (struct sockaddr *)&sockname,
                     68:                sizeof(struct sockaddr_un)) < 0) {
1.1       ratchov    69:                perror("bind");
1.8       ratchov    70:                goto bad_close;
1.2       ratchov    71:        }
1.7       ratchov    72:        umask(oldumask);
1.1       ratchov    73:        if (listen(sock, 1) < 0) {
                     74:                perror("listen");
1.8       ratchov    75:                goto bad_close;
1.1       ratchov    76:        }
1.12      ratchov    77:        f = (struct listen *)file_new(&listen_ops, path, 1);
1.8       ratchov    78:        if (f == NULL)
                     79:                goto bad_close;
1.1       ratchov    80:        f->path = strdup(path);
                     81:        if (f->path == NULL) {
                     82:                perror("strdup");
                     83:                exit(1);
                     84:        }
                     85:        f->fd = sock;
1.12      ratchov    86:        return;
1.8       ratchov    87:  bad_close:
                     88:        close(sock);
1.12      ratchov    89:        exit(1);
1.1       ratchov    90: }
                     91:
                     92: int
                     93: listen_nfds(struct file *f) {
                     94:        return 1;
                     95: }
                     96:
                     97: int
                     98: listen_pollfd(struct file *file, struct pollfd *pfd, int events)
                     99: {
                    100:        struct listen *f = (struct listen *)file;
                    101:
                    102:        pfd->fd = f->fd;
                    103:        pfd->events = POLLIN;
                    104:        return 1;
                    105: }
                    106:
                    107: int
                    108: listen_revents(struct file *file, struct pollfd *pfd)
                    109: {
                    110:        struct listen *f = (struct listen *)file;
                    111:        struct sockaddr caddr;
                    112:        socklen_t caddrlen;
                    113:        int sock;
                    114:
                    115:        if (pfd->revents & POLLIN) {
                    116:                caddrlen = sizeof(caddrlen);
                    117:                sock = accept(f->fd, &caddr, &caddrlen);
                    118:                if (sock < 0) {
1.12      ratchov   119:                        /* XXX: should we kill the socket here ? */
1.1       ratchov   120:                        perror("accept");
                    121:                        return 0;
                    122:                }
                    123:                if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
                    124:                        perror("fcntl(sock, O_NONBLOCK)");
                    125:                        close(sock);
                    126:                        return 0;
                    127:                }
1.9       ratchov   128:                if (sock_new(&sock_ops, sock) == NULL) {
1.8       ratchov   129:                        close(sock);
                    130:                        return 0;
                    131:                }
1.1       ratchov   132:        }
                    133:        return 0;
                    134: }
                    135:
                    136: void
                    137: listen_close(struct file *file)
                    138: {
                    139:        struct listen *f = (struct listen *)file;
                    140:
1.12      ratchov   141:        if (f->path != NULL) {
                    142:                unlink(f->path);
                    143:                free(f->path);
                    144:        }
1.10      ratchov   145:        close(f->fd);
1.12      ratchov   146: }
                    147:
                    148: void
                    149: listen_closeall(void)
                    150: {
                    151:        struct file *f, *fnext;
                    152:
                    153:        for (f = LIST_FIRST(&file_list); f != NULL; f = fnext) {
                    154:                fnext = LIST_NEXT(f, entry);
                    155:                if (f->ops == &listen_ops)
                    156:                        file_close(f);
                    157:        }
1.1       ratchov   158: }