int safe_system(const char *s)
{
/* XXX This function needs some optimization work XXX */
pid_t pid;
int x;
int res;
struct rusage rusage;
int status;
void (*prev_handler) = signal(SIGCHLD, null_sig_handler);
pid = fork();
if (pid == 0) {
/* Close file descriptors and launch system command */
for (x=STDERR_FILENO + 1; x<4096;x++) {
close(x);
}
res = execl("/bin/sh", "/bin/sh", "-c", s, NULL);
exit(1);
} else if (pid > 0) {
gpid = pid;
alarm(5);
signal(SIGALRM, alarm_handle);
res = wait4(pid, &status, 0, &rusage);
if (res > -1) {
if (WIFEXITED(status)) {
res = 0;
}
else {
res = -1;
}
} else {
if (errno != EINTR) {
res = -1;
}
}
} else {
fprintf(stderr, "Fork failed: %s\n", strerror(errno));
res = -1;
}
alarm(0);
signal(SIGCHLD, prev_handler);
return res;
}