Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

pipe.c

Go to the documentation of this file.
00001 /*  -*- c-file-style: "linux" -*-
00002  * 
00003  * Copyright (C) 1996-2000 by Andrew Tridgell 
00004  * Copyright (C) Paul Mackerras 1996
00005  * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
00006  * 
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include "rsync.h"
00023 
00024 /**
00025  * Create a child connected to use on stdin/stdout.
00026  *
00027  * This is derived from CVS code 
00028  * 
00029  * Note that in the child STDIN is set to blocking and STDOUT
00030  * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
00031  *  and ssh relies on stdout being non-blocking
00032  *
00033  * If blocking_io is set then use blocking io on both fds. That can be
00034  * used to cope with badly broken rsh implementations like the one on
00035  * Solaris.
00036  **/
00037 pid_t piped_child(char **command, int *f_in, int *f_out)
00038 {
00039         pid_t pid;
00040         int to_child_pipe[2];
00041         int from_child_pipe[2];
00042         extern int blocking_io;
00043         
00044         if (verbose >= 2) {
00045                 print_child_argv(command);
00046         }
00047 
00048         if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
00049                 rprintf(FERROR, "pipe: %s\n", strerror(errno));
00050                 exit_cleanup(RERR_IPC);
00051         }
00052 
00053 
00054         pid = do_fork();
00055         if (pid == -1) {
00056                 rprintf(FERROR, "fork: %s\n", strerror(errno));
00057                 exit_cleanup(RERR_IPC);
00058         }
00059 
00060         if (pid == 0) {
00061                 extern int orig_umask;
00062                 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
00063                     close(to_child_pipe[1]) < 0 ||
00064                     close(from_child_pipe[0]) < 0 ||
00065                     dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
00066                         rprintf(FERROR, "Failed to dup/close : %s\n",
00067                                 strerror(errno));
00068                         exit_cleanup(RERR_IPC);
00069                 }
00070                 if (to_child_pipe[0] != STDIN_FILENO)
00071                         close(to_child_pipe[0]);
00072                 if (from_child_pipe[1] != STDOUT_FILENO)
00073                         close(from_child_pipe[1]);
00074                 umask(orig_umask);
00075                 set_blocking(STDIN_FILENO);
00076                 if (blocking_io) {
00077                         set_blocking(STDOUT_FILENO);
00078                 }
00079                 execvp(command[0], command);
00080                 rprintf(FERROR, "Failed to exec %s : %s\n",
00081                         command[0], strerror(errno));
00082                 exit_cleanup(RERR_IPC);
00083         }
00084 
00085         if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
00086                 rprintf(FERROR, "Failed to close : %s\n", strerror(errno));
00087                 exit_cleanup(RERR_IPC);
00088         }
00089 
00090         *f_in = from_child_pipe[0];
00091         *f_out = to_child_pipe[1];
00092 
00093         return pid;
00094 }
00095 
00096 pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
00097                   int (*child_main)(int, char*[]))
00098 {
00099         pid_t pid;
00100         int to_child_pipe[2];
00101         int from_child_pipe[2];
00102         extern int read_batch;  /* dw */
00103 
00104         if (fd_pair(to_child_pipe) < 0 ||
00105             fd_pair(from_child_pipe) < 0) {
00106                 rprintf(FERROR,"pipe: %s\n",strerror(errno));
00107                 exit_cleanup(RERR_IPC);
00108         }
00109 
00110 
00111         pid = do_fork();
00112         if (pid == -1) {
00113                 rprintf(FERROR,"fork: %s\n",strerror(errno));
00114                 exit_cleanup(RERR_IPC);
00115         }
00116 
00117         if (pid == 0) {
00118                 extern int am_sender;
00119                 extern int am_server;
00120 
00121                 am_sender = read_batch ? 0 : !am_sender;
00122                 am_server = 1;          
00123 
00124                 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
00125                     close(to_child_pipe[1]) < 0 ||
00126                     close(from_child_pipe[0]) < 0 ||
00127                     dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
00128                         rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
00129                         exit_cleanup(RERR_IPC);
00130                 }
00131                 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
00132                 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
00133                 child_main(argc, argv);
00134         }
00135 
00136         if (close(from_child_pipe[1]) < 0 ||
00137             close(to_child_pipe[0]) < 0) {
00138                 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));   
00139                 exit_cleanup(RERR_IPC);
00140         }
00141 
00142         *f_in = from_child_pipe[0];
00143         *f_out = to_child_pipe[1];
00144   
00145         return pid;
00146 }
00147 
00148 

Generated on Tue Apr 16 12:37:37 2002 for rsync by doxygen1.2.15