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 3 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, see <http://www.gnu.org/licenses/>.
23
/*******************************************************************
24
this is like socketpair but uses tcp. It is used by the Samba
26
The function guarantees that nobody else can attach to the socket,
27
or if they do that this function fails and the socket gets closed
28
returns 0 on success, -1 on failure
29
the resulting file descriptors are symmetrical
30
******************************************************************/
31
static int socketpair_tcp(int fd[2])
34
struct sockaddr_in sock;
35
struct sockaddr_in sock2;
36
socklen_t socklen = sizeof(sock);
39
fd[0] = fd[1] = listener = -1;
41
memset(&sock, 0, sizeof(sock));
43
if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
45
memset(&sock2, 0, sizeof(sock2));
46
#ifdef HAVE_SOCK_SIN_LEN
47
sock2.sin_len = sizeof(sock2);
49
sock2.sin_family = PF_INET;
51
if (bind(listener, (struct sockaddr *)&sock2, sizeof(sock2)) != 0) goto failed;
53
if (listen(listener, 1) != 0) goto failed;
55
if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
57
if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
59
set_blocking(fd[1], 0);
61
sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
63
if (sys_connect(fd[1], (struct sockaddr *)&sock) == -1) {
64
if (errno != EINPROGRESS) goto failed;
69
if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
72
if (connect_done == 0) {
73
if (sys_connect(fd[1], (struct sockaddr *)&sock) != 0
74
&& errno != EISCONN) goto failed;
77
set_blocking(fd[1], 1);
83
if (fd[0] != -1) close(fd[0]);
84
if (fd[1] != -1) close(fd[1]);
85
if (listener != -1) close(listener);
90
/*******************************************************************
91
run a program on a local tcp socket, this is used to launch smbd
92
when regression testing
93
the return value is a socket which is attached to a subprocess
94
running "prog". stdin and stdout are attached. stderr is left
95
attached to the original stderr
96
******************************************************************/
97
int sock_exec(const char *prog)
100
if (socketpair_tcp(fd) != 0) {
101
DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));
108
if (dup(fd[1]) == -1) {
111
if (dup(fd[1]) == -1) {