/** * Run a program on a local tcp socket, so that we can talk to it's * stdin and stdout. This is used to fake a connection to a daemon * for testing -- not for the normal case of running SSH. * * @return a socket which is attached to a subprocess running * "prog". stdin and stdout are attached. stderr is left attached to * the original stderr **/ int sock_exec(const char *prog) { int fd[2]; if (socketpair_tcp(fd) != 0) { rprintf (FERROR, RSYNC_NAME ": socketpair_tcp failed (%s)\n", strerror(errno)); return -1; } if (fork() == 0) { close(fd[0]); close(0); close(1); dup(fd[1]); dup(fd[1]); if (verbose > 3) { /* Can't use rprintf because we've forked. */ fprintf (stderr, RSYNC_NAME ": execute socket program \"%s\"\n", prog); } exit (system (prog)); } close (fd[1]); return fd[0]; }
main() { int fd[2], ret; char buf[1024*1024]; int maxfd; fd_set set; alarm(5); if (socketpair_tcp(fd) != 0) exit(1); FD_ZERO(&set); FD_SET(fd[0], &set); maxfd = fd[0] > fd[1] ? fd[0] : fd[1]; select(maxfd+1, NULL, &set, NULL, NULL); ret = write(fd[0], buf, sizeof(buf)); if (ret > 0 && ret < sizeof(buf)) exit(0); exit(1); }
/******************************************************************* run a program on a local tcp socket, this is used to launch smbd when regression testing the return value is a socket which is attached to a subprocess running "prog". stdin and stdout are attached. stderr is left attached to the original stderr ******************************************************************/ int sock_exec(const char *prog) { int fd[2]; if (socketpair_tcp(fd) != 0) { DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno))); return -1; } if (fork() == 0) { close(fd[0]); close(0); close(1); if (dup(fd[1]) == -1) { exit(1); } if (dup(fd[1]) == -1) { exit(1); } exit(system(prog)); } close(fd[1]); return fd[0]; }