tools.c 6.3 KB

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