tools.c 6.3 KB

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