tools.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. char *run_with_timeout(const void *ctx, const char *cmd,
  57. bool *ok, unsigned *timeout_ms)
  58. {
  59. pid_t pid;
  60. int p[2];
  61. char *ret;
  62. int status, ms;
  63. struct timeval start, end;
  64. *ok = false;
  65. if (pipe(p) != 0)
  66. return talloc_asprintf(ctx, "Failed to create pipe: %s",
  67. strerror(errno));
  68. gettimeofday(&start, NULL);
  69. pid = fork();
  70. if (pid == -1) {
  71. close_noerr(p[0]);
  72. close_noerr(p[1]);
  73. return talloc_asprintf(ctx, "Failed to fork: %s",
  74. strerror(errno));
  75. return NULL;
  76. }
  77. if (pid == 0) {
  78. struct itimerval itim;
  79. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  80. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  81. || close(p[0]) != 0
  82. || close(STDIN_FILENO) != 0
  83. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  84. exit(128);
  85. signal(SIGALRM, killme);
  86. itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
  87. itim.it_value.tv_sec = *timeout_ms / 1000;
  88. itim.it_value.tv_usec = (*timeout_ms % 1000) * 1000;
  89. setitimer(ITIMER_REAL, &itim, NULL);
  90. status = system(cmd);
  91. if (WIFEXITED(status))
  92. exit(WEXITSTATUS(status));
  93. /* Here's a hint... */
  94. exit(128 + WTERMSIG(status));
  95. }
  96. close(p[1]);
  97. ret = grab_fd(ctx, p[0], NULL);
  98. /* This shouldn't fail... */
  99. if (waitpid(pid, &status, 0) != pid)
  100. err(1, "Failed to wait for child");
  101. gettimeofday(&end, NULL);
  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 = (WIFEXITED(status) && 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. else if (*time_ms == 0)
  125. return talloc_strdup(ctx, "\n== TIMED OUT ==\n");
  126. va_start(ap, fmt);
  127. cmd = talloc_vasprintf(ctx, fmt, ap);
  128. va_end(ap);
  129. contents = run_with_timeout(ctx, cmd, &ok, time_ms);
  130. if (ok) {
  131. talloc_free(contents);
  132. return NULL;
  133. }
  134. if (!contents)
  135. err(1, "Problem running child");
  136. if (*time_ms == 0)
  137. contents = talloc_asprintf_append(contents,
  138. "\n== TIMED OUT ==\n");
  139. return contents;
  140. }
  141. static int unlink_all(char *dir)
  142. {
  143. char cmd[strlen(dir) + sizeof("rm -rf ")];
  144. sprintf(cmd, "rm -rf %s", dir);
  145. if (system(cmd) != 0)
  146. warn("Could not remove temporary work in %s", dir);
  147. return 0;
  148. }
  149. char *temp_dir(const void *ctx)
  150. {
  151. /* For first call, create dir. */
  152. while (!tmpdir) {
  153. tmpdir = getenv("TMPDIR");
  154. if (!tmpdir)
  155. tmpdir = "/tmp";
  156. tmpdir = talloc_asprintf(talloc_autofree_context(),
  157. "%s/ccanlint-%u.%lu",
  158. tmpdir, getpid(), random());
  159. if (mkdir(tmpdir, 0700) != 0) {
  160. if (errno == EEXIST) {
  161. talloc_free(tmpdir);
  162. tmpdir = NULL;
  163. continue;
  164. }
  165. err(1, "mkdir %s failed", tmpdir);
  166. }
  167. talloc_set_destructor(tmpdir, unlink_all);
  168. }
  169. return tmpdir;
  170. }
  171. char *temp_file(const void *ctx, const char *extension)
  172. {
  173. return talloc_asprintf(ctx, "%s/%u%s",
  174. temp_dir(ctx), count++, extension);
  175. }
  176. char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
  177. const char *srcname)
  178. {
  179. size_t baselen;
  180. if (!keep)
  181. return temp_file(ctx, extension);
  182. baselen = strrchr(srcname, '.') - srcname;
  183. return talloc_asprintf(ctx, "%.*s%s", baselen, srcname, extension);
  184. }
  185. bool move_file(const char *oldname, const char *newname)
  186. {
  187. char *contents;
  188. size_t size;
  189. int fd;
  190. bool ret;
  191. /* Simple case: rename works. */
  192. if (rename(oldname, newname) == 0)
  193. return true;
  194. /* Try copy and delete: not atomic! */
  195. contents = grab_file(NULL, oldname, &size);
  196. if (!contents)
  197. return false;
  198. fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  199. if (fd < 0) {
  200. ret = false;
  201. goto free;
  202. }
  203. ret = write_all(fd, contents, size);
  204. if (close(fd) != 0)
  205. ret = false;
  206. if (ret)
  207. unlink(oldname);
  208. else
  209. unlink(newname);
  210. free:
  211. talloc_free(contents);
  212. return ret;
  213. }