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>
9 #include <ccan/tal/grab_file/grab_file.h>
11 #include <sys/types.h>
24 static const char *tmpdir = NULL;
25 bool tools_verbose = false;
28 const unsigned int default_timeout_ms = 10 * 60 * 1000;
30 static void killme(int sig)
32 kill(-getpid(), SIGKILL);
35 char *run_with_timeout(const void *ctx, const char *cmd,
36 bool *ok, unsigned *timeout_ms)
46 return tal_fmt(ctx, "Failed to create pipe: %s",
50 printf("Running: %s\n", cmd);
52 /* Always flush buffers before fork! */
59 return tal_fmt(ctx, "Failed to fork: %s", strerror(errno));
63 struct itimerval itim;
65 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
66 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
68 || close(STDIN_FILENO) != 0
69 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
72 signal(SIGALRM, killme);
73 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
74 itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms).ts);
75 setitimer(ITIMER_REAL, &itim, NULL);
78 if (WIFEXITED(status))
79 exit(WEXITSTATUS(status));
80 /* Here's a hint... */
81 exit(128 + WTERMSIG(status));
85 rbuf_init(&in, p[0], tal_arr(ctx, char, 4096), 4096);
86 if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
87 in.buf = tal_free(in.buf);
89 /* This shouldn't fail... */
90 if (waitpid(pid, &status, 0) != pid)
91 err(1, "Failed to wait for child");
93 ms = time_to_msec(time_between(time_now(), start));
100 printf("%s", in.buf);
101 printf("Finished: %u ms, %s %u\n", ms,
102 WIFEXITED(status) ? "exit status" : "killed by signal",
103 WIFEXITED(status) ? WEXITSTATUS(status)
106 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
110 /* Tals *output off ctx; return false if command fails. */
111 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
112 const char *fmt, ...)
117 unsigned int default_time = default_timeout_ms;
120 time_ms = &default_time;
121 else if (*time_ms == 0) {
122 *output = tal_strdup(ctx, "\n== TIMED OUT ==\n");
127 cmd = tal_vfmt(ctx, fmt, ap);
130 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
134 err(1, "Problem running child");
136 *output = tal_strcat(ctx, take(*output), "\n== TIMED OUT ==\n");
140 static void unlink_all(const char *dir)
142 char cmd[strlen(dir) + sizeof("rm -rf ")];
143 sprintf(cmd, "rm -rf %s", dir);
145 printf("Running: %s\n", cmd);
146 if (system(cmd) != 0)
147 warn("Could not remove temporary work in %s", dir);
151 static void free_autofree(void)
153 if (*afree == getpid())
157 tal_t *autofree(void)
160 afree = tal(NULL, pid_t);
162 atexit(free_autofree);
167 const char *temp_dir(void)
169 /* For first call, create dir. */
171 tmpdir = getenv("TMPDIR");
174 tmpdir = tal_fmt(autofree(), "%s/ccanlint-%u.%lu",
175 tmpdir, getpid(), random());
176 if (mkdir(tmpdir, 0700) != 0) {
177 if (errno == EEXIST) {
182 err(1, "mkdir %s failed", tmpdir);
184 tal_add_destructor(tmpdir, unlink_all);
186 printf("Created temporary directory %s\n", tmpdir);
191 void keep_temp_dir(void)
193 tal_del_destructor(temp_dir(), unlink_all);
196 char *temp_file(const void *ctx, const char *extension, const char *srcname)
198 char *f, *base, *suffix;
200 unsigned int count = 0;
202 base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname)));
203 /* Trim extension. */
204 base[path_ext_off(base)] = '\0';
205 suffix = tal_strdup(ctx, extension);
208 f = tal_strcat(ctx, base, suffix);
209 suffix = tal_fmt(base, "-%u%s", ++count, extension);
210 } while (lstat(f, &st) == 0);
213 printf("Creating file %s\n", f);
219 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 = grab_file(NULL, oldname);
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, tal_count(contents)-1);
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);