~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/lib/sock_exec.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/*******************************************************************
 
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])
 
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);
 
62
 
 
63
        if (sys_connect(fd[1], (struct sockaddr *)&sock) == -1) {
 
64
                if (errno != EINPROGRESS) goto failed;
 
65
        } else {
 
66
                connect_done = 1;
 
67
        }
 
68
 
 
69
        if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
 
70
 
 
71
        close(listener);
 
72
        if (connect_done == 0) {
 
73
                if (sys_connect(fd[1], (struct sockaddr *)&sock) != 0
 
74
                    && errno != EISCONN) goto failed;
 
75
        }
 
76
 
 
77
        set_blocking(fd[1], 1);
 
78
 
 
79
        /* all OK! */
 
80
        return 0;
 
81
 
 
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;
 
87
}
 
88
 
 
89
 
 
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)
 
98
{
 
99
        int fd[2];
 
100
        if (socketpair_tcp(fd) != 0) {
 
101
                DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));
 
102
                return -1;
 
103
        }
 
104
        if (fork() == 0) {
 
105
                close(fd[0]);
 
106
                close(0);
 
107
                close(1);
 
108
                if (dup(fd[1]) == -1) {
 
109
                        exit(1);
 
110
                }
 
111
                if (dup(fd[1]) == -1) {
 
112
                        exit(1);
 
113
                }
 
114
                exit(system(prog));
 
115
        }
 
116
        close(fd[1]);
 
117
        return fd[0];
 
118
}