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>
19 static char *tmpdir = NULL;
20 static unsigned int count;
23 const unsigned int default_timeout_ms = 10 * 60 * 1000;
25 char *talloc_basename(const void *ctx, const char *dir)
27 char *p = strrchr(dir, '/');
31 return talloc_strdup(ctx, p+1);
34 char *talloc_dirname(const void *ctx, const char *dir)
36 char *p = strrchr(dir, '/');
39 return talloc_strdup(ctx, ".");
40 return talloc_strndup(ctx, dir, p - dir);
43 char *talloc_getcwd(const void *ctx)
48 /* *This* is why people hate C. */
50 cwd = talloc_array(ctx, char, len);
51 while (!getcwd(cwd, len)) {
52 if (errno != ERANGE) {
56 cwd = talloc_realloc(ctx, cwd, char, len *= 2);
61 static void killme(int sig)
63 kill(-getpid(), SIGKILL);
66 static char *run_with_timeout(const void *ctx,
75 struct timeval start, end;
79 return talloc_asprintf(ctx, "Failed to create pipe: %s",
82 gettimeofday(&start, NULL);
87 return talloc_asprintf(ctx, "Failed to fork: %s",
93 struct itimerval itim;
95 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
96 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
98 || close(STDIN_FILENO) != 0
99 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
103 signal(SIGALRM, killme);
104 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
105 itim.it_value.tv_sec = *timeout_ms / 1000;
106 itim.it_value.tv_usec = (*timeout_ms % 1000) * 1000;
107 setitimer(ITIMER_REAL, &itim, NULL);
109 status = system(cmd);
110 if (WIFEXITED(status))
111 exit(WEXITSTATUS(status));
112 /* Here's a hint... */
113 exit(128 + WTERMSIG(status));
117 ret = grab_fd(ctx, p[0], NULL);
118 /* This shouldn't fail... */
119 if (waitpid(pid, &status, 0) != pid)
120 err(1, "Failed to wait for child");
122 gettimeofday(&end, NULL);
123 if (end.tv_usec < start.tv_usec) {
124 end.tv_usec += 1000000;
127 ms = (end.tv_sec - start.tv_sec) * 1000
128 + (end.tv_usec - start.tv_usec) / 1000;
129 if (ms > *timeout_ms)
134 *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
138 /* Returns output if command fails. */
139 char *run_command(const void *ctx, unsigned int *time_ms, const char *fmt, ...)
142 char *cmd, *contents;
144 unsigned int default_time = default_timeout_ms;
147 time_ms = &default_time;
148 else if (*time_ms == 0)
149 return talloc_strdup(ctx, "\n== TIMED OUT ==\n");
152 cmd = talloc_vasprintf(ctx, fmt, ap);
155 contents = run_with_timeout(ctx, cmd, &ok, time_ms);
157 talloc_free(contents);
162 err(1, "Problem running child");
164 contents = talloc_asprintf_append(contents,
165 "\n== TIMED OUT ==\n");
169 static int unlink_all(char *dir)
171 char cmd[strlen(dir) + sizeof("rm -rf ")];
172 sprintf(cmd, "rm -rf %s", dir);
173 if (system(cmd) != 0)
174 warn("Could not remove temporary work in %s", dir);
178 char *temp_file(const void *ctx, const char *extension)
180 /* For first call, create dir. */
182 tmpdir = getenv("TMPDIR");
185 tmpdir = talloc_asprintf(talloc_autofree_context(),
186 "%s/ccanlint-%u.%lu",
187 tmpdir, getpid(), random());
188 if (mkdir(tmpdir, 0700) != 0) {
189 if (errno == EEXIST) {
194 err(1, "mkdir %s failed", tmpdir);
196 talloc_set_destructor(tmpdir, unlink_all);
199 return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension);
202 bool move_file(const char *oldname, const char *newname)
209 /* Simple case: rename works. */
210 if (rename(oldname, newname) == 0)
213 /* Try copy and delete: not atomic! */
214 contents = grab_file(NULL, oldname, &size);
218 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
224 ret = write_all(fd, contents, size);
234 talloc_free(contents);