[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.6

1.6     ! ratchov     1: /*     $OpenBSD: listen.c,v 1.5 2008/11/16 20:44:03 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.2       ratchov    19: #include <sys/stat.h>
1.1       ratchov    20: #include <sys/un.h>
                     21:
                     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"
                     32: #include "sock.h"
                     33: #include "listen.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:
                     48: struct listen *
1.4       ratchov    49: listen_new(struct fileops *ops, char *path,
                     50:     struct aparams *wpar, struct aparams *rpar, int maxweight)
1.1       ratchov    51: {
                     52:        int sock;
                     53:        struct sockaddr_un sockname;
                     54:        struct listen *f;
                     55:
                     56:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                     57:        if (sock < 0) {
                     58:                perror("socket");
                     59:                exit(1);
                     60:        }
                     61:        if (unlink(path) < 0 && errno != ENOENT) {
                     62:                perror("unlink");
                     63:                exit(1);
                     64:        }
                     65:        sockname.sun_family = AF_UNIX;
                     66:        strlcpy(sockname.sun_path, path, sizeof(sockname.sun_path));
1.6     ! ratchov    67:        if (bind(sock, (struct sockaddr *)&sockname,
        !            68:                sizeof(struct sockaddr_un)) < 0) {
1.1       ratchov    69:                perror("bind");
                     70:                exit(1);
1.2       ratchov    71:        }
                     72:        if (chmod(sockname.sun_path, 0777) < 0) {
                     73:                /* not fatal, just print error */
                     74:                perror(sockname.sun_path);
1.1       ratchov    75:        }
                     76:        if (listen(sock, 1) < 0) {
                     77:                perror("listen");
                     78:                exit(1);
                     79:        }
                     80:        f = (struct listen *)file_new(ops, path, 1);
                     81:        f->path = strdup(path);
                     82:        if (f->path == NULL) {
                     83:                perror("strdup");
                     84:                exit(1);
                     85:        }
                     86:        f->fd = sock;
1.4       ratchov    87:        f->wpar = *wpar;
                     88:        f->rpar = *rpar;
1.3       ratchov    89:        f->maxweight = maxweight;
1.5       ratchov    90: #ifdef DEBUG
                     91:        if (debug_level > 0) {
                     92:                fprintf(stderr, "listen_new: %s: wpar=", f->path);
                     93:                aparams_print(&f->wpar);
                     94:                fprintf(stderr, ", rpar=");
                     95:                aparams_print(&f->rpar);
                     96:                fprintf(stderr, ", vol=%u\n", f->maxweight);
                     97:        }
                     98: #endif
1.1       ratchov    99:        return f;
                    100: }
                    101:
                    102: int
                    103: listen_nfds(struct file *f) {
                    104:        return 1;
                    105: }
                    106:
                    107: int
                    108: listen_pollfd(struct file *file, struct pollfd *pfd, int events)
                    109: {
                    110:        struct listen *f = (struct listen *)file;
                    111:
                    112:        pfd->fd = f->fd;
                    113:        pfd->events = POLLIN;
                    114:        return 1;
                    115: }
                    116:
                    117: int
                    118: listen_revents(struct file *file, struct pollfd *pfd)
                    119: {
                    120:        struct listen *f = (struct listen *)file;
                    121:        struct sockaddr caddr;
                    122:        socklen_t caddrlen;
                    123:        int sock;
                    124:
                    125:        if (pfd->revents & POLLIN) {
1.5       ratchov   126:                DPRINTF("listen_revents: %s: accepting connection\n", f->path);
1.1       ratchov   127:                caddrlen = sizeof(caddrlen);
                    128:                sock = accept(f->fd, &caddr, &caddrlen);
                    129:                if (sock < 0) {
                    130:                        perror("accept");
                    131:                        return 0;
                    132:                }
                    133:                if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
                    134:                        perror("fcntl(sock, O_NONBLOCK)");
                    135:                        close(sock);
                    136:                        return 0;
                    137:                }
1.4       ratchov   138:                (void)sock_new(&sock_ops, sock, "socket",
                    139:                    &f->wpar, &f->rpar, f->maxweight);
1.1       ratchov   140:        }
                    141:        return 0;
                    142: }
                    143:
                    144: void
                    145: listen_close(struct file *file)
                    146: {
                    147:        struct listen *f = (struct listen *)file;
                    148:
                    149:        (void)unlink(f->path);
                    150:        free(f->path);
                    151:        (void)close(f->fd);
                    152: }