From 392ca6ca04b2a1744a54165898bbd3aab531919f Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Tue, 19 Apr 2011 00:02:30 +0000 Subject: don't hold a pointer to "listen" structure. Instead, when it must be free()ed, iterate over the file_list, and find the structure to free. This is safer and simpler. No behaviour change --- usr.bin/aucat/aucat.c | 20 +++++++------------- usr.bin/aucat/listen.c | 34 ++++++++++++++++++++++++---------- usr.bin/aucat/listen.h | 6 +++--- 3 files changed, 34 insertions(+), 26 deletions(-) (limited to 'usr.bin') 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 * @@ -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 * @@ -18,7 +18,6 @@ #include #include #include - #include #include #include @@ -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 * @@ -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) */ -- cgit v1.2.3