-
Notifications
You must be signed in to change notification settings - Fork 714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update signal handlers to use sigaction, and re-open logfile on SIGHUP to integrate logrotated support #268
base: master
Are you sure you want to change the base?
Conversation
Thanks for the reference to PR #189 from @BugLight which instantly came into my mind too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the kernel coding style:
https://github.com/torvalds/linux/blob/master/Documentation/process/coding-style.rst
candump.c
Outdated
extern int optind, opterr, optopt; | ||
|
||
static volatile int running = 1; | ||
static volatile int flag_reopen_file = 0; | ||
static volatile unsigned long sighup_count = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to initialize statics with 0
candump.c
Outdated
FILE *logfile = NULL; | ||
static char log_filename[MAXLOGNAMESZ]; | ||
|
||
unsigned char silent = SILENT_INI; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static
candump.c
Outdated
void sighup(int signo) | ||
{ | ||
if(signo == SIGHUP) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use kernel coding style
if (signo == SIGHUP) {
@@ -214,6 +233,100 @@ int idx2dindex(int ifidx, int socket) { | |||
return i; | |||
} | |||
|
|||
int sprint_auto_filename_format(char * buffer) | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(char *buffer)
candump.c
Outdated
return 0; | ||
} | ||
|
||
//opens file using global filehandle logfile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no c++ comments please
candump.c
Outdated
{ | ||
if(arg != 0) | ||
{ | ||
size_t len = strnlen(arg,MAXLOGNAMESZ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a newline after the variable declaration and add a space after the ,
candump.c
Outdated
{ | ||
strncpy(log_filename,arg,MAXLOGNAMESZ-1); | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else {
candump.c
Outdated
else | ||
{ | ||
//print_usage(basename(argv[0])); | ||
exit(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please return the error and handle it
candump.c
Outdated
} | ||
is_auto_log_name = 0; | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else {
candump.c
Outdated
signal(SIGTERM, sigterm); | ||
signal(SIGHUP, sigterm); | ||
signal(SIGINT, sigterm); | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you open a new scope here?
92806ce
to
60be78c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the remaining coding style and then squash all patches, that there is only one.
candump.c
Outdated
static volatile int flag_reopen_file; | ||
static volatile unsigned long sighup_count; | ||
|
||
static int is_auto_log_name = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to initialize static vars with 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are general issues with this patch. See #268 (comment)
It does not make sense to discuss style problems until the general approach and concept is under discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO if we implement this kind of multi-chunk file generation we should support either the size based triggers as suggested by @BugLight AND the SIGHUP approach from you.
What do you mean: "either...or" (as in one or the other) or "and" (as in both)?
Once file naming and new file generation is solved, triggering the switch to a new file due to SIGHUP
or size limit is hopefully not that complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this was my intention!
But the way the filename is generated still needs some discussion IMO.
And finally I thought the PR #189 to be a simpler starting point, from which we might add the SIGHUP handling. But that's probably just my view.
candump.c
Outdated
@@ -259,14 +257,13 @@ int sprint_auto_filename_format(char * buffer) | |||
return 0; | |||
} | |||
|
|||
//opens file using global filehandle logfile | |||
int open_log_file(const char * fileName) | |||
/*opens file using global var logfile*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a space before and after the *
/* comment */
candump.c
Outdated
const size_t len = strnlen(arg, MAXLOGNAMESZ); | ||
|
||
if (len > 0 && len < MAXLOGNAMESZ) { | ||
strncpy(log_filename, arg, MAXLOGNAMESZ-1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAXLOGNAMESZ - 1
candump.c
Outdated
sigaction(SIGHUP, &usr_action, NULL); | ||
} | ||
|
||
/*Configure SIGHUP handler to reopen file if logging*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* add space */
candump.c
Outdated
/*Configure SIGHUP handler to reopen file if logging*/ | ||
sigset_t sig_block_mask; | ||
sigfillset(&sig_block_mask); | ||
struct sigaction usr_action = { .sa_mask = sig_block_mask, .sa_flags = SA_RESTART }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move the variable declaration to the beginning of the scope
candump.c
Outdated
if(reopen_file(logfile) != 0) | ||
{ | ||
if (flag_reopen_file == 1) { | ||
if(reopen_file(logfile) != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (
@hartkopp, for me, it seems that name files using only timestamp (no part number) is more general and better approach. |
60be78c
to
1730bb4
Compare
Thanks for the feedback everybody. I have rebased on the recent commits to master, and most importantly the epoll changes. Regarding file name generation I want the ability to choose the output filename/format. Below is a simplified example of a logrotate setup that I am looking to support. When triggered, logrotate copies that static filename to configured name / path, then sends SIGHUP. The idea is that previous data is archived, and candump recreates the original filename and continues logging. /etc/logrotate.d/candump1
|
1730bb4
to
b3d0b3c
Compare
This sounds strange! You COPY the file and THEN issue the SIGHUP? Isn't there the chance that you record some frames twice? This code makes a very clear close/open sequence between two CAN frames:
Instead of your suggestion |
Sorry I meant move/rename the file. This is the debug output of related logrotated action.
|
It is possible to do something like this, but with >> always append mode and logrotated, but it is less ideal, as it is possible to lose data. This is because it requires the "copytruncate" option, which copies the file, and truncates it so the original filehandle is preserved. I agree that logrotate is kind of weird. However, it is widely deployed so building in compatibility would be helpful. I like the idea of unifying the internal rotation functionality to internally use SIGHUP to trigger the size based rotation events.
Since a SIGHUP can occur at any time (even when can messages aren't being received), I am currently setting a flag on the signal and handling the rotation in the main loop. I also needed to handle the error result from interrupting epoll with sighup signal, so i also rotate/reopen file immediately if this occurs. |
b3d0b3c
to
fd7ad60
Compare
1. Treats sighup as signal to close and reopen log file 2. Candump only supports autogenerated log file name by default. For use with logrotation daemon it expects consistent filename. I updated -l option to accept optional filename. 3. Since interface is a required argument, getopt skips final arg. Made -l take optional filename. Without filename maintains existing behavior. 4. Since sighup can interupt the select system call, now checking epoll error code for interupted syscal and logfile enabled. If those conditions are met, the errorcode is non critical.
fd7ad60
to
3b5cbc0
Compare
|
||
switch (optopt) | ||
{ | ||
case 'l': | ||
log = 1; | ||
|
||
if (process_logname_arg(optarg) != 0) { | ||
print_usage(basename(argv[0])); | ||
exit(1); | ||
} | ||
break; | ||
default: | ||
fprintf(stderr, "option -%c is missing a required argument\n", optopt); | ||
return EXIT_FAILURE; | ||
} | ||
break; | ||
|
||
case 'l': | ||
log = 1; | ||
|
||
if (process_logname_arg(optarg) != 0) { | ||
is_auto_log_name = 0; | ||
print_usage(basename(argv[0])); | ||
exit(1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please think about an integration into a logrotated configuration, that can cope with the assumptions:
- The option '-l' remains untouched
- The logfile is closed and a new logfile is opened with a new date-generated filename
So no re-open/append stuff and no change with the naming.
You might also execute candump in /tmp/candump/
to place the files there for logrotation post-processing.
I'm fine with creating two file splitting triggers about the length and the SIGHUP. But the current approach just adds too much complexity that does not need to be inside candump.
Is there any movement on this PR or the linked one? It would be nice to have log rotation when recording very long logs for lengthy tests. |
+1 on getting this in |
The infrastructure to create individual filenames changed in ad250a6 and c70d0a8 which solved my main concern to not break the So the original patch needs to be reworked and rebased. Additionally it has to be discussed whether the logfile names are autogenerated with date/time or should be based on an individual filename with an increasing counter (see #268 (comment)). Edit: Suggestion, which would could add the signal handling and file splitting without adding new options: Use of option
|
resolves #267
Also attempt to resolve #38 as I was updating signal handling anyways.
A similar PR #189 was created to support integrated file rotation based on size. It however was not accepted due to increase bloat in candump, and not following unix philosophy of doing one thing well, and chaining functionality. This PR attempts to address these concerns by implementing the signal handler SIGHUP required, to integrate with existing log rotation tools.