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>
18 static char *tmpdir = NULL;
19 static unsigned int count;
22 const unsigned int default_timeout_ms = 10 * 60 * 1000;
24 char *talloc_basename(const void *ctx, const char *dir)
26 char *p = strrchr(dir, '/');
30 return talloc_strdup(ctx, p+1);
33 char *talloc_dirname(const void *ctx, const char *dir)
35 char *p = strrchr(dir, '/');
38 return talloc_strdup(ctx, ".");
39 return talloc_strndup(ctx, dir, p - dir);
42 char *talloc_getcwd(const void *ctx)
47 /* *This* is why people hate C. */
49 cwd = talloc_array(ctx, char, len);
50 while (!getcwd(cwd, len)) {
51 if (errno != ERANGE) {
55 cwd = talloc_realloc(ctx, cwd, char, len *= 2);
60 static char *run_with_timeout(const void *ctx,
69 struct timeval start, end;
73 return talloc_asprintf(ctx, "Failed to create pipe: %s",
80 return talloc_asprintf(ctx, "Failed to fork: %s",
86 struct itimerval itim;
88 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
89 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
91 || close(STDIN_FILENO) != 0
92 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
95 itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
96 itim.it_value.tv_sec = *timeout_ms / 1000;
97 itim.it_value.tv_usec = (*timeout_ms % 1000) * 1000;
98 setitimer(ITIMER_REAL, &itim, NULL);
100 status = system(cmd);
101 if (WIFEXITED(status))
102 exit(WEXITSTATUS(status));
103 /* Here's a hint... */
104 exit(128 + WTERMSIG(status));
108 gettimeofday(&start, NULL);
109 ret = grab_fd(ctx, p[0], NULL);
110 /* This shouldn't fail... */
111 if (waitpid(pid, &status, 0) != pid)
112 err(1, "Failed to wait for child");
114 gettimeofday(&end, NULL);
115 if (WIFSIGNALED(status)) {
119 if (end.tv_usec < start.tv_usec) {
120 end.tv_usec += 1000000;
123 ms = (end.tv_sec - start.tv_sec) * 1000
124 + (end.tv_usec - start.tv_usec) / 1000;
125 if (ms > *timeout_ms)
130 *ok = (WEXITSTATUS(status) == 0);
134 /* Returns output if command fails. */
135 char *run_command(const void *ctx, unsigned int *time_ms, const char *fmt, ...)
138 char *cmd, *contents;
140 unsigned int default_time = default_timeout_ms;
143 time_ms = &default_time;
146 cmd = talloc_vasprintf(ctx, fmt, ap);
149 contents = run_with_timeout(ctx, cmd, &ok, time_ms);
151 talloc_free(contents);
156 err(1, "Problem running child");
158 talloc_asprintf_append(contents, "\n== TIMED OUT ==\n");
162 static int unlink_all(char *dir)
164 char cmd[strlen(dir) + sizeof("rm -rf ")];
165 sprintf(cmd, "rm -rf %s", dir);
166 if (system(cmd) != 0)
167 warn("Could not remove temporary work in %s", dir);
171 char *temp_file(const void *ctx, const char *extension)
173 /* For first call, create dir. */
175 tmpdir = getenv("TMPDIR");
178 tmpdir = talloc_asprintf(talloc_autofree_context(),
179 "%s/ccanlint-%u.%lu",
180 tmpdir, getpid(), random());
181 if (mkdir(tmpdir, 0700) != 0) {
182 if (errno == EEXIST) {
187 err(1, "mkdir %s failed", tmpdir);
189 talloc_set_destructor(tmpdir, unlink_all);
192 return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension);
195 bool move_file(const char *oldname, const char *newname)
202 /* Simple case: rename works. */
203 if (rename(oldname, newname) == 0)
206 /* Try copy and delete: not atomic! */
207 contents = grab_file(NULL, oldname, &size);
211 fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
217 ret = write_all(fd, contents, size);
227 talloc_free(contents);