docs: use the stdarg.option entity in the popt.common.samba entity
[Samba/gebeck_regimport.git] / source3 / lib / sock_exec.c
blob3d2ace9e3604e92cc51bdb8a07f8250e11effeaa
1 /*
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/>.
21 #include "includes.h"
23 /*******************************************************************
24 this is like socketpair but uses tcp. It is used by the Samba
25 regression test code
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])
33 int listener;
34 struct sockaddr_in sock;
35 struct sockaddr_in sock2;
36 socklen_t socklen = sizeof(sock);
37 int connect_done = 0;
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);
48 #endif
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 (connect(fd[1], (struct sockaddr *)&sock, socklen) == -1) {
64 if (errno != EINPROGRESS) goto failed;
65 } else {
66 connect_done = 1;
69 if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
71 if (connect_done == 0) {
72 if (connect(fd[1], (struct sockaddr *)&sock, socklen) != 0
73 && errno != EISCONN) goto failed;
75 close(listener);
77 set_blocking(fd[1], 1);
79 /* all OK! */
80 return 0;
82 failed:
83 if (fd[0] != -1) close(fd[0]);
84 if (fd[1] != -1) close(fd[1]);
85 if (listener != -1) close(listener);
86 return -1;
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)
99 int fd[2];
100 if (socketpair_tcp(fd) != 0) {
101 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));
102 return -1;
104 if (fork() == 0) {
105 close(fd[0]);
106 close(0);
107 close(1);
108 if (dup(fd[1]) == -1) {
109 exit(1);
111 if (dup(fd[1]) == -1) {
112 exit(1);
114 exit(system(prog));
116 close(fd[1]);
117 return fd[0];