]> rtime.felk.cvut.cz Git - can-utils.git/blob - candump.c
Removed the limitation for the number of CAN filters for each interface.
[can-utils.git] / candump.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * candump.c
7  *
8  * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <ctype.h>
54 #include <libgen.h>
55 #include <time.h>
56
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
61 #include <sys/uio.h>
62 #include <net/if.h>
63
64 #include <linux/can.h>
65 #include <linux/can/raw.h>
66
67 #include "terminal.h"
68 #include "lib.h"
69
70 #define MAXSOCK 16    /* max. number of CAN interfaces given on the cmdline */
71 #define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
72 #define MAXCOL 6      /* number of different colors for colorized output */
73 #define ANYDEV "any"  /* name of interface to receive from any CAN interface */
74 #define ANL "\r\n"    /* newline in ASC mode */
75
76 #define SILENT_INI 42 /* detect user setting on commandline */
77 #define SILENT_OFF 0  /* no silent mode */
78 #define SILENT_ANI 1  /* silent mode with animation */
79 #define SILENT_ON  2  /* silent mode (completely silent) */
80
81 #define BOLD    ATTBOLD
82 #define RED     ATTBOLD FGRED
83 #define GREEN   ATTBOLD FGGREEN
84 #define YELLOW  ATTBOLD FGYELLOW
85 #define BLUE    ATTBOLD FGBLUE
86 #define MAGENTA ATTBOLD FGMAGENTA
87 #define CYAN    ATTBOLD FGCYAN
88
89 const char col_on [MAXCOL][19] = {BLUE, RED, GREEN, BOLD, MAGENTA, CYAN};
90 const char col_off [] = ATTRESET;
91
92 static char devname[MAXIFNAMES][IFNAMSIZ+1];
93 static int  dindex[MAXIFNAMES];
94 static int  max_devname_len; /* to prevent frazzled device name output */ 
95
96 #define MAXANI 4
97 const char anichar[MAXANI] = {'|', '/', '-', '\\'};
98
99 extern int optind, opterr, optopt;
100
101 static volatile int running = 1;
102
103 void print_usage(char *prg)
104 {
105         fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
106         fprintf(stderr, "  (use CTRL-C to terminate %s)\n\n", prg);
107         fprintf(stderr, "Options: -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
108         fprintf(stderr, "         -c          (increment color mode level)\n");
109         fprintf(stderr, "         -i          (binary output - may exceed 80 chars/line)\n");
110         fprintf(stderr, "         -a          (enable additional ASCII output)\n");
111         fprintf(stderr, "         -S          (swap byte order in printed CAN data[] - marked with '%c' )\n", SWAP_DELIMITER);
112         fprintf(stderr, "         -s <level>  (silent mode - %d: off (default) %d: animation %d: silent)\n", SILENT_OFF, SILENT_ANI, SILENT_ON);
113         fprintf(stderr, "         -b <can>    (bridge mode - send received frames to <can>)\n");
114         fprintf(stderr, "         -B <can>    (bridge mode - like '-b' with disabled loopback)\n");
115         fprintf(stderr, "         -l          (log CAN-frames into file. Sets '-s %d' by default)\n", SILENT_ON);
116         fprintf(stderr, "         -L          (use log file format on stdout)\n");
117         fprintf(stderr, "         -n <count>  (terminate after receiption of <count> CAN frames)\n");
118         fprintf(stderr, "\n");
119         fprintf(stderr, "Up to %d CAN interfaces with optional filter sets can be specified\n", MAXSOCK);
120         fprintf(stderr, "on the commandline in the form: <ifname>[,filter]*\n");
121         fprintf(stderr, "\nComma separated filters can be specified for each given CAN interface:\n");
122         fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
123         fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
124         fprintf(stderr, " #<error_mask>       (set error frame filter, see include/linux/can/error.h)\n");
125         fprintf(stderr, "\nCAN IDs, masks and data content are given and expected in hexadecimal values.\n");
126         fprintf(stderr, "When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.\n");
127         fprintf(stderr, "Without any given filter all data frames are received ('0:0' default filter).\n");
128         fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n", ANYDEV);
129         fprintf(stderr, "\nExamples:\n");
130         fprintf(stderr, "%s -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8\n", prg);
131         fprintf(stderr, "%s -l any,0~0,#FFFFFFFF    (log only error frames but no(!) data frames)\n", prg);
132         fprintf(stderr, "%s -l any,0:0,#FFFFFFFF    (log error frames and also all data frames)\n", prg);
133         fprintf(stderr, "%s vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678)\n", prg);
134         fprintf(stderr, "%s vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames)\n", prg);
135         fprintf(stderr, "%s vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)\n", prg);
136         fprintf(stderr, "\n");
137 }
138
139 void sigterm(int signo)
140 {
141         running = 0;
142 }
143
144 int idx2dindex(int ifidx, int socket) {
145
146         int i;
147         struct ifreq ifr;
148
149         for (i=0; i < MAXIFNAMES; i++) {
150                 if (dindex[i] == ifidx)
151                         return i;
152         }
153
154         /* create new interface index cache entry */
155
156         /* remove index cache zombies first */
157         for (i=0; i < MAXIFNAMES; i++) {
158                 if (dindex[i]) {
159                         ifr.ifr_ifindex = dindex[i];
160                         if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
161                                 dindex[i] = 0;
162                 }
163         }
164
165         for (i=0; i < MAXIFNAMES; i++)
166                 if (!dindex[i]) /* free entry */
167                         break;
168
169         if (i == MAXIFNAMES) {
170                 fprintf(stderr, "Interface index cache only supports %d interfaces.\n",
171                        MAXIFNAMES);
172                 exit(1);
173         }
174
175         dindex[i] = ifidx;
176
177         ifr.ifr_ifindex = ifidx;
178         if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
179                 perror("SIOCGIFNAME");
180
181         if (max_devname_len < strlen(ifr.ifr_name))
182                 max_devname_len = strlen(ifr.ifr_name);
183
184         strcpy(devname[i], ifr.ifr_name);
185
186 #ifdef DEBUG
187         printf("new index %d (%s)\n", i, devname[i]);
188 #endif
189
190         return i;
191 }
192
193 int main(int argc, char **argv)
194 {
195         fd_set rdfs;
196         int s[MAXSOCK];
197         int bridge = 0;
198         unsigned char timestamp = 0;
199         unsigned char silent = SILENT_INI;
200         unsigned char silentani = 0;
201         unsigned char color = 0;
202         unsigned char view = 0;
203         unsigned char log = 0;
204         unsigned char logfrmt = 0;
205         int count = 0;
206         int opt, ret;
207         int currmax, numfilter;
208         char *ptr, *nptr;
209         struct sockaddr_can addr;
210         struct can_filter *rfilter;
211         can_err_mask_t err_mask;
212         struct can_frame frame;
213         int nbytes, i;
214         struct ifreq ifr;
215         struct timeval tv, last_tv;
216         FILE *logfile = NULL;
217
218         signal(SIGTERM, sigterm);
219         signal(SIGHUP, sigterm);
220         signal(SIGINT, sigterm);
221
222         last_tv.tv_sec  = 0;
223         last_tv.tv_usec = 0;
224
225         while ((opt = getopt(argc, argv, "t:ciaSs:b:B:lLn:h?")) != -1) {
226                 switch (opt) {
227                 case 't':
228                         timestamp = optarg[0];
229                         if ((timestamp != 'a') && (timestamp != 'A') &&
230                             (timestamp != 'd') && (timestamp != 'z')) {
231                                 fprintf(stderr, "%s: unknown timestamp mode '%c' - ignored\n",
232                                        basename(argv[0]), optarg[0]);
233                                 timestamp = 0;
234                         }
235                         break;
236
237                 case 'c':
238                         color++;
239                         break;
240
241                 case 'i':
242                         view |= CANLIB_VIEW_BINARY;
243                         break;
244
245                 case 'a':
246                         view |= CANLIB_VIEW_ASCII;
247                         break;
248
249                 case 'S':
250                         view |= CANLIB_VIEW_SWAP;
251                         break;
252
253                 case 's':
254                         silent = atoi(optarg);
255                         if (silent > SILENT_ON) {
256                                 print_usage(basename(argv[0]));
257                                 exit(1);
258                         }
259                         break;
260
261                 case 'b':
262                 case 'B':
263                         if (strlen(optarg) >= IFNAMSIZ) {
264                                 fprintf(stderr, "Name of CAN device '%s' is too long!\n\n", optarg);
265                                 return 1;
266                         } else {
267                                 bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW);
268                                 if (bridge < 0) {
269                                         perror("bridge socket");
270                                         return 1;
271                                 }
272                                 addr.can_family = AF_CAN;
273                                 strcpy(ifr.ifr_name, optarg);
274                                 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
275                                         perror("SIOCGIFINDEX");
276                                 addr.can_ifindex = ifr.ifr_ifindex;
277                 
278                                 if (!addr.can_ifindex) {
279                                         perror("invalid bridge interface");
280                                         return 1;
281                                 }
282
283                                 /* disable default receive filter on this write-only RAW socket */
284                                 setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
285
286                                 if (opt == 'B') {
287                                         int loopback = 0;
288
289                                         setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
290                                                    &loopback, sizeof(loopback));
291                                 }
292
293                                 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
294                                         perror("bridge bind");
295                                         return 1;
296                                 }
297                         }
298                         break;
299             
300                 case 'l':
301                         log = 1;
302                         break;
303
304                 case 'L':
305                         logfrmt = 1;
306                         break;
307
308                 case 'n':
309                         count = atoi(optarg);
310                         if (count < 1) {
311                                 print_usage(basename(argv[0]));
312                                 exit(1);
313                         }
314                         break;
315
316                 default:
317                         print_usage(basename(argv[0]));
318                         exit(1);
319                         break;
320                 }
321         }
322
323         if (optind == argc) {
324                 print_usage(basename(argv[0]));
325                 exit(0);
326         }
327         
328         if (logfrmt && view) {
329                 fprintf(stderr, "Log file format selected: Please disable ASCII/BINARY/SWAP options!\n");
330                 exit(0);
331         }
332
333         if (silent == SILENT_INI) {
334                 if (log) {
335                         fprintf(stderr, "\nDisabled standard output while logging.");
336                         silent = SILENT_ON; /* disable output on stdout */
337                 } else
338                         silent = SILENT_OFF; /* default output */
339         }
340
341         currmax = argc - optind; /* find real number of CAN devices */
342
343         if (currmax > MAXSOCK) {
344                 fprintf(stderr, "More than %d CAN devices given on commandline!\n", MAXSOCK);
345                 return 1;
346         }
347
348         for (i=0; i < currmax; i++) {
349
350                 ptr = argv[optind+i];
351                 nptr = strchr(ptr, ',');
352
353 #ifdef DEBUG
354                 printf("open %d '%s'.\n", i, ptr);
355 #endif
356
357                 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
358                 if (s[i] < 0) {
359                         perror("socket");
360                         return 1;
361                 }
362
363                 if (nptr)
364                         nbytes = nptr - ptr;  /* interface name is up the first ',' */
365                 else
366                         nbytes = strlen(ptr); /* no ',' found => no filter definitions */
367
368                 if (nbytes >= IFNAMSIZ) {
369                         fprintf(stderr, "name of CAN device '%s' is too long!\n", ptr);
370                         return 1;
371                 }
372
373                 if (nbytes > max_devname_len)
374                         max_devname_len = nbytes; /* for nice printing */
375
376                 addr.can_family = AF_CAN;
377
378                 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
379                 strncpy(ifr.ifr_name, ptr, nbytes);
380
381 #ifdef DEBUG
382                 printf("using interface name '%s'.\n", ifr.ifr_name);
383 #endif
384
385                 if (strcmp(ANYDEV, ifr.ifr_name)) {
386                         if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
387                                 perror("SIOCGIFINDEX");
388                                 exit(1);
389                         }
390                         addr.can_ifindex = ifr.ifr_ifindex;
391                 } else
392                         addr.can_ifindex = 0; /* any can interface */
393
394                 if (nptr) {
395
396                         /* found a ',' after the interface name => check for filters */
397
398                         /* determine number of filters to alloc the filter space */
399                         numfilter = 0;
400                         ptr = nptr;
401                         while (ptr) {
402                                 numfilter++;
403                                 ptr++; /* hop behind the ',' */
404                                 ptr = strchr(ptr, ','); /* exit condition */
405                         }
406
407                         rfilter = malloc(sizeof(struct can_filter) * numfilter);
408                         if (!rfilter) {
409                                 fprintf(stderr, "Failed to create filter space!\n");
410                                 return 1;
411                         }
412
413                         numfilter = 0;
414                         err_mask = 0;
415
416                         while (nptr) {
417
418                                 ptr = nptr+1; /* hop behind the ',' */
419                                 nptr = strchr(ptr, ','); /* update exit condition */
420
421                                 if (sscanf(ptr, "%lx:%lx",
422                                            (long unsigned int *)
423                                            &rfilter[numfilter].can_id, 
424                                            (long unsigned int *)
425                                            &rfilter[numfilter].can_mask) == 2) {
426                                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
427                                         numfilter++;
428                                 } else if (sscanf(ptr, "%lx~%lx",
429                                                   (long unsigned int *)
430                                                   &rfilter[numfilter].can_id, 
431                                                   (long unsigned int *)
432                                                   &rfilter[numfilter].can_mask) == 2) {
433                                         rfilter[numfilter].can_id |= CAN_INV_FILTER;
434                                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
435                                         numfilter++;
436                                 } else if (sscanf(ptr, "#%lx",
437                                                   (long unsigned int *)&err_mask) != 1) { 
438                                         fprintf(stderr, "Error in filter option parsing: '%s'\n", ptr);
439                                         exit(1);
440                                 }
441                         }
442
443                         if (err_mask)
444                                 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
445                                            &err_mask, sizeof(err_mask));
446
447                         if (numfilter)
448                                 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
449                                            rfilter, numfilter * sizeof(struct can_filter));
450
451                         free(rfilter);
452
453                 } /* if (nptr) */
454
455                 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
456                         perror("bind");
457                         return 1;
458                 }
459         }
460
461         if (log) {
462                 time_t currtime;
463                 struct tm now;
464                 char fname[sizeof("candump-2006-11-20_202026.log")+1];
465
466                 if (time(&currtime) == (time_t)-1) {
467                         perror("time");
468                         return 1;
469                 }
470
471                 localtime_r(&currtime, &now);
472
473                 sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log",
474                         now.tm_year + 1900,
475                         now.tm_mon + 1,
476                         now.tm_mday,
477                         now.tm_hour,
478                         now.tm_min,
479                         now.tm_sec);
480
481                 if (silent != SILENT_ON)
482                         printf("\nWarning: console output active while logging!");
483
484                 fprintf(stderr, "\nEnabling Logfile '%s'\n\n", fname);
485
486                 logfile = fopen(fname, "w");
487                 if (!logfile) {
488                         perror("logfile");
489                         return 1;
490                 }
491         }
492
493         while (running) {
494
495                 FD_ZERO(&rdfs);
496                 for (i=0; i<currmax; i++)
497                         FD_SET(s[i], &rdfs);
498
499                 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
500                         //perror("select");
501                         running = 0;
502                         continue;
503                 }
504
505                 for (i=0; i<currmax; i++) {  /* check all CAN RAW sockets */
506
507                         if (FD_ISSET(s[i], &rdfs)) {
508
509                                 socklen_t len = sizeof(addr);
510                                 int idx;
511
512                                 nbytes = recvfrom(s[i], &frame, sizeof(struct can_frame), 0,
513                                                   (struct sockaddr*)&addr, &len);
514                                 if (nbytes < 0) {
515                                         perror("read");
516                                         return 1;
517                                 }
518
519                                 if (nbytes < sizeof(struct can_frame)) {
520                                         fprintf(stderr, "read: incomplete CAN frame\n");
521                                         return 1;
522                                 }
523
524                                 if (count && (--count == 0))
525                                         running = 0;
526
527                                 if (bridge) {
528                                         nbytes = write(bridge, &frame, sizeof(struct can_frame));
529                                         if (nbytes < 0) {
530                                                 perror("bridge write");
531                                                 return 1;
532                                         } else if (nbytes < sizeof(struct can_frame)) {
533                                                 fprintf(stderr,"bridge write: incomplete CAN frame\n");
534                                                 return 1;
535                                         }
536                                 }
537                     
538                                 if (timestamp || log || logfrmt)
539                                         if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
540                                                 perror("SIOCGSTAMP");
541
542
543                                 idx = idx2dindex(addr.can_ifindex, s[i]);
544
545                                 if (log) {
546                                         /* log CAN frame with absolute timestamp & device */
547                                         fprintf(logfile, "(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
548                                         fprintf(logfile, "%*s ", max_devname_len, devname[idx]);
549                                         /* without seperator as logfile use-case is parsing */
550                                         fprint_canframe(logfile, &frame, "\n", 0);
551                                 }
552
553                                 if (logfrmt) {
554                                         /* print CAN frame in log file style to stdout */
555                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
556                                         printf("%*s ", max_devname_len, devname[idx]);
557                                         fprint_canframe(stdout, &frame, "\n", 0);
558                                         goto out_fflush; /* no other output to stdout */
559                                 }
560
561                                 if (silent != SILENT_OFF){
562                                         if (silent == SILENT_ANI) {
563                                                 printf("%c\b", anichar[silentani%=MAXANI]);
564                                                 silentani++;
565                                         }
566                                         goto out_fflush; /* no other output to stdout */
567                                 }
568                       
569                                 printf(" %s", (color>2)?col_on[idx%MAXCOL]:"");
570
571                                 switch (timestamp) {
572
573                                 case 'a': /* absolute with timestamp */
574                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
575                                         break;
576
577                                 case 'A': /* absolute with date */
578                                 {
579                                         struct tm tm;
580                                         char timestring[25];
581
582                                         tm = *localtime(&tv.tv_sec);
583                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
584                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
585                                 }
586                                 break;
587
588                                 case 'd': /* delta */
589                                 case 'z': /* starting with zero */
590                                 {
591                                         struct timeval diff;
592
593                                         if (last_tv.tv_sec == 0)   /* first init */
594                                                 last_tv = tv;
595                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
596                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
597                                         if (diff.tv_usec < 0)
598                                                 diff.tv_sec--, diff.tv_usec += 1000000;
599                                         if (diff.tv_sec < 0)
600                                                 diff.tv_sec = diff.tv_usec = 0;
601                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
602                                 
603                                         if (timestamp == 'd')
604                                                 last_tv = tv; /* update for delta calculation */
605                                 }
606                                 break;
607
608                                 default: /* no timestamp output */
609                                         break;
610                                 }
611
612                                 printf(" %s", (color && (color<3))?col_on[idx%MAXCOL]:"");
613                                 printf("%*s", max_devname_len, devname[idx]);
614                                 printf("%s  ", (color==1)?col_off:"");
615
616                                 fprint_long_canframe(stdout, &frame, NULL, view);
617
618                                 printf("%s", (color>1)?col_off:"");
619                                 printf("\n");
620                         }
621
622                 out_fflush:
623                         fflush(stdout);
624                 }
625         }
626
627         for (i=0; i<currmax; i++)
628                 close(s[i]);
629
630         if (bridge)
631                 close(bridge);
632
633         if (log)
634                 fclose(logfile);
635
636         return 0;
637 }