2007-07-30 17:25:13 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Evoke, head honcho of everything
|
|
|
|
* Part of Equinox Desktop Environment (EDE).
|
|
|
|
* Copyright (c) 2000-2007 EDE Authors.
|
|
|
|
*
|
|
|
|
* This program is licensed under terms of the
|
|
|
|
* GNU General Public License version 2 or newer.
|
|
|
|
* See COPYING for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Spawn.h"
|
|
|
|
|
|
|
|
#include <sys/types.h> // fork
|
|
|
|
#include <unistd.h> // fork, open, close, dup
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
2007-12-03 16:39:22 +03:00
|
|
|
#include <signal.h>
|
2007-07-30 17:25:13 +04:00
|
|
|
#include <errno.h>
|
2007-08-06 16:39:15 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/time.h> // getrlimit, setrlimit
|
|
|
|
#include <sys/resource.h> //
|
2007-07-30 17:25:13 +04:00
|
|
|
|
|
|
|
extern char** environ;
|
2007-08-06 16:39:15 +04:00
|
|
|
SignalWatch* global_watch = 0;
|
|
|
|
|
|
|
|
void sigchld_handler(int sig) {
|
|
|
|
int pid, status;
|
|
|
|
do {
|
|
|
|
errno = 0;
|
2007-09-24 15:47:42 +04:00
|
|
|
pid = waitpid(WAIT_ANY, &status, WNOHANG | WUNTRACED);
|
2007-08-06 16:39:15 +04:00
|
|
|
|
2007-08-07 15:54:57 +04:00
|
|
|
if(global_watch != 0) {
|
|
|
|
if(WIFEXITED(status))
|
|
|
|
status = WEXITSTATUS(status);
|
|
|
|
else if(WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)
|
|
|
|
status = SPAWN_CHILD_CRASHED;
|
|
|
|
else
|
|
|
|
status = SPAWN_CHILD_KILLED;
|
|
|
|
|
2007-08-06 16:39:15 +04:00
|
|
|
global_watch(pid, status);
|
2007-08-07 15:54:57 +04:00
|
|
|
}
|
2007-08-06 16:39:15 +04:00
|
|
|
|
|
|
|
} while(pid <= 0 && errno == EINTR);
|
|
|
|
}
|
2007-07-30 17:25:13 +04:00
|
|
|
|
2007-08-31 17:41:27 +04:00
|
|
|
int spawn_program(const char* cmd, SignalWatch wf, pid_t* child_pid_ret, const char* ofile) {
|
2007-07-30 17:25:13 +04:00
|
|
|
if(!cmd)
|
|
|
|
return SPAWN_EMPTY;
|
|
|
|
|
|
|
|
int nulldev = -1;
|
|
|
|
int status_ret = 0;
|
2007-08-06 16:39:15 +04:00
|
|
|
|
2007-09-24 15:47:42 +04:00
|
|
|
#if 0
|
2007-09-20 12:47:50 +04:00
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = sigchld_handler;
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
2007-09-24 15:47:42 +04:00
|
|
|
/*sa.sa_flags = SA_NOCLDWAIT;*/
|
2007-09-20 12:47:50 +04:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGCHLD, &sa, (struct sigaction*)0);
|
2007-09-24 15:47:42 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
sigset_t chld_mask, old_mask;
|
|
|
|
|
|
|
|
sa.sa_handler = sigchld_handler;
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGCHLD, &sa, (struct sigaction*)0);
|
|
|
|
|
|
|
|
// block SIGCHLD until we set up stuff
|
|
|
|
sigemptyset(&chld_mask);
|
|
|
|
sigaddset(&chld_mask, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &chld_mask, &old_mask);
|
2007-08-06 16:39:15 +04:00
|
|
|
|
2007-09-20 12:47:50 +04:00
|
|
|
global_watch = wf;
|
2007-08-06 16:39:15 +04:00
|
|
|
|
2007-07-30 17:25:13 +04:00
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid == -1)
|
|
|
|
return SPAWN_FORK_FAILED;
|
|
|
|
|
|
|
|
if(pid == 0) {
|
2007-09-24 15:47:42 +04:00
|
|
|
// child
|
2007-07-30 17:25:13 +04:00
|
|
|
char* argv[4];
|
|
|
|
argv[0] = "/bin/sh";
|
|
|
|
argv[1] = "-c";
|
|
|
|
argv[2] = (char*)cmd;
|
|
|
|
argv[3] = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following is to avoid X locking when executing
|
|
|
|
* terminal based application that requires user input
|
|
|
|
*/
|
2007-08-31 17:41:27 +04:00
|
|
|
if(ofile)
|
|
|
|
nulldev = open(ofile, O_WRONLY | O_TRUNC | O_CREAT, 0770);
|
|
|
|
else
|
|
|
|
nulldev = open("/dev/null", O_RDWR);
|
|
|
|
|
|
|
|
if(nulldev == -1)
|
|
|
|
return SPAWN_OPEN_FAILED;
|
2007-07-30 17:25:13 +04:00
|
|
|
|
|
|
|
/* TODO: redirect these to EvokeService log */
|
|
|
|
close(0); dup(nulldev);
|
|
|
|
close(1); dup(nulldev);
|
|
|
|
close(2); dup(nulldev);
|
|
|
|
|
2007-09-17 14:36:24 +04:00
|
|
|
errno = 0;
|
2007-07-30 17:25:13 +04:00
|
|
|
if(execve(argv[0], argv, environ) == -1) {
|
|
|
|
// should not get here
|
|
|
|
return SPAWN_EXECVE_FAILED;
|
|
|
|
}
|
2007-08-06 16:39:15 +04:00
|
|
|
}
|
|
|
|
|
2007-09-24 15:47:42 +04:00
|
|
|
// parent
|
2007-08-06 16:39:15 +04:00
|
|
|
if(nulldev != -1)
|
|
|
|
close(nulldev);
|
|
|
|
/*
|
|
|
|
* Record child pid; it is returned by fork(), but since this
|
|
|
|
* function does not wait until child quits, it will return
|
|
|
|
* immediately, filling (if) requested child pid
|
|
|
|
*/
|
|
|
|
if(child_pid_ret)
|
|
|
|
*child_pid_ret = pid;
|
2007-12-28 13:58:56 +03:00
|
|
|
|
2007-09-24 15:47:42 +04:00
|
|
|
// unblock SIGCHLD
|
|
|
|
sigprocmask(SIG_SETMASK, &old_mask, NULL);
|
|
|
|
|
2007-07-30 17:25:13 +04:00
|
|
|
return status_ret;
|
|
|
|
}
|
2007-08-06 16:39:15 +04:00
|
|
|
|
|
|
|
int spawn_program_with_core(const char* cmd, SignalWatch* wf, pid_t* child_pid_ret) {
|
|
|
|
struct rlimit r;
|
|
|
|
if(getrlimit(RLIMIT_CORE, &r) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rlim_t old = r.rlim_cur;
|
|
|
|
r.rlim_cur = RLIM_INFINITY;
|
|
|
|
|
|
|
|
// FIXME: add own core limit ?
|
|
|
|
if(setrlimit(RLIMIT_CORE, &r) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int ret = spawn_program(cmd, wf, child_pid_ret);
|
|
|
|
|
|
|
|
r.rlim_cur = old;
|
|
|
|
if(setrlimit(RLIMIT_CORE, &r) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-03 16:24:01 +04:00
|
|
|
int spawn_backtrace(const char* gdb_path, const char* program, const char* core, const char* output, const char* script) {
|
2007-08-06 16:39:15 +04:00
|
|
|
const char* gdb_script = "bt\nquit\n";
|
|
|
|
const int gdb_script_len = 8;
|
|
|
|
|
2007-09-24 15:47:42 +04:00
|
|
|
//signal(SIGCHLD, SIG_DFL);
|
|
|
|
|
2007-08-06 16:39:15 +04:00
|
|
|
// file with gdb commands
|
|
|
|
int sfd = open(script, O_WRONLY | O_TRUNC | O_CREAT, 0770);
|
|
|
|
if(sfd == -1)
|
|
|
|
return -1;
|
|
|
|
write(sfd, gdb_script, gdb_script_len);
|
|
|
|
close(sfd);
|
|
|
|
|
|
|
|
// output file with gdb backtrace
|
|
|
|
int ofd = open(output, O_WRONLY | O_TRUNC | O_CREAT, 0770);
|
|
|
|
if(ofd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
|
|
|
if(pid == -1) {
|
|
|
|
close(ofd);
|
|
|
|
return -1;
|
|
|
|
} else if(pid == 0) {
|
|
|
|
dup2(ofd, 1);
|
|
|
|
close(ofd);
|
|
|
|
|
|
|
|
char* argv[8];
|
2007-09-03 16:24:01 +04:00
|
|
|
argv[0] = (char*)gdb_path;
|
2007-08-06 16:39:15 +04:00
|
|
|
argv[1] = "--quiet";
|
|
|
|
argv[2] = "--batch";
|
|
|
|
argv[3] = "-x";
|
|
|
|
argv[4] = (char*)script;
|
|
|
|
argv[5] = (char*)program;
|
2007-08-07 15:54:57 +04:00
|
|
|
argv[6] = (char*)core;
|
2007-08-06 16:39:15 +04:00
|
|
|
argv[7] = 0;
|
|
|
|
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
if(waitpid(pid, &status, 0) != pid)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|