Annotation of src/usr.bin/aucat/listen.c, Revision 1.10
1.10 ! ratchov 1: /* $OpenBSD: listen.c,v 1.9 2009/07/25 08:44:27 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"
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:
48: struct listen *
1.9 ratchov 49: listen_new(struct fileops *ops, 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.8 ratchov 58: return NULL;
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: }
77: f = (struct listen *)file_new(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;
86: return f;
1.8 ratchov 87: bad_close:
88: close(sock);
89: return NULL;
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) {
1.5 ratchov 116: DPRINTF("listen_revents: %s: accepting connection\n", f->path);
1.1 ratchov 117: caddrlen = sizeof(caddrlen);
118: sock = accept(f->fd, &caddr, &caddrlen);
119: if (sock < 0) {
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.10 ! ratchov 141: unlink(f->path);
1.1 ratchov 142: free(f->path);
1.10 ! ratchov 143: close(f->fd);
1.1 ratchov 144: }