Make link-dest aware of --perms, --owner and --group.
[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
24/**
25 * Create a child connected to use on stdin/stdout.
26 *
27 * This is derived from CVS code
28 *
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
32 *
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
35 * Solaris.
36 **/
37pid_t piped_child(char **command, int *f_in, int *f_out)
38{
39 pid_t pid;
40 int to_child_pipe[2];
41 int from_child_pipe[2];
42 extern int blocking_io;
43
44 if (verbose >= 2) {
45 print_child_argv(command);
46 }
47
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);
51 }
52
53
54 pid = do_fork();
55 if (pid == -1) {
56 rprintf(FERROR, "fork: %s\n", strerror(errno));
57 exit_cleanup(RERR_IPC);
58 }
59
60 if (pid == 0) {
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",
67 strerror(errno));
68 exit_cleanup(RERR_IPC);
69 }
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]);
74 umask(orig_umask);
75 set_blocking(STDIN_FILENO);
76 if (blocking_io) {
77 set_blocking(STDOUT_FILENO);
78 }
79 execvp(command[0], command);
80 rprintf(FERROR, "Failed to exec %s : %s\n",
81 command[0], strerror(errno));
82 exit_cleanup(RERR_IPC);
83 }
84
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);
88 }
89
90 *f_in = from_child_pipe[0];
91 *f_out = to_child_pipe[1];
92
93 return pid;
94}
95
96pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
b44be3e9 97 int (*child_main)(int, char*[]))
b35d0d8e
MP
98{
99 pid_t pid;
100 int to_child_pipe[2];
101 int from_child_pipe[2];
102 extern int read_batch; /* dw */
103
104 if (fd_pair(to_child_pipe) < 0 ||
105 fd_pair(from_child_pipe) < 0) {
106 rprintf(FERROR,"pipe: %s\n",strerror(errno));
107 exit_cleanup(RERR_IPC);
108 }
109
110
111 pid = do_fork();
112 if (pid == -1) {
113 rprintf(FERROR,"fork: %s\n",strerror(errno));
114 exit_cleanup(RERR_IPC);
115 }
116
117 if (pid == 0) {
118 extern int am_sender;
119 extern int am_server;
120
121 am_sender = read_batch ? 0 : !am_sender;
122 am_server = 1;
123
124 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
125 close(to_child_pipe[1]) < 0 ||
126 close(from_child_pipe[0]) < 0 ||
127 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
128 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
129 exit_cleanup(RERR_IPC);
130 }
131 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
132 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
133 child_main(argc, argv);
134 }
135
136 if (close(from_child_pipe[1]) < 0 ||
137 close(to_child_pipe[0]) < 0) {
138 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
139 exit_cleanup(RERR_IPC);
140 }
141
142 *f_in = from_child_pipe[0];
143 *f_out = to_child_pipe[1];
144
145 return pid;
146}
147
148