author | Jun Wu <quark@fb.com> |
Wed, 17 Feb 2016 14:51:38 +0000 | |
changeset 28165 | c6705c6303dd |
parent 28084 | 3fc45956c978 |
child 28787 | ea86cdcd9b50 |
permissions | -rw-r--r-- |
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> |
|
28084
3fc45956c978
chg: initialize sigaction fields more reliably
Yuya Nishihara <yuya@tcha.org>
parents:
28060
diff
changeset
|
14 |
#include <string.h> |
28060 | 15 |
#include <sys/types.h> |
16 |
#include <sys/wait.h> |
|
17 |
#include <unistd.h> |
|
18 |
||
19 |
#include "util.h" |
|
20 |
||
21 |
void abortmsg(const char *fmt, ...) |
|
22 |
{ |
|
23 |
va_list args; |
|
24 |
va_start(args, fmt); |
|
25 |
fputs("\033[1;31mchg: abort: ", stderr); |
|
26 |
vfprintf(stderr, fmt, args); |
|
27 |
fputs("\033[m\n", stderr); |
|
28 |
va_end(args); |
|
29 |
||
30 |
exit(255); |
|
31 |
} |
|
32 |
||
33 |
static int debugmsgenabled = 0; |
|
34 |
||
35 |
void enabledebugmsg(void) |
|
36 |
{ |
|
37 |
debugmsgenabled = 1; |
|
38 |
} |
|
39 |
||
40 |
void debugmsg(const char *fmt, ...) |
|
41 |
{ |
|
42 |
if (!debugmsgenabled) |
|
43 |
return; |
|
44 |
||
45 |
va_list args; |
|
46 |
va_start(args, fmt); |
|
47 |
fputs("\033[1;30mchg: debug: ", stderr); |
|
48 |
vfprintf(stderr, fmt, args); |
|
49 |
fputs("\033[m\n", stderr); |
|
50 |
va_end(args); |
|
51 |
} |
|
52 |
||
28165
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
53 |
void *mallocx(size_t size) |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
54 |
{ |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
55 |
void *result = malloc(size); |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
56 |
if (!result) |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
57 |
abortmsg("failed to malloc"); |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
58 |
return result; |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
59 |
} |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
60 |
|
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
61 |
void *reallocx(void *ptr, size_t size) |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
62 |
{ |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
63 |
void *result = realloc(ptr, size); |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
64 |
if (!result) |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
65 |
abortmsg("failed to realloc"); |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
66 |
return result; |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
67 |
} |
c6705c6303dd
chg: add utility functions mallocx, reallocx
Jun Wu <quark@fb.com>
parents:
28084
diff
changeset
|
68 |
|
28060 | 69 |
/* |
70 |
* Execute a shell command in mostly the same manner as system(), with the |
|
71 |
* give environment variables, after chdir to the given cwd. Returns a status |
|
72 |
* code compatible with the Python subprocess module. |
|
73 |
*/ |
|
74 |
int runshellcmd(const char *cmd, const char *envp[], const char *cwd) |
|
75 |
{ |
|
76 |
enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 }; |
|
77 |
unsigned int doneflags = 0; |
|
78 |
int status = 0; |
|
79 |
struct sigaction newsa, oldsaint, oldsaquit; |
|
80 |
sigset_t oldmask; |
|
81 |
||
82 |
/* block or mask signals just as system() does */ |
|
28084
3fc45956c978
chg: initialize sigaction fields more reliably
Yuya Nishihara <yuya@tcha.org>
parents:
28060
diff
changeset
|
83 |
memset(&newsa, 0, sizeof(newsa)); |
28060 | 84 |
newsa.sa_handler = SIG_IGN; |
85 |
newsa.sa_flags = 0; |
|
86 |
if (sigemptyset(&newsa.sa_mask) < 0) |
|
87 |
goto done; |
|
88 |
if (sigaction(SIGINT, &newsa, &oldsaint) < 0) |
|
89 |
goto done; |
|
90 |
doneflags |= F_SIGINT; |
|
91 |
if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0) |
|
92 |
goto done; |
|
93 |
doneflags |= F_SIGQUIT; |
|
94 |
||
95 |
if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0) |
|
96 |
goto done; |
|
97 |
if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0) |
|
98 |
goto done; |
|
99 |
doneflags |= F_SIGMASK; |
|
100 |
||
101 |
pid_t pid = fork(); |
|
102 |
if (pid < 0) |
|
103 |
goto done; |
|
104 |
if (pid == 0) { |
|
105 |
sigaction(SIGINT, &oldsaint, NULL); |
|
106 |
sigaction(SIGQUIT, &oldsaquit, NULL); |
|
107 |
sigprocmask(SIG_SETMASK, &oldmask, NULL); |
|
108 |
if (cwd && chdir(cwd) < 0) |
|
109 |
_exit(127); |
|
110 |
const char *argv[] = {"sh", "-c", cmd, NULL}; |
|
111 |
if (envp) { |
|
112 |
execve("/bin/sh", (char **)argv, (char **)envp); |
|
113 |
} else { |
|
114 |
execv("/bin/sh", (char **)argv); |
|
115 |
} |
|
116 |
_exit(127); |
|
117 |
} else { |
|
118 |
if (waitpid(pid, &status, 0) < 0) |
|
119 |
goto done; |
|
120 |
doneflags |= F_WAITPID; |
|
121 |
} |
|
122 |
||
123 |
done: |
|
124 |
if (doneflags & F_SIGINT) |
|
125 |
sigaction(SIGINT, &oldsaint, NULL); |
|
126 |
if (doneflags & F_SIGQUIT) |
|
127 |
sigaction(SIGQUIT, &oldsaquit, NULL); |
|
128 |
if (doneflags & F_SIGMASK) |
|
129 |
sigprocmask(SIG_SETMASK, &oldmask, NULL); |
|
130 |
||
131 |
/* no way to report other errors, use 127 (= shell termination) */ |
|
132 |
if (!(doneflags & F_WAITPID)) |
|
133 |
return 127; |
|
134 |
if (WIFEXITED(status)) |
|
135 |
return WEXITSTATUS(status); |
|
136 |
if (WIFSIGNALED(status)) |
|
137 |
return -WTERMSIG(status); |
|
138 |
return 127; |
|
139 |
} |