1 #include <ccan/talloc/talloc.h>
2 #include <ccan/grab_file/grab_file.h>
3 #include <ccan/err/err.h>
4 #include <ccan/noerr/noerr.h>
5 #include <ccan/read_write_all/read_write_all.h>
6 #include <ccan/noerr/noerr.h>
7 #include <ccan/time/time.h>
22 static const char *tmpdir = NULL;
23 bool tools_verbose = false;
26 const unsigned int default_timeout_ms = 10 * 60 * 1000;
28 char *talloc_basename(const void *ctx, const char *dir)
30 char *p = strrchr(dir, '/');
33 return talloc_strdup(ctx, dir);
34 return talloc_strdup(ctx, p+1);
37 char *talloc_dirname(const void *ctx, const char *dir)
39 char *p = strrchr(dir, '/');
42 return talloc_strdup(ctx, ".");
43 return talloc_strndup(ctx, dir, p - dir);
46 char *talloc_getcwd(const void *ctx)
51 /* *This* is why people hate C. */
53 cwd = talloc_array(ctx, char, len);
54 while (!getcwd(cwd, len)) {
55 if (errno != ERANGE) {
59 cwd = talloc_realloc(ctx, cwd, char, len *= 2);
64 static void killme(int sig)
66 kill(-getpid(), SIGKILL);
69 char *run_with_timeout(const void *ctx, const char *cmd,
70 bool *ok, unsigned *timeout_ms)
76 struct timespec start;
80 return talloc_asprintf(ctx, "Failed to create pipe: %s",
84 printf("Running: %s\n", cmd);
86 /* Always flush buffers before fork! */
93 return talloc_asprintf(ctx, "Failed to fork: %s",
98 struct itimerval itim;
100 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
101 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
103 || close(STDIN_FILENO) != 0
104 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
107 signal(SIGALRM, killme);
108 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
109 itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
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 ms = time_to_msec(time_sub(time_now(), start));
126 if (ms > *timeout_ms)
133 printf("Finished: %u ms, %s %u\n", ms,
134 WIFEXITED(status) ? "exit status" : "killed by signal",
135 WIFEXITED(status) ? WEXITSTATUS(status)
138 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
142 /* Tallocs *output off ctx; return false if command fails. */
143 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
144 const char *fmt, ...)
149 unsigned int default_time = default_timeout_ms;
152 time_ms = &default_time;
153 else if (*time_ms == 0) {
154 *output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
159 cmd = talloc_vasprintf(ctx, fmt, ap);
162 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
166 err(1, "Problem running child");
168 *output = talloc_asprintf_append(*output,
169 "\n== TIMED OUT ==\n");
173 static int unlink_all(const char *dir)
175 char cmd[strlen(dir) + sizeof("rm -rf ")];
176 sprintf(cmd, "rm -rf %s", dir);
178 printf("Running: %s\n", cmd);
179 if (system(cmd) != 0)
180 warn("Could not remove temporary work in %s", dir);
184 const char *temp_dir(const void *ctx)
186 /* For first call, create dir. */
188 tmpdir = getenv("TMPDIR");
191 tmpdir = talloc_asprintf(talloc_autofree_context(),
192 "%s/ccanlint-%u.%lu",
193 tmpdir, getpid(), random());
194 if (mkdir(tmpdir, 0700) != 0) {
195 if (errno == EEXIST) {
200 err(1, "mkdir %s failed", tmpdir);
202 talloc_set_destructor(tmpdir, unlink_all);
204 printf("Created temporary directory %s\n", tmpdir);
209 int unlink_file_destructor(char *filename)
215 char *temp_file(const void *ctx, const char *extension, const char *srcname)
218 char *f, *suffix = talloc_strdup(ctx, "");
220 unsigned int count = 0;
222 srcname = talloc_basename(ctx, srcname);
223 if (strrchr(srcname, '.'))
224 baselen = strrchr(srcname, '.') - srcname;
226 baselen = strlen(srcname);
229 f = talloc_asprintf(ctx, "%s/%.*s%s%s",
234 suffix = talloc_asprintf(ctx, "-%u", ++count);
235 } while (lstat(f, &st) == 0);
238 printf("Creating file %s\n", f);
244 bool move_file(const char *oldname, const char *newname)
252 printf("Moving file %s to %s: ", oldname, newname);
254 /* Simple case: rename works. */
255 if (rename(oldname, newname) == 0) {
257 printf("rename worked\n");
261 /* Try copy and delete: not atomic! */
262 contents = grab_file(NULL, oldname, &size);
265 printf("read failed: %s\n", strerror(errno));
269 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
272 printf("output open failed: %s\n", strerror(errno));
277 ret = write_all(fd, contents, size);
283 printf("copy worked\n");
287 printf("write/close failed\n");
292 talloc_free(contents);