canlogserver: fix infinite loops during signal handling
Previously, the `canlogserver` could get stuck in infinite loops when receiving a signal (e.g., SIGINT via Ctrl-C) in two scenarios: 1. Inside the `bind()` retry loop: If the port was busy (e.g., in TIME_WAIT), the loop retried indefinitely using `nanosleep` without checking the `running` flag set by the signal handler. 2. Inside the `accept()` loop: The loop was unconditional (`while(1)`). If `accept()` was interrupted by a signal, it returned with `EINTR`, but the loop immediately restarted, effectively ignoring the shutdown request. This patch adds checks for the `running` flag in both loops to ensure the server terminates correctly upon receiving a signal. It also ensures the socket is closed properly and the correct exit code (128 + signal number) is returned. Signed-off-by: Oleksij Rempel <linux@rempel-privat.de>pull/615/head
parent
1f038b3cd8
commit
03cf00cbf3
|
|
@ -177,7 +177,8 @@ int main(int argc, char **argv)
|
||||||
sigset_t sigset;
|
sigset_t sigset;
|
||||||
fd_set rdfs;
|
fd_set rdfs;
|
||||||
int s[MAXDEV];
|
int s[MAXDEV];
|
||||||
int socki, accsocket;
|
int socki;
|
||||||
|
int accsocket = -1;
|
||||||
canid_t mask[MAXDEV] = {0};
|
canid_t mask[MAXDEV] = {0};
|
||||||
canid_t value[MAXDEV] = {0};
|
canid_t value[MAXDEV] = {0};
|
||||||
int inv_filter[MAXDEV] = {0};
|
int inv_filter[MAXDEV] = {0};
|
||||||
|
|
@ -286,7 +287,7 @@ int main(int argc, char **argv)
|
||||||
inaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
inaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
inaddr.sin_port = htons(port);
|
inaddr.sin_port = htons(port);
|
||||||
|
|
||||||
while(bind(socki, (struct sockaddr*)&inaddr, sizeof(inaddr)) < 0) {
|
while(running && bind(socki, (struct sockaddr*)&inaddr, sizeof(inaddr)) < 0) {
|
||||||
struct timespec f = {
|
struct timespec f = {
|
||||||
.tv_nsec = 100 * 1000 * 1000,
|
.tv_nsec = 100 * 1000 * 1000,
|
||||||
};
|
};
|
||||||
|
|
@ -295,12 +296,17 @@ int main(int argc, char **argv)
|
||||||
nanosleep(&f, NULL);
|
nanosleep(&f, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
close(socki);
|
||||||
|
return 128 + signal_num;
|
||||||
|
}
|
||||||
|
|
||||||
if (listen(socki, 3) != 0) {
|
if (listen(socki, 3) != 0) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while(1) {
|
while(running) {
|
||||||
accsocket = accept(socki, (struct sockaddr*)&clientaddr, &sin_size);
|
accsocket = accept(socki, (struct sockaddr*)&clientaddr, &sin_size);
|
||||||
if (accsocket > 0) {
|
if (accsocket > 0) {
|
||||||
//printf("accepted\n");
|
//printf("accepted\n");
|
||||||
|
|
@ -314,6 +320,11 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
close(socki);
|
||||||
|
return 128 + signal_num;
|
||||||
|
}
|
||||||
|
|
||||||
for (i=0; i<currmax; i++) {
|
for (i=0; i<currmax; i++) {
|
||||||
|
|
||||||
pr_debug("open %d '%s' m%08X v%08X i%d e%d.\n",
|
pr_debug("open %d '%s' m%08X v%08X i%d e%d.\n",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue