1 #include "syshdrs.h"
2 
3 #if !defined(NO_UNIX_DOMAIN_SOCKETS)
4 
5 int
UBind(int sockfd,const char * const astr,const int nTries,const int reuseFlag)6 UBind(int sockfd, const char *const astr, const int nTries, const int reuseFlag)
7 {
8 	unsigned int i;
9 	int on;
10 	int onsize;
11 	struct sockaddr_un localAddr;
12 	int ualen;
13 
14 	ualen = MakeSockAddrUn(&localAddr, astr);
15 	(void) unlink(localAddr.sun_path);
16 
17 	if (reuseFlag != kReUseAddrNo) {
18 		/* This is mostly so you can quit the server and re-run it
19 		 * again right away.  If you don't do this, the OS may complain
20 		 * that the address is still in use.
21 		 */
22 		on = 1;
23 		onsize = (int) sizeof(on);
24 		(void) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
25 			(char *) &on, onsize);
26 	}
27 
28 	for (i=1; ; i++) {
29 		/* Try binding a few times, in case we get Address in Use
30 		 * errors.
31 		 */
32 		if (bind(sockfd, (struct sockaddr *) &localAddr, ualen) == 0) {
33 			break;
34 		}
35 		if (i == (unsigned int) nTries) {
36 			return (-1);
37 		}
38 		/* Give the OS time to clean up the old socket,
39 		 * and then try again.
40 		 */
41 		sleep(i * 3);
42 	}
43 
44 	return (0);
45 }	/* UBind */
46 
47 
48 
49 
50 int
UListen(int sfd,int backlog)51 UListen(int sfd, int backlog)
52 {
53 	return (listen(sfd, backlog));
54 }	/* UListen */
55 
56 #endif
57 
58