1 #include <ccan/take/take.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>
8 #include <ccan/tal/path/path.h>
10 #include <sys/types.h>
23 static const char *tmpdir = NULL;
24 bool tools_verbose = false;
27 const unsigned int default_timeout_ms = 10 * 60 * 1000;
29 static void killme(int sig)
31 kill(-getpid(), SIGKILL);
34 char *run_with_timeout(const void *ctx, const char *cmd,
35 bool *ok, unsigned *timeout_ms)
41 struct timespec start;
45 return tal_fmt(ctx, "Failed to create pipe: %s",
49 printf("Running: %s\n", cmd);
51 /* Always flush buffers before fork! */
58 return tal_fmt(ctx, "Failed to fork: %s", strerror(errno));
62 struct itimerval itim;
64 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
65 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
67 || close(STDIN_FILENO) != 0
68 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
71 signal(SIGALRM, killme);
72 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
73 itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
74 setitimer(ITIMER_REAL, &itim, NULL);
77 if (WIFEXITED(status))
78 exit(WEXITSTATUS(status));
79 /* Here's a hint... */
80 exit(128 + WTERMSIG(status));
84 rbuf_init(&in, p[0], tal_arr(ctx, char, 4096), 4096);
85 if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
86 in.buf = tal_free(in.buf);
88 /* This shouldn't fail... */
89 if (waitpid(pid, &status, 0) != pid)
90 err(1, "Failed to wait for child");
92 ms = time_to_msec(time_sub(time_now(), start));
100 printf("Finished: %u ms, %s %u\n", ms,
101 WIFEXITED(status) ? "exit status" : "killed by signal",
102 WIFEXITED(status) ? WEXITSTATUS(status)
105 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
109 /* Tals *output off ctx; return false if command fails. */
110 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
111 const char *fmt, ...)
116 unsigned int default_time = default_timeout_ms;
119 time_ms = &default_time;
120 else if (*time_ms == 0) {
121 *output = tal_strdup(ctx, "\n== TIMED OUT ==\n");
126 cmd = tal_vfmt(ctx, fmt, ap);
129 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
133 err(1, "Problem running child");
135 *output = tal_strcat(ctx, take(*output), "\n== TIMED OUT ==\n");
139 static void unlink_all(const char *dir)
141 char cmd[strlen(dir) + sizeof("rm -rf ")];
142 sprintf(cmd, "rm -rf %s", dir);
144 printf("Running: %s\n", cmd);
145 if (system(cmd) != 0)
146 warn("Could not remove temporary work in %s", dir);
150 static void free_autofree(void)
152 if (*afree == getpid())
156 tal_t *autofree(void)
159 afree = tal(NULL, pid_t);
161 atexit(free_autofree);
166 const char *temp_dir(void)
168 /* For first call, create dir. */
170 tmpdir = getenv("TMPDIR");
173 tmpdir = tal_fmt(autofree(), "%s/ccanlint-%u.%lu",
174 tmpdir, getpid(), random());
175 if (mkdir(tmpdir, 0700) != 0) {
176 if (errno == EEXIST) {
181 err(1, "mkdir %s failed", tmpdir);
183 tal_add_destructor(tmpdir, unlink_all);
185 printf("Created temporary directory %s\n", tmpdir);
190 void keep_temp_dir(void)
192 tal_del_destructor(temp_dir(), unlink_all);
195 char *temp_file(const void *ctx, const char *extension, const char *srcname)
197 char *f, *base, *suffix;
199 unsigned int count = 0;
201 base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname)));
202 /* Trim extension. */
203 base[path_ext_off(base)] = '\0';
204 suffix = tal_strdup(ctx, extension);
207 f = tal_strcat(ctx, base, suffix);
208 suffix = tal_fmt(base, "-%u%s", ++count, extension);
209 } while (lstat(f, &st) == 0);
212 printf("Creating file %s\n", f);
218 bool move_file(const char *oldname, const char *newname)
226 printf("Moving file %s to %s: ", oldname, newname);
228 /* Simple case: rename works. */
229 if (rename(oldname, newname) == 0) {
231 printf("rename worked\n");
235 /* Try copy and delete: not atomic! */
236 contents = tal_grab_file(NULL, oldname, &size);
239 printf("read failed: %s\n", strerror(errno));
243 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
246 printf("output open failed: %s\n", strerror(errno));
251 ret = write_all(fd, contents, size);
257 printf("copy worked\n");
261 printf("write/close failed\n");
270 void *do_tal_realloc(void *p, size_t size)
272 tal_resize((char **)&p, size);
276 void *tal_grab_file(const void *ctx, const char *filename, size_t *size)
279 char *buf = tal_arr(ctx, char, 0);
281 if (!rbuf_open(&rbuf, filename, buf, 0))
282 return tal_free(buf);
284 if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno)
285 rbuf.buf = tal_free(rbuf.buf);
287 rbuf.buf[rbuf.len] = '\0';