Skip to content
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

canlogserver: Close accsocket and can when tcp client disconnected #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions canlogserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ int idx2dindex(int ifidx, int socket)
return i;
}

/*
/*
* This is a Signalhandler. When we get a signal, that a child
* terminated, we wait for it, so the zombie will disappear.
*/
Expand Down Expand Up @@ -303,7 +303,7 @@ int main(int argc, char **argv)
exit(1);
}
}

for (i=0; i<currmax; i++) {

#ifdef DEBUG
Expand All @@ -314,7 +314,9 @@ int main(int argc, char **argv)

if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("socket");
return 1;
while (--i > 0)
close(s[i]);
goto close_accsocket;
}

if (mask[i] || value[i]) {
Expand Down Expand Up @@ -371,17 +373,32 @@ int main(int argc, char **argv)

while (running) {

int maxfd = accsocket;

FD_ZERO(&rdfs);
for (i=0; i<currmax; i++)
FD_SET(accsocket, &rdfs);
for (i=0; i<currmax; i++) {
maxfd = s[i] > maxfd ? s[i] : maxfd;
FD_SET(s[i], &rdfs);
}

if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
if ((ret = select(maxfd + 1, &rdfs, NULL, NULL, NULL)) < 0) {
//perror("select");
running = 0;
continue;
}

for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
if (FD_ISSET(accsocket, &rdfs)) {
char unused[64];

nbytes = recv(accsocket, unused, sizeof(unused), 0);
if (nbytes <= 0) {
if (nbytes < 0)
perror("accsocket read");
goto close_can;
}
}

if (FD_ISSET(s[i], &rdfs)) {

Expand All @@ -391,7 +408,7 @@ int main(int argc, char **argv)
if ((nbytes = recvfrom(s[i], &frame, CANFD_MTU, 0,
(struct sockaddr*)&addr, &len)) < 0) {
perror("read");
return 1;
goto close_can;
}

if ((size_t)nbytes == CAN_MTU)
Expand All @@ -400,7 +417,7 @@ int main(int argc, char **argv)
maxdlen = CANFD_MAX_DLEN;
else {
fprintf(stderr, "read: incomplete CAN frame\n");
return 1;
goto close_can;
}

if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
Expand All @@ -411,14 +428,14 @@ int main(int argc, char **argv)

sprintf(temp, "(%ld.%06ld) %*s ",
tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]);
sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen);
sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen);
strcat(temp, "\n");

if (write(accsocket, temp, strlen(temp)) < 0) {
perror("writeaccsock");
return 1;
goto close_can;
}

#if 0
/* print CAN frame in log file style to stdout */
printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
Expand All @@ -430,9 +447,11 @@ int main(int argc, char **argv)
}
}

close_can:
for (i=0; i<currmax; i++)
close(s[i]);

close_accsocket:
close(accsocket);
return 0;
}