tools.c 5.9 KB

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