tools.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. signal(SIGALRM, killme);
  88. itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
  89. itim.it_value.tv_sec = *timeout_ms / 1000;
  90. itim.it_value.tv_usec = (*timeout_ms % 1000) * 1000;
  91. setitimer(ITIMER_REAL, &itim, NULL);
  92. status = system(cmd);
  93. if (WIFEXITED(status))
  94. exit(WEXITSTATUS(status));
  95. /* Here's a hint... */
  96. exit(128 + WTERMSIG(status));
  97. }
  98. close(p[1]);
  99. ret = grab_fd(ctx, p[0], NULL);
  100. /* This shouldn't fail... */
  101. if (waitpid(pid, &status, 0) != pid)
  102. err(1, "Failed to wait for child");
  103. gettimeofday(&end, NULL);
  104. if (end.tv_usec < start.tv_usec) {
  105. end.tv_usec += 1000000;
  106. end.tv_sec--;
  107. }
  108. ms = (end.tv_sec - start.tv_sec) * 1000
  109. + (end.tv_usec - start.tv_usec) / 1000;
  110. if (ms > *timeout_ms)
  111. *timeout_ms = 0;
  112. else
  113. *timeout_ms -= ms;
  114. *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
  115. return ret;
  116. }
  117. /* Returns output if command fails. */
  118. char *run_command(const void *ctx, unsigned int *time_ms, const char *fmt, ...)
  119. {
  120. va_list ap;
  121. char *cmd, *contents;
  122. bool ok;
  123. unsigned int default_time = default_timeout_ms;
  124. if (!time_ms)
  125. time_ms = &default_time;
  126. else if (*time_ms == 0)
  127. return talloc_strdup(ctx, "\n== TIMED OUT ==\n");
  128. va_start(ap, fmt);
  129. cmd = talloc_vasprintf(ctx, fmt, ap);
  130. va_end(ap);
  131. contents = run_with_timeout(ctx, cmd, &ok, time_ms);
  132. if (ok) {
  133. talloc_free(contents);
  134. return NULL;
  135. }
  136. if (!contents)
  137. err(1, "Problem running child");
  138. if (*time_ms == 0)
  139. contents = talloc_asprintf_append(contents,
  140. "\n== TIMED OUT ==\n");
  141. return contents;
  142. }
  143. static int unlink_all(char *dir)
  144. {
  145. char cmd[strlen(dir) + sizeof("rm -rf ")];
  146. sprintf(cmd, "rm -rf %s", dir);
  147. if (system(cmd) != 0)
  148. warn("Could not remove temporary work in %s", dir);
  149. return 0;
  150. }
  151. char *temp_file(const void *ctx, const char *extension)
  152. {
  153. /* For first call, create dir. */
  154. while (!tmpdir) {
  155. tmpdir = getenv("TMPDIR");
  156. if (!tmpdir)
  157. tmpdir = "/tmp";
  158. tmpdir = talloc_asprintf(talloc_autofree_context(),
  159. "%s/ccanlint-%u.%lu",
  160. tmpdir, getpid(), random());
  161. if (mkdir(tmpdir, 0700) != 0) {
  162. if (errno == EEXIST) {
  163. talloc_free(tmpdir);
  164. tmpdir = NULL;
  165. continue;
  166. }
  167. err(1, "mkdir %s failed", tmpdir);
  168. }
  169. talloc_set_destructor(tmpdir, unlink_all);
  170. }
  171. return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension);
  172. }
  173. char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
  174. const char *srcname)
  175. {
  176. size_t baselen;
  177. if (!keep)
  178. return temp_file(ctx, extension);
  179. baselen = strrchr(srcname, '.') - srcname;
  180. return talloc_asprintf(ctx, "%.*s%s", baselen, srcname, extension);
  181. }
  182. bool move_file(const char *oldname, const char *newname)
  183. {
  184. char *contents;
  185. size_t size;
  186. int fd;
  187. bool ret;
  188. /* Simple case: rename works. */
  189. if (rename(oldname, newname) == 0)
  190. return true;
  191. /* Try copy and delete: not atomic! */
  192. contents = grab_file(NULL, oldname, &size);
  193. if (!contents)
  194. return false;
  195. fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  196. if (fd < 0) {
  197. ret = false;
  198. goto free;
  199. }
  200. ret = write_all(fd, contents, size);
  201. if (close(fd) != 0)
  202. ret = false;
  203. if (ret)
  204. unlink(oldname);
  205. else
  206. unlink(newname);
  207. free:
  208. talloc_free(contents);
  209. return ret;
  210. }