1 /* net/connectu_timeout.c - Make an UNIX domain connection with a timeout
2  * Copyright (C) 2004,2005  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18 #include "sysdeps.h"
19 #include <errno.h>
20 #include "socket.h"
21 #include "unix.h"
22 
23 /** Connect a socket to an UNIX domain address, honouring a timeout.
24  *
25  * \note The socket will have nonblocking mode turned on on return from
26  * this function. */
socket_connectu_timeout(int sock,const char * path,int timeout)27 int socket_connectu_timeout(int sock, const char* path, int timeout)
28 {
29   iopoll_fd pf;
30   if (!nonblock_on(sock))
31     return 0;
32   if (socket_connectu(sock, path))
33     return 1;
34   if (errno != EINPROGRESS && errno != EWOULDBLOCK)
35     return 0;
36   pf.fd = sock;
37   pf.events = IOPOLL_WRITE;
38   switch (iopoll_restart(&pf, 1, timeout)) {
39   case 0:
40     errno = ETIMEDOUT;
41     return 0;
42   case 1:
43     if (socket_connected(sock))
44       return 1;
45     /* No break, fall through */
46   default:
47     return 0;
48   }
49 }
50