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>
20 static char *tmpdir = NULL;
21 bool tools_verbose = false;
24 const unsigned int default_timeout_ms = 10 * 60 * 1000;
26 char *talloc_basename(const void *ctx, const char *dir)
28 char *p = strrchr(dir, '/');
31 return talloc_strdup(ctx, dir);
32 return talloc_strdup(ctx, p+1);
35 char *talloc_dirname(const void *ctx, const char *dir)
37 char *p = strrchr(dir, '/');
40 return talloc_strdup(ctx, ".");
41 return talloc_strndup(ctx, dir, p - dir);
44 char *talloc_getcwd(const void *ctx)
49 /* *This* is why people hate C. */
51 cwd = talloc_array(ctx, char, len);
52 while (!getcwd(cwd, len)) {
53 if (errno != ERANGE) {
57 cwd = talloc_realloc(ctx, cwd, char, len *= 2);
62 static void killme(int sig)
64 kill(-getpid(), SIGKILL);
67 char *run_with_timeout(const void *ctx, const char *cmd,
68 bool *ok, unsigned *timeout_ms)
74 struct timeval start, end;
78 return talloc_asprintf(ctx, "Failed to create pipe: %s",
82 printf("Running: %s\n", cmd);
84 /* Always flush buffers before fork! */
86 gettimeofday(&start, NULL);
91 return talloc_asprintf(ctx, "Failed to fork: %s",
97 struct itimerval itim;
99 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
100 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
102 || close(STDIN_FILENO) != 0
103 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
106 signal(SIGALRM, killme);
107 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
108 itim.it_value.tv_sec = *timeout_ms / 1000;
109 itim.it_value.tv_usec = (*timeout_ms % 1000) * 1000;
110 setitimer(ITIMER_REAL, &itim, NULL);
112 status = system(cmd);
113 if (WIFEXITED(status))
114 exit(WEXITSTATUS(status));
115 /* Here's a hint... */
116 exit(128 + WTERMSIG(status));
120 ret = grab_fd(ctx, p[0], NULL);
121 /* This shouldn't fail... */
122 if (waitpid(pid, &status, 0) != pid)
123 err(1, "Failed to wait for child");
125 gettimeofday(&end, NULL);
126 if (end.tv_usec < start.tv_usec) {
127 end.tv_usec += 1000000;
130 ms = (end.tv_sec - start.tv_sec) * 1000
131 + (end.tv_usec - start.tv_usec) / 1000;
132 if (ms > *timeout_ms)
139 printf("Finished: %u ms, %s %u\n", ms,
140 WIFEXITED(status) ? "exit status" : "killed by signal",
141 WIFEXITED(status) ? WEXITSTATUS(status)
144 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
148 /* Tallocs *output off ctx; return false if command fails. */
149 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
150 const char *fmt, ...)
155 unsigned int default_time = default_timeout_ms;
158 time_ms = &default_time;
159 else if (*time_ms == 0) {
160 *output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
165 cmd = talloc_vasprintf(ctx, fmt, ap);
168 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
172 err(1, "Problem running child");
174 *output = talloc_asprintf_append(*output,
175 "\n== TIMED OUT ==\n");
179 static int unlink_all(char *dir)
181 char cmd[strlen(dir) + sizeof("rm -rf ")];
182 sprintf(cmd, "rm -rf %s", dir);
184 printf("Running: %s\n", cmd);
185 if (system(cmd) != 0)
186 warn("Could not remove temporary work in %s", dir);
190 char *temp_dir(const void *ctx)
192 /* For first call, create dir. */
194 tmpdir = getenv("TMPDIR");
197 tmpdir = talloc_asprintf(talloc_autofree_context(),
198 "%s/ccanlint-%u.%lu",
199 tmpdir, getpid(), random());
200 if (mkdir(tmpdir, 0700) != 0) {
201 if (errno == EEXIST) {
206 err(1, "mkdir %s failed", tmpdir);
208 talloc_set_destructor(tmpdir, unlink_all);
210 printf("Created temporary directory %s\n", tmpdir);
215 int unlink_file_destructor(char *filename)
221 char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
225 char *f, *suffix = talloc_strdup(ctx, "");
227 unsigned int count = 0;
229 srcname = talloc_basename(ctx, srcname);
230 if (strrchr(srcname, '.'))
231 baselen = strrchr(srcname, '.') - srcname;
233 baselen = strlen(srcname);
236 f = talloc_asprintf(ctx, "%s/%.*s%s%s",
241 suffix = talloc_asprintf(ctx, "-%u", ++count);
242 } while (lstat(f, &st) == 0);
245 printf("Creating %sfile %s\n", keep ? "" : "temporary ", f);
248 talloc_set_destructor(f, unlink_file_destructor);
254 bool move_file(const char *oldname, const char *newname)
262 printf("Moving file %s to %s: ", oldname, newname);
264 /* Simple case: rename works. */
265 if (rename(oldname, newname) == 0) {
267 printf("rename worked\n");
271 /* Try copy and delete: not atomic! */
272 contents = grab_file(NULL, oldname, &size);
275 printf("read failed: %s\n", strerror(errno));
279 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
282 printf("output open failed: %s\n", strerror(errno));
287 ret = write_all(fd, contents, size);
293 printf("copy worked\n");
297 printf("write/close failed\n");
302 talloc_free(contents);