Annotation of src/usr.bin/aucat/listen.c, Revision 1.4
1.4 ! ratchov 1: /* $OpenBSD: listen.c,v 1.3 2008/11/16 16:30:22 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));
67: if (bind(sock, (struct sockaddr *)&sockname,
68: sizeof(struct sockaddr_un)) < 0) {
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.1 ratchov 90: return f;
91: }
92:
93: int
94: listen_nfds(struct file *f) {
95: return 1;
96: }
97:
98: int
99: listen_pollfd(struct file *file, struct pollfd *pfd, int events)
100: {
101: struct listen *f = (struct listen *)file;
102:
103: pfd->fd = f->fd;
104: pfd->events = POLLIN;
105: return 1;
106: }
107:
108: int
109: listen_revents(struct file *file, struct pollfd *pfd)
110: {
111: struct listen *f = (struct listen *)file;
112: struct sockaddr caddr;
113: socklen_t caddrlen;
114: int sock;
115:
116: if (pfd->revents & POLLIN) {
117: DPRINTF("listen_revents: accepting connection\n");
118: caddrlen = sizeof(caddrlen);
119: sock = accept(f->fd, &caddr, &caddrlen);
120: if (sock < 0) {
121: perror("accept");
122: return 0;
123: }
124: if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
125: perror("fcntl(sock, O_NONBLOCK)");
126: close(sock);
127: return 0;
128: }
1.4 ! ratchov 129: (void)sock_new(&sock_ops, sock, "socket",
! 130: &f->wpar, &f->rpar, f->maxweight);
1.1 ratchov 131: }
132: return 0;
133: }
134:
135: void
136: listen_close(struct file *file)
137: {
138: struct listen *f = (struct listen *)file;
139:
140: (void)unlink(f->path);
141: free(f->path);
142: (void)close(f->fd);
143: }