1 #include <ccan/talloc/talloc.h>
2 #include <ccan/err/err.h>
3 #include <ccan/noerr/noerr.h>
4 #include <ccan/rbuf/rbuf.h>
5 #include <ccan/read_write_all/read_write_all.h>
6 #include <ccan/noerr/noerr.h>
7 #include <ccan/time/time.h>
22 static const char *tmpdir = NULL;
23 bool tools_verbose = false;
26 const unsigned int default_timeout_ms = 10 * 60 * 1000;
28 char *talloc_basename(const void *ctx, const char *dir)
30 char *p = strrchr(dir, '/');
33 return talloc_strdup(ctx, dir);
34 return talloc_strdup(ctx, p+1);
37 char *talloc_dirname(const void *ctx, const char *dir)
39 char *p = strrchr(dir, '/');
42 return talloc_strdup(ctx, ".");
43 return talloc_strndup(ctx, dir, p - dir);
46 char *talloc_getcwd(const void *ctx)
51 /* *This* is why people hate C. */
53 cwd = talloc_array(ctx, char, len);
54 while (!getcwd(cwd, len)) {
55 if (errno != ERANGE) {
59 cwd = talloc_realloc(ctx, cwd, char, len *= 2);
64 static void killme(int sig)
66 kill(-getpid(), SIGKILL);
69 char *run_with_timeout(const void *ctx, const char *cmd,
70 bool *ok, unsigned *timeout_ms)
76 struct timespec start;
80 return talloc_asprintf(ctx, "Failed to create pipe: %s",
84 printf("Running: %s\n", cmd);
86 /* Always flush buffers before fork! */
93 return talloc_asprintf(ctx, "Failed to fork: %s",
98 struct itimerval itim;
100 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
101 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
103 || close(STDIN_FILENO) != 0
104 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
107 signal(SIGALRM, killme);
108 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
109 itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
110 setitimer(ITIMER_REAL, &itim, NULL);
112 status = system(cmd);
113 if (WIFEXITED(status))
114 exit(WEXITSTATUS(status));
115 /* Here's a hint... */
116 exit(128 + WTERMSIG(status));
120 rbuf_init(&in, p[0], talloc_array(ctx, char, 4096), 4096);
121 if (!rbuf_read_str(&in, 0, do_talloc_realloc) && errno) {
126 /* This shouldn't fail... */
127 if (waitpid(pid, &status, 0) != pid)
128 err(1, "Failed to wait for child");
130 ms = time_to_msec(time_sub(time_now(), start));
131 if (ms > *timeout_ms)
137 printf("%s", in.buf);
138 printf("Finished: %u ms, %s %u\n", ms,
139 WIFEXITED(status) ? "exit status" : "killed by signal",
140 WIFEXITED(status) ? WEXITSTATUS(status)
143 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
147 /* Tallocs *output off ctx; return false if command fails. */
148 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
149 const char *fmt, ...)
154 unsigned int default_time = default_timeout_ms;
157 time_ms = &default_time;
158 else if (*time_ms == 0) {
159 *output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
164 cmd = talloc_vasprintf(ctx, fmt, ap);
167 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
171 err(1, "Problem running child");
173 *output = talloc_asprintf_append(*output,
174 "\n== TIMED OUT ==\n");
178 static int unlink_all(const char *dir)
180 char cmd[strlen(dir) + sizeof("rm -rf ")];
181 sprintf(cmd, "rm -rf %s", dir);
183 printf("Running: %s\n", cmd);
184 if (system(cmd) != 0)
185 warn("Could not remove temporary work in %s", dir);
189 const char *temp_dir(const void *ctx)
191 /* For first call, create dir. */
193 tmpdir = getenv("TMPDIR");
196 tmpdir = talloc_asprintf(talloc_autofree_context(),
197 "%s/ccanlint-%u.%lu",
198 tmpdir, getpid(), random());
199 if (mkdir(tmpdir, 0700) != 0) {
200 if (errno == EEXIST) {
205 err(1, "mkdir %s failed", tmpdir);
207 talloc_set_destructor(tmpdir, unlink_all);
209 printf("Created temporary directory %s\n", tmpdir);
214 int unlink_file_destructor(char *filename)
220 char *temp_file(const void *ctx, const char *extension, const char *srcname)
223 char *f, *suffix = talloc_strdup(ctx, "");
225 unsigned int count = 0;
227 srcname = talloc_basename(ctx, srcname);
228 if (strrchr(srcname, '.'))
229 baselen = strrchr(srcname, '.') - srcname;
231 baselen = strlen(srcname);
234 f = talloc_asprintf(ctx, "%s/%.*s%s%s",
239 suffix = talloc_asprintf(ctx, "-%u", ++count);
240 } while (lstat(f, &st) == 0);
243 printf("Creating file %s\n", f);
249 bool move_file(const char *oldname, const char *newname)
257 printf("Moving file %s to %s: ", oldname, newname);
259 /* Simple case: rename works. */
260 if (rename(oldname, newname) == 0) {
262 printf("rename worked\n");
266 /* Try copy and delete: not atomic! */
267 contents = talloc_grab_file(NULL, oldname, &size);
270 printf("read failed: %s\n", strerror(errno));
274 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
277 printf("output open failed: %s\n", strerror(errno));
282 ret = write_all(fd, contents, size);
288 printf("copy worked\n");
292 printf("write/close failed\n");
297 talloc_free(contents);
301 void *do_talloc_realloc(void *p, size_t size)
303 return talloc_realloc(NULL, p, char, size);
306 void *talloc_grab_file(const void *ctx, const char *filename, size_t *size)
309 char *buf = talloc_size(ctx, 4096);
311 if (!rbuf_open(&rbuf, filename, buf, 4096)) {
315 if (!rbuf_fill_all(&rbuf, do_talloc_realloc)) {
316 talloc_free(rbuf.buf);
319 rbuf.buf[rbuf.len] = '\0';