]>
Commit | Line | Data |
---|---|---|
1a9d15db MH |
1 | /* |
2 | * State diagram and cleanup | |
3 | * ------------------------- | |
4 | * | |
5 | * If the program exits while a temporary file is active, we want to | |
6 | * make sure that we remove it. This is done by remembering the active | |
7 | * temporary files in a linked list, `tempfile_list`. An `atexit(3)` | |
8 | * handler and a signal handler are registered, to clean up any active | |
9 | * temporary files. | |
10 | * | |
11 | * Because the signal handler can run at any time, `tempfile_list` and | |
12 | * the `tempfile` objects that comprise it must be kept in | |
13 | * self-consistent states at all times. | |
14 | * | |
15 | * The possible states of a `tempfile` object are as follows: | |
16 | * | |
320fa579 JK |
17 | * - Inactive/unallocated. The only way to get a tempfile is via a creation |
18 | * function like create_tempfile(). Once allocated, the tempfile is on the | |
19 | * global tempfile_list and considered active. | |
1a9d15db MH |
20 | * |
21 | * - Active, file open (after `create_tempfile()` or | |
22 | * `reopen_tempfile()`). In this state: | |
23 | * | |
24 | * - the temporary file exists | |
1a9d15db MH |
25 | * - `filename` holds the filename of the temporary file |
26 | * - `fd` holds a file descriptor open for writing to it | |
27 | * - `fp` holds a pointer to an open `FILE` object if and only if | |
28 | * `fdopen_tempfile()` has been called on the object | |
29 | * - `owner` holds the PID of the process that created the file | |
30 | * | |
49bd0fc2 | 31 | * - Active, file closed (after `close_tempfile_gently()`). Same |
1a9d15db MH |
32 | * as the previous state, except that the temporary file is closed, |
33 | * `fd` is -1, and `fp` is `NULL`. | |
34 | * | |
49bd0fc2 | 35 | * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a |
320fa579 JK |
36 | * failed attempt to create a temporary file). The struct is removed from |
37 | * the global tempfile_list and deallocated. | |
1a9d15db MH |
38 | * |
39 | * A temporary file is owned by the process that created it. The | |
40 | * `tempfile` has an `owner` field that records the owner's PID. This | |
41 | * field is used to prevent a forked process from deleting a temporary | |
42 | * file created by its parent. | |
43 | */ | |
44 | ||
a64acf72 | 45 | #include "git-compat-util.h" |
5d1344b4 | 46 | #include "abspath.h" |
a64acf72 | 47 | #include "path.h" |
1a9d15db MH |
48 | #include "tempfile.h" |
49 | #include "sigchain.h" | |
50 | ||
24d82185 | 51 | static VOLATILE_LIST_HEAD(tempfile_list); |
1a9d15db | 52 | |
2c2db194 RS |
53 | static void remove_template_directory(struct tempfile *tempfile, |
54 | int in_signal_handler) | |
55 | { | |
babe2e05 | 56 | if (tempfile->directory) { |
2c2db194 | 57 | if (in_signal_handler) |
babe2e05 | 58 | rmdir(tempfile->directory); |
2c2db194 | 59 | else |
babe2e05 | 60 | rmdir_or_warn(tempfile->directory); |
2c2db194 RS |
61 | } |
62 | } | |
63 | ||
6b935066 | 64 | static void remove_tempfiles(int in_signal_handler) |
1a9d15db MH |
65 | { |
66 | pid_t me = getpid(); | |
24d82185 JK |
67 | volatile struct volatile_list_head *pos; |
68 | ||
69 | list_for_each(pos, &tempfile_list) { | |
70 | struct tempfile *p = list_entry(pos, struct tempfile, list); | |
1a9d15db | 71 | |
6b935066 JK |
72 | if (!is_tempfile_active(p) || p->owner != me) |
73 | continue; | |
74 | ||
75 | if (p->fd >= 0) | |
76 | close(p->fd); | |
77 | ||
78 | if (in_signal_handler) | |
79 | unlink(p->filename.buf); | |
80 | else | |
81 | unlink_or_warn(p->filename.buf); | |
2c2db194 | 82 | remove_template_directory(p, in_signal_handler); |
1a9d15db MH |
83 | } |
84 | } | |
85 | ||
86 | static void remove_tempfiles_on_exit(void) | |
87 | { | |
88 | remove_tempfiles(0); | |
89 | } | |
90 | ||
91 | static void remove_tempfiles_on_signal(int signo) | |
92 | { | |
93 | remove_tempfiles(1); | |
94 | sigchain_pop(signo); | |
95 | raise(signo); | |
96 | } | |
97 | ||
076aa2cb | 98 | static struct tempfile *new_tempfile(void) |
1a9d15db | 99 | { |
076aa2cb | 100 | struct tempfile *tempfile = xmalloc(sizeof(*tempfile)); |
422a21c6 JK |
101 | tempfile->fd = -1; |
102 | tempfile->fp = NULL; | |
422a21c6 JK |
103 | tempfile->owner = 0; |
104 | INIT_LIST_HEAD(&tempfile->list); | |
105 | strbuf_init(&tempfile->filename, 0); | |
babe2e05 | 106 | tempfile->directory = NULL; |
076aa2cb | 107 | return tempfile; |
7eba6ce5 MH |
108 | } |
109 | ||
2933ebba JK |
110 | static void activate_tempfile(struct tempfile *tempfile) |
111 | { | |
422a21c6 JK |
112 | static int initialized; |
113 | ||
422a21c6 JK |
114 | if (!initialized) { |
115 | sigchain_push_common(remove_tempfiles_on_signal); | |
116 | atexit(remove_tempfiles_on_exit); | |
117 | initialized = 1; | |
118 | } | |
119 | ||
120 | volatile_list_add(&tempfile->list, &tempfile_list); | |
2933ebba | 121 | tempfile->owner = getpid(); |
2933ebba JK |
122 | } |
123 | ||
b5f4dcb5 JK |
124 | static void deactivate_tempfile(struct tempfile *tempfile) |
125 | { | |
77a42b3b | 126 | volatile_list_del(&tempfile->list); |
102cf7a6 | 127 | strbuf_release(&tempfile->filename); |
babe2e05 | 128 | free(tempfile->directory); |
076aa2cb | 129 | free(tempfile); |
b5f4dcb5 JK |
130 | } |
131 | ||
7eba6ce5 | 132 | /* Make sure errno contains a meaningful value on error */ |
bef0413c | 133 | struct tempfile *create_tempfile_mode(const char *path, int mode) |
7eba6ce5 | 134 | { |
076aa2cb | 135 | struct tempfile *tempfile = new_tempfile(); |
1a9d15db MH |
136 | |
137 | strbuf_add_absolute_path(&tempfile->filename, path); | |
05d1ed61 | 138 | tempfile->fd = open(tempfile->filename.buf, |
bef0413c | 139 | O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
05d1ed61 BW |
140 | if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) |
141 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ | |
142 | tempfile->fd = open(tempfile->filename.buf, | |
bef0413c | 143 | O_RDWR | O_CREAT | O_EXCL, mode); |
1a9d15db | 144 | if (tempfile->fd < 0) { |
b5f4dcb5 | 145 | deactivate_tempfile(tempfile); |
076aa2cb | 146 | return NULL; |
1a9d15db | 147 | } |
2933ebba | 148 | activate_tempfile(tempfile); |
1a9d15db MH |
149 | if (adjust_shared_perm(tempfile->filename.buf)) { |
150 | int save_errno = errno; | |
151 | error("cannot fix permission bits on %s", tempfile->filename.buf); | |
076aa2cb | 152 | delete_tempfile(&tempfile); |
1a9d15db | 153 | errno = save_errno; |
076aa2cb | 154 | return NULL; |
1a9d15db | 155 | } |
076aa2cb JK |
156 | |
157 | return tempfile; | |
1a9d15db MH |
158 | } |
159 | ||
076aa2cb | 160 | struct tempfile *register_tempfile(const char *path) |
99397152 | 161 | { |
076aa2cb | 162 | struct tempfile *tempfile = new_tempfile(); |
99397152 | 163 | strbuf_add_absolute_path(&tempfile->filename, path); |
2933ebba | 164 | activate_tempfile(tempfile); |
076aa2cb | 165 | return tempfile; |
99397152 MH |
166 | } |
167 | ||
ea8ace4a | 168 | struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode) |
354ab112 | 169 | { |
076aa2cb | 170 | struct tempfile *tempfile = new_tempfile(); |
354ab112 | 171 | |
ea8ace4a | 172 | strbuf_add_absolute_path(&tempfile->filename, filename_template); |
354ab112 MH |
173 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
174 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 175 | deactivate_tempfile(tempfile); |
076aa2cb | 176 | return NULL; |
354ab112 | 177 | } |
2933ebba | 178 | activate_tempfile(tempfile); |
076aa2cb | 179 | return tempfile; |
354ab112 MH |
180 | } |
181 | ||
ea8ace4a | 182 | struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode) |
354ab112 | 183 | { |
076aa2cb | 184 | struct tempfile *tempfile = new_tempfile(); |
354ab112 MH |
185 | const char *tmpdir; |
186 | ||
354ab112 MH |
187 | tmpdir = getenv("TMPDIR"); |
188 | if (!tmpdir) | |
189 | tmpdir = "/tmp"; | |
190 | ||
ea8ace4a | 191 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template); |
354ab112 MH |
192 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
193 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 194 | deactivate_tempfile(tempfile); |
076aa2cb | 195 | return NULL; |
354ab112 | 196 | } |
2933ebba | 197 | activate_tempfile(tempfile); |
076aa2cb | 198 | return tempfile; |
354ab112 MH |
199 | } |
200 | ||
2c2db194 RS |
201 | struct tempfile *mks_tempfile_dt(const char *directory_template, |
202 | const char *filename) | |
203 | { | |
204 | struct tempfile *tempfile; | |
205 | const char *tmpdir; | |
206 | struct strbuf sb = STRBUF_INIT; | |
207 | int fd; | |
208 | size_t directorylen; | |
209 | ||
210 | if (!ends_with(directory_template, "XXXXXX")) { | |
211 | errno = EINVAL; | |
212 | return NULL; | |
213 | } | |
214 | ||
215 | tmpdir = getenv("TMPDIR"); | |
216 | if (!tmpdir) | |
217 | tmpdir = "/tmp"; | |
218 | ||
219 | strbuf_addf(&sb, "%s/%s", tmpdir, directory_template); | |
220 | directorylen = sb.len; | |
221 | if (!mkdtemp(sb.buf)) { | |
222 | int orig_errno = errno; | |
223 | strbuf_release(&sb); | |
224 | errno = orig_errno; | |
225 | return NULL; | |
226 | } | |
227 | ||
228 | strbuf_addf(&sb, "/%s", filename); | |
229 | fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600); | |
230 | if (fd < 0) { | |
231 | int orig_errno = errno; | |
232 | strbuf_setlen(&sb, directorylen); | |
233 | rmdir(sb.buf); | |
234 | strbuf_release(&sb); | |
235 | errno = orig_errno; | |
236 | return NULL; | |
237 | } | |
238 | ||
239 | tempfile = new_tempfile(); | |
240 | strbuf_swap(&tempfile->filename, &sb); | |
babe2e05 | 241 | tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen); |
2c2db194 RS |
242 | tempfile->fd = fd; |
243 | activate_tempfile(tempfile); | |
244 | return tempfile; | |
245 | } | |
246 | ||
ea8ace4a | 247 | struct tempfile *xmks_tempfile_m(const char *filename_template, int mode) |
354ab112 | 248 | { |
076aa2cb | 249 | struct tempfile *tempfile; |
354ab112 MH |
250 | struct strbuf full_template = STRBUF_INIT; |
251 | ||
ea8ace4a | 252 | strbuf_add_absolute_path(&full_template, filename_template); |
076aa2cb JK |
253 | tempfile = mks_tempfile_m(full_template.buf, mode); |
254 | if (!tempfile) | |
354ab112 MH |
255 | die_errno("Unable to create temporary file '%s'", |
256 | full_template.buf); | |
257 | ||
258 | strbuf_release(&full_template); | |
076aa2cb | 259 | return tempfile; |
354ab112 MH |
260 | } |
261 | ||
1a9d15db MH |
262 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
263 | { | |
e6fc2673 | 264 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 265 | BUG("fdopen_tempfile() called for inactive object"); |
1a9d15db | 266 | if (tempfile->fp) |
9b028aa4 | 267 | BUG("fdopen_tempfile() called for open object"); |
1a9d15db MH |
268 | |
269 | tempfile->fp = fdopen(tempfile->fd, mode); | |
270 | return tempfile->fp; | |
271 | } | |
272 | ||
273 | const char *get_tempfile_path(struct tempfile *tempfile) | |
274 | { | |
e6fc2673 | 275 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 276 | BUG("get_tempfile_path() called for inactive object"); |
1a9d15db MH |
277 | return tempfile->filename.buf; |
278 | } | |
279 | ||
280 | int get_tempfile_fd(struct tempfile *tempfile) | |
281 | { | |
e6fc2673 | 282 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 283 | BUG("get_tempfile_fd() called for inactive object"); |
1a9d15db MH |
284 | return tempfile->fd; |
285 | } | |
286 | ||
287 | FILE *get_tempfile_fp(struct tempfile *tempfile) | |
288 | { | |
e6fc2673 | 289 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 290 | BUG("get_tempfile_fp() called for inactive object"); |
1a9d15db MH |
291 | return tempfile->fp; |
292 | } | |
293 | ||
49bd0fc2 | 294 | int close_tempfile_gently(struct tempfile *tempfile) |
1a9d15db | 295 | { |
f5b4dc76 JK |
296 | int fd; |
297 | FILE *fp; | |
1a9d15db MH |
298 | int err; |
299 | ||
f5b4dc76 | 300 | if (!is_tempfile_active(tempfile) || tempfile->fd < 0) |
1a9d15db MH |
301 | return 0; |
302 | ||
f5b4dc76 JK |
303 | fd = tempfile->fd; |
304 | fp = tempfile->fp; | |
1a9d15db MH |
305 | tempfile->fd = -1; |
306 | if (fp) { | |
307 | tempfile->fp = NULL; | |
7e8c9355 JK |
308 | if (ferror(fp)) { |
309 | err = -1; | |
310 | if (!fclose(fp)) | |
311 | errno = EIO; | |
312 | } else { | |
313 | err = fclose(fp); | |
314 | } | |
1a9d15db MH |
315 | } else { |
316 | err = close(fd); | |
317 | } | |
318 | ||
49bd0fc2 | 319 | return err ? -1 : 0; |
1a9d15db MH |
320 | } |
321 | ||
322 | int reopen_tempfile(struct tempfile *tempfile) | |
323 | { | |
e6fc2673 | 324 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 325 | BUG("reopen_tempfile called for an inactive object"); |
f5b4dc76 | 326 | if (0 <= tempfile->fd) |
9b028aa4 | 327 | BUG("reopen_tempfile called for an open object"); |
6c003d6f | 328 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC); |
1a9d15db MH |
329 | return tempfile->fd; |
330 | } | |
331 | ||
076aa2cb | 332 | int rename_tempfile(struct tempfile **tempfile_p, const char *path) |
1a9d15db | 333 | { |
076aa2cb JK |
334 | struct tempfile *tempfile = *tempfile_p; |
335 | ||
e6fc2673 | 336 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 337 | BUG("rename_tempfile called for inactive object"); |
1a9d15db | 338 | |
49bd0fc2 | 339 | if (close_tempfile_gently(tempfile)) { |
076aa2cb | 340 | delete_tempfile(tempfile_p); |
1a9d15db | 341 | return -1; |
49bd0fc2 | 342 | } |
1a9d15db MH |
343 | |
344 | if (rename(tempfile->filename.buf, path)) { | |
345 | int save_errno = errno; | |
076aa2cb | 346 | delete_tempfile(tempfile_p); |
1a9d15db MH |
347 | errno = save_errno; |
348 | return -1; | |
349 | } | |
350 | ||
b5f4dcb5 | 351 | deactivate_tempfile(tempfile); |
076aa2cb | 352 | *tempfile_p = NULL; |
1a9d15db MH |
353 | return 0; |
354 | } | |
355 | ||
076aa2cb | 356 | void delete_tempfile(struct tempfile **tempfile_p) |
1a9d15db | 357 | { |
076aa2cb JK |
358 | struct tempfile *tempfile = *tempfile_p; |
359 | ||
e6fc2673 | 360 | if (!is_tempfile_active(tempfile)) |
1a9d15db MH |
361 | return; |
362 | ||
49bd0fc2 JK |
363 | close_tempfile_gently(tempfile); |
364 | unlink_or_warn(tempfile->filename.buf); | |
2c2db194 | 365 | remove_template_directory(tempfile, 0); |
b5f4dcb5 | 366 | deactivate_tempfile(tempfile); |
076aa2cb | 367 | *tempfile_p = NULL; |
1a9d15db | 368 | } |