canlogserver: Close accsocket and can when tcp client disconnected
Close all can socket and send "FYI" to tcp client when the tcp client disconnected. Signed-off-by: qianfan Zhao <qianfanguijin@163.com>pull/149/head
parent
c10c049ce7
commit
0003cad81a
|
|
@ -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.
|
||||
*/
|
||||
|
|
@ -303,7 +303,7 @@ int main(int argc, char **argv)
|
|||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (i=0; i<currmax; i++) {
|
||||
|
||||
#ifdef DEBUG
|
||||
|
|
@ -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]) {
|
||||
|
|
@ -371,17 +373,32 @@ int main(int argc, char **argv)
|
|||
|
||||
while (running) {
|
||||
|
||||
FD_ZERO(&rdfs);
|
||||
for (i=0; i<currmax; i++)
|
||||
FD_SET(s[i], &rdfs);
|
||||
int maxfd = accsocket;
|
||||
|
||||
if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
|
||||
FD_ZERO(&rdfs);
|
||||
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(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)) {
|
||||
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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);
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue