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 <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)
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",
99 struct itimerval itim;
101 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
102 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
104 || close(STDIN_FILENO) != 0
105 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
108 signal(SIGALRM, killme);
109 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
110 itim.it_value = time_from_msec(*timeout_ms);
111 setitimer(ITIMER_REAL, &itim, NULL);
113 status = system(cmd);
114 if (WIFEXITED(status))
115 exit(WEXITSTATUS(status));
116 /* Here's a hint... */
117 exit(128 + WTERMSIG(status));
121 ret = grab_fd(ctx, p[0], NULL);
122 /* This shouldn't fail... */
123 if (waitpid(pid, &status, 0) != pid)
124 err(1, "Failed to wait for child");
126 ms = time_to_msec(time_sub(time_now(), start));
127 if (ms > *timeout_ms)
134 printf("Finished: %u ms, %s %u\n", ms,
135 WIFEXITED(status) ? "exit status" : "killed by signal",
136 WIFEXITED(status) ? WEXITSTATUS(status)
139 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
143 /* Tallocs *output off ctx; return false if command fails. */
144 bool run_command(const void *ctx, unsigned int *time_ms, char **output,
145 const char *fmt, ...)
150 unsigned int default_time = default_timeout_ms;
153 time_ms = &default_time;
154 else if (*time_ms == 0) {
155 *output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
160 cmd = talloc_vasprintf(ctx, fmt, ap);
163 *output = run_with_timeout(ctx, cmd, &ok, time_ms);
167 err(1, "Problem running child");
169 *output = talloc_asprintf_append(*output,
170 "\n== TIMED OUT ==\n");
174 static int unlink_all(const char *dir)
176 char cmd[strlen(dir) + sizeof("rm -rf ")];
177 sprintf(cmd, "rm -rf %s", dir);
179 printf("Running: %s\n", cmd);
180 if (system(cmd) != 0)
181 warn("Could not remove temporary work in %s", dir);
185 const char *temp_dir(const void *ctx)
187 /* For first call, create dir. */
189 tmpdir = getenv("TMPDIR");
192 tmpdir = talloc_asprintf(talloc_autofree_context(),
193 "%s/ccanlint-%u.%lu",
194 tmpdir, getpid(), random());
195 if (mkdir(tmpdir, 0700) != 0) {
196 if (errno == EEXIST) {
201 err(1, "mkdir %s failed", tmpdir);
203 talloc_set_destructor(tmpdir, unlink_all);
205 printf("Created temporary directory %s\n", tmpdir);
210 int unlink_file_destructor(char *filename)
216 char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
220 char *f, *suffix = talloc_strdup(ctx, "");
222 unsigned int count = 0;
224 srcname = talloc_basename(ctx, srcname);
225 if (strrchr(srcname, '.'))
226 baselen = strrchr(srcname, '.') - srcname;
228 baselen = strlen(srcname);
231 f = talloc_asprintf(ctx, "%s/%.*s%s%s",
236 suffix = talloc_asprintf(ctx, "-%u", ++count);
237 } while (lstat(f, &st) == 0);
240 printf("Creating %sfile %s\n", keep ? "" : "temporary ", f);
243 talloc_set_destructor(f, unlink_file_destructor);
249 bool move_file(const char *oldname, const char *newname)
257 printf("Moving file %s to %s: ", oldname, newname);
259 /* Simple case: rename works. */
260 if (rename(oldname, newname) == 0) {
262 printf("rename worked\n");
266 /* Try copy and delete: not atomic! */
267 contents = grab_file(NULL, oldname, &size);
270 printf("read failed: %s\n", strerror(errno));
274 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
277 printf("output open failed: %s\n", strerror(errno));
282 ret = write_all(fd, contents, size);
288 printf("copy worked\n");
292 printf("write/close failed\n");
297 talloc_free(contents);