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.
26 extern int blocking_io;
27 extern int orig_umask;
28 extern int read_batch;
29 extern int filesfrom_fd;
32 * Create a child connected to use on stdin/stdout.
34 * This is derived from CVS code
36 * Note that in the child STDIN is set to blocking and STDOUT
37 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
38 * and ssh relies on stdout being non-blocking
40 * If blocking_io is set then use blocking io on both fds. That can be
41 * used to cope with badly broken rsh implementations like the one on
44 pid_t piped_child(char **command, int *f_in, int *f_out)
48 int from_child_pipe[2];
51 print_child_argv(command);
54 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
55 rsyserr(FERROR, errno, "pipe");
56 exit_cleanup(RERR_IPC);
62 rsyserr(FERROR, errno, "fork");
63 exit_cleanup(RERR_IPC);
67 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
68 close(to_child_pipe[1]) < 0 ||
69 close(from_child_pipe[0]) < 0 ||
70 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
71 rsyserr(FERROR, errno, "Failed to dup/close");
72 exit_cleanup(RERR_IPC);
74 if (to_child_pipe[0] != STDIN_FILENO)
75 close(to_child_pipe[0]);
76 if (from_child_pipe[1] != STDOUT_FILENO)
77 close(from_child_pipe[1]);
79 set_blocking(STDIN_FILENO);
81 set_blocking(STDOUT_FILENO);
82 execvp(command[0], command);
83 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
84 exit_cleanup(RERR_IPC);
87 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
88 rsyserr(FERROR, errno, "Failed to close");
89 exit_cleanup(RERR_IPC);
92 *f_in = from_child_pipe[0];
93 *f_out = to_child_pipe[1];
98 pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
99 int (*child_main)(int, char*[]))
102 int to_child_pipe[2];
103 int from_child_pipe[2];
105 if (fd_pair(to_child_pipe) < 0 ||
106 fd_pair(from_child_pipe) < 0) {
107 rsyserr(FERROR, errno, "pipe");
108 exit_cleanup(RERR_IPC);
114 rsyserr(FERROR, errno, "fork");
115 exit_cleanup(RERR_IPC);
119 am_sender = read_batch ? 0 : !am_sender;
125 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
126 close(to_child_pipe[1]) < 0 ||
127 close(from_child_pipe[0]) < 0 ||
128 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
129 rsyserr(FERROR, errno, "Failed to dup/close");
130 exit_cleanup(RERR_IPC);
132 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
133 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
134 child_main(argc, argv);
140 if (close(from_child_pipe[1]) < 0 ||
141 close(to_child_pipe[0]) < 0) {
142 rsyserr(FERROR, errno, "Failed to close");
143 exit_cleanup(RERR_IPC);
146 *f_in = from_child_pipe[0];
147 *f_out = to_child_pipe[1];