2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Tim Potter 2000-2001
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.
24 /*******************************************************************
25 this is like socketpair but uses tcp. It is used by the Samba
27 The function guarantees that nobody else can attach to the socket,
28 or if they do that this function fails and the socket gets closed
29 returns 0 on success, -1 on failure
30 the resulting file descriptors are symmetrical
31 ******************************************************************/
32 static int socketpair_tcp(int fd
[2])
35 struct sockaddr_in sock
;
36 struct sockaddr_in sock2
;
37 socklen_t socklen
= sizeof(sock
);
40 fd
[0] = fd
[1] = listener
= -1;
42 memset(&sock
, 0, sizeof(sock
));
44 if ((listener
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1) goto failed
;
46 memset(&sock2
, 0, sizeof(sock2
));
47 #ifdef HAVE_SOCK_SIN_LEN
48 sock2
.sin_len
= sizeof(sock2
);
50 sock2
.sin_family
= PF_INET
;
52 bind(listener
, (struct sockaddr
*)&sock2
, sizeof(sock2
));
54 if (listen(listener
, 1) != 0) goto failed
;
56 if (getsockname(listener
, (struct sockaddr
*)&sock
, &socklen
) != 0) goto failed
;
58 if ((fd
[1] = socket(PF_INET
, SOCK_STREAM
, 0)) == -1) goto failed
;
60 set_blocking(fd
[1], 0);
62 sock
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
64 if (connect(fd
[1],(struct sockaddr
*)&sock
,sizeof(sock
)) == -1) {
65 if (errno
!= EINPROGRESS
) goto failed
;
70 if ((fd
[0] = accept(listener
, (struct sockaddr
*)&sock
, &socklen
)) == -1) goto failed
;
73 if (connect_done
== 0) {
74 if (connect(fd
[1],(struct sockaddr
*)&sock
,sizeof(sock
)) != 0
75 && errno
!= EISCONN
) goto failed
;
78 set_blocking(fd
[1], 1);
84 if (fd
[0] != -1) close(fd
[0]);
85 if (fd
[1] != -1) close(fd
[1]);
86 if (listener
!= -1) close(listener
);
91 /*******************************************************************
92 run a program on a local tcp socket, this is used to launch smbd
93 when regression testing
94 the return value is a socket which is attached to a subprocess
95 running "prog". stdin and stdout are attached. stderr is left
96 attached to the original stderr
97 ******************************************************************/
98 int sock_exec(const char *prog
)
101 if (socketpair_tcp(fd
) != 0) {
102 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno
)));