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 gettimeofday(&start, NULL);
89 return talloc_asprintf(ctx, "Failed to fork: %s",
95 struct itimerval itim;
97 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
98 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
100 || close(STDIN_FILENO) != 0
101 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
104 signal(SIGALRM, killme);
105 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
106 itim.it_value.tv_sec = *timeout_ms / 1000;
107 itim.it_value.tv_usec = (*timeout_ms % 1000) * 1000;
108 setitimer(ITIMER_REAL, &itim, NULL);
110 status = system(cmd);
111 if (WIFEXITED(status))
112 exit(WEXITSTATUS(status));
113 /* Here's a hint... */
114 exit(128 + WTERMSIG(status));
118 ret = grab_fd(ctx, p[0], NULL);
119 /* This shouldn't fail... */
120 if (waitpid(pid, &status, 0) != pid)
121 err(1, "Failed to wait for child");
123 gettimeofday(&end, NULL);
124 if (end.tv_usec < start.tv_usec) {
125 end.tv_usec += 1000000;
128 ms = (end.tv_sec - start.tv_sec) * 1000
129 + (end.tv_usec - start.tv_usec) / 1000;
130 if (ms > *timeout_ms)
137 printf("Finished: %u ms, %s %u\n", ms,
138 WIFEXITED(status) ? "exit status" : "killed by signal",
139 WIFEXITED(status) ? WEXITSTATUS(status)
142 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
146 /* Tallocs *output off ctx; return false if command fails. */
147 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
148 const char *fmt, ...)
153 unsigned int default_time = default_timeout_ms;
156 time_ms = &default_time;
157 else if (*time_ms == 0) {
158 *output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
163 cmd = talloc_vasprintf(ctx, fmt, ap);
166 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
170 err(1, "Problem running child");
172 *output = talloc_asprintf_append(*output,
173 "\n== TIMED OUT ==\n");
177 static int unlink_all(char *dir)
179 char cmd[strlen(dir) + sizeof("rm -rf ")];
180 sprintf(cmd, "rm -rf %s", dir);
182 printf("Running: %s\n", cmd);
183 if (system(cmd) != 0)
184 warn("Could not remove temporary work in %s", dir);
188 char *temp_dir(const void *ctx)
190 /* For first call, create dir. */
192 tmpdir = getenv("TMPDIR");
195 tmpdir = talloc_asprintf(talloc_autofree_context(),
196 "%s/ccanlint-%u.%lu",
197 tmpdir, getpid(), random());
198 if (mkdir(tmpdir, 0700) != 0) {
199 if (errno == EEXIST) {
204 err(1, "mkdir %s failed", tmpdir);
206 talloc_set_destructor(tmpdir, unlink_all);
208 printf("Created temporary directory %s\n", tmpdir);
213 int unlink_file_destructor(char *filename)
219 char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
223 char *f, *suffix = talloc_strdup(ctx, "");
225 unsigned int count = 0;
227 srcname = talloc_basename(ctx, srcname);
228 if (strrchr(srcname, '.'))
229 baselen = strrchr(srcname, '.') - srcname;
231 baselen = strlen(srcname);
234 f = talloc_asprintf(ctx, "%s/%.*s%s%s",
239 suffix = talloc_asprintf(ctx, "-%u", ++count);
240 } while (lstat(f, &st) == 0);
243 printf("Creating %sfile %s\n", keep ? "" : "temporary ", f);
246 talloc_set_destructor(f, unlink_file_destructor);
252 bool move_file(const char *oldname, const char *newname)
260 printf("Moving file %s to %s: ", oldname, newname);
262 /* Simple case: rename works. */
263 if (rename(oldname, newname) == 0) {
265 printf("rename worked\n");
269 /* Try copy and delete: not atomic! */
270 contents = grab_file(NULL, oldname, &size);
273 printf("read failed: %s\n", strerror(errno));
277 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
280 printf("output open failed: %s\n", strerror(errno));
285 ret = write_all(fd, contents, size);
291 printf("copy worked\n");
295 printf("write/close failed\n");
300 talloc_free(contents);