summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2009-12-01 12:32:16 (GMT)
committerChristian Pointner <equinox@helsinki.at>2009-12-01 12:32:16 (GMT)
commit5bbdf1632ebb4c41d4520dc373b625267c4df16a (patch)
tree7e9e6a0568fd2c36138bd3fc7e50e9e48f739bcc
parent388413cc9a55c3935c332159ef9a74f195193cf1 (diff)
heartbeatclient reconnects after socket error now
-rw-r--r--heartbeatclient.c58
1 files changed, 36 insertions, 22 deletions
diff --git a/heartbeatclient.c b/heartbeatclient.c
index f663945..71a381f 100644
--- a/heartbeatclient.c
+++ b/heartbeatclient.c
@@ -157,8 +157,16 @@ int main_loop(int serial_fd, int cmd_fd, options_t* opt)
break;
}
- if(FD_ISSET(cmd_fd, &tmpfds))
- return_value = 0; // ignore data from command sock
+ if(FD_ISSET(cmd_fd, &tmpfds)) {
+ u_int8_t buf[100];
+ int ret = recv(cmd_fd, buf, 100, 0);
+ if(!ret)
+ return_value = 3;
+ if(ret == -1 && errno == EAGAIN)
+ return_value = 0;
+ else if(ret < 0)
+ return_value = ret;
+ }
}
signal_stop();
@@ -259,28 +267,33 @@ int main(int argc, char* argv[])
fclose(pid_file);
}
- int cmd_fd = connect_command_socket(opt.command_sock_);
- if(cmd_fd < 0) {
- options_clear(&opt);
- log_close();
- exit(-1);
- }
-
+ int cmd_fd = 0;
int serial_fd = 0;
for(;;) {
- serial_fd = open(opt.serial_dev_, O_RDWR | O_NOCTTY);
- if(serial_fd < 0)
- ret = 2;
- else {
- ret = setup_tty(serial_fd, opt.baudrate_);
- if(ret)
+ cmd_fd = connect_command_socket(opt.command_sock_);
+ if(cmd_fd < 0)
+ ret = 3;
+ else {
+ serial_fd = open(opt.serial_dev_, O_RDWR | O_NOCTTY);
+ if(serial_fd < 0)
ret = 2;
- else
- ret = main_loop(serial_fd, cmd_fd, &opt);
- }
-
- if(ret == 2) {
- log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.serial_dev_);
+ else {
+ ret = setup_tty(serial_fd, opt.baudrate_);
+ if(ret)
+ ret = 2;
+ else
+ ret = main_loop(serial_fd, cmd_fd, &opt);
+ }
+ }
+
+ if(ret == 2 || ret == 3) {
+ if(ret == 2)
+ log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.serial_dev_);
+ if(ret == 3)
+ log_printf(ERROR, "socket error, trying to reconnect in 5 seconds..");
+
+ if(cmd_fd > 0)
+ close(cmd_fd);
if(serial_fd > 0)
close(serial_fd);
sleep(5);
@@ -289,7 +302,8 @@ int main(int argc, char* argv[])
break;
}
- close(cmd_fd);
+ if(cmd_fd > 0)
+ close(cmd_fd);
if(serial_fd > 0)
close(serial_fd);