]>
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 | ||
45 | #include "cache.h" | |
46 | #include "tempfile.h" | |
47 | #include "sigchain.h" | |
48 | ||
24d82185 | 49 | static VOLATILE_LIST_HEAD(tempfile_list); |
1a9d15db | 50 | |
2c2db194 RS |
51 | static void remove_template_directory(struct tempfile *tempfile, |
52 | int in_signal_handler) | |
53 | { | |
babe2e05 | 54 | if (tempfile->directory) { |
2c2db194 | 55 | if (in_signal_handler) |
babe2e05 | 56 | rmdir(tempfile->directory); |
2c2db194 | 57 | else |
babe2e05 | 58 | rmdir_or_warn(tempfile->directory); |
2c2db194 RS |
59 | } |
60 | } | |
61 | ||
6b935066 | 62 | static void remove_tempfiles(int in_signal_handler) |
1a9d15db MH |
63 | { |
64 | pid_t me = getpid(); | |
24d82185 JK |
65 | volatile struct volatile_list_head *pos; |
66 | ||
67 | list_for_each(pos, &tempfile_list) { | |
68 | struct tempfile *p = list_entry(pos, struct tempfile, list); | |
1a9d15db | 69 | |
6b935066 JK |
70 | if (!is_tempfile_active(p) || p->owner != me) |
71 | continue; | |
72 | ||
73 | if (p->fd >= 0) | |
74 | close(p->fd); | |
75 | ||
76 | if (in_signal_handler) | |
77 | unlink(p->filename.buf); | |
78 | else | |
79 | unlink_or_warn(p->filename.buf); | |
2c2db194 | 80 | remove_template_directory(p, in_signal_handler); |
1a9d15db MH |
81 | } |
82 | } | |
83 | ||
84 | static void remove_tempfiles_on_exit(void) | |
85 | { | |
86 | remove_tempfiles(0); | |
87 | } | |
88 | ||
89 | static void remove_tempfiles_on_signal(int signo) | |
90 | { | |
91 | remove_tempfiles(1); | |
92 | sigchain_pop(signo); | |
93 | raise(signo); | |
94 | } | |
95 | ||
076aa2cb | 96 | static struct tempfile *new_tempfile(void) |
1a9d15db | 97 | { |
076aa2cb | 98 | struct tempfile *tempfile = xmalloc(sizeof(*tempfile)); |
422a21c6 JK |
99 | tempfile->fd = -1; |
100 | tempfile->fp = NULL; | |
422a21c6 JK |
101 | tempfile->owner = 0; |
102 | INIT_LIST_HEAD(&tempfile->list); | |
103 | strbuf_init(&tempfile->filename, 0); | |
babe2e05 | 104 | tempfile->directory = NULL; |
076aa2cb | 105 | return tempfile; |
7eba6ce5 MH |
106 | } |
107 | ||
2933ebba JK |
108 | static void activate_tempfile(struct tempfile *tempfile) |
109 | { | |
422a21c6 JK |
110 | static int initialized; |
111 | ||
422a21c6 JK |
112 | if (!initialized) { |
113 | sigchain_push_common(remove_tempfiles_on_signal); | |
114 | atexit(remove_tempfiles_on_exit); | |
115 | initialized = 1; | |
116 | } | |
117 | ||
118 | volatile_list_add(&tempfile->list, &tempfile_list); | |
2933ebba | 119 | tempfile->owner = getpid(); |
2933ebba JK |
120 | } |
121 | ||
b5f4dcb5 JK |
122 | static void deactivate_tempfile(struct tempfile *tempfile) |
123 | { | |
77a42b3b | 124 | volatile_list_del(&tempfile->list); |
102cf7a6 | 125 | strbuf_release(&tempfile->filename); |
babe2e05 | 126 | free(tempfile->directory); |
076aa2cb | 127 | free(tempfile); |
b5f4dcb5 JK |
128 | } |
129 | ||
7eba6ce5 | 130 | /* Make sure errno contains a meaningful value on error */ |
bef0413c | 131 | struct tempfile *create_tempfile_mode(const char *path, int mode) |
7eba6ce5 | 132 | { |
076aa2cb | 133 | struct tempfile *tempfile = new_tempfile(); |
1a9d15db MH |
134 | |
135 | strbuf_add_absolute_path(&tempfile->filename, path); | |
05d1ed61 | 136 | tempfile->fd = open(tempfile->filename.buf, |
bef0413c | 137 | O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
05d1ed61 BW |
138 | if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) |
139 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ | |
140 | tempfile->fd = open(tempfile->filename.buf, | |
bef0413c | 141 | O_RDWR | O_CREAT | O_EXCL, mode); |
1a9d15db | 142 | if (tempfile->fd < 0) { |
b5f4dcb5 | 143 | deactivate_tempfile(tempfile); |
076aa2cb | 144 | return NULL; |
1a9d15db | 145 | } |
2933ebba | 146 | activate_tempfile(tempfile); |
1a9d15db MH |
147 | if (adjust_shared_perm(tempfile->filename.buf)) { |
148 | int save_errno = errno; | |
149 | error("cannot fix permission bits on %s", tempfile->filename.buf); | |
076aa2cb | 150 | delete_tempfile(&tempfile); |
1a9d15db | 151 | errno = save_errno; |
076aa2cb | 152 | return NULL; |
1a9d15db | 153 | } |
076aa2cb JK |
154 | |
155 | return tempfile; | |
1a9d15db MH |
156 | } |
157 | ||
076aa2cb | 158 | struct tempfile *register_tempfile(const char *path) |
99397152 | 159 | { |
076aa2cb | 160 | struct tempfile *tempfile = new_tempfile(); |
99397152 | 161 | strbuf_add_absolute_path(&tempfile->filename, path); |
2933ebba | 162 | activate_tempfile(tempfile); |
076aa2cb | 163 | return tempfile; |
99397152 MH |
164 | } |
165 | ||
ea8ace4a | 166 | struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode) |
354ab112 | 167 | { |
076aa2cb | 168 | struct tempfile *tempfile = new_tempfile(); |
354ab112 | 169 | |
ea8ace4a | 170 | strbuf_add_absolute_path(&tempfile->filename, filename_template); |
354ab112 MH |
171 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
172 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 173 | deactivate_tempfile(tempfile); |
076aa2cb | 174 | return NULL; |
354ab112 | 175 | } |
2933ebba | 176 | activate_tempfile(tempfile); |
076aa2cb | 177 | return tempfile; |
354ab112 MH |
178 | } |
179 | ||
ea8ace4a | 180 | struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode) |
354ab112 | 181 | { |
076aa2cb | 182 | struct tempfile *tempfile = new_tempfile(); |
354ab112 MH |
183 | const char *tmpdir; |
184 | ||
354ab112 MH |
185 | tmpdir = getenv("TMPDIR"); |
186 | if (!tmpdir) | |
187 | tmpdir = "/tmp"; | |
188 | ||
ea8ace4a | 189 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template); |
354ab112 MH |
190 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
191 | if (tempfile->fd < 0) { | |
b5f4dcb5 | 192 | deactivate_tempfile(tempfile); |
076aa2cb | 193 | return NULL; |
354ab112 | 194 | } |
2933ebba | 195 | activate_tempfile(tempfile); |
076aa2cb | 196 | return tempfile; |
354ab112 MH |
197 | } |
198 | ||
2c2db194 RS |
199 | struct tempfile *mks_tempfile_dt(const char *directory_template, |
200 | const char *filename) | |
201 | { | |
202 | struct tempfile *tempfile; | |
203 | const char *tmpdir; | |
204 | struct strbuf sb = STRBUF_INIT; | |
205 | int fd; | |
206 | size_t directorylen; | |
207 | ||
208 | if (!ends_with(directory_template, "XXXXXX")) { | |
209 | errno = EINVAL; | |
210 | return NULL; | |
211 | } | |
212 | ||
213 | tmpdir = getenv("TMPDIR"); | |
214 | if (!tmpdir) | |
215 | tmpdir = "/tmp"; | |
216 | ||
217 | strbuf_addf(&sb, "%s/%s", tmpdir, directory_template); | |
218 | directorylen = sb.len; | |
219 | if (!mkdtemp(sb.buf)) { | |
220 | int orig_errno = errno; | |
221 | strbuf_release(&sb); | |
222 | errno = orig_errno; | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | strbuf_addf(&sb, "/%s", filename); | |
227 | fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600); | |
228 | if (fd < 0) { | |
229 | int orig_errno = errno; | |
230 | strbuf_setlen(&sb, directorylen); | |
231 | rmdir(sb.buf); | |
232 | strbuf_release(&sb); | |
233 | errno = orig_errno; | |
234 | return NULL; | |
235 | } | |
236 | ||
237 | tempfile = new_tempfile(); | |
238 | strbuf_swap(&tempfile->filename, &sb); | |
babe2e05 | 239 | tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen); |
2c2db194 RS |
240 | tempfile->fd = fd; |
241 | activate_tempfile(tempfile); | |
242 | return tempfile; | |
243 | } | |
244 | ||
ea8ace4a | 245 | struct tempfile *xmks_tempfile_m(const char *filename_template, int mode) |
354ab112 | 246 | { |
076aa2cb | 247 | struct tempfile *tempfile; |
354ab112 MH |
248 | struct strbuf full_template = STRBUF_INIT; |
249 | ||
ea8ace4a | 250 | strbuf_add_absolute_path(&full_template, filename_template); |
076aa2cb JK |
251 | tempfile = mks_tempfile_m(full_template.buf, mode); |
252 | if (!tempfile) | |
354ab112 MH |
253 | die_errno("Unable to create temporary file '%s'", |
254 | full_template.buf); | |
255 | ||
256 | strbuf_release(&full_template); | |
076aa2cb | 257 | return tempfile; |
354ab112 MH |
258 | } |
259 | ||
1a9d15db MH |
260 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
261 | { | |
e6fc2673 | 262 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 263 | BUG("fdopen_tempfile() called for inactive object"); |
1a9d15db | 264 | if (tempfile->fp) |
9b028aa4 | 265 | BUG("fdopen_tempfile() called for open object"); |
1a9d15db MH |
266 | |
267 | tempfile->fp = fdopen(tempfile->fd, mode); | |
268 | return tempfile->fp; | |
269 | } | |
270 | ||
271 | const char *get_tempfile_path(struct tempfile *tempfile) | |
272 | { | |
e6fc2673 | 273 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 274 | BUG("get_tempfile_path() called for inactive object"); |
1a9d15db MH |
275 | return tempfile->filename.buf; |
276 | } | |
277 | ||
278 | int get_tempfile_fd(struct tempfile *tempfile) | |
279 | { | |
e6fc2673 | 280 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 281 | BUG("get_tempfile_fd() called for inactive object"); |
1a9d15db MH |
282 | return tempfile->fd; |
283 | } | |
284 | ||
285 | FILE *get_tempfile_fp(struct tempfile *tempfile) | |
286 | { | |
e6fc2673 | 287 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 288 | BUG("get_tempfile_fp() called for inactive object"); |
1a9d15db MH |
289 | return tempfile->fp; |
290 | } | |
291 | ||
49bd0fc2 | 292 | int close_tempfile_gently(struct tempfile *tempfile) |
1a9d15db | 293 | { |
f5b4dc76 JK |
294 | int fd; |
295 | FILE *fp; | |
1a9d15db MH |
296 | int err; |
297 | ||
f5b4dc76 | 298 | if (!is_tempfile_active(tempfile) || tempfile->fd < 0) |
1a9d15db MH |
299 | return 0; |
300 | ||
f5b4dc76 JK |
301 | fd = tempfile->fd; |
302 | fp = tempfile->fp; | |
1a9d15db MH |
303 | tempfile->fd = -1; |
304 | if (fp) { | |
305 | tempfile->fp = NULL; | |
7e8c9355 JK |
306 | if (ferror(fp)) { |
307 | err = -1; | |
308 | if (!fclose(fp)) | |
309 | errno = EIO; | |
310 | } else { | |
311 | err = fclose(fp); | |
312 | } | |
1a9d15db MH |
313 | } else { |
314 | err = close(fd); | |
315 | } | |
316 | ||
49bd0fc2 | 317 | return err ? -1 : 0; |
1a9d15db MH |
318 | } |
319 | ||
320 | int reopen_tempfile(struct tempfile *tempfile) | |
321 | { | |
e6fc2673 | 322 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 323 | BUG("reopen_tempfile called for an inactive object"); |
f5b4dc76 | 324 | if (0 <= tempfile->fd) |
9b028aa4 | 325 | BUG("reopen_tempfile called for an open object"); |
6c003d6f | 326 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC); |
1a9d15db MH |
327 | return tempfile->fd; |
328 | } | |
329 | ||
076aa2cb | 330 | int rename_tempfile(struct tempfile **tempfile_p, const char *path) |
1a9d15db | 331 | { |
076aa2cb JK |
332 | struct tempfile *tempfile = *tempfile_p; |
333 | ||
e6fc2673 | 334 | if (!is_tempfile_active(tempfile)) |
9b028aa4 | 335 | BUG("rename_tempfile called for inactive object"); |
1a9d15db | 336 | |
49bd0fc2 | 337 | if (close_tempfile_gently(tempfile)) { |
076aa2cb | 338 | delete_tempfile(tempfile_p); |
1a9d15db | 339 | return -1; |
49bd0fc2 | 340 | } |
1a9d15db MH |
341 | |
342 | if (rename(tempfile->filename.buf, path)) { | |
343 | int save_errno = errno; | |
076aa2cb | 344 | delete_tempfile(tempfile_p); |
1a9d15db MH |
345 | errno = save_errno; |
346 | return -1; | |
347 | } | |
348 | ||
b5f4dcb5 | 349 | deactivate_tempfile(tempfile); |
076aa2cb | 350 | *tempfile_p = NULL; |
1a9d15db MH |
351 | return 0; |
352 | } | |
353 | ||
076aa2cb | 354 | void delete_tempfile(struct tempfile **tempfile_p) |
1a9d15db | 355 | { |
076aa2cb JK |
356 | struct tempfile *tempfile = *tempfile_p; |
357 | ||
e6fc2673 | 358 | if (!is_tempfile_active(tempfile)) |
1a9d15db MH |
359 | return; |
360 | ||
49bd0fc2 JK |
361 | close_tempfile_gently(tempfile); |
362 | unlink_or_warn(tempfile->filename.buf); | |
2c2db194 | 363 | remove_template_directory(tempfile, 0); |
b5f4dcb5 | 364 | deactivate_tempfile(tempfile); |
076aa2cb | 365 | *tempfile_p = NULL; |
1a9d15db | 366 | } |