diff options
-rw-r--r-- | usr.bin/aucat/aucat.c | 20 | ||||
-rw-r--r-- | usr.bin/aucat/listen.c | 34 | ||||
-rw-r--r-- | usr.bin/aucat/listen.h | 6 |
3 files changed, 34 insertions, 26 deletions
diff --git a/usr.bin/aucat/aucat.c b/usr.bin/aucat/aucat.c index 9eca115936d..bc2a57dc0bc 100644 --- a/usr.bin/aucat/aucat.c +++ b/usr.bin/aucat/aucat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aucat.c,v 1.109 2011/04/16 11:51:48 ratchov Exp $ */ +/* $OpenBSD: aucat.c,v 1.110 2011/04/19 00:02:28 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -428,7 +428,6 @@ aucat_main(int argc, char **argv) struct cfmid *cm; struct cfstr *cs; struct cfdev *cd; - struct listen *listen = NULL; int c, u_flag, d_flag, l_flag, n_flag, unit; char base[PATH_MAX], path[PATH_MAX]; unsigned mode, rate; @@ -765,9 +764,7 @@ aucat_main(int argc, char **argv) if (nsock > 0) { snprintf(path, sizeof(path), "%s/%s%u", base, AUCAT_PATH, unit); - listen = listen_new(&listen_ops, path); - if (listen == NULL) - exit(1); + listen_new_un(path); } if (geteuid() == 0) privdrop(); @@ -802,8 +799,8 @@ aucat_main(int argc, char **argv) break; } fatal: - if (nsock > 0) - file_close(&listen->file); + listen_closeall(); + /* * give a chance to drain */ @@ -838,7 +835,6 @@ midicat_main(int argc, char **argv) struct cfmid *cm; struct cfstr *cs; struct cfdev *cd; - struct listen *listen = NULL; int c, d_flag, l_flag, unit, fd; char base[PATH_MAX], path[PATH_MAX]; struct file *stdx; @@ -1035,9 +1031,7 @@ midicat_main(int argc, char **argv) if (nsock > 0) { snprintf(path, sizeof(path), "%s/%s%u", base, MIDICAT_PATH, unit); - listen = listen_new(&listen_ops, path); - if (listen == NULL) - exit(1); + listen_new_un(path); } if (geteuid() == 0) privdrop(); @@ -1065,8 +1059,8 @@ midicat_main(int argc, char **argv) break; } fatal: - if (nsock > 0) - file_close(&listen->file); + listen_closeall(); + /* * give a chance to drain */ diff --git a/usr.bin/aucat/listen.c b/usr.bin/aucat/listen.c index 90be6b9628a..6939a0b8829 100644 --- a/usr.bin/aucat/listen.c +++ b/usr.bin/aucat/listen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: listen.c,v 1.11 2009/09/27 11:51:20 ratchov Exp $ */ +/* $OpenBSD: listen.c,v 1.12 2011/04/19 00:02:29 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -18,7 +18,6 @@ #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> - #include <err.h> #include <errno.h> #include <fcntl.h> @@ -45,8 +44,8 @@ struct fileops listen_ops = { listen_revents }; -struct listen * -listen_new(struct fileops *ops, char *path) +void +listen_new_un(char *path) { int sock, oldumask; struct sockaddr_un sockname; @@ -55,7 +54,7 @@ listen_new(struct fileops *ops, char *path) sock = socket(AF_UNIX, SOCK_STREAM, 0); if (sock < 0) { perror("socket"); - return NULL; + exit(1); } if (unlink(path) < 0 && errno != ENOENT) { perror("unlink"); @@ -74,7 +73,7 @@ listen_new(struct fileops *ops, char *path) perror("listen"); goto bad_close; } - f = (struct listen *)file_new(ops, path, 1); + f = (struct listen *)file_new(&listen_ops, path, 1); if (f == NULL) goto bad_close; f->path = strdup(path); @@ -83,10 +82,10 @@ listen_new(struct fileops *ops, char *path) exit(1); } f->fd = sock; - return f; + return; bad_close: close(sock); - return NULL; + exit(1); } int @@ -116,6 +115,7 @@ listen_revents(struct file *file, struct pollfd *pfd) caddrlen = sizeof(caddrlen); sock = accept(f->fd, &caddr, &caddrlen); if (sock < 0) { + /* XXX: should we kill the socket here ? */ perror("accept"); return 0; } @@ -137,7 +137,21 @@ listen_close(struct file *file) { struct listen *f = (struct listen *)file; - unlink(f->path); - free(f->path); + if (f->path != NULL) { + unlink(f->path); + free(f->path); + } close(f->fd); } + +void +listen_closeall(void) +{ + struct file *f, *fnext; + + for (f = LIST_FIRST(&file_list); f != NULL; f = fnext) { + fnext = LIST_NEXT(f, entry); + if (f->ops == &listen_ops) + file_close(f); + } +} diff --git a/usr.bin/aucat/listen.h b/usr.bin/aucat/listen.h index 8b156d61925..d063af50c05 100644 --- a/usr.bin/aucat/listen.h +++ b/usr.bin/aucat/listen.h @@ -1,4 +1,4 @@ -/* $OpenBSD: listen.h,v 1.5 2009/07/25 10:52:19 ratchov Exp $ */ +/* $OpenBSD: listen.h,v 1.6 2011/04/19 00:02:29 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -28,11 +28,11 @@ struct listen { int fd; }; -struct listen *listen_new(struct fileops *, char *); +void listen_new_un(char *); int listen_nfds(struct file *); int listen_pollfd(struct file *, struct pollfd *, int); int listen_revents(struct file *, struct pollfd *); void listen_close(struct file *); -extern struct fileops listen_ops; +void listen_closeall(void); #endif /* !defined(LISTEN_H) */ |