Fix the val reading for MSG_ERROR_EXIT. Use 0-length MSG_DATA when
[rsync/rsync.git] / pipe.c
... / ...
CommitLineData
1/*
2 * Routines used to setup various kinds of inter-process pipes.
3 *
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2004-2009 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
21 */
22
23#include "rsync.h"
24
25extern int am_sender;
26extern int am_server;
27extern int blocking_io;
28extern int filesfrom_fd;
29extern int munge_symlinks;
30extern mode_t orig_umask;
31extern char *logfile_name;
32extern int remote_option_cnt;
33extern const char **remote_options;
34extern struct chmod_mode_struct *chmod_modes;
35
36/**
37 * Create a child connected to us via its stdin/stdout.
38 *
39 * This is derived from CVS code
40 *
41 * Note that in the child STDIN is set to blocking and STDOUT
42 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
43 * and ssh relies on stdout being non-blocking
44 *
45 * If blocking_io is set then use blocking io on both fds. That can be
46 * used to cope with badly broken rsh implementations like the one on
47 * Solaris.
48 **/
49pid_t piped_child(char **command, int *f_in, int *f_out)
50{
51 pid_t pid;
52 int to_child_pipe[2];
53 int from_child_pipe[2];
54
55 if (DEBUG_GTE(CMD, 1))
56 print_child_argv("opening connection using:", command);
57
58 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
59 rsyserr(FERROR, errno, "pipe");
60 exit_cleanup(RERR_IPC);
61 }
62
63 pid = do_fork();
64 if (pid == -1) {
65 rsyserr(FERROR, errno, "fork");
66 exit_cleanup(RERR_IPC);
67 }
68
69 if (pid == 0) {
70 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
71 close(to_child_pipe[1]) < 0 ||
72 close(from_child_pipe[0]) < 0 ||
73 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
74 rsyserr(FERROR, errno, "Failed to dup/close");
75 exit_cleanup(RERR_IPC);
76 }
77 if (to_child_pipe[0] != STDIN_FILENO)
78 close(to_child_pipe[0]);
79 if (from_child_pipe[1] != STDOUT_FILENO)
80 close(from_child_pipe[1]);
81 umask(orig_umask);
82 set_blocking(STDIN_FILENO);
83 if (blocking_io > 0)
84 set_blocking(STDOUT_FILENO);
85 execvp(command[0], command);
86 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
87 exit_cleanup(RERR_IPC);
88 }
89
90 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
91 rsyserr(FERROR, errno, "Failed to close");
92 exit_cleanup(RERR_IPC);
93 }
94
95 *f_in = from_child_pipe[0];
96 *f_out = to_child_pipe[1];
97
98 return pid;
99}
100
101/* This function forks a child which calls child_main(). First,
102 * however, it has to establish communication paths to and from the
103 * newborn child. It creates two socket pairs -- one for writing to
104 * the child (from the parent) and one for reading from the child
105 * (writing to the parent). Since that's four socket ends, each
106 * process has to close the two ends it doesn't need. The remaining
107 * two socket ends are retained for reading and writing. In the
108 * child, the STDIN and STDOUT file descriptors refer to these
109 * sockets. In the parent, the function arguments f_in and f_out are
110 * set to refer to these sockets. */
111pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
112 int (*child_main)(int, char*[]))
113{
114 pid_t pid;
115 int to_child_pipe[2];
116 int from_child_pipe[2];
117
118 /* The parent process is always the sender for a local rsync. */
119 assert(am_sender);
120
121 if (fd_pair(to_child_pipe) < 0 ||
122 fd_pair(from_child_pipe) < 0) {
123 rsyserr(FERROR, errno, "pipe");
124 exit_cleanup(RERR_IPC);
125 }
126
127 pid = do_fork();
128 if (pid == -1) {
129 rsyserr(FERROR, errno, "fork");
130 exit_cleanup(RERR_IPC);
131 }
132
133 if (pid == 0) {
134 am_sender = 0;
135 am_server = 1;
136 filesfrom_fd = -1;
137 munge_symlinks = 0; /* Each side needs its own option. */
138 chmod_modes = NULL; /* Let the sending side handle this. */
139
140 /* Let the client side handle this. */
141 if (logfile_name) {
142 logfile_name = NULL;
143 logfile_close();
144 }
145
146 if (remote_option_cnt) {
147 int rc = remote_option_cnt + 1;
148 const char **rv = remote_options;
149 if (!parse_arguments(&rc, &rv)) {
150 option_error();
151 exit_cleanup(RERR_SYNTAX);
152 }
153 }
154
155 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
156 close(to_child_pipe[1]) < 0 ||
157 close(from_child_pipe[0]) < 0 ||
158 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
159 rsyserr(FERROR, errno, "Failed to dup/close");
160 exit_cleanup(RERR_IPC);
161 }
162 if (to_child_pipe[0] != STDIN_FILENO)
163 close(to_child_pipe[0]);
164 if (from_child_pipe[1] != STDOUT_FILENO)
165 close(from_child_pipe[1]);
166#ifdef ICONV_CONST
167 setup_iconv();
168#endif
169 child_main(argc, argv);
170 }
171
172 if (close(from_child_pipe[1]) < 0 ||
173 close(to_child_pipe[0]) < 0) {
174 rsyserr(FERROR, errno, "Failed to close");
175 exit_cleanup(RERR_IPC);
176 }
177
178 *f_in = from_child_pipe[0];
179 *f_out = to_child_pipe[1];
180
181 return pid;
182}