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

Diff of /monit/net.c

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

revision 1.27 by hauk, Fri Aug 1 21:35:59 2003 UTC revision 1.28 by hauk, Mon Aug 4 07:52:12 2003 UTC
# Line 100  Line 100 
100  /* -------------------------------------------------------------- Prototypes */  /* -------------------------------------------------------------- Prototypes */
101    
102    
103  static int do_connect(int s, const struct sockaddr *addr, socklen_t addrlen);  static int do_connect(int s, const struct sockaddr *addr,
104                          socklen_t addrlen, int timeout);
105    
106    
107  /* ------------------------------------------------------------------ Public */  /* ------------------------------------------------------------------ Public */
# Line 122  int check_connect(char *hostname, int po Line 123  int check_connect(char *hostname, int po
123    
124    ASSERT(hostname);    ASSERT(hostname);
125        
126    if((socket= create_socket(hostname, port, protocol)) < 0) {    if((socket= create_socket(hostname, port, protocol, 3)) < 0) {
127      rv= FALSE;      rv= FALSE;
128    } else if(! check_socket(socket)) {    } else if(! check_socket(socket)) {
129      rv= FALSE;      rv= FALSE;
# Line 209  int check_udp_socket(int socket) { Line 210  int check_udp_socket(int socket) {
210   * @param hostname The host to open a socket at   * @param hostname The host to open a socket at
211   * @param port The port number to connect to   * @param port The port number to connect to
212   * @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM)   * @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM)
213     * @param timeout If not connected within timeout seconds abort and return -1
214   * @return The socket or -1 if an error occured.   * @return The socket or -1 if an error occured.
215   */   */
216  int create_socket(char *hostname, int port, int protocol) {  int create_socket(char *hostname, int port, int protocol, int timeout) {
217    
218    int s;    int s;
219    struct hostent *hp;    struct hostent *hp;
# Line 235  int create_socket(char *hostname, int po Line 237  int create_socket(char *hostname, int po
237      goto error;      goto error;
238    }    }
239    
240    if(do_connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {    if(do_connect(s, (struct sockaddr *)&sin, sizeof(sin), timeout) < 0) {
241      goto error;      goto error;
242    }    }
243    
# Line 262  int create_generic_socket(Port_T p) { Line 264  int create_generic_socket(Port_T p) {
264    
265    switch(p->family) {    switch(p->family) {
266    case AF_UNIX:    case AF_UNIX:
267        socket_fd= create_unix_socket(p->pathname);        socket_fd= create_unix_socket(p->pathname, p->timeout);
268        break;        break;
269    case AF_INET:    case AF_INET:
270        socket_fd= create_socket(p->hostname, p->port, p->type);        socket_fd= create_socket(p->hostname, p->port, p->type, p->timeout);
271        break;        break;
272    default:    default:
273        socket_fd= -1;        socket_fd= -1;
# Line 279  int create_generic_socket(Port_T p) { Line 281  int create_generic_socket(Port_T p) {
281  /**  /**
282   * Create a non-blocking UNIX socket.   * Create a non-blocking UNIX socket.
283   * @param pathname The pathname to use for the unix socket   * @param pathname The pathname to use for the unix socket
284     * @param timeout If not connected within timeout seconds abort and return -1
285   * @return The socket or -1 if an error occured.   * @return The socket or -1 if an error occured.
286   */   */
287  int create_unix_socket(char *pathname) {  int create_unix_socket(char *pathname, int timeout) {
288    
289    int s;    int s;
290    struct sockaddr_un unixsocket;    struct sockaddr_un unixsocket;
# Line 299  int create_unix_socket(char *pathname) { Line 302  int create_unix_socket(char *pathname) {
302      goto error;      goto error;
303    }    }
304        
305    if(do_connect(s, (struct sockaddr *)&unixsocket, sizeof(unixsocket)) < 0) {    if(do_connect(s, (struct sockaddr *)&unixsocket, sizeof(unixsocket),
306                    timeout) < 0) {
307      goto error;      goto error;
308    }    }
309        
# Line 554  int sock_read(int socket, void *buffer, Line 558  int sock_read(int socket, void *buffer,
558    
559    
560  /*  /*
561   * Do a non blocking connect, timeout if not connected within 5 seconds   * Do a non blocking connect, timeout if not connected within timeout seconds
562   */   */
563  static int do_connect(int s, const struct sockaddr *addr, socklen_t addrlen) {  static int do_connect(int s, const struct sockaddr *addr,
564                          socklen_t addrlen, int timeout) {
565    
566    int n, error;    int n, error;
567    fd_set wset, rset;    fd_set wset, rset;
# Line 572  static int do_connect(int s, const struc Line 577  static int do_connect(int s, const struc
577    FD_ZERO(&rset);    FD_ZERO(&rset);
578    FD_SET(s, &rset);    FD_SET(s, &rset);
579    wset= rset;    wset= rset;
580    tv.tv_sec= 5;    tv.tv_sec= timeout;
581    tv.tv_usec= 0;    tv.tv_usec= 0;
582        
583    if(select(s+1, NULL, &rset, &wset, &tv)==0) {    if(select(s+1, NULL, &rset, &wset, &tv)==0) {
# Line 589  static int do_connect(int s, const struc Line 594  static int do_connect(int s, const struc
594        return -1;        return -1;
595    
596    if(error) {    if(error) {
     close(s);  
597      errno= error;      errno= error;
598      return -1;      return -1;
599    }    }

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.28

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