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>
22 static const char *tmpdir = NULL;
23 bool tools_verbose = false;
26 const unsigned int default_timeout_ms = 10 * 60 * 1000;
28 char *tal_basename(const void *ctx, const char *dir)
30 const char *p = strrchr(dir, '/');
33 return tal_strdup(ctx, dir);
34 return tal_strdup(ctx, p+1);
37 char *tal_dirname(const void *ctx, const char *dir)
39 const char *p = strrchr(dir, '/');
42 return tal_strdup(ctx, ".");
43 return tal_strndup(ctx, dir, p - dir);
46 char *tal_getcwd(const void *ctx)
51 /* *This* is why people hate C. */
53 cwd = tal_arr(ctx, char, len);
54 while (!getcwd(cwd, len)) {
55 if (errno != ERANGE) {
59 tal_resize(&cwd, 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 tal_fmt(ctx, "Failed to create pipe: %s",
84 printf("Running: %s\n", cmd);
86 /* Always flush buffers before fork! */
93 return tal_fmt(ctx, "Failed to fork: %s", strerror(errno));
97 struct itimerval itim;
99 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
100 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
102 || close(STDIN_FILENO) != 0
103 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
106 signal(SIGALRM, killme);
107 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
108 itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
109 setitimer(ITIMER_REAL, &itim, NULL);
111 status = system(cmd);
112 if (WIFEXITED(status))
113 exit(WEXITSTATUS(status));
114 /* Here's a hint... */
115 exit(128 + WTERMSIG(status));
119 rbuf_init(&in, p[0], tal_arr(ctx, char, 4096), 4096);
120 if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
121 in.buf = tal_free(in.buf);
123 /* This shouldn't fail... */
124 if (waitpid(pid, &status, 0) != pid)
125 err(1, "Failed to wait for child");
127 ms = time_to_msec(time_sub(time_now(), start));
128 if (ms > *timeout_ms)
134 printf("%s", in.buf);
135 printf("Finished: %u ms, %s %u\n", ms,
136 WIFEXITED(status) ? "exit status" : "killed by signal",
137 WIFEXITED(status) ? WEXITSTATUS(status)
140 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
144 /* Tals *output off ctx; return false if command fails. */
145 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
146 const char *fmt, ...)
151 unsigned int default_time = default_timeout_ms;
154 time_ms = &default_time;
155 else if (*time_ms == 0) {
156 *output = tal_strdup(ctx, "\n== TIMED OUT ==\n");
161 cmd = tal_vfmt(ctx, fmt, ap);
164 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
168 err(1, "Problem running child");
170 *output = tal_strcat(ctx, take(*output), "\n== TIMED OUT ==\n");
174 static void unlink_all(const char *dir)
176 char cmd[strlen(dir) + sizeof("rm -rf ")];
177 sprintf(cmd, "rm -rf %s", dir);
179 printf("Running: %s\n", cmd);
180 if (system(cmd) != 0)
181 warn("Could not remove temporary work in %s", dir);
185 static void free_autofree(void)
187 if (*afree == getpid())
191 tal_t *autofree(void)
194 afree = tal(NULL, pid_t);
196 atexit(free_autofree);
201 const char *temp_dir(void)
203 /* For first call, create dir. */
205 tmpdir = getenv("TMPDIR");
208 tmpdir = tal_fmt(autofree(), "%s/ccanlint-%u.%lu",
209 tmpdir, getpid(), random());
210 if (mkdir(tmpdir, 0700) != 0) {
211 if (errno == EEXIST) {
216 err(1, "mkdir %s failed", tmpdir);
218 tal_add_destructor(tmpdir, unlink_all);
220 printf("Created temporary directory %s\n", tmpdir);
225 void keep_temp_dir(void)
227 tal_del_destructor(temp_dir(), unlink_all);
230 char *temp_file(const void *ctx, const char *extension, const char *srcname)
233 char *f, *suffix = tal_strdup(ctx, "");
235 unsigned int count = 0;
237 srcname = tal_basename(ctx, srcname);
238 if (strrchr(srcname, '.'))
239 baselen = strrchr(srcname, '.') - srcname;
241 baselen = strlen(srcname);
244 f = tal_fmt(ctx, "%s/%.*s%s%s",
245 temp_dir(), baselen, srcname, suffix, extension);
247 suffix = tal_fmt(ctx, "-%u", ++count);
248 } while (lstat(f, &st) == 0);
251 printf("Creating file %s\n", f);
257 bool move_file(const char *oldname, const char *newname)
265 printf("Moving file %s to %s: ", oldname, newname);
267 /* Simple case: rename works. */
268 if (rename(oldname, newname) == 0) {
270 printf("rename worked\n");
274 /* Try copy and delete: not atomic! */
275 contents = tal_grab_file(NULL, oldname, &size);
278 printf("read failed: %s\n", strerror(errno));
282 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
285 printf("output open failed: %s\n", strerror(errno));
290 ret = write_all(fd, contents, size);
296 printf("copy worked\n");
300 printf("write/close failed\n");
309 void *do_tal_realloc(void *p, size_t size)
311 tal_resize((char **)&p, size);
315 void *tal_grab_file(const void *ctx, const char *filename, size_t *size)
318 char *buf = tal_arr(ctx, char, 0);
320 if (!rbuf_open(&rbuf, filename, buf, 0))
321 return tal_free(buf);
323 if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno)
324 rbuf.buf = tal_free(rbuf.buf);
326 rbuf.buf[rbuf.len] = '\0';