]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/pf/pflogd/pflogd.c
MFV r333789: libpcap 1.9.0 (pre-release)
[FreeBSD/FreeBSD.git] / contrib / pf / pflogd / pflogd.c
1 /*      $OpenBSD: pflogd.c,v 1.46 2008/10/22 08:16:49 henning Exp $     */
2
3 /*
4  * Copyright (c) 2001 Theo de Raadt
5  * Copyright (c) 2001 Can Erkin Acar
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/file.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #include <net/bpf.h>
42 #include <net/if.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <pcap-int.h>
48 #include <pcap.h>
49 #include <syslog.h>
50 #include <signal.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <stdarg.h>
54 #include <fcntl.h>
55 #ifdef __FreeBSD__
56 #include <ifaddrs.h>
57 #include "pidfile.h"
58 #else
59 #include <util.h>
60 #endif
61 #include "pflogd.h"
62
63 pcap_t *hpcap;
64 static FILE *dpcap;
65
66 int Debug = 0;
67 static int snaplen = DEF_SNAPLEN;
68 static int cur_snaplen = DEF_SNAPLEN;
69
70 volatile sig_atomic_t gotsig_close, gotsig_alrm, gotsig_hup, gotsig_usr1;
71
72 char *filename = PFLOGD_LOG_FILE;
73 char *interface = PFLOGD_DEFAULT_IF;
74 char *filter = NULL;
75
76 char errbuf[PCAP_ERRBUF_SIZE];
77
78 int log_debug = 0;
79 unsigned int delay = FLUSH_DELAY;
80
81 char *copy_argv(char * const *);
82 void  dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
83 void  dump_packet_nobuf(u_char *, const struct pcap_pkthdr *, const u_char *);
84 void  log_pcap_stats(void);
85 int   flush_buffer(FILE *);
86 int   if_exists(char *);
87 int   init_pcap(void);
88 void  logmsg(int, const char *, ...);
89 void  purge_buffer(void);
90 int   reset_dump(int);
91 int   scan_dump(FILE *, off_t);
92 int   set_snaplen(int);
93 void  set_suspended(int);
94 void  sig_alrm(int);
95 void  sig_usr1(int);
96 void  sig_close(int);
97 void  sig_hup(int);
98 void  usage(void);
99
100 static int try_reset_dump(int);
101
102 /* buffer must always be greater than snaplen */
103 static int    bufpkt = 0;       /* number of packets in buffer */
104 static int    buflen = 0;       /* allocated size of buffer */
105 static char  *buffer = NULL;    /* packet buffer */
106 static char  *bufpos = NULL;    /* position in buffer */
107 static int    bufleft = 0;      /* bytes left in buffer */
108
109 /* if error, stop logging but count dropped packets */
110 static int suspended = -1;
111 static long packets_dropped = 0;
112
113 void
114 set_suspended(int s)
115 {
116         if (suspended == s)
117                 return;
118
119         suspended = s;
120         setproctitle("[%s] -s %d -i %s -f %s",
121             suspended ? "suspended" : "running",
122             cur_snaplen, interface, filename);
123 }
124
125 char *
126 copy_argv(char * const *argv)
127 {
128         size_t len = 0, n;
129         char *buf;
130
131         if (argv == NULL)
132                 return (NULL);
133
134         for (n = 0; argv[n]; n++)
135                 len += strlen(argv[n])+1;
136         if (len == 0)
137                 return (NULL);
138
139         buf = malloc(len);
140         if (buf == NULL)
141                 return (NULL);
142
143         strlcpy(buf, argv[0], len);
144         for (n = 1; argv[n]; n++) {
145                 strlcat(buf, " ", len);
146                 strlcat(buf, argv[n], len);
147         }
148         return (buf);
149 }
150
151 void
152 logmsg(int pri, const char *message, ...)
153 {
154         va_list ap;
155         va_start(ap, message);
156
157         if (log_debug) {
158                 vfprintf(stderr, message, ap);
159                 fprintf(stderr, "\n");
160         } else
161                 vsyslog(pri, message, ap);
162         va_end(ap);
163 }
164
165 #ifdef __FreeBSD__
166 __dead2 void
167 #else
168 __dead void
169 #endif
170 usage(void)
171 {
172         fprintf(stderr, "usage: pflogd [-Dx] [-d delay] [-f filename]");
173         fprintf(stderr, " [-i interface] [-p pidfile]\n");
174         fprintf(stderr, "              [-s snaplen] [expression]\n");
175         exit(1);
176 }
177
178 void
179 sig_close(int sig)
180 {
181         gotsig_close = 1;
182 }
183
184 void
185 sig_hup(int sig)
186 {
187         gotsig_hup = 1;
188 }
189
190 void
191 sig_alrm(int sig)
192 {
193         gotsig_alrm = 1;
194 }
195
196 void
197 sig_usr1(int sig)
198 {
199         gotsig_usr1 = 1;
200 }
201
202 void
203 set_pcap_filter(void)
204 {
205         struct bpf_program bprog;
206
207         if (pcap_compile(hpcap, &bprog, filter, PCAP_OPT_FIL, 0) < 0)
208                 logmsg(LOG_WARNING, "%s", pcap_geterr(hpcap));
209         else {
210                 if (pcap_setfilter(hpcap, &bprog) < 0)
211                         logmsg(LOG_WARNING, "%s", pcap_geterr(hpcap));
212                 pcap_freecode(&bprog);
213         }
214 }
215
216 int
217 if_exists(char *ifname)
218 {
219 #ifdef __FreeBSD__
220         struct ifaddrs *ifdata, *mb;
221         int exists = 0;
222
223         getifaddrs(&ifdata);
224         if (ifdata == NULL)
225                 return (0);
226
227         for (mb = ifdata; mb != NULL; mb = mb->ifa_next) {
228                 if (mb == NULL)
229                         continue;
230                 if (strlen(ifname) != strlen(mb->ifa_name))
231                         continue;
232                 if (strncmp(ifname, mb->ifa_name, strlen(ifname)) != 0)
233                         continue;
234                 exists = 1;
235                 break;
236         }
237         freeifaddrs(ifdata);
238
239         return (exists);
240 #else
241         int s;
242         struct ifreq ifr;
243         struct if_data ifrdat;
244
245         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
246                 err(1, "socket");
247         bzero(&ifr, sizeof(ifr));
248         if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
249                 sizeof(ifr.ifr_name))
250                         errx(1, "main ifr_name: strlcpy");
251         ifr.ifr_data = (caddr_t)&ifrdat;
252         if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
253                 return (0);
254         if (close(s))
255                 err(1, "close");
256
257         return (1);
258 #endif
259 }
260
261 int
262 init_pcap(void)
263 {
264         hpcap = pcap_open_live(interface, snaplen, 1, PCAP_TO_MS, errbuf);
265         if (hpcap == NULL) {
266                 logmsg(LOG_ERR, "Failed to initialize: %s", errbuf);
267                 return (-1);
268         }
269
270         if (pcap_datalink(hpcap) != DLT_PFLOG) {
271                 logmsg(LOG_ERR, "Invalid datalink type");
272                 pcap_close(hpcap);
273                 hpcap = NULL;
274                 return (-1);
275         }
276
277         set_pcap_filter();
278
279         cur_snaplen = snaplen = pcap_snapshot(hpcap);
280
281         /* lock */
282         if (ioctl(pcap_fileno(hpcap), BIOCLOCK) < 0) {
283                 logmsg(LOG_ERR, "BIOCLOCK: %s", strerror(errno));
284                 return (-1);
285         }
286
287         return (0);
288 }
289
290 int
291 set_snaplen(int snap)
292 {
293         if (priv_set_snaplen(snap))
294                 return (1);
295
296         if (cur_snaplen > snap)
297                 purge_buffer();
298
299         cur_snaplen = snap;
300
301         return (0);
302 }
303
304 int
305 reset_dump(int nomove)
306 {
307         int ret;
308
309         for (;;) {
310                 ret = try_reset_dump(nomove);
311                 if (ret <= 0)
312                         break;
313         }
314
315         return (ret);
316 }
317
318 /*
319  * tries to (re)open log file, nomove flag is used with -x switch
320  * returns 0: success, 1: retry (log moved), -1: error
321  */
322 int
323 try_reset_dump(int nomove)
324 {
325         struct pcap_file_header hdr;
326         struct stat st;
327         int fd;
328         FILE *fp;
329
330         if (hpcap == NULL)
331                 return (-1);
332
333         if (dpcap) {
334                 flush_buffer(dpcap);
335                 fclose(dpcap);
336                 dpcap = NULL;
337         }
338
339         /*
340          * Basically reimplement pcap_dump_open() because it truncates
341          * files and duplicates headers and such.
342          */
343         fd = priv_open_log();
344         if (fd < 0)
345                 return (-1);
346
347         fp = fdopen(fd, "a+");
348
349         if (fp == NULL) {
350                 logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
351                 close(fd);
352                 return (-1);
353         }
354         if (fstat(fileno(fp), &st) == -1) {
355                 logmsg(LOG_ERR, "Error: %s: %s", filename, strerror(errno));
356                 fclose(fp);
357                 return (-1);
358         }
359
360         /* set FILE unbuffered, we do our own buffering */
361         if (setvbuf(fp, NULL, _IONBF, 0)) {
362                 logmsg(LOG_ERR, "Failed to set output buffers");
363                 fclose(fp);
364                 return (-1);
365         }
366
367 #define TCPDUMP_MAGIC 0xa1b2c3d4
368
369         if (st.st_size == 0) {
370                 if (snaplen != cur_snaplen) {
371                         logmsg(LOG_NOTICE, "Using snaplen %d", snaplen);
372                         if (set_snaplen(snaplen))
373                                 logmsg(LOG_WARNING,
374                                     "Failed, using old settings");
375                 }
376                 hdr.magic = TCPDUMP_MAGIC;
377                 hdr.version_major = PCAP_VERSION_MAJOR;
378                 hdr.version_minor = PCAP_VERSION_MINOR;
379                 hdr.thiszone = hpcap->tzoff;
380                 hdr.snaplen = hpcap->snapshot;
381                 hdr.sigfigs = 0;
382                 hdr.linktype = hpcap->linktype;
383
384                 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
385                         fclose(fp);
386                         return (-1);
387                 }
388         } else if (scan_dump(fp, st.st_size)) {
389                 fclose(fp);
390                 if (nomove || priv_move_log()) {
391                         logmsg(LOG_ERR,
392                             "Invalid/incompatible log file, move it away");
393                         return (-1);
394                 }
395                 return (1);
396         }
397
398         dpcap = fp;
399
400         set_suspended(0);
401         flush_buffer(fp);
402
403         return (0);
404 }
405
406 int
407 scan_dump(FILE *fp, off_t size)
408 {
409         struct pcap_file_header hdr;
410 #ifdef __FreeBSD__
411         struct pcap_sf_pkthdr ph;
412 #else
413         struct pcap_pkthdr ph;
414 #endif
415         off_t pos;
416
417         /*
418          * Must read the file, compare the header against our new
419          * options (in particular, snaplen) and adjust our options so
420          * that we generate a correct file. Furthermore, check the file
421          * for consistency so that we can append safely.
422          *
423          * XXX this may take a long time for large logs.
424          */
425         (void) fseek(fp, 0L, SEEK_SET);
426
427         if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
428                 logmsg(LOG_ERR, "Short file header");
429                 return (1);
430         }
431
432         if (hdr.magic != TCPDUMP_MAGIC ||
433             hdr.version_major != PCAP_VERSION_MAJOR ||
434             hdr.version_minor != PCAP_VERSION_MINOR ||
435             hdr.linktype != hpcap->linktype ||
436             hdr.snaplen > PFLOGD_MAXSNAPLEN) {
437                 return (1);
438         }
439
440         pos = sizeof(hdr);
441
442         while (!feof(fp)) {
443                 off_t len = fread((char *)&ph, 1, sizeof(ph), fp);
444                 if (len == 0)
445                         break;
446
447                 if (len != sizeof(ph))
448                         goto error;
449                 if (ph.caplen > hdr.snaplen || ph.caplen > PFLOGD_MAXSNAPLEN)
450                         goto error;
451                 pos += sizeof(ph) + ph.caplen;
452                 if (pos > size)
453                         goto error;
454                 fseek(fp, ph.caplen, SEEK_CUR);
455         }
456
457         if (pos != size)
458                 goto error;
459
460         if (hdr.snaplen != cur_snaplen) {
461                 logmsg(LOG_WARNING,
462                        "Existing file has different snaplen %u, using it",
463                        hdr.snaplen);
464                 if (set_snaplen(hdr.snaplen)) {
465                         logmsg(LOG_WARNING,
466                                "Failed, using old settings, offset %llu",
467                                (unsigned long long) size);
468                 }
469         }
470
471         return (0);
472
473  error:
474         logmsg(LOG_ERR, "Corrupted log file.");
475         return (1);
476 }
477
478 /* dump a packet directly to the stream, which is unbuffered */
479 void
480 dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
481 {
482         FILE *f = (FILE *)user;
483 #ifdef __FreeBSD__
484         struct pcap_sf_pkthdr sh;
485 #endif
486
487         if (suspended) {
488                 packets_dropped++;
489                 return;
490         }
491
492 #ifdef __FreeBSD__
493         sh.ts.tv_sec = (bpf_int32)h->ts.tv_sec;
494         sh.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
495         sh.caplen = h->caplen;
496         sh.len = h->len;
497
498         if (fwrite((char *)&sh, sizeof(sh), 1, f) != 1) {
499 #else
500         if (fwrite((char *)h, sizeof(*h), 1, f) != 1) {
501 #endif
502                 off_t pos = ftello(f);
503
504                 /* try to undo header to prevent corruption */
505 #ifdef __FreeBSD__
506                 if (pos < sizeof(sh) ||
507                     ftruncate(fileno(f), pos - sizeof(sh))) {
508 #else
509                 if (pos < sizeof(*h) ||
510                     ftruncate(fileno(f), pos - sizeof(*h))) {
511 #endif
512                         logmsg(LOG_ERR, "Write failed, corrupted logfile!");
513                         set_suspended(1);
514                         gotsig_close = 1;
515                         return;
516                 }
517                 goto error;
518         }
519
520         if (fwrite((char *)sp, h->caplen, 1, f) != 1)
521                 goto error;
522
523         return;
524
525 error:
526         set_suspended(1);
527         packets_dropped ++;
528         logmsg(LOG_ERR, "Logging suspended: fwrite: %s", strerror(errno));
529 }
530
531 int
532 flush_buffer(FILE *f)
533 {
534         off_t offset;
535         int len = bufpos - buffer;
536
537         if (len <= 0)
538                 return (0);
539
540         offset = ftello(f);
541         if (offset == (off_t)-1) {
542                 set_suspended(1);
543                 logmsg(LOG_ERR, "Logging suspended: ftello: %s",
544                     strerror(errno));
545                 return (1);
546         }
547
548         if (fwrite(buffer, len, 1, f) != 1) {
549                 set_suspended(1);
550                 logmsg(LOG_ERR, "Logging suspended: fwrite: %s",
551                     strerror(errno));
552                 ftruncate(fileno(f), offset);
553                 return (1);
554         }
555
556         set_suspended(0);
557         bufpos = buffer;
558         bufleft = buflen;
559         bufpkt = 0;
560
561         return (0);
562 }
563
564 void
565 purge_buffer(void)
566 {
567         packets_dropped += bufpkt;
568
569         set_suspended(0);
570         bufpos = buffer;
571         bufleft = buflen;
572         bufpkt = 0;
573 }
574
575 /* append packet to the buffer, flushing if necessary */
576 void
577 dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
578 {
579         FILE *f = (FILE *)user;
580 #ifdef __FreeBSD__
581         struct pcap_sf_pkthdr sh;
582         size_t len = sizeof(sh) + h->caplen;
583 #else
584         size_t len = sizeof(*h) + h->caplen;
585 #endif
586
587         if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) {
588                 logmsg(LOG_NOTICE, "invalid size %u (%u/%u), packet dropped",
589                        len, cur_snaplen, snaplen);
590                 packets_dropped++;
591                 return;
592         }
593
594         if (len <= bufleft)
595                 goto append;
596
597         if (suspended) {
598                 packets_dropped++;
599                 return;
600         }
601
602         if (flush_buffer(f)) {
603                 packets_dropped++;
604                 return;
605         }
606
607         if (len > bufleft) {
608                 dump_packet_nobuf(user, h, sp);
609                 return;
610         }
611
612  append:       
613 #ifdef __FreeBSD__
614         sh.ts.tv_sec = (bpf_int32)h->ts.tv_sec;
615         sh.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
616         sh.caplen = h->caplen;
617         sh.len = h->len;
618
619         memcpy(bufpos, &sh, sizeof(sh));
620         memcpy(bufpos + sizeof(sh), sp, h->caplen);
621 #else
622         memcpy(bufpos, h, sizeof(*h));
623         memcpy(bufpos + sizeof(*h), sp, h->caplen);
624 #endif
625
626         bufpos += len;
627         bufleft -= len;
628         bufpkt++;
629
630         return;
631 }
632
633 void
634 log_pcap_stats(void)
635 {
636         struct pcap_stat pstat;
637         if (pcap_stats(hpcap, &pstat) < 0)
638                 logmsg(LOG_WARNING, "Reading stats: %s", pcap_geterr(hpcap));
639         else
640                 logmsg(LOG_NOTICE,
641                         "%u packets received, %u/%u dropped (kernel/pflogd)",
642                         pstat.ps_recv, pstat.ps_drop, packets_dropped);
643 }
644
645 int
646 main(int argc, char **argv)
647 {
648         int ch, np, ret, Xflag = 0;
649         pcap_handler phandler = dump_packet;
650         const char *errstr = NULL;
651         char *pidf = NULL;
652
653         ret = 0;
654
655         closefrom(STDERR_FILENO + 1);
656
657         while ((ch = getopt(argc, argv, "Dxd:f:i:p:s:")) != -1) {
658                 switch (ch) {
659                 case 'D':
660                         Debug = 1;
661                         break;
662                 case 'd':
663                         delay = strtonum(optarg, 5, 60*60, &errstr);
664                         if (errstr)
665                                 usage();
666                         break;
667                 case 'f':
668                         filename = optarg;
669                         break;
670                 case 'i':
671                         interface = optarg;
672                         break;
673                 case 'p':
674                         pidf = optarg;
675                         break;
676                 case 's':
677                         snaplen = strtonum(optarg, 0, PFLOGD_MAXSNAPLEN,
678                             &errstr);
679                         if (snaplen <= 0)
680                                 snaplen = DEF_SNAPLEN;
681                         if (errstr)
682                                 snaplen = PFLOGD_MAXSNAPLEN;
683                         break;
684                 case 'x':
685                         Xflag++;
686                         break;
687                 default:
688                         usage();
689                 }
690
691         }
692
693         log_debug = Debug;
694         argc -= optind;
695         argv += optind;
696
697         /* does interface exist */
698         if (!if_exists(interface)) {
699                 warn("Failed to initialize: %s", interface);
700                 logmsg(LOG_ERR, "Failed to initialize: %s", interface);
701                 logmsg(LOG_ERR, "Exiting, init failure");
702                 exit(1);
703         }
704
705         if (!Debug) {
706                 openlog("pflogd", LOG_PID | LOG_CONS, LOG_DAEMON);
707                 if (daemon(0, 0)) {
708                         logmsg(LOG_WARNING, "Failed to become daemon: %s",
709                             strerror(errno));
710                 }
711                 pidfile(pidf);
712         }
713
714         tzset();
715         (void)umask(S_IRWXG | S_IRWXO);
716
717         /* filter will be used by the privileged process */
718         if (argc) {
719                 filter = copy_argv(argv);
720                 if (filter == NULL)
721                         logmsg(LOG_NOTICE, "Failed to form filter expression");
722         }
723
724         /* initialize pcap before dropping privileges */
725         if (init_pcap()) {
726                 logmsg(LOG_ERR, "Exiting, init failure");
727                 exit(1);
728         }
729
730         /* Privilege separation begins here */
731         if (priv_init()) {
732                 logmsg(LOG_ERR, "unable to privsep");
733                 exit(1);
734         }
735
736         setproctitle("[initializing]");
737         /* Process is now unprivileged and inside a chroot */
738         signal(SIGTERM, sig_close);
739         signal(SIGINT, sig_close);
740         signal(SIGQUIT, sig_close);
741         signal(SIGALRM, sig_alrm);
742         signal(SIGUSR1, sig_usr1);
743         signal(SIGHUP, sig_hup);
744         alarm(delay);
745
746         buffer = malloc(PFLOGD_BUFSIZE);
747
748         if (buffer == NULL) {
749                 logmsg(LOG_WARNING, "Failed to allocate output buffer");
750                 phandler = dump_packet_nobuf;
751         } else {
752                 bufleft = buflen = PFLOGD_BUFSIZE;
753                 bufpos = buffer;
754                 bufpkt = 0;
755         }
756
757         if (reset_dump(Xflag) < 0) {
758                 if (Xflag)
759                         return (1);
760
761                 logmsg(LOG_ERR, "Logging suspended: open error");
762                 set_suspended(1);
763         } else if (Xflag)
764                 return (0);
765
766         while (1) {
767                 np = pcap_dispatch(hpcap, PCAP_NUM_PKTS,
768                     phandler, (u_char *)dpcap);
769                 if (np < 0) {
770                         if (!if_exists(interface)) {
771                                 logmsg(LOG_NOTICE, "interface %s went away",
772                                     interface);
773                                 ret = -1;
774                                 break;
775                         }
776                         logmsg(LOG_NOTICE, "%s", pcap_geterr(hpcap));
777                 }
778
779                 if (gotsig_close)
780                         break;
781                 if (gotsig_hup) {
782                         if (reset_dump(0)) {
783                                 logmsg(LOG_ERR,
784                                     "Logging suspended: open error");
785                                 set_suspended(1);
786                         }
787                         gotsig_hup = 0;
788                 }
789
790                 if (gotsig_alrm) {
791                         if (dpcap)
792                                 flush_buffer(dpcap);
793                         else 
794                                 gotsig_hup = 1;
795                         gotsig_alrm = 0;
796                         alarm(delay);
797                 }
798
799                 if (gotsig_usr1) {
800                         log_pcap_stats();
801                         gotsig_usr1 = 0;
802                 }
803         }
804
805         logmsg(LOG_NOTICE, "Exiting");
806         if (dpcap) {
807                 flush_buffer(dpcap);
808                 fclose(dpcap);
809         }
810         purge_buffer();
811
812         log_pcap_stats();
813         pcap_close(hpcap);
814         if (!Debug)
815                 closelog();
816         return (ret);
817 }