Make idev, hlink and file_struct + strings use allocation
[rsync/rsync.git] / pipe.c
CommitLineData
fcb69e5c
MP
1/* -*- c-file-style: "linux" -*-
2 *
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>
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 2 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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
b35d0d8e
MP
22#include "rsync.h"
23
72a6e631
WD
24extern int am_sender;
25extern int am_server;
26extern int blocking_io;
27extern int orig_umask;
28extern int read_batch;
29extern int filesfrom_fd;
30
b35d0d8e
MP
31/**
32 * Create a child connected to use on stdin/stdout.
33 *
34 * This is derived from CVS code
35 *
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
39 *
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
42 * Solaris.
43 **/
44pid_t piped_child(char **command, int *f_in, int *f_out)
45{
46 pid_t pid;
47 int to_child_pipe[2];
48 int from_child_pipe[2];
b35d0d8e
MP
49
50 if (verbose >= 2) {
51 print_child_argv(command);
52 }
53
54 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
55 rprintf(FERROR, "pipe: %s\n", strerror(errno));
56 exit_cleanup(RERR_IPC);
57 }
58
59
60 pid = do_fork();
61 if (pid == -1) {
62 rprintf(FERROR, "fork: %s\n", strerror(errno));
63 exit_cleanup(RERR_IPC);
64 }
65
66 if (pid == 0) {
b35d0d8e
MP
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 rprintf(FERROR, "Failed to dup/close : %s\n",
72 strerror(errno));
73 exit_cleanup(RERR_IPC);
74 }
75 if (to_child_pipe[0] != STDIN_FILENO)
76 close(to_child_pipe[0]);
77 if (from_child_pipe[1] != STDOUT_FILENO)
78 close(from_child_pipe[1]);
79 umask(orig_umask);
80 set_blocking(STDIN_FILENO);
7d3f8ae2 81 if (blocking_io > 0)
b35d0d8e 82 set_blocking(STDOUT_FILENO);
b35d0d8e
MP
83 execvp(command[0], command);
84 rprintf(FERROR, "Failed to exec %s : %s\n",
85 command[0], strerror(errno));
86 exit_cleanup(RERR_IPC);
87 }
88
89 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
90 rprintf(FERROR, "Failed to close : %s\n", strerror(errno));
91 exit_cleanup(RERR_IPC);
92 }
93
94 *f_in = from_child_pipe[0];
95 *f_out = to_child_pipe[1];
96
97 return pid;
98}
99
100pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
b44be3e9 101 int (*child_main)(int, char*[]))
b35d0d8e
MP
102{
103 pid_t pid;
104 int to_child_pipe[2];
105 int from_child_pipe[2];
b35d0d8e
MP
106
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);
111 }
112
113
114 pid = do_fork();
115 if (pid == -1) {
116 rprintf(FERROR,"fork: %s\n",strerror(errno));
117 exit_cleanup(RERR_IPC);
118 }
119
120 if (pid == 0) {
b35d0d8e
MP
121 am_sender = read_batch ? 0 : !am_sender;
122 am_server = 1;
123
7c2a9e76
WD
124 if (!am_sender)
125 filesfrom_fd = -1;
126
b35d0d8e
MP
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);
133 }
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);
137 }
138
7c2a9e76
WD
139 if (!am_sender)
140 filesfrom_fd = -1;
141
b35d0d8e
MP
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);
146 }
147
148 *f_in = from_child_pipe[0];
149 *f_out = to_child_pipe[1];
150
151 return pid;
152}