tools.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 timeval 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. return NULL;
  83. }
  84. if (pid == 0) {
  85. struct itimerval itim;
  86. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  87. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  88. || close(p[0]) != 0
  89. || close(STDIN_FILENO) != 0
  90. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  91. exit(128);
  92. signal(SIGALRM, killme);
  93. itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
  94. itim.it_value = time_from_msec(*timeout_ms);
  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. ms = time_to_msec(time_sub(time_now(), start));
  108. if (ms > *timeout_ms)
  109. *timeout_ms = 0;
  110. else
  111. *timeout_ms -= ms;
  112. close(p[0]);
  113. if (tools_verbose) {
  114. printf("%s", ret);
  115. printf("Finished: %u ms, %s %u\n", ms,
  116. WIFEXITED(status) ? "exit status" : "killed by signal",
  117. WIFEXITED(status) ? WEXITSTATUS(status)
  118. : WTERMSIG(status));
  119. }
  120. *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
  121. return ret;
  122. }
  123. /* Tallocs *output off ctx; return false if command fails. */
  124. bool run_command(const void *ctx, unsigned int *time_ms, char **output,
  125. const char *fmt, ...)
  126. {
  127. va_list ap;
  128. char *cmd;
  129. bool ok;
  130. unsigned int default_time = default_timeout_ms;
  131. if (!time_ms)
  132. time_ms = &default_time;
  133. else if (*time_ms == 0) {
  134. *output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
  135. return false;
  136. }
  137. va_start(ap, fmt);
  138. cmd = talloc_vasprintf(ctx, fmt, ap);
  139. va_end(ap);
  140. *output = run_with_timeout(ctx, cmd, &ok, time_ms);
  141. if (ok)
  142. return true;
  143. if (!*output)
  144. err(1, "Problem running child");
  145. if (*time_ms == 0)
  146. *output = talloc_asprintf_append(*output,
  147. "\n== TIMED OUT ==\n");
  148. return false;
  149. }
  150. static int unlink_all(const char *dir)
  151. {
  152. char cmd[strlen(dir) + sizeof("rm -rf ")];
  153. sprintf(cmd, "rm -rf %s", dir);
  154. if (tools_verbose)
  155. printf("Running: %s\n", cmd);
  156. if (system(cmd) != 0)
  157. warn("Could not remove temporary work in %s", dir);
  158. return 0;
  159. }
  160. const char *temp_dir(const void *ctx)
  161. {
  162. /* For first call, create dir. */
  163. while (!tmpdir) {
  164. tmpdir = getenv("TMPDIR");
  165. if (!tmpdir)
  166. tmpdir = "/tmp";
  167. tmpdir = talloc_asprintf(talloc_autofree_context(),
  168. "%s/ccanlint-%u.%lu",
  169. tmpdir, getpid(), random());
  170. if (mkdir(tmpdir, 0700) != 0) {
  171. if (errno == EEXIST) {
  172. talloc_free(tmpdir);
  173. tmpdir = NULL;
  174. continue;
  175. }
  176. err(1, "mkdir %s failed", tmpdir);
  177. }
  178. talloc_set_destructor(tmpdir, unlink_all);
  179. if (tools_verbose)
  180. printf("Created temporary directory %s\n", tmpdir);
  181. }
  182. return tmpdir;
  183. }
  184. int unlink_file_destructor(char *filename)
  185. {
  186. unlink(filename);
  187. return 0;
  188. }
  189. char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
  190. const char *srcname)
  191. {
  192. unsigned baselen;
  193. char *f, *suffix = talloc_strdup(ctx, "");
  194. struct stat st;
  195. unsigned int count = 0;
  196. srcname = talloc_basename(ctx, srcname);
  197. if (strrchr(srcname, '.'))
  198. baselen = strrchr(srcname, '.') - srcname;
  199. else
  200. baselen = strlen(srcname);
  201. do {
  202. f = talloc_asprintf(ctx, "%s/%.*s%s%s",
  203. temp_dir(ctx),
  204. baselen, srcname,
  205. suffix, extension);
  206. talloc_free(suffix);
  207. suffix = talloc_asprintf(ctx, "-%u", ++count);
  208. } while (lstat(f, &st) == 0);
  209. if (tools_verbose)
  210. printf("Creating %sfile %s\n", keep ? "" : "temporary ", f);
  211. if (!keep)
  212. talloc_set_destructor(f, unlink_file_destructor);
  213. talloc_free(suffix);
  214. return f;
  215. }
  216. bool move_file(const char *oldname, const char *newname)
  217. {
  218. char *contents;
  219. size_t size;
  220. int fd;
  221. bool ret;
  222. if (tools_verbose)
  223. printf("Moving file %s to %s: ", oldname, newname);
  224. /* Simple case: rename works. */
  225. if (rename(oldname, newname) == 0) {
  226. if (tools_verbose)
  227. printf("rename worked\n");
  228. return true;
  229. }
  230. /* Try copy and delete: not atomic! */
  231. contents = grab_file(NULL, oldname, &size);
  232. if (!contents) {
  233. if (tools_verbose)
  234. printf("read failed: %s\n", strerror(errno));
  235. return false;
  236. }
  237. fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  238. if (fd < 0) {
  239. if (tools_verbose)
  240. printf("output open failed: %s\n", strerror(errno));
  241. ret = false;
  242. goto free;
  243. }
  244. ret = write_all(fd, contents, size);
  245. if (close(fd) != 0)
  246. ret = false;
  247. if (ret) {
  248. if (tools_verbose)
  249. printf("copy worked\n");
  250. unlink(oldname);
  251. } else {
  252. if (tools_verbose)
  253. printf("write/close failed\n");
  254. unlink(newname);
  255. }
  256. free:
  257. talloc_free(contents);
  258. return ret;
  259. }