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 * - Uninitialized. In this state the object's `on_list` field must be
18 * zero but the rest of its contents need not be initialized. As
19 * soon as the object is used in any way, it is irrevocably
20 * registered in `tempfile_list`, and `on_list` is set.
22 * - Active, file open (after `create_tempfile()` or
23 * `reopen_tempfile()`). In this state:
25 * - the temporary file exists
27 * - `filename` holds the filename of the temporary file
28 * - `fd` holds a file descriptor open for writing to it
29 * - `fp` holds a pointer to an open `FILE` object if and only if
30 * `fdopen_tempfile()` has been called on the object
31 * - `owner` holds the PID of the process that created the file
33 * - Active, file closed (after `close_tempfile_gently()`). Same
34 * as the previous state, except that the temporary file is closed,
35 * `fd` is -1, and `fp` is `NULL`.
37 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
38 * failed attempt to create a temporary file). In this state:
41 * - `filename` is empty (usually, though there are transitory
42 * states in which this condition doesn't hold). Client code should
43 * *not* rely on the filename being empty in this state.
44 * - `fd` is -1 and `fp` is `NULL`
45 * - the object is removed from `tempfile_list` (but could be used again)
47 * A temporary file is owned by the process that created it. The
48 * `tempfile` has an `owner` field that records the owner's PID. This
49 * field is used to prevent a forked process from deleting a temporary
50 * file created by its parent.
57 static VOLATILE_LIST_HEAD(tempfile_list
);
59 static void remove_template_directory(struct tempfile
*tempfile
,
60 int in_signal_handler
)
62 if (tempfile
->directorylen
> 0 &&
63 tempfile
->directorylen
< tempfile
->filename
.len
&&
64 tempfile
->filename
.buf
[tempfile
->directorylen
] == '/') {
65 strbuf_setlen(&tempfile
->filename
, tempfile
->directorylen
);
66 if (in_signal_handler
)
67 rmdir(tempfile
->filename
.buf
);
69 rmdir_or_warn(tempfile
->filename
.buf
);
73 static void remove_tempfiles(int in_signal_handler
)
76 volatile struct volatile_list_head
*pos
;
78 list_for_each(pos
, &tempfile_list
) {
79 struct tempfile
*p
= list_entry(pos
, struct tempfile
, list
);
81 if (!is_tempfile_active(p
) || p
->owner
!= me
)
87 if (in_signal_handler
)
88 unlink(p
->filename
.buf
);
90 unlink_or_warn(p
->filename
.buf
);
91 remove_template_directory(p
, in_signal_handler
);
97 static void remove_tempfiles_on_exit(void)
102 static void remove_tempfiles_on_signal(int signo
)
109 static struct tempfile
*new_tempfile(void)
111 struct tempfile
*tempfile
= xmalloc(sizeof(*tempfile
));
114 tempfile
->active
= 0;
116 INIT_LIST_HEAD(&tempfile
->list
);
117 strbuf_init(&tempfile
->filename
, 0);
118 tempfile
->directorylen
= 0;
122 static void activate_tempfile(struct tempfile
*tempfile
)
124 static int initialized
;
126 if (is_tempfile_active(tempfile
))
127 BUG("activate_tempfile called for active object");
130 sigchain_push_common(remove_tempfiles_on_signal
);
131 atexit(remove_tempfiles_on_exit
);
135 volatile_list_add(&tempfile
->list
, &tempfile_list
);
136 tempfile
->owner
= getpid();
137 tempfile
->active
= 1;
140 static void deactivate_tempfile(struct tempfile
*tempfile
)
142 tempfile
->active
= 0;
143 strbuf_release(&tempfile
->filename
);
144 volatile_list_del(&tempfile
->list
);
148 /* Make sure errno contains a meaningful value on error */
149 struct tempfile
*create_tempfile_mode(const char *path
, int mode
)
151 struct tempfile
*tempfile
= new_tempfile();
153 strbuf_add_absolute_path(&tempfile
->filename
, path
);
154 tempfile
->fd
= open(tempfile
->filename
.buf
,
155 O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, mode
);
156 if (O_CLOEXEC
&& tempfile
->fd
< 0 && errno
== EINVAL
)
157 /* Try again w/o O_CLOEXEC: the kernel might not support it */
158 tempfile
->fd
= open(tempfile
->filename
.buf
,
159 O_RDWR
| O_CREAT
| O_EXCL
, mode
);
160 if (tempfile
->fd
< 0) {
161 deactivate_tempfile(tempfile
);
164 activate_tempfile(tempfile
);
165 if (adjust_shared_perm(tempfile
->filename
.buf
)) {
166 int save_errno
= errno
;
167 error("cannot fix permission bits on %s", tempfile
->filename
.buf
);
168 delete_tempfile(&tempfile
);
176 struct tempfile
*register_tempfile(const char *path
)
178 struct tempfile
*tempfile
= new_tempfile();
179 strbuf_add_absolute_path(&tempfile
->filename
, path
);
180 activate_tempfile(tempfile
);
184 struct tempfile
*mks_tempfile_sm(const char *filename_template
, int suffixlen
, int mode
)
186 struct tempfile
*tempfile
= new_tempfile();
188 strbuf_add_absolute_path(&tempfile
->filename
, filename_template
);
189 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
190 if (tempfile
->fd
< 0) {
191 deactivate_tempfile(tempfile
);
194 activate_tempfile(tempfile
);
198 struct tempfile
*mks_tempfile_tsm(const char *filename_template
, int suffixlen
, int mode
)
200 struct tempfile
*tempfile
= new_tempfile();
203 tmpdir
= getenv("TMPDIR");
207 strbuf_addf(&tempfile
->filename
, "%s/%s", tmpdir
, filename_template
);
208 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
209 if (tempfile
->fd
< 0) {
210 deactivate_tempfile(tempfile
);
213 activate_tempfile(tempfile
);
217 struct tempfile
*mks_tempfile_dt(const char *directory_template
,
218 const char *filename
)
220 struct tempfile
*tempfile
;
222 struct strbuf sb
= STRBUF_INIT
;
226 if (!ends_with(directory_template
, "XXXXXX")) {
231 tmpdir
= getenv("TMPDIR");
235 strbuf_addf(&sb
, "%s/%s", tmpdir
, directory_template
);
236 directorylen
= sb
.len
;
237 if (!mkdtemp(sb
.buf
)) {
238 int orig_errno
= errno
;
244 strbuf_addf(&sb
, "/%s", filename
);
245 fd
= open(sb
.buf
, O_CREAT
| O_EXCL
| O_RDWR
, 0600);
247 int orig_errno
= errno
;
248 strbuf_setlen(&sb
, directorylen
);
255 tempfile
= new_tempfile();
256 strbuf_swap(&tempfile
->filename
, &sb
);
257 tempfile
->directorylen
= directorylen
;
259 activate_tempfile(tempfile
);
263 struct tempfile
*xmks_tempfile_m(const char *filename_template
, int mode
)
265 struct tempfile
*tempfile
;
266 struct strbuf full_template
= STRBUF_INIT
;
268 strbuf_add_absolute_path(&full_template
, filename_template
);
269 tempfile
= mks_tempfile_m(full_template
.buf
, mode
);
271 die_errno("Unable to create temporary file '%s'",
274 strbuf_release(&full_template
);
278 FILE *fdopen_tempfile(struct tempfile
*tempfile
, const char *mode
)
280 if (!is_tempfile_active(tempfile
))
281 BUG("fdopen_tempfile() called for inactive object");
283 BUG("fdopen_tempfile() called for open object");
285 tempfile
->fp
= fdopen(tempfile
->fd
, mode
);
289 const char *get_tempfile_path(struct tempfile
*tempfile
)
291 if (!is_tempfile_active(tempfile
))
292 BUG("get_tempfile_path() called for inactive object");
293 return tempfile
->filename
.buf
;
296 int get_tempfile_fd(struct tempfile
*tempfile
)
298 if (!is_tempfile_active(tempfile
))
299 BUG("get_tempfile_fd() called for inactive object");
303 FILE *get_tempfile_fp(struct tempfile
*tempfile
)
305 if (!is_tempfile_active(tempfile
))
306 BUG("get_tempfile_fp() called for inactive object");
310 int close_tempfile_gently(struct tempfile
*tempfile
)
316 if (!is_tempfile_active(tempfile
) || tempfile
->fd
< 0)
338 int reopen_tempfile(struct tempfile
*tempfile
)
340 if (!is_tempfile_active(tempfile
))
341 BUG("reopen_tempfile called for an inactive object");
342 if (0 <= tempfile
->fd
)
343 BUG("reopen_tempfile called for an open object");
344 tempfile
->fd
= open(tempfile
->filename
.buf
, O_WRONLY
|O_TRUNC
);
348 int rename_tempfile(struct tempfile
**tempfile_p
, const char *path
)
350 struct tempfile
*tempfile
= *tempfile_p
;
352 if (!is_tempfile_active(tempfile
))
353 BUG("rename_tempfile called for inactive object");
355 if (close_tempfile_gently(tempfile
)) {
356 delete_tempfile(tempfile_p
);
360 if (rename(tempfile
->filename
.buf
, path
)) {
361 int save_errno
= errno
;
362 delete_tempfile(tempfile_p
);
367 deactivate_tempfile(tempfile
);
372 void delete_tempfile(struct tempfile
**tempfile_p
)
374 struct tempfile
*tempfile
= *tempfile_p
;
376 if (!is_tempfile_active(tempfile
))
379 close_tempfile_gently(tempfile
);
380 unlink_or_warn(tempfile
->filename
.buf
);
381 remove_template_directory(tempfile
, 0);
382 deactivate_tempfile(tempfile
);