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

pipe.c File Reference

Go to the source code of this file.

Functions

pid_t piped_child (char **command, int *f_in, int *f_out)
 Create a child connected to use on stdin/stdout. More...

pid_t local_child (int argc, char **argv, int *f_in, int *f_out, int(*child_main)(int, char *[]))


Function Documentation

pid_t piped_child char **    command,
int *    f_in,
int *    f_out
 

Create a child connected to use on stdin/stdout.

This is derived from CVS code

Note that in the child STDIN is set to blocking and STDOUT is set to non-blocking. This is necessary as rsh relies on stdin being blocking and ssh relies on stdout being non-blocking

If blocking_io is set then use blocking io on both fds. That can be used to cope with badly broken rsh implementations like the one on Solaris.

Definition at line 37 of file pipe.c.

References blocking_io, do_fork(), fd_pair(), FERROR, orig_umask, print_child_argv(), rprintf(), and set_blocking().

Referenced by do_cmd().

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 }

pid_t local_child int    argc,
char **    argv,
int *    f_in,
int *    f_out,
int(*    child_main)(int, char *[])
 

Definition at line 96 of file pipe.c.

References am_sender, child_main(), do_fork(), fd_pair(), FERROR, and rprintf().

Referenced by do_cmd().

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 }


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