/[monit]/monit/net.c
ViewVC logotype

Diff of /monit/net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.25 by hauk, Thu Jul 31 00:32:13 2003 UTC revision 1.26 by hauk, Fri Aug 1 01:10:59 2003 UTC
# Line 161  int check_host(char *hostname) { Line 161  int check_host(char *hostname) {
161   * @return TRUE if the socket is ready, otherwise FALSE.   * @return TRUE if the socket is ready, otherwise FALSE.
162   */   */
163  int check_socket(int socket) {  int check_socket(int socket) {
     
   fd_set rset, wset;  
   struct timeval tv;  
164    
165    FD_ZERO(&rset);    return (can_read(socket, 0) || can_write(socket, 0));
   FD_ZERO(&wset);  
   FD_SET(socket, &rset);  
   FD_SET(socket, &wset);  
   tv.tv_sec= SELECT_TIMEOUT;  
   tv.tv_usec= 0;  
     
   return (select(socket+1, &rset, &wset, NULL, &tv) > 0);  
166        
167  }  }
168    
# Line 241  int create_socket(char *hostname, int po Line 231  int create_socket(char *hostname, int po
231    sin.sin_port= htons(port);    sin.sin_port= htons(port);
232    memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);    memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
233        
234    if(set_noblock(s) < 0) {    if(! set_noblock(s)) {
235      goto error;      goto error;
236    }    }
237    
# Line 305  int create_unix_socket(char *pathname) { Line 295  int create_unix_socket(char *pathname) {
295    unixsocket.sun_family= AF_UNIX;    unixsocket.sun_family= AF_UNIX;
296    snprintf(unixsocket.sun_path, sizeof(unixsocket.sun_path), "%s", pathname);    snprintf(unixsocket.sun_path, sizeof(unixsocket.sun_path), "%s", pathname);
297        
298    if(set_noblock(s) < 0) {    if(! set_noblock(s)) {
299      goto error;      goto error;
300    }    }
301        
# Line 397  int close_socket(int socket) { Line 387  int close_socket(int socket) {
387      r= close(socket);      r= close(socket);
388    } while(r == -1 && errno == EINTR);    } while(r == -1 && errno == EINTR);
389        
     
390    return r;    return r;
391    
392  }  }
# Line 446  int set_block(int socket) { Line 435  int set_block(int socket) {
435   */   */
436  int can_read(int socket, int timeout) {  int can_read(int socket, int timeout) {
437    
438      int r= 0;
439    fd_set rset;    fd_set rset;
440    struct timeval tv;    struct timeval tv;
441        
# Line 454  int can_read(int socket, int timeout) { Line 444  int can_read(int socket, int timeout) {
444    tv.tv_sec= timeout;    tv.tv_sec= timeout;
445    tv.tv_usec= 0;    tv.tv_usec= 0;
446        
447    return (select(socket+1, &rset, NULL, NULL, &tv) > 0);    do {
448          r= select(socket+1, &rset, NULL, NULL, &tv);
449      } while(r == -1 && errno == EINTR);
450    
451      return (r > 0);
452    
453  }  }
454    
455    
# Line 468  int can_read(int socket, int timeout) { Line 462  int can_read(int socket, int timeout) {
462   */   */
463  int can_write(int socket, int timeout) {  int can_write(int socket, int timeout) {
464    
465      int r= 0;
466    fd_set wset;    fd_set wset;
467    struct timeval tv;    struct timeval tv;
468    
# Line 476  int can_write(int socket, int timeout) { Line 471  int can_write(int socket, int timeout) {
471    tv.tv_sec= timeout;    tv.tv_sec= timeout;
472    tv.tv_usec= 0;    tv.tv_usec= 0;
473    
474    return (select(socket+1, NULL, &wset, NULL, &tv) > 0);    do {
475        r= select(socket+1, NULL, &wset, NULL, &tv);
476      } while(r == -1 && errno == EINTR);
477    
478      return (r > 0);
479        
480  }  }
481    
# Line 487  int can_write(int socket, int timeout) { Line 486  int can_write(int socket, int timeout) {
486   * @param socket the socket to write to   * @param socket the socket to write to
487   * @param buffer The buffer to write   * @param buffer The buffer to write
488   * @param size Number of bytes to send   * @param size Number of bytes to send
489   * @return The number of bytes sent or -1 if an error   * @return The number of bytes sent or -1 if an error occured.
  * occured. Returning 0 means EOF  
490   */   */
491  int sock_write(int socket, void *buffer, int size) {  int sock_write(int socket, void *buffer, int size) {
492    
# Line 503  int sock_write(int socket, void *buffer, Line 501  int sock_write(int socket, void *buffer,
501    } while(n == -1 && errno == EINTR);    } while(n == -1 && errno == EINTR);
502        
503    if(n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {    if(n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
504      if(can_write(socket, 1) <= 0) {      if(! can_write(socket, 0)) {
505        return -1;        return -1;
506      }      }
507      do {      do {
# Line 511  int sock_write(int socket, void *buffer, Line 509  int sock_write(int socket, void *buffer,
509      } while(n == -1 && errno == EINTR);      } while(n == -1 && errno == EINTR);
510    }    }
511        
   if(n < 0) {  
     return -1;  
   }  
   
512    return n;    return n;
513    
514  }  }
# Line 528  int sock_write(int socket, void *buffer, Line 522  int sock_write(int socket, void *buffer,
522   * @param buffer The buffer to write the data to   * @param buffer The buffer to write the data to
523   * @param size Number of bytes to read from the socket   * @param size Number of bytes to read from the socket
524   * @param timeout Seconds to wait for data to be available   * @param timeout Seconds to wait for data to be available
525   * @return The number of bytes read or -1 if an error   * @return The number of bytes read or -1 if an error occured.
  * occured. Returning 0 means EOF  
526  */  */
527  int sock_read(int socket, void *buffer, int size, int timeout) {  int sock_read(int socket, void *buffer, int size, int timeout) {
528        
529    ssize_t n;    ssize_t n;
530    
531      if(size<=0)
532          return 0;
533        
534    errno= 0;    errno= 0;
535    do {    do {
# Line 541  int sock_read(int socket, void *buffer, Line 537  int sock_read(int socket, void *buffer,
537    } while(n == -1 && errno == EINTR);    } while(n == -1 && errno == EINTR);
538        
539    if(n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {    if(n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
540      if(can_read(socket, timeout) <= 0) {      if(! can_read(socket, timeout)) {
541        return -1;        return -1;
542      }      }
543      do {      do {

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.26

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26