tools.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <sys/wait.h>
  13. #include <fcntl.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <assert.h>
  20. #include <signal.h>
  21. #include "tools.h"
  22. static const char *tmpdir = NULL;
  23. bool tools_verbose = false;
  24. /* Ten minutes. */
  25. const unsigned int default_timeout_ms = 10 * 60 * 1000;
  26. static void killme(int sig)
  27. {
  28. kill(-getpid(), SIGKILL);
  29. }
  30. char *run_with_timeout(const void *ctx, const char *cmd,
  31. bool *ok, unsigned *timeout_ms)
  32. {
  33. pid_t pid;
  34. int p[2];
  35. struct rbuf in;
  36. int status, ms;
  37. struct timespec start;
  38. *ok = false;
  39. if (pipe(p) != 0)
  40. return tal_fmt(ctx, "Failed to create pipe: %s",
  41. strerror(errno));
  42. if (tools_verbose)
  43. printf("Running: %s\n", cmd);
  44. /* Always flush buffers before fork! */
  45. fflush(stdout);
  46. start = time_now();
  47. pid = fork();
  48. if (pid == -1) {
  49. close_noerr(p[0]);
  50. close_noerr(p[1]);
  51. return tal_fmt(ctx, "Failed to fork: %s", strerror(errno));
  52. }
  53. if (pid == 0) {
  54. struct itimerval itim;
  55. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  56. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  57. || close(p[0]) != 0
  58. || close(STDIN_FILENO) != 0
  59. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  60. exit(128);
  61. signal(SIGALRM, killme);
  62. itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
  63. itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
  64. setitimer(ITIMER_REAL, &itim, NULL);
  65. status = system(cmd);
  66. if (WIFEXITED(status))
  67. exit(WEXITSTATUS(status));
  68. /* Here's a hint... */
  69. exit(128 + WTERMSIG(status));
  70. }
  71. close(p[1]);
  72. rbuf_init(&in, p[0], tal_arr(ctx, char, 4096), 4096);
  73. if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
  74. in.buf = tal_free(in.buf);
  75. /* This shouldn't fail... */
  76. if (waitpid(pid, &status, 0) != pid)
  77. err(1, "Failed to wait for child");
  78. ms = time_to_msec(time_sub(time_now(), start));
  79. if (ms > *timeout_ms)
  80. *timeout_ms = 0;
  81. else
  82. *timeout_ms -= ms;
  83. close(p[0]);
  84. if (tools_verbose) {
  85. printf("%s", in.buf);
  86. printf("Finished: %u ms, %s %u\n", ms,
  87. WIFEXITED(status) ? "exit status" : "killed by signal",
  88. WIFEXITED(status) ? WEXITSTATUS(status)
  89. : WTERMSIG(status));
  90. }
  91. *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
  92. return in.buf;
  93. }
  94. /* Tals *output off ctx; return false if command fails. */
  95. bool run_command(const void *ctx, unsigned int *time_ms, char **output,
  96. const char *fmt, ...)
  97. {
  98. va_list ap;
  99. char *cmd;
  100. bool ok;
  101. unsigned int default_time = default_timeout_ms;
  102. if (!time_ms)
  103. time_ms = &default_time;
  104. else if (*time_ms == 0) {
  105. *output = tal_strdup(ctx, "\n== TIMED OUT ==\n");
  106. return false;
  107. }
  108. va_start(ap, fmt);
  109. cmd = tal_vfmt(ctx, fmt, ap);
  110. va_end(ap);
  111. *output = run_with_timeout(ctx, cmd, &ok, time_ms);
  112. if (ok)
  113. return true;
  114. if (!*output)
  115. err(1, "Problem running child");
  116. if (*time_ms == 0)
  117. *output = tal_strcat(ctx, take(*output), "\n== TIMED OUT ==\n");
  118. return false;
  119. }
  120. static void unlink_all(const char *dir)
  121. {
  122. char cmd[strlen(dir) + sizeof("rm -rf ")];
  123. sprintf(cmd, "rm -rf %s", dir);
  124. if (tools_verbose)
  125. printf("Running: %s\n", cmd);
  126. if (system(cmd) != 0)
  127. warn("Could not remove temporary work in %s", dir);
  128. }
  129. static pid_t *afree;
  130. static void free_autofree(void)
  131. {
  132. if (*afree == getpid())
  133. tal_free(afree);
  134. }
  135. tal_t *autofree(void)
  136. {
  137. if (!afree) {
  138. afree = tal(NULL, pid_t);
  139. *afree = getpid();
  140. atexit(free_autofree);
  141. }
  142. return afree;
  143. }
  144. const char *temp_dir(void)
  145. {
  146. /* For first call, create dir. */
  147. while (!tmpdir) {
  148. tmpdir = getenv("TMPDIR");
  149. if (!tmpdir)
  150. tmpdir = "/tmp";
  151. tmpdir = tal_fmt(autofree(), "%s/ccanlint-%u.%lu",
  152. tmpdir, getpid(), random());
  153. if (mkdir(tmpdir, 0700) != 0) {
  154. if (errno == EEXIST) {
  155. tal_free(tmpdir);
  156. tmpdir = NULL;
  157. continue;
  158. }
  159. err(1, "mkdir %s failed", tmpdir);
  160. }
  161. tal_add_destructor(tmpdir, unlink_all);
  162. if (tools_verbose)
  163. printf("Created temporary directory %s\n", tmpdir);
  164. }
  165. return tmpdir;
  166. }
  167. void keep_temp_dir(void)
  168. {
  169. tal_del_destructor(temp_dir(), unlink_all);
  170. }
  171. char *temp_file(const void *ctx, const char *extension, const char *srcname)
  172. {
  173. char *f, *base, *suffix;
  174. struct stat st;
  175. unsigned int count = 0;
  176. base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname)));
  177. /* Trim extension. */
  178. base[path_ext_off(base)] = '\0';
  179. suffix = tal_strdup(ctx, extension);
  180. do {
  181. f = tal_strcat(ctx, base, suffix);
  182. suffix = tal_fmt(base, "-%u%s", ++count, extension);
  183. } while (lstat(f, &st) == 0);
  184. if (tools_verbose)
  185. printf("Creating file %s\n", f);
  186. tal_free(base);
  187. return f;
  188. }
  189. bool move_file(const char *oldname, const char *newname)
  190. {
  191. char *contents;
  192. size_t size;
  193. int fd;
  194. bool ret;
  195. if (tools_verbose)
  196. printf("Moving file %s to %s: ", oldname, newname);
  197. /* Simple case: rename works. */
  198. if (rename(oldname, newname) == 0) {
  199. if (tools_verbose)
  200. printf("rename worked\n");
  201. return true;
  202. }
  203. /* Try copy and delete: not atomic! */
  204. contents = tal_grab_file(NULL, oldname, &size);
  205. if (!contents) {
  206. if (tools_verbose)
  207. printf("read failed: %s\n", strerror(errno));
  208. return false;
  209. }
  210. fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  211. if (fd < 0) {
  212. if (tools_verbose)
  213. printf("output open failed: %s\n", strerror(errno));
  214. ret = false;
  215. goto free;
  216. }
  217. ret = write_all(fd, contents, size);
  218. if (close(fd) != 0)
  219. ret = false;
  220. if (ret) {
  221. if (tools_verbose)
  222. printf("copy worked\n");
  223. unlink(oldname);
  224. } else {
  225. if (tools_verbose)
  226. printf("write/close failed\n");
  227. unlink(newname);
  228. }
  229. free:
  230. tal_free(contents);
  231. return ret;
  232. }
  233. void *do_tal_realloc(void *p, size_t size)
  234. {
  235. tal_resize((char **)&p, size);
  236. return p;
  237. }
  238. void *tal_grab_file(const void *ctx, const char *filename, size_t *size)
  239. {
  240. struct rbuf rbuf;
  241. char *buf = tal_arr(ctx, char, 0);
  242. if (!rbuf_open(&rbuf, filename, buf, 0))
  243. return tal_free(buf);
  244. if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno)
  245. rbuf.buf = tal_free(rbuf.buf);
  246. else {
  247. rbuf.buf[rbuf.len] = '\0';
  248. if (size)
  249. *size = rbuf.len;
  250. }
  251. close(rbuf.fd);
  252. return rbuf.buf;
  253. }