2 * State diagram and cleanup
3 * -------------------------
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
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.
15 * The possible states of a `tempfile` object are as follows:
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.
21 * - Active, file open (after `create_tempfile()` or
22 * `reopen_tempfile()`). In this state:
24 * - the temporary file exists
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
31 * - Active, file closed (after `close_tempfile_gently()`). Same
32 * as the previous state, except that the temporary file is closed,
33 * `fd` is -1, and `fp` is `NULL`.
35 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
36 * failed attempt to create a temporary file). The struct is removed from
37 * the global tempfile_list and deallocated.
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.
49 static VOLATILE_LIST_HEAD(tempfile_list
);
51 static void remove_template_directory(struct tempfile
*tempfile
,
52 int in_signal_handler
)
54 if (tempfile
->directory
) {
55 if (in_signal_handler
)
56 rmdir(tempfile
->directory
);
58 rmdir_or_warn(tempfile
->directory
);
62 static void remove_tempfiles(int in_signal_handler
)
65 volatile struct volatile_list_head
*pos
;
67 list_for_each(pos
, &tempfile_list
) {
68 struct tempfile
*p
= list_entry(pos
, struct tempfile
, list
);
70 if (!is_tempfile_active(p
) || p
->owner
!= me
)
76 if (in_signal_handler
)
77 unlink(p
->filename
.buf
);
79 unlink_or_warn(p
->filename
.buf
);
80 remove_template_directory(p
, in_signal_handler
);
84 static void remove_tempfiles_on_exit(void)
89 static void remove_tempfiles_on_signal(int signo
)
96 static struct tempfile
*new_tempfile(void)
98 struct tempfile
*tempfile
= xmalloc(sizeof(*tempfile
));
102 INIT_LIST_HEAD(&tempfile
->list
);
103 strbuf_init(&tempfile
->filename
, 0);
104 tempfile
->directory
= NULL
;
108 static void activate_tempfile(struct tempfile
*tempfile
)
110 static int initialized
;
113 sigchain_push_common(remove_tempfiles_on_signal
);
114 atexit(remove_tempfiles_on_exit
);
118 volatile_list_add(&tempfile
->list
, &tempfile_list
);
119 tempfile
->owner
= getpid();
122 static void deactivate_tempfile(struct tempfile
*tempfile
)
124 volatile_list_del(&tempfile
->list
);
125 strbuf_release(&tempfile
->filename
);
126 free(tempfile
->directory
);
130 /* Make sure errno contains a meaningful value on error */
131 struct tempfile
*create_tempfile_mode(const char *path
, int mode
)
133 struct tempfile
*tempfile
= new_tempfile();
135 strbuf_add_absolute_path(&tempfile
->filename
, path
);
136 tempfile
->fd
= open(tempfile
->filename
.buf
,
137 O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, mode
);
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
,
141 O_RDWR
| O_CREAT
| O_EXCL
, mode
);
142 if (tempfile
->fd
< 0) {
143 deactivate_tempfile(tempfile
);
146 activate_tempfile(tempfile
);
147 if (adjust_shared_perm(tempfile
->filename
.buf
)) {
148 int save_errno
= errno
;
149 error("cannot fix permission bits on %s", tempfile
->filename
.buf
);
150 delete_tempfile(&tempfile
);
158 struct tempfile
*register_tempfile(const char *path
)
160 struct tempfile
*tempfile
= new_tempfile();
161 strbuf_add_absolute_path(&tempfile
->filename
, path
);
162 activate_tempfile(tempfile
);
166 struct tempfile
*mks_tempfile_sm(const char *filename_template
, int suffixlen
, int mode
)
168 struct tempfile
*tempfile
= new_tempfile();
170 strbuf_add_absolute_path(&tempfile
->filename
, filename_template
);
171 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
172 if (tempfile
->fd
< 0) {
173 deactivate_tempfile(tempfile
);
176 activate_tempfile(tempfile
);
180 struct tempfile
*mks_tempfile_tsm(const char *filename_template
, int suffixlen
, int mode
)
182 struct tempfile
*tempfile
= new_tempfile();
185 tmpdir
= getenv("TMPDIR");
189 strbuf_addf(&tempfile
->filename
, "%s/%s", tmpdir
, filename_template
);
190 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
191 if (tempfile
->fd
< 0) {
192 deactivate_tempfile(tempfile
);
195 activate_tempfile(tempfile
);
199 struct tempfile
*mks_tempfile_dt(const char *directory_template
,
200 const char *filename
)
202 struct tempfile
*tempfile
;
204 struct strbuf sb
= STRBUF_INIT
;
208 if (!ends_with(directory_template
, "XXXXXX")) {
213 tmpdir
= getenv("TMPDIR");
217 strbuf_addf(&sb
, "%s/%s", tmpdir
, directory_template
);
218 directorylen
= sb
.len
;
219 if (!mkdtemp(sb
.buf
)) {
220 int orig_errno
= errno
;
226 strbuf_addf(&sb
, "/%s", filename
);
227 fd
= open(sb
.buf
, O_CREAT
| O_EXCL
| O_RDWR
, 0600);
229 int orig_errno
= errno
;
230 strbuf_setlen(&sb
, directorylen
);
237 tempfile
= new_tempfile();
238 strbuf_swap(&tempfile
->filename
, &sb
);
239 tempfile
->directory
= xmemdupz(tempfile
->filename
.buf
, directorylen
);
241 activate_tempfile(tempfile
);
245 struct tempfile
*xmks_tempfile_m(const char *filename_template
, int mode
)
247 struct tempfile
*tempfile
;
248 struct strbuf full_template
= STRBUF_INIT
;
250 strbuf_add_absolute_path(&full_template
, filename_template
);
251 tempfile
= mks_tempfile_m(full_template
.buf
, mode
);
253 die_errno("Unable to create temporary file '%s'",
256 strbuf_release(&full_template
);
260 FILE *fdopen_tempfile(struct tempfile
*tempfile
, const char *mode
)
262 if (!is_tempfile_active(tempfile
))
263 BUG("fdopen_tempfile() called for inactive object");
265 BUG("fdopen_tempfile() called for open object");
267 tempfile
->fp
= fdopen(tempfile
->fd
, mode
);
271 const char *get_tempfile_path(struct tempfile
*tempfile
)
273 if (!is_tempfile_active(tempfile
))
274 BUG("get_tempfile_path() called for inactive object");
275 return tempfile
->filename
.buf
;
278 int get_tempfile_fd(struct tempfile
*tempfile
)
280 if (!is_tempfile_active(tempfile
))
281 BUG("get_tempfile_fd() called for inactive object");
285 FILE *get_tempfile_fp(struct tempfile
*tempfile
)
287 if (!is_tempfile_active(tempfile
))
288 BUG("get_tempfile_fp() called for inactive object");
292 int close_tempfile_gently(struct tempfile
*tempfile
)
298 if (!is_tempfile_active(tempfile
) || tempfile
->fd
< 0)
320 int reopen_tempfile(struct tempfile
*tempfile
)
322 if (!is_tempfile_active(tempfile
))
323 BUG("reopen_tempfile called for an inactive object");
324 if (0 <= tempfile
->fd
)
325 BUG("reopen_tempfile called for an open object");
326 tempfile
->fd
= open(tempfile
->filename
.buf
, O_WRONLY
|O_TRUNC
);
330 int rename_tempfile(struct tempfile
**tempfile_p
, const char *path
)
332 struct tempfile
*tempfile
= *tempfile_p
;
334 if (!is_tempfile_active(tempfile
))
335 BUG("rename_tempfile called for inactive object");
337 if (close_tempfile_gently(tempfile
)) {
338 delete_tempfile(tempfile_p
);
342 if (rename(tempfile
->filename
.buf
, path
)) {
343 int save_errno
= errno
;
344 delete_tempfile(tempfile_p
);
349 deactivate_tempfile(tempfile
);
354 void delete_tempfile(struct tempfile
**tempfile_p
)
356 struct tempfile
*tempfile
= *tempfile_p
;
358 if (!is_tempfile_active(tempfile
))
361 close_tempfile_gently(tempfile
);
362 unlink_or_warn(tempfile
->filename
.buf
);
363 remove_template_directory(tempfile
, 0);
364 deactivate_tempfile(tempfile
);