tools.c 6.5 KB

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