source: trunk/server/source3/lib/sock_exec.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 16 years ago

Samba 3.5.0: Initial import

File size: 3.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Tim Potter 2000-2001
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22
23/*******************************************************************
24this is like socketpair but uses tcp. It is used by the Samba
25regression test code
26The function guarantees that nobody else can attach to the socket,
27or if they do that this function fails and the socket gets closed
28returns 0 on success, -1 on failure
29the resulting file descriptors are symmetrical
30 ******************************************************************/
31static int socketpair_tcp(int fd[2])
32{
33 int listener;
34 struct sockaddr_in sock;
35 struct sockaddr_in sock2;
36 socklen_t socklen = sizeof(sock);
37 int connect_done = 0;
38
39 fd[0] = fd[1] = listener = -1;
40
41 memset(&sock, 0, sizeof(sock));
42
43 if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
44
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;
50
51 if (bind(listener, (struct sockaddr *)&sock2, sizeof(sock2)) != 0) goto failed;
52
53 if (listen(listener, 1) != 0) goto failed;
54
55 if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
56
57 if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
58
59 set_blocking(fd[1], 0);
60
61 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);