1 /* -*- c-file-style: "linux" -*-
3 * Copyright (C) 1996-2000 by Andrew Tridgell
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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.
25 * Create a child connected to use on stdin/stdout.
27 * This is derived from CVS code
29 * Note that in the child STDIN is set to blocking and STDOUT
30 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
31 * and ssh relies on stdout being non-blocking
33 * If blocking_io is set then use blocking io on both fds. That can be
34 * used to cope with badly broken rsh implementations like the one on
37 pid_t
piped_child(char **command
, int *f_in
, int *f_out
)
41 int from_child_pipe
[2];
42 extern int blocking_io
;
45 print_child_argv(command
);
48 if (fd_pair(to_child_pipe
) < 0 || fd_pair(from_child_pipe
) < 0) {
49 rprintf(FERROR
, "pipe: %s\n", strerror(errno
));
50 exit_cleanup(RERR_IPC
);
56 rprintf(FERROR
, "fork: %s\n", strerror(errno
));
57 exit_cleanup(RERR_IPC
);
61 extern int orig_umask
;
62 if (dup2(to_child_pipe
[0], STDIN_FILENO
) < 0 ||
63 close(to_child_pipe
[1]) < 0 ||
64 close(from_child_pipe
[0]) < 0 ||
65 dup2(from_child_pipe
[1], STDOUT_FILENO
) < 0) {
66 rprintf(FERROR
, "Failed to dup/close : %s\n",
68 exit_cleanup(RERR_IPC
);
70 if (to_child_pipe
[0] != STDIN_FILENO
)
71 close(to_child_pipe
[0]);
72 if (from_child_pipe
[1] != STDOUT_FILENO
)
73 close(from_child_pipe
[1]);
75 set_blocking(STDIN_FILENO
);
77 set_blocking(STDOUT_FILENO
);
79 execvp(command
[0], command
);
80 rprintf(FERROR
, "Failed to exec %s : %s\n",
81 command
[0], strerror(errno
));
82 exit_cleanup(RERR_IPC
);
85 if (close(from_child_pipe
[1]) < 0 || close(to_child_pipe
[0]) < 0) {
86 rprintf(FERROR
, "Failed to close : %s\n", strerror(errno
));
87 exit_cleanup(RERR_IPC
);
90 *f_in
= from_child_pipe
[0];
91 *f_out
= to_child_pipe
[1];
96 pid_t
local_child(int argc
, char **argv
,int *f_in
,int *f_out
,
97 int (*child_main
)(int, char*[]))
100 int to_child_pipe
[2];
101 int from_child_pipe
[2];
102 extern int read_batch
;
103 extern int am_sender
;
104 extern int am_server
;
105 extern int filesfrom_fd
;
107 if (fd_pair(to_child_pipe
) < 0 ||
108 fd_pair(from_child_pipe
) < 0) {
109 rprintf(FERROR
,"pipe: %s\n",strerror(errno
));
110 exit_cleanup(RERR_IPC
);
116 rprintf(FERROR
,"fork: %s\n",strerror(errno
));
117 exit_cleanup(RERR_IPC
);
121 am_sender
= read_batch
? 0 : !am_sender
;
127 if (dup2(to_child_pipe
[0], STDIN_FILENO
) < 0 ||
128 close(to_child_pipe
[1]) < 0 ||
129 close(from_child_pipe
[0]) < 0 ||
130 dup2(from_child_pipe
[1], STDOUT_FILENO
) < 0) {
131 rprintf(FERROR
,"Failed to dup/close : %s\n",strerror(errno
));
132 exit_cleanup(RERR_IPC
);
134 if (to_child_pipe
[0] != STDIN_FILENO
) close(to_child_pipe
[0]);
135 if (from_child_pipe
[1] != STDOUT_FILENO
) close(from_child_pipe
[1]);
136 child_main(argc
, argv
);
142 if (close(from_child_pipe
[1]) < 0 ||
143 close(to_child_pipe
[0]) < 0) {
144 rprintf(FERROR
,"Failed to close : %s\n",strerror(errno
));
145 exit_cleanup(RERR_IPC
);
148 *f_in
= from_child_pipe
[0];
149 *f_out
= to_child_pipe
[1];