/* * "safer" versions of system(3), popen(3) and pclose(3) which give up * all privs before running a command. * * This is based on the code in FreeBSD 2.2 libc. * * XXX It'd be good to redirect stderr so that it ends up in the log file * as well. As it is now, xkbcomp messages don't end up in the log file. */ int System(char *command) { int pid, p; void (*csig)(int); int status; if (!command) return(1); csig = signal(SIGCHLD, SIG_DFL); ErrorF("System: `%s'\n", command); switch (pid = fork()) { case -1: /* error */ p = -1; case 0: /* child */ setgid(getgid()); setuid(getuid()); execl("/bin/sh", "sh", "-c", command, (char *)NULL); _exit(127); default: /* parent */ do { p = waitpid(pid, &status, 0); } while (p == -1 && errno == EINTR); } signal(SIGCHLD, csig); return p == -1 ? -1 : status; }