tools.c 4.7 KB

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