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