/* wrappi * Copyright (C) 2011-2012 Red Hat Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include "wrappi.h" #include "internal.h" void wrap_int_connect_ssh (wrap_h *w) { int rfd[2]; int wfd[2]; if (pipe (rfd) == -1) { set_error_errno ("pipe"); return; } if (pipe (wfd) == -1) { set_error_errno ("pipe"); close (rfd[0]); close (rfd[1]); return; } w->pid = fork (); if (w->pid == -1) { set_error_errno ("fork"); close (rfd[0]); close (rfd[1]); close (wfd[0]); close (wfd[1]); return; } if (w->pid == 0) { /* Child process. */ close (wfd[1]); close (rfd[0]); close (0); close (1); dup2 (rfd[1], 1); dup2 (wfd[0], 0); execlp ("ssh", "ssh", "-T", "-o", "BatchMode=yes", "-e", "none", w->hostname, "sh", "-c", w->wrappid_path ? : "wrappid", NULL); perror ("ssh"); _exit (EXIT_FAILURE); } /* Parent process. */ close (wfd[0]); close (rfd[1]); if (w->rfp) { fclose (w->rfp); w->rfp = NULL; } if (w->wfp) { fclose (w->wfp); w->wfp = NULL; } w->rfp = fdopen (rfd[0], "r"); if (w->rfp == NULL) { set_error_errno ("fdopen: rfp"); close (rfd[0]); close (wfd[1]); waitpid (w->pid, NULL, 0); w->pid = 0; return; } w->wfp = fdopen (wfd[1], "w"); if (w->wfp == NULL) { set_error_errno ("fdopen: wfp"); fclose (w->rfp); w->rfp = NULL; close (wfd[1]); waitpid (w->pid, NULL, 0); w->pid = 0; return; } }