tools.c 5.2 KB

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