Annotation of src/usr.bin/aucat/listen.c, Revision 1.2
1.2 ! ratchov 1: /* $OpenBSD: listen.c,v 1.1 2008/10/26 08:49:44 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 *
49: listen_new(struct fileops *ops, char *path)
50: {
51: int sock;
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");
58: exit(1);
59: }
60: if (unlink(path) < 0 && errno != ENOENT) {
61: perror("unlink");
62: exit(1);
63: }
64: sockname.sun_family = AF_UNIX;
65: strlcpy(sockname.sun_path, path, sizeof(sockname.sun_path));
66: if (bind(sock, (struct sockaddr *)&sockname,
67: sizeof(struct sockaddr_un)) < 0) {
68: perror("bind");
69: exit(1);
1.2 ! ratchov 70: }
! 71: if (chmod(sockname.sun_path, 0777) < 0) {
! 72: /* not fatal, just print error */
! 73: perror(sockname.sun_path);
1.1 ratchov 74: }
75: if (listen(sock, 1) < 0) {
76: perror("listen");
77: exit(1);
78: }
79: f = (struct listen *)file_new(ops, path, 1);
80: f->path = strdup(path);
81: if (f->path == NULL) {
82: perror("strdup");
83: exit(1);
84: }
85: f->fd = sock;
86: return f;
87: }
88:
89: int
90: listen_nfds(struct file *f) {
91: return 1;
92: }
93:
94: int
95: listen_pollfd(struct file *file, struct pollfd *pfd, int events)
96: {
97: struct listen *f = (struct listen *)file;
98:
99: pfd->fd = f->fd;
100: pfd->events = POLLIN;
101: return 1;
102: }
103:
104: int
105: listen_revents(struct file *file, struct pollfd *pfd)
106: {
107: struct listen *f = (struct listen *)file;
108: struct sockaddr caddr;
109: socklen_t caddrlen;
110: int sock;
111:
112: if (pfd->revents & POLLIN) {
113: DPRINTF("listen_revents: accepting connection\n");
114: caddrlen = sizeof(caddrlen);
115: sock = accept(f->fd, &caddr, &caddrlen);
116: if (sock < 0) {
117: perror("accept");
118: return 0;
119: }
120: if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
121: perror("fcntl(sock, O_NONBLOCK)");
122: close(sock);
123: return 0;
124: }
125: (void)sock_new(&sock_ops, sock, "socket");
126: }
127: return 0;
128: }
129:
130: void
131: listen_close(struct file *file)
132: {
133: struct listen *f = (struct listen *)file;
134:
135: (void)unlink(f->path);
136: free(f->path);
137: (void)close(f->fd);
138: }