improved error handling
[svn42.git] / door_daemon / door_daemon.c
index 8690d64..e9c02a1 100644 (file)
@@ -95,7 +95,7 @@ int send_command(int door_fd, cmd_t* cmd)
   } while(!ret || (ret == -1 && errno == EINTR));
 
   if(ret > 0) {
-    cmd->sent = 1;
+    cmd_sent(cmd);
     return 0;
   }
 
@@ -230,11 +230,26 @@ int nonblock_recvline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* cl
 int process_door(read_buffer_t* buffer, int door_fd, cmd_t **cmd_q, client_t* client_lst)
 {
   int ret = 0;
+  struct timeval tv;
+  fd_set fds;
+  FD_ZERO(&fds);
+  FD_SET(door_fd, &fds);
+
   for(;;) {
+    tv.tv_sec = 0;
+    tv.tv_usec = 0;
+    ret = select(door_fd+1, &fds, NULL, NULL, &tv);
+    if(!ret)
+      return 0;
+    else if(ret < 0)
+      return ret;
+
     ret = read(door_fd, &buffer->buf[buffer->offset], 1);
+    if(!ret)
+      return 2;
     if(ret == -1 && errno == EAGAIN)
       return 0;
-    else if(ret <= 0)
+    else if(ret < 0)
       break;
 
     if(buffer->buf[buffer->offset] == '\n') {
@@ -292,18 +307,29 @@ int main_loop(int door_fd, int cmd_listen_fd)
   FD_SET(sig_fd, &readfds);
   max_fd = (max_fd < sig_fd) ? sig_fd : max_fd;
 
+  struct timeval timeout;
   int return_value = 0;
   while(!return_value) {
     memcpy(&tmpfds, &readfds, sizeof(tmpfds));
 
-    int ret = select(max_fd+1, &tmpfds, NULL, NULL, NULL);
+    timeout.tv_sec = 0;
+    timeout.tv_usec = 200000;
+    int ret = select(max_fd+1, &tmpfds, NULL, NULL, &timeout);
     if(ret == -1 && errno != EINTR) {
       log_printf(ERROR, "select returned with error: %s", strerror(errno));
       return_value = -1;
       break;
     }
-    if(!ret || ret == -1)
+    if(ret == -1)
       continue;
+    if(!ret) {
+      if(cmd_q && cmd_has_expired(*cmd_q)) {
+        log_printf(ERROR, "last command expired");
+        cmd_pop(&cmd_q);
+      }
+      else
+        continue;
+    }
 
     if(FD_ISSET(sig_fd, &tmpfds)) {
       if(signal_handle()) {
@@ -513,7 +539,7 @@ int main(int argc, char* argv[])
   
   int door_fd = 0;
   for(;;) {
-    door_fd = open(opt.door_dev_, O_RDWR | O_NOCTTY | O_NONBLOCK);
+    door_fd = open(opt.door_dev_, O_RDWR | O_NOCTTY);
     if(door_fd < 0)
       ret = 2;
     else {