2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #undef Realloc /* SSLeay defines this and samba has a function of this name */
31 extern int DEBUGLEVEL;
35 /* the client file descriptor */
38 /* the last IP received from */
39 struct in_addr lastip;
41 /* the last port received from */
44 int smb_read_error = 0;
46 /****************************************************************************
47 Determine if a file descriptor is in fact a socket.
48 ****************************************************************************/
50 BOOL is_a_socket(int fd)
54 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
57 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
66 } socket_options[] = {
67 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
68 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
69 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
71 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
74 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
76 #ifdef IPTOS_THROUGHPUT
77 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
80 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
83 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
86 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
89 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
92 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
95 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
98 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
102 /****************************************************************************
103 Set user socket options.
104 ****************************************************************************/
106 void set_socket_options(int fd, char *options)
110 while (next_token(&options,tok," \t,", sizeof(tok))) {
114 BOOL got_value = False;
116 if ((p = strchr(tok,'='))) {
122 for (i=0;socket_options[i].name;i++)
123 if (strequal(socket_options[i].name,tok))
126 if (!socket_options[i].name) {
127 DEBUG(0,("Unknown socket option %s\n",tok));
131 switch (socket_options[i].opttype) {
134 ret = setsockopt(fd,socket_options[i].level,
135 socket_options[i].option,(char *)&value,sizeof(int));
140 DEBUG(0,("syntax error - %s does not take a value\n",tok));
143 int on = socket_options[i].value;
144 ret = setsockopt(fd,socket_options[i].level,
145 socket_options[i].option,(char *)&on,sizeof(int));
151 DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
155 /****************************************************************************
156 Close the socket communication.
157 ****************************************************************************/
159 void close_sockets(void )
162 sslutil_disconnect(Client);
163 #endif /* WITH_SSL */
169 /****************************************************************************
171 ****************************************************************************/
173 ssize_t read_udp_socket(int fd,char *buf,size_t len)
176 struct sockaddr_in sock;
179 socklen = sizeof(sock);
180 memset((char *)&sock,'\0',socklen);
181 memset((char *)&lastip,'\0',sizeof(lastip));
182 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
184 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
188 lastip = sock.sin_addr;
189 lastport = ntohs(sock.sin_port);
191 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
192 inet_ntoa(lastip), lastport, ret));
197 /****************************************************************************
198 Read data from a socket with a timout in msec.
199 mincount = if timeout, minimum to read before returning
200 maxcount = number to be read.
201 time_out = timeout in milliseconds
202 ****************************************************************************/
204 static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
210 struct timeval timeout;
212 /* just checking .... */
220 if (mincnt == 0) mincnt = maxcnt;
222 while (nread < mincnt) {
225 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
227 readret = read(fd, buf + nread, maxcnt - nread);
230 readret = read(fd, buf + nread, maxcnt - nread);
231 #endif /* WITH_SSL */
234 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
235 smb_read_error = READ_EOF;
240 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
241 smb_read_error = READ_ERROR;
246 return((ssize_t)nread);
249 /* Most difficult - timeout read */
250 /* If this is ever called on a disk file and
251 mincnt is greater then the filesize then
252 system performance will suffer severely as
253 select always returns true on disk files */
255 /* Set initial timeout */
256 timeout.tv_sec = (time_t)(time_out / 1000);
257 timeout.tv_usec = (long)(1000 * (time_out % 1000));
259 for (nread=0; nread < mincnt; ) {
263 selrtn = sys_select(fd+1,&fds,&timeout);
267 /* something is wrong. Maybe the socket is dead? */
268 DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
269 smb_read_error = READ_ERROR;
273 /* Did we timeout ? */
275 DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
276 smb_read_error = READ_TIMEOUT;
282 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
284 readret = read(fd, buf + nread, maxcnt - nread);
287 readret = read(fd, buf+nread, maxcnt-nread);
288 #endif /* WITH_SSL */
291 /* we got EOF on the file descriptor */
292 DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
293 smb_read_error = READ_EOF;
298 /* the descriptor is probably dead */
299 DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
300 smb_read_error = READ_ERROR;
307 /* Return the number we got */
308 return((ssize_t)nread);
311 /****************************************************************************
312 Read data from a fd with a timout in msec.
313 mincount = if timeout, minimum to read before returning
314 maxcount = number to be read.
315 time_out = timeout in milliseconds
316 ****************************************************************************/
318 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
324 struct timeval timeout;
326 /* just checking .... */
332 if (mincnt == 0) mincnt = maxcnt;
334 while (nread < mincnt) {
337 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
339 readret = read(fd, buf + nread, maxcnt - nread);
342 readret = read(fd, buf + nread, maxcnt - nread);
343 #endif /* WITH_SSL */
350 return((ssize_t)nread);
353 /* Most difficult - timeout read */
354 /* If this is ever called on a disk file and
355 mincnt is greater then the filesize then
356 system performance will suffer severely as
357 select always returns true on disk files */
359 /* Set initial timeout */
360 timeout.tv_sec = (time_t)(time_out / 1000);
361 timeout.tv_usec = (long)(1000 * (time_out % 1000));
363 for (nread=0; nread < mincnt; ) {
367 selrtn = sys_select(fd+1,&fds,&timeout);
374 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
376 readret = read(fd, buf + nread, maxcnt - nread);
379 readret = read(fd, buf+nread, maxcnt-nread);
380 #endif /* WITH_SSL */
388 /* Return the number we got */
389 return((ssize_t)nread);
392 /****************************************************************************
393 send a keepalive packet (rfc1002)
394 ****************************************************************************/
396 BOOL send_keepalive(int client)
398 unsigned char buf[4];
401 buf[1] = buf[2] = buf[3] = 0;
403 return(write_socket_data(client,(char *)buf,4) == 4);
406 /****************************************************************************
407 read data from the client, reading exactly N bytes.
408 ****************************************************************************/
410 ssize_t read_data(int fd,char *buffer,size_t N)
421 ret = SSL_read(ssl, buffer + total, N - total);
423 ret = read(fd,buffer + total,N - total);
426 ret = read(fd,buffer + total,N - total);
427 #endif /* WITH_SSL */
431 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
432 smb_read_error = READ_EOF;
437 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
438 smb_read_error = READ_ERROR;
443 return (ssize_t)total;
446 /****************************************************************************
447 Read data from a socket, reading exactly N bytes.
448 ****************************************************************************/
450 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
461 ret = SSL_read(ssl, buffer + total, N - total);
463 ret = read(fd,buffer + total,N - total);
466 ret = read(fd,buffer + total,N - total);
467 #endif /* WITH_SSL */
471 DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
472 smb_read_error = READ_EOF;
477 DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
478 smb_read_error = READ_ERROR;
483 return (ssize_t)total;
486 /****************************************************************************
488 ****************************************************************************/
490 ssize_t write_data(int fd,char *buffer,size_t N)
499 ret = SSL_write(ssl,buffer + total,N - total);
501 ret = write(fd,buffer + total,N - total);
504 ret = write(fd,buffer + total,N - total);
505 #endif /* WITH_SSL */
508 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
511 if (ret == 0) return total;
515 return (ssize_t)total;
518 /****************************************************************************
519 Write data to a socket - use send rather than write.
520 ****************************************************************************/
522 ssize_t write_socket_data(int fd,char *buffer,size_t N)
531 ret = SSL_write(ssl,buffer + total,N - total);
533 ret = send(fd,buffer + total,N - total, 0);
536 ret = send(fd,buffer + total,N - total,0);
537 #endif /* WITH_SSL */
540 DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
543 if (ret == 0) return total;
547 return (ssize_t)total;
550 /****************************************************************************
552 ****************************************************************************/
554 ssize_t write_socket(int fd,char *buf,size_t len)
560 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
561 ret = write_socket_data(fd,buf,len);
563 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
565 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
566 (int)len, fd, strerror(errno) ));
571 /****************************************************************************
572 read 4 bytes of a smb packet and return the smb length of the packet
573 store the result in the buffer
574 This version of the function will return a length of zero on receiving
576 timeout is in milliseconds.
577 ****************************************************************************/
579 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
588 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
590 ok = (read_socket_data(fd,inbuf,4) == 4);
595 len = smb_len(inbuf);
596 msg_type = CVAL(inbuf,0);
598 if (msg_type == 0x85)
599 DEBUG(5,("Got keepalive packet\n"));
602 DEBUG(10,("got smb length of %d\n",len));
607 /****************************************************************************
608 read 4 bytes of a smb packet and return the smb length of the packet
609 store the result in the buffer. This version of the function will
610 never return a session keepalive (length of zero).
611 timeout is in milliseconds.
612 ****************************************************************************/
614 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
620 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
625 /* Ignore session keepalives. */
626 if(CVAL(inbuf,0) != 0x85)
630 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
635 /****************************************************************************
636 read an smb from a fd. Note that the buffer *MUST* be of size
637 BUFFER_SIZE+SAFETY_MARGIN.
638 The timeout is in milliseconds.
639 This function will return on a
640 receipt of a session keepalive packet.
641 ****************************************************************************/
643 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
649 memset(buffer,'\0',smb_size + 100);
651 len = read_smb_length_return_keepalive(fd,buffer,timeout);
654 DEBUG(10,("receive_smb: length < 0!\n"));
658 if (len > BUFFER_SIZE) {
659 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
660 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
667 ret = read_socket_data(fd,buffer+4,len);
669 smb_read_error = READ_ERROR;
676 /****************************************************************************
677 read an smb from a fd ignoring all keepalive packets. Note that the buffer
678 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
679 The timeout is in milliseconds
681 This is exactly the same as receive_smb except that it never returns
682 a session keepalive packet (just as receive_smb used to do).
683 receive_smb was changed to return keepalives as the oplock processing means this call
684 should never go into a blocking read.
685 ****************************************************************************/
687 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
693 ret = receive_smb(fd, buffer, timeout);
697 DEBUG(10,("client_receive_smb failed\n"));
702 /* Ignore session keepalive packets. */
703 if(CVAL(buffer,0) != 0x85)
710 /****************************************************************************
711 send an null session message to a fd
712 ****************************************************************************/
714 BOOL send_null_session_msg(int fd)
720 char *buffer = (char *)␣
722 while (nwritten < len)
724 ret = write_socket(fd,buffer+nwritten,len - nwritten);
727 DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
734 DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
738 /****************************************************************************
740 ****************************************************************************/
742 BOOL send_smb(int fd,char *buffer)
747 len = smb_len(buffer) + 4;
749 while (nwritten < len)
751 ret = write_socket(fd,buffer+nwritten,len - nwritten);
754 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
764 /****************************************************************************
765 send a single packet to a port on another machine
766 ****************************************************************************/
768 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
772 struct sockaddr_in sock_out;
777 /* create a socket to write to */
778 out_fd = socket(AF_INET, type, 0);
781 DEBUG(0,("socket failed"));
785 /* set the address and port */
786 memset((char *)&sock_out,'\0',sizeof(sock_out));
787 putip((char *)&sock_out.sin_addr,(char *)&ip);
788 sock_out.sin_port = htons( port );
789 sock_out.sin_family = AF_INET;
792 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
793 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
796 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
799 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
800 inet_ntoa(ip),port,strerror(errno)));
806 /****************************************************************************
807 open a socket of the specified type, port and address for incoming data
808 ****************************************************************************/
810 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
813 struct sockaddr_in sock;
817 /* get my host name */
818 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
819 { DEBUG(0,("gethostname failed\n")); return -1; }
822 if ((hp = Get_Hostbyname(host_name)) == 0)
824 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
828 memset((char *)&sock,'\0',sizeof(sock));
829 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
831 #ifdef HAVE_SOCK_SIN_LEN
832 sock.sin_len = sizeof(sock);
834 sock.sin_port = htons( port );
835 sock.sin_family = hp->h_addrtype;
836 sock.sin_addr.s_addr = socket_addr;
837 res = socket(hp->h_addrtype, type, 0);
839 { DEBUG(0,("socket failed\n")); return -1; }
847 if(setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1)
848 DEBUG(dlevel,("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n",
849 val, port, strerror(errno) ));
851 if(setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1)
852 DEBUG(dlevel,("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n",
853 val, port, strerror(errno) ));
854 #endif /* SO_REUSEPORT */
857 /* now we've got a socket - we need to bind it */
858 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
861 if (port == SMB_PORT || port == NMB_PORT)
862 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
863 port,inet_ntoa(sock.sin_addr),strerror(errno)));
866 if (dlevel > 0 && port < 1000)
869 if (port >= 1000 && port < 9000)
870 return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
875 DEBUG(3,("bind succeeded on port %d\n",port));
880 /****************************************************************************
881 create an outgoing socket. timeout is in milliseconds.
882 **************************************************************************/
884 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
886 struct sockaddr_in sock_out;
888 int connect_loop = 250; /* 250 milliseconds */
889 int loops = (timeout) / connect_loop;
891 /* create a socket to write to */
892 res = socket(PF_INET, type, 0);
894 { DEBUG(0,("socket error\n")); return -1; }
896 if (type != SOCK_STREAM) return(res);
898 memset((char *)&sock_out,'\0',sizeof(sock_out));
899 putip((char *)&sock_out.sin_addr,(char *)addr);
901 sock_out.sin_port = htons( port );
902 sock_out.sin_family = PF_INET;
904 /* set it non-blocking */
905 set_blocking(res,False);
907 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
909 /* and connect it to the destination */
911 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
913 /* Some systems return EAGAIN when they mean EINPROGRESS */
914 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
915 errno == EAGAIN) && loops--) {
916 msleep(connect_loop);
920 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
922 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
928 if (ret < 0 && errno == EISCONN) {
935 DEBUG(1,("error connecting to %s:%d (%s)\n",
936 inet_ntoa(*addr),port,strerror(errno)));
941 /* set it blocking again */
942 set_blocking(res,True);
948 /*******************************************************************
949 Reset the 'done' variables so after a client process is created
950 from a fork call these calls will be re-done. This should be
951 expanded if more variables need reseting.
952 ******************************************************************/
954 static BOOL global_client_name_done = False;
955 static BOOL global_client_addr_done = False;
957 void reset_globals_after_fork(void)
959 global_client_name_done = False;
960 global_client_addr_done = False;
963 * Re-seed the random crypto generator, so all smbd's
964 * started from the same parent won't generate the same
969 generate_random_buffer( &dummy, 1, True);
973 /*******************************************************************
974 return the DNS name of the client
975 ******************************************************************/
977 char *client_name(int fd)
980 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
981 int length = sizeof(sa);
982 static pstring name_buf;
984 static int last_fd=-1;
986 if (global_client_name_done && last_fd == fd)
990 global_client_name_done = False;
992 pstrcpy(name_buf,"UNKNOWN");
998 if (getpeername(fd, &sa, &length) < 0) {
999 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
1003 /* Look up the remote host name. */
1004 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
1005 sizeof(sockin->sin_addr),
1007 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
1008 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
1010 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
1011 if (!matchname(name_buf, sockin->sin_addr)) {
1012 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
1013 pstrcpy(name_buf,"UNKNOWN");
1016 global_client_name_done = True;
1020 /*******************************************************************
1021 return the IP addr of the client as a string
1022 ******************************************************************/
1024 char *client_addr(int fd)
1027 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
1028 int length = sizeof(sa);
1029 static fstring addr_buf;
1030 static int last_fd = -1;
1032 if (global_client_addr_done && fd == last_fd)
1036 global_client_addr_done = False;
1038 fstrcpy(addr_buf,"0.0.0.0");
1044 if (getpeername(fd, &sa, &length) < 0) {
1045 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
1049 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
1051 global_client_addr_done = True;
1055 /*******************************************************************
1056 opens and connects to a unix pipe socket
1057 ******************************************************************/
1058 int open_pipe_sock(char *path)
1061 struct sockaddr_un sa;
1063 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1067 DEBUG(0, ("unix socket open failed\n"));
1072 sa.sun_family = AF_UNIX;
1073 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1075 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
1077 if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1079 DEBUG(0,("socket connect to %s failed\n", sa.sun_path));
1087 int create_pipe_socket(char *dir, int dir_perms,
1088 char *path, int path_perms)
1091 struct sockaddr_un sa;
1093 DEBUG(0,("create_pipe_socket: %s %d %s %d\n",
1094 dir, dir_perms, path, path_perms));
1096 DEBUG(0,("*** RACE CONDITION. PLEASE SOMEONE EXAMINE create_pipe_Socket AND FIX IT ***\n"));
1098 mkdir(dir, dir_perms);
1100 if (chmod(dir, dir_perms) < 0)
1102 DEBUG(0, ("chmod on %s failed\n", dir));
1108 DEBUG(0, ("remove on %s failed\n", path));
1111 /* start listening on unix socket */
1112 s = socket(AF_UNIX, SOCK_STREAM, 0);
1116 DEBUG(0, ("socket open failed\n"));
1121 sa.sun_family = AF_UNIX;
1122 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1124 if (bind(s, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1126 DEBUG(0, ("socket bind to %s failed\n", sa.sun_path));
1134 DEBUG(0,("bind failed\n"));
1139 if (path_perms != 0)
1141 chmod(path, path_perms);
1144 if (listen(s, 5) == -1)
1146 DEBUG(0,("listen failed\n"));
1150 DEBUG(5,("unix socket opened: %s\n", path));