tools.c 6.4 KB

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