r10455: merging privilege fix from SAMBA_3_0
[Samba.git] / source / lib / sock_exec.c
blob52c5a8ce52cd4c6e2aada766d7d9719e6e830f89
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 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.
22 #include "includes.h"
24 /*******************************************************************
25 this is like socketpair but uses tcp. It is used by the Samba
26 regression test code
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])
34 int listener;
35 struct sockaddr_in sock;
36 struct sockaddr_in sock2;
37 socklen_t socklen = sizeof(sock);
38 int connect_done = 0;
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);
49 #endif
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;
66 } else {
67 connect_done = 1;
70 if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
72 close(listener);
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);
80 /* all OK! */
81 return 0;
83 failed:
84 if (fd[0] != -1) close(fd[0]);
85 if (fd[1] != -1) close(fd[1]);
86 if (listener != -1) close(listener);
87 return -1;
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)
100 int fd[2];
101 if (socketpair_tcp(fd) != 0) {
102 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));
103 return -1;
105 if (fork() == 0) {
106 close(fd[0]);
107 close(0);
108 close(1);
109 dup(fd[1]);
110 dup(fd[1]);
111 exit(system(prog));
113 close(fd[1]);
114 return fd[0];