28060
|
1 |
/*
|
|
2 |
* Utility functions
|
|
3 |
*
|
|
4 |
* Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
|
|
5 |
*
|
|
6 |
* This software may be used and distributed according to the terms of the
|
|
7 |
* GNU General Public License version 2 or any later version.
|
|
8 |
*/
|
|
9 |
|
|
10 |
#include <signal.h>
|
|
11 |
#include <stdarg.h>
|
|
12 |
#include <stdio.h>
|
|
13 |
#include <stdlib.h>
|
|
14 |
#include <sys/types.h>
|
|
15 |
#include <sys/wait.h>
|
|
16 |
#include <unistd.h>
|
|
17 |
|
|
18 |
#include "util.h"
|
|
19 |
|
|
20 |
void abortmsg(const char *fmt, ...)
|
|
21 |
{
|
|
22 |
va_list args;
|
|
23 |
va_start(args, fmt);
|
|
24 |
fputs("\033[1;31mchg: abort: ", stderr);
|
|
25 |
vfprintf(stderr, fmt, args);
|
|
26 |
fputs("\033[m\n", stderr);
|
|
27 |
va_end(args);
|
|
28 |
|
|
29 |
exit(255);
|
|
30 |
}
|
|
31 |
|
|
32 |
static int debugmsgenabled = 0;
|
|
33 |
|
|
34 |
void enabledebugmsg(void)
|
|
35 |
{
|
|
36 |
debugmsgenabled = 1;
|
|
37 |
}
|
|
38 |
|
|
39 |
void debugmsg(const char *fmt, ...)
|
|
40 |
{
|
|
41 |
if (!debugmsgenabled)
|
|
42 |
return;
|
|
43 |
|
|
44 |
va_list args;
|
|
45 |
va_start(args, fmt);
|
|
46 |
fputs("\033[1;30mchg: debug: ", stderr);
|
|
47 |
vfprintf(stderr, fmt, args);
|
|
48 |
fputs("\033[m\n", stderr);
|
|
49 |
va_end(args);
|
|
50 |
}
|
|
51 |
|
|
52 |
/*
|
|
53 |
* Execute a shell command in mostly the same manner as system(), with the
|
|
54 |
* give environment variables, after chdir to the given cwd. Returns a status
|
|
55 |
* code compatible with the Python subprocess module.
|
|
56 |
*/
|
|
57 |
int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
|
|
58 |
{
|
|
59 |
enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
|
|
60 |
unsigned int doneflags = 0;
|
|
61 |
int status = 0;
|
|
62 |
struct sigaction newsa, oldsaint, oldsaquit;
|
|
63 |
sigset_t oldmask;
|
|
64 |
|
|
65 |
/* block or mask signals just as system() does */
|
|
66 |
newsa.sa_handler = SIG_IGN;
|
|
67 |
newsa.sa_flags = 0;
|
|
68 |
if (sigemptyset(&newsa.sa_mask) < 0)
|
|
69 |
goto done;
|
|
70 |
if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
|
|
71 |
goto done;
|
|
72 |
doneflags |= F_SIGINT;
|
|
73 |
if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
|
|
74 |
goto done;
|
|
75 |
doneflags |= F_SIGQUIT;
|
|
76 |
|
|
77 |
if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
|
|
78 |
goto done;
|
|
79 |
if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
|
|
80 |
goto done;
|
|
81 |
doneflags |= F_SIGMASK;
|
|
82 |
|
|
83 |
pid_t pid = fork();
|
|
84 |
if (pid < 0)
|
|
85 |
goto done;
|
|
86 |
if (pid == 0) {
|
|
87 |
sigaction(SIGINT, &oldsaint, NULL);
|
|
88 |
sigaction(SIGQUIT, &oldsaquit, NULL);
|
|
89 |
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
|
90 |
if (cwd && chdir(cwd) < 0)
|
|
91 |
_exit(127);
|
|
92 |
const char *argv[] = {"sh", "-c", cmd, NULL};
|
|
93 |
if (envp) {
|
|
94 |
execve("/bin/sh", (char **)argv, (char **)envp);
|
|
95 |
} else {
|
|
96 |
execv("/bin/sh", (char **)argv);
|
|
97 |
}
|
|
98 |
_exit(127);
|
|
99 |
} else {
|
|
100 |
if (waitpid(pid, &status, 0) < 0)
|
|
101 |
goto done;
|
|
102 |
doneflags |= F_WAITPID;
|
|
103 |
}
|
|
104 |
|
|
105 |
done:
|
|
106 |
if (doneflags & F_SIGINT)
|
|
107 |
sigaction(SIGINT, &oldsaint, NULL);
|
|
108 |
if (doneflags & F_SIGQUIT)
|
|
109 |
sigaction(SIGQUIT, &oldsaquit, NULL);
|
|
110 |
if (doneflags & F_SIGMASK)
|
|
111 |
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
|
112 |
|
|
113 |
/* no way to report other errors, use 127 (= shell termination) */
|
|
114 |
if (!(doneflags & F_WAITPID))
|
|
115 |
return 127;
|
|
116 |
if (WIFEXITED(status))
|
|
117 |
return WEXITSTATUS(status);
|
|
118 |
if (WIFSIGNALED(status))
|
|
119 |
return -WTERMSIG(status);
|
|
120 |
return 127;
|
|
121 |
}
|