62
62
63
63
#include <linux/can.h>
64
64
#include <linux/can/raw.h>
65
+ #include <errno.h>
65
66
66
67
#include "terminal.h"
67
68
#include "lib.h"
@@ -111,18 +112,18 @@ const char anichar[MAXANI] = {'|', '/', '-', '\\'};
111
112
const char extra_m_info [4 ][4 ] = {"- -" , "B -" , "- E" , "B E" };
112
113
113
114
#define MAXLOGNAMESZ 100
114
- FILE * logfile = NULL ;
115
+ static FILE * logfile = NULL ;
115
116
static char log_filename [MAXLOGNAMESZ ];
116
117
117
- unsigned char silent = SILENT_INI ;
118
+ static unsigned char silent = SILENT_INI ;
118
119
119
120
extern int optind , opterr , optopt ;
120
121
121
122
static volatile int running = 1 ;
122
- static volatile int flag_reopen_file = 0 ;
123
- static volatile unsigned long sighup_count = 0 ;
124
- int is_auto_log_name = 0 ;
125
- int open_log_file ( const char * filename ) ;
123
+ static volatile int flag_reopen_file ;
124
+ static volatile unsigned long sighup_count ;
125
+
126
+ static int is_auto_log_name = 0 ;
126
127
127
128
void print_usage (char * prg )
128
129
{
@@ -177,8 +178,7 @@ void sigterm(int signo)
177
178
178
179
void sighup (int signo )
179
180
{
180
- if (signo == SIGHUP )
181
- {
181
+ if (signo == SIGHUP && running ) {
182
182
flag_reopen_file = 1 ;
183
183
sighup_count ++ ;
184
184
}
@@ -233,14 +233,13 @@ int idx2dindex(int ifidx, int socket) {
233
233
return i ;
234
234
}
235
235
236
- int sprint_auto_filename_format (char * buffer )
236
+ static int sprint_auto_filename_format (char * buffer )
237
237
{
238
238
time_t currtime ;
239
239
struct tm now ;
240
240
241
241
if (time (& currtime ) == (time_t )- 1 ) {
242
242
perror ("time" );
243
- //running = 0;
244
243
return 1 ;
245
244
}
246
245
@@ -259,14 +258,13 @@ int sprint_auto_filename_format(char * buffer)
259
258
return 0 ;
260
259
}
261
260
262
- // opens file using global filehandle logfile
263
- int open_log_file (const char * fileName )
261
+ /* opens file using global var logfile*/
262
+ static int open_log_file (const char * file_name )
264
263
{
265
264
if (silent != SILENT_ON )
266
265
fprintf (stderr , "Warning: Console output active while logging!\n" );
267
266
268
- const char * fopen_opts = (sighup_count > 0 ) ? "a" : "w" ;
269
- logfile = fopen (fileName , fopen_opts );
267
+ logfile = fopen (file_name , "w" );
270
268
271
269
if (!logfile ) {
272
270
perror ("logfile" );
@@ -276,55 +274,45 @@ int open_log_file(const char * fileName)
276
274
return 0 ;
277
275
}
278
276
279
- //flush,close then reopen log
280
- int reopen_file (FILE * fileHandle )
277
+ static int reopen_file (FILE * file_handle )
281
278
{
282
- if (!fileHandle )
283
- return 1 ;
279
+ const char * fopen_opts = (sighup_count > 0 && !is_auto_log_name ) ? "a" : "w" ;
284
280
285
- int errcode = 0 ;
281
+ if (!file_handle )
282
+ return 1 ;
286
283
287
- if (is_auto_log_name == 1 )
288
- {
289
- errcode = sprint_auto_filename_format (& log_filename [0 ]);
284
+ if (is_auto_log_name == 1 ) {
285
+ const int errcode = sprint_auto_filename_format (log_filename );
290
286
291
- if (errcode != 0 )
292
- {
293
- running = 0 ;
287
+ if (errcode != 0 ) {
294
288
return 1 ;
295
289
}
296
290
}
297
291
298
- const char * fopen_opts = (sighup_count > 0 ) ? "a" : "w" ;
299
- logfile = freopen (log_filename ,fopen_opts ,fileHandle );
292
+ logfile = freopen (log_filename , fopen_opts , file_handle );
300
293
301
- running = (errcode == 0 );
302
294
flag_reopen_file = 0 ;
303
295
304
- return 0 ;
296
+ return logfile != 0 ;
305
297
}
306
298
307
- void get_logname_arg (const char * arg )
299
+ static int process_logname_arg (const char * arg )
308
300
{
309
- if (arg != 0 )
310
- {
311
- size_t len = strnlen (arg ,MAXLOGNAMESZ );
312
- if (len > 0 && len < MAXLOGNAMESZ )
313
- {
314
- strncpy (log_filename ,arg ,MAXLOGNAMESZ - 1 );
315
- }
316
- else
317
- {
318
- //print_usage(basename(argv[0]));
319
- exit (1 );
301
+ if (arg != 0 ) {
302
+ const size_t len = strnlen (arg , MAXLOGNAMESZ );
303
+
304
+ if (len > 0 && len < MAXLOGNAMESZ ) {
305
+ strncpy (log_filename , arg , MAXLOGNAMESZ - 1 );
306
+ } else {
307
+ return 1 ;
320
308
}
321
309
is_auto_log_name = 0 ;
322
- }
323
- else
324
- {
310
+ } else {
325
311
is_auto_log_name = 1 ;
326
- sprint_auto_filename_format (& log_filename [ 0 ] );
312
+ sprint_auto_filename_format (log_filename );
327
313
}
314
+
315
+ return 0 ;
328
316
}
329
317
330
318
int main (int argc , char * * argv )
@@ -361,14 +349,12 @@ int main(int argc, char **argv)
361
349
struct timeval tv , last_tv ;
362
350
struct timeval timeout , timeout_config = { 0 , 0 }, * timeout_current = NULL ;
363
351
364
- {
365
- sigset_t sig_mask ;
366
- sigemptyset (& sig_mask );
352
+ sigset_t sig_mask ;
353
+ sigemptyset (& sig_mask );
367
354
368
- struct sigaction sigterm_action = {.sa_mask = sig_mask , .sa_flags = SA_RESTART , .sa_handler = sigterm };
369
- sigaction (SIGHUP , & sigterm_action , NULL );
370
- sigaction (SIGINT , & sigterm_action , NULL );
371
- }
355
+ struct sigaction sigterm_action = { .sa_mask = sig_mask , .sa_flags = SA_RESTART , .sa_handler = sigterm };
356
+ sigaction (SIGHUP , & sigterm_action , NULL );
357
+ sigaction (SIGINT , & sigterm_action , NULL );
372
358
373
359
last_tv .tv_sec = 0 ;
374
360
last_tv .tv_usec = 0 ;
@@ -377,9 +363,8 @@ int main(int argc, char **argv)
377
363
378
364
//Since interface is a required argument, we don't need to parse opt for final arg
379
365
//This enabled the -l option to take an optional filename
380
- if (getoptargc > 0 )
381
- {
382
- getoptargc = argc - 1 ;
366
+ if (getoptargc > 0 ) {
367
+ getoptargc = argc - 1 ;
383
368
}
384
369
385
370
while ((opt = getopt (getoptargc , argv , ":t:HciaSs:l:DdxLn:r:he8T:?" )) != -1 ) {
@@ -438,21 +423,26 @@ int main(int argc, char **argv)
438
423
{
439
424
log = 1 ;
440
425
441
- get_logname_arg (optarg );
442
-
426
+ if (process_logname_arg (optarg ) != 0 ) {
427
+ print_usage (basename (argv [0 ]));
428
+ exit (1 );
429
+ }
443
430
break ;
444
431
}
445
432
default :
446
433
fprintf (stderr , "option -%c is missing a required argument\n" , optopt );
447
434
return EXIT_FAILURE ;
448
435
}
449
-
450
436
break ;
451
437
452
438
case 'l' :
453
439
log = 1 ;
454
440
455
- get_logname_arg (optarg );
441
+ if (process_logname_arg (optarg ) != 0 ) {
442
+ is_auto_log_name = 0 ;
443
+ print_usage (basename (argv [0 ]));
444
+ exit (1 );
445
+ }
456
446
457
447
break ;
458
448
@@ -511,15 +501,13 @@ int main(int argc, char **argv)
511
501
exit (0 );
512
502
}
513
503
514
- //Configure SIGHUP handler to reopen file if logging
515
- {
516
- sigset_t sig_block_mask ;
517
- sigfillset (& sig_block_mask );
518
- struct sigaction usr_action = {.sa_mask = sig_block_mask , .sa_flags = SA_RESTART };
519
- usr_action .sa_handler = log ? sighup : sigterm ;
520
- sigaction (SIGHUP , & usr_action , NULL );
521
- }
522
-
504
+ /*Configure SIGHUP handler to reopen file if logging*/
505
+ sigset_t sig_block_mask ;
506
+ sigfillset (& sig_block_mask );
507
+ struct sigaction usr_action = { .sa_mask = sig_block_mask , .sa_flags = SA_RESTART };
508
+ usr_action .sa_handler = log ? sighup : sigterm ;
509
+ sigaction (SIGHUP , & usr_action , NULL );
510
+
523
511
if (logfrmt && view ) {
524
512
fprintf (stderr , "Log file format selected: Please disable ASCII/BINARY/SWAP/RAWDLC options!\n" );
525
513
exit (0 );
@@ -734,10 +722,8 @@ int main(int argc, char **argv)
734
722
if (log ) {
735
723
int result = open_log_file (log_filename );
736
724
737
- if (result != 0 )
738
- {
725
+ if (result != 0 )
739
726
return 1 ;
740
- }
741
727
}
742
728
743
729
/* these settings are static and can be held out of the hot path */
@@ -759,20 +745,15 @@ int main(int argc, char **argv)
759
745
if ((ret = select (s [currmax - 1 ]+ 1 , & rdfs , NULL , NULL , timeout_current )) <= 0 ) {
760
746
//perror("select");
761
747
762
- error_t serr = errno ;
748
+ const error_t serr = errno ;
763
749
764
- if (serr == EINTR )
765
- {
766
- if (flag_reopen_file == 1 )
767
- {
768
- if (reopen_file (logfile ) != 0 )
769
- {
770
- return 1 ;
750
+ if (serr == EINTR ) {
751
+ if (flag_reopen_file == 1 ) {
752
+ if (reopen_file (logfile ) != 0 ) {
753
+ return -1 ;
771
754
}
772
755
}
773
- }
774
- else
775
- {
756
+ } else {
776
757
running = 0 ;
777
758
}
778
759
@@ -962,10 +943,8 @@ int main(int argc, char **argv)
962
943
fflush (stdout );
963
944
}
964
945
965
- if (flag_reopen_file == 1 )
966
- {
967
- if (reopen_file (logfile ) != 0 )
968
- {
946
+ if (flag_reopen_file == 1 ) {
947
+ if (reopen_file (logfile ) != 0 ) {
969
948
return -1 ;
970
949
}
971
950
}
0 commit comments