2013-03-20 05:27:32 +00:00
|
|
|
/*
|
|
|
|
* The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-10-31 02:53:15 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2013-03-20 05:27:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2014-02-07 04:27:29 +00:00
|
|
|
#include "libqrexec-utils.h"
|
2013-03-20 05:27:32 +00:00
|
|
|
|
2014-02-07 04:27:29 +00:00
|
|
|
static do_exec_t *exec_func = NULL;
|
|
|
|
void register_exec_func(do_exec_t *func) {
|
|
|
|
exec_func = func;
|
|
|
|
}
|
2013-03-20 05:27:32 +00:00
|
|
|
|
|
|
|
void fix_fds(int fdin, int fdout, int fderr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
if (i != fdin && i != fdout && i != fderr)
|
|
|
|
close(i);
|
|
|
|
dup2(fdin, 0);
|
|
|
|
dup2(fdout, 1);
|
|
|
|
dup2(fderr, 2);
|
|
|
|
close(fdin);
|
|
|
|
close(fdout);
|
|
|
|
if (fderr != 2)
|
|
|
|
close(fderr);
|
|
|
|
}
|
|
|
|
|
2013-12-28 11:48:00 +00:00
|
|
|
void do_fork_exec(const char *cmdline, int *pid, int *stdin_fd, int *stdout_fd,
|
2013-03-20 05:27:32 +00:00
|
|
|
int *stderr_fd)
|
|
|
|
{
|
|
|
|
int inpipe[2], outpipe[2], errpipe[2];
|
|
|
|
|
2014-10-31 02:53:15 +00:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, inpipe) ||
|
|
|
|
socketpair(AF_UNIX, SOCK_STREAM, 0, outpipe) ||
|
|
|
|
(stderr_fd && socketpair(AF_UNIX, SOCK_STREAM, 0, errpipe))) {
|
|
|
|
perror("socketpair");
|
2013-03-20 05:27:32 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
switch (*pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(-1);
|
|
|
|
case 0:
|
|
|
|
if (stderr_fd) {
|
|
|
|
fix_fds(inpipe[0], outpipe[1], errpipe[1]);
|
|
|
|
} else
|
|
|
|
fix_fds(inpipe[0], outpipe[1], 2);
|
|
|
|
|
2014-02-07 04:27:29 +00:00
|
|
|
if (exec_func != NULL)
|
|
|
|
exec_func(cmdline);
|
2013-03-20 05:27:32 +00:00
|
|
|
exit(-1);
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
close(inpipe[0]);
|
|
|
|
close(outpipe[1]);
|
|
|
|
*stdin_fd = inpipe[1];
|
|
|
|
*stdout_fd = outpipe[0];
|
|
|
|
if (stderr_fd) {
|
|
|
|
close(errpipe[1]);
|
|
|
|
*stderr_fd = errpipe[0];
|
|
|
|
}
|
|
|
|
}
|