| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: npfd.c,v 1.6 2017/01/07 16:48:03 christos Exp $ */ | | 1 | /* $NetBSD: npfd.c,v 1.7 2017/01/24 20:24:59 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2015 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2015 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Mindaugas Rasiukevicius. | | 8 | * by Mindaugas Rasiukevicius. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -20,27 +20,27 @@ | | | @@ -20,27 +20,27 @@ |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | __RCSID("$NetBSD: npfd.c,v 1.6 2017/01/07 16:48:03 christos Exp $"); | | 33 | __RCSID("$NetBSD: npfd.c,v 1.7 2017/01/24 20:24:59 christos Exp $"); |
34 | | | 34 | |
35 | #include <stdio.h> | | 35 | #include <stdio.h> |
36 | #include <string.h> | | 36 | #include <string.h> |
37 | #include <fcntl.h> | | 37 | #include <fcntl.h> |
38 | #include <signal.h> | | 38 | #include <signal.h> |
39 | #include <stdlib.h> | | 39 | #include <stdlib.h> |
40 | #include <unistd.h> | | 40 | #include <unistd.h> |
41 | #include <stdbool.h> | | 41 | #include <stdbool.h> |
42 | #include <poll.h> | | 42 | #include <poll.h> |
43 | #include <errno.h> | | 43 | #include <errno.h> |
44 | #include <err.h> | | 44 | #include <err.h> |
45 | #include <syslog.h> | | 45 | #include <syslog.h> |
46 | #include <util.h> | | 46 | #include <util.h> |
| @@ -65,54 +65,61 @@ npfd_getctl(void) | | | @@ -65,54 +65,61 @@ npfd_getctl(void) |
65 | } | | 65 | } |
66 | if (ver != NPF_VERSION) { | | 66 | if (ver != NPF_VERSION) { |
67 | errx(EXIT_FAILURE, | | 67 | errx(EXIT_FAILURE, |
68 | "Incompatible NPF interface version (%d, kernel %d)\n" | | 68 | "Incompatible NPF interface version (%d, kernel %d)\n" |
69 | "Hint: update userland?", NPF_VERSION, ver); | | 69 | "Hint: update userland?", NPF_VERSION, ver); |
70 | } | | 70 | } |
71 | return fd; | | 71 | return fd; |
72 | } | | 72 | } |
73 | | | 73 | |
74 | static void | | 74 | static void |
75 | npfd_event_loop(npfd_log_t *log, int delay) | | 75 | npfd_event_loop(npfd_log_t *log, int delay) |
76 | { | | 76 | { |
77 | struct pollfd pfd; | | 77 | struct pollfd pfd; |
| | | 78 | size_t count = 0; |
78 | | | 79 | |
79 | pfd.fd = npfd_log_getsock(log); | | 80 | pfd.fd = npfd_log_getsock(log); |
80 | pfd.events = POLLHUP | POLLIN; | | 81 | pfd.events = POLLHUP | POLLIN; |
81 | | | 82 | |
82 | while (!done) { | | 83 | while (!done) { |
83 | if (hup) { | | 84 | if (hup) { |
84 | hup = false; | | 85 | hup = false; |
85 | npfd_log_reopen(log, false); | | 86 | npfd_log_reopen(log, false); |
86 | } | | 87 | } |
87 | if (stats) { | | 88 | if (stats) { |
88 | stats = false; | | 89 | stats = false; |
89 | npfd_log_stats(log); | | 90 | npfd_log_stats(log); |
90 | } | | 91 | } |
91 | if (flush) { | | 92 | if (flush) { |
92 | flush = false; | | 93 | flush = false; |
93 | npfd_log_flush(log); | | 94 | npfd_log_flush(log); |
| | | 95 | count = 0; |
94 | } | | 96 | } |
95 | switch (poll(&pfd, 1, delay)) { | | 97 | switch (poll(&pfd, 1, delay)) { |
96 | case -1: | | 98 | case -1: |
97 | if (errno == EINTR) | | 99 | if (errno == EINTR) |
98 | continue; | | 100 | continue; |
99 | syslog(LOG_ERR, "poll failed: %m"); | | 101 | syslog(LOG_ERR, "poll failed: %m"); |
100 | exit(EXIT_FAILURE); | | 102 | exit(EXIT_FAILURE); |
101 | /*NOTREACHED*/ | | 103 | /*NOTREACHED*/ |
102 | case 0: | | 104 | case 0: |
103 | npfd_log_flush(log); | | 105 | npfd_log_flush(log); |
| | | 106 | count = 0; |
104 | continue; | | 107 | continue; |
105 | default: | | 108 | default: |
| | | 109 | if (count++ >= 100) { |
| | | 110 | npfd_log_flush(log); |
| | | 111 | count = 0; |
| | | 112 | } |
106 | npfd_log(log); | | 113 | npfd_log(log); |
107 | } | | 114 | } |
108 | | | 115 | |
109 | } | | 116 | } |
110 | npfd_log_destroy(log); | | 117 | npfd_log_destroy(log); |
111 | } | | 118 | } |
112 | | | 119 | |
113 | static void | | 120 | static void |
114 | sighandler(int sig) | | 121 | sighandler(int sig) |
115 | { | | 122 | { |
116 | switch (sig) { | | 123 | switch (sig) { |
117 | case SIGHUP: | | 124 | case SIGHUP: |
118 | hup = true; | | 125 | hup = true; |
| @@ -158,27 +165,27 @@ copyargs(int argc, char **argv) | | | @@ -158,27 +165,27 @@ copyargs(int argc, char **argv) |
158 | memcpy(buf + p, argv[i], l); | | 165 | memcpy(buf + p, argv[i], l); |
159 | p += l; | | 166 | p += l; |
160 | buf[p++] = i == argc - 1 ? '\0' : ' '; | | 167 | buf[p++] = i == argc - 1 ? '\0' : ' '; |
161 | } | | 168 | } |
162 | return buf; | | 169 | return buf; |
163 | } | | 170 | } |
164 | | | 171 | |
165 | int | | 172 | int |
166 | main(int argc, char **argv) | | 173 | main(int argc, char **argv) |
167 | { | | 174 | { |
168 | bool daemon_off = false; | | 175 | bool daemon_off = false; |
169 | int ch; | | 176 | int ch; |
170 | | | 177 | |
171 | int delay = 60 * 1000; | | 178 | int delay = 5 * 1000; |
172 | const char *iface = "npflog0"; | | 179 | const char *iface = "npflog0"; |
173 | int snaplen = 116; | | 180 | int snaplen = 116; |
174 | char *pidname = NULL; | | 181 | char *pidname = NULL; |
175 | char *filename = NULL; | | 182 | char *filename = NULL; |
176 | | | 183 | |
177 | int fd = npfd_getctl(); | | 184 | int fd = npfd_getctl(); |
178 | (void)close(fd); | | 185 | (void)close(fd); |
179 | | | 186 | |
180 | while ((ch = getopt(argc, argv, "Dd:f:i:p:s:")) != -1) { | | 187 | while ((ch = getopt(argc, argv, "Dd:f:i:p:s:")) != -1) { |
181 | switch (ch) { | | 188 | switch (ch) { |
182 | case 'D': | | 189 | case 'D': |
183 | daemon_off = true; | | 190 | daemon_off = true; |
184 | break; | | 191 | break; |