Debian Codegraph @

contrib/chg/util.c

raw
1 /*
2 * Utility functions
3 *
4 * Copyright (c) 2011 Yuya Nishihara <[email protected]>
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 <errno.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21
22 #include "util.h"
23
24 static int colorenabled = 0;
25
26 static inline void fsetcolor(FILE *fp, const char *code)
27 {
28 if (!colorenabled) {
29 return;
30 }
31 fprintf(fp, "\033[%sm", code);
32 }
33
34 static void vabortmsgerrno(int no, const char *fmt, va_list args)
35 {
36 fsetcolor(stderr, "1;31");
37 fputs("chg: abort: ", stderr);
38 vfprintf(stderr, fmt, args);
39 if (no != 0) {
40 fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
41 }
42 fsetcolor(stderr, "");
43 fputc('\n', stderr);
44 exit(255);
45 }
46
47 void abortmsg(const char *fmt, ...)
48 {
49 va_list args;
50 va_start(args, fmt);
51 vabortmsgerrno(0, fmt, args);
52 va_end(args);
53 }
54
55 void abortmsgerrno(const char *fmt, ...)
56 {
57 int no = errno;
58 va_list args;
59 va_start(args, fmt);
60 vabortmsgerrno(no, fmt, args);
61 va_end(args);
62 }
63
64 static int debugmsgenabled = 0;
65 static double debugstart = 0;
66
67 static double now()
68 {
69 struct timeval t;
70 gettimeofday(&t, NULL);
71 return t.tv_usec / 1e6 + t.tv_sec;
72 }
73
74 void enablecolor(void)
75 {
76 colorenabled = 1;
77 }
78
79 void enabledebugmsg(void)
80 {
81 debugmsgenabled = 1;
82 debugstart = now();
83 }
84
85 void debugmsg(const char *fmt, ...)
86 {
87 if (!debugmsgenabled) {
88 return;
89 }
90
91 va_list args;
92 va_start(args, fmt);
93 fsetcolor(stderr, "1;30");
94 fprintf(stderr, "chg: debug: %4.6f ", now() - debugstart);
95 vfprintf(stderr, fmt, args);
96 fsetcolor(stderr, "");
97 fputc('\n', stderr);
98 va_end(args);
99 }
100
101 void fchdirx(int dirfd)
102 {
103 int r = fchdir(dirfd);
104 if (r == -1) {
105 abortmsgerrno("failed to fchdir");
106 }
107 }
108
109 void fsetcloexec(int fd)
110 {
111 int flags = fcntl(fd, F_GETFD);
112 if (flags < 0) {
113 abortmsgerrno("cannot get flags of fd %d", fd);
114 }
115 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
116 abortmsgerrno("cannot set flags of fd %d", fd);
117 }
118 }
119
120 void *mallocx(size_t size)
121 {
122 void *result = malloc(size);
123 if (!result) {
124 abortmsg("failed to malloc");
125 }
126 return result;
127 }
128
129 void *reallocx(void *ptr, size_t size)
130 {
131 void *result = realloc(ptr, size);
132 if (!result) {
133 abortmsg("failed to realloc");
134 }
135 return result;
136 }
137
138 /*
139 * Execute a shell command in mostly the same manner as system(), with the
140 * give environment variables, after chdir to the given cwd. Returns a status
141 * code compatible with the Python subprocess module.
142 */
143 int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
144 {
145 enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
146 unsigned int doneflags = 0;
147 int status = 0;
148 struct sigaction newsa, oldsaint, oldsaquit;
149 sigset_t oldmask;
150
151 /* block or mask signals just as system() does */
152 memset(&newsa, 0, sizeof(newsa));
153 newsa.sa_handler = SIG_IGN;
154 newsa.sa_flags = 0;
155 if (sigemptyset(&newsa.sa_mask) < 0) {
156 goto done;
157 }
158 if (sigaction(SIGINT, &newsa, &oldsaint) < 0) {
159 goto done;
160 }
161 doneflags |= F_SIGINT;
162 if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0) {
163 goto done;
164 }
165 doneflags |= F_SIGQUIT;
166
167 if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0) {
168 goto done;
169 }
170 if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0) {
171 goto done;
172 }
173 doneflags |= F_SIGMASK;
174
175 pid_t pid = fork();
176 if (pid < 0) {
177 goto done;
178 }
179 if (pid == 0) {
180 sigaction(SIGINT, &oldsaint, NULL);
181 sigaction(SIGQUIT, &oldsaquit, NULL);
182 sigprocmask(SIG_SETMASK, &oldmask, NULL);
183 if (cwd && chdir(cwd) < 0) {
184 _exit(127);
185 }
186 const char *argv[] = {"sh", "-c", cmd, NULL};
187 if (envp) {
188 execve("/bin/sh", (char **)argv, (char **)envp);
189 } else {
190 execv("/bin/sh", (char **)argv);
191 }
192 _exit(127);
193 } else {
194 if (waitpid(pid, &status, 0) < 0) {
195 goto done;
196 }
197 doneflags |= F_WAITPID;
198 }
199
200 done:
201 if (doneflags & F_SIGINT) {
202 sigaction(SIGINT, &oldsaint, NULL);
203 }
204 if (doneflags & F_SIGQUIT) {
205 sigaction(SIGQUIT, &oldsaquit, NULL);
206 }
207 if (doneflags & F_SIGMASK) {
208 sigprocmask(SIG_SETMASK, &oldmask, NULL);
209 }
210
211 /* no way to report other errors, use 127 (= shell termination) */
212 if (!(doneflags & F_WAITPID)) {
213 return 127;
214 }
215 if (WIFEXITED(status)) {
216 return WEXITSTATUS(status);
217 }
218 if (WIFSIGNALED(status)) {
219 return -WTERMSIG(status);
220 }
221 return 127;
222 }