tools.c 6.1 KB

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