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_tempfiles(int in_signal_handler
)
62 volatile struct volatile_list_head
*pos
;
64 list_for_each(pos
, &tempfile_list
) {
65 struct tempfile
*p
= list_entry(pos
, struct tempfile
, list
);
67 if (!is_tempfile_active(p
) || p
->owner
!= me
)
73 if (in_signal_handler
)
74 unlink(p
->filename
.buf
);
76 unlink_or_warn(p
->filename
.buf
);
82 static void remove_tempfiles_on_exit(void)
87 static void remove_tempfiles_on_signal(int signo
)
94 static void prepare_tempfile_object(struct tempfile
*tempfile
)
100 INIT_LIST_HEAD(&tempfile
->list
);
101 strbuf_init(&tempfile
->filename
, 0);
104 static void activate_tempfile(struct tempfile
*tempfile
)
106 static int initialized
;
108 if (is_tempfile_active(tempfile
))
109 BUG("activate_tempfile called for active object");
112 sigchain_push_common(remove_tempfiles_on_signal
);
113 atexit(remove_tempfiles_on_exit
);
117 volatile_list_add(&tempfile
->list
, &tempfile_list
);
118 tempfile
->owner
= getpid();
119 tempfile
->active
= 1;
122 static void deactivate_tempfile(struct tempfile
*tempfile
)
124 tempfile
->active
= 0;
125 strbuf_release(&tempfile
->filename
);
126 volatile_list_del(&tempfile
->list
);
129 /* Make sure errno contains a meaningful value on error */
130 int create_tempfile(struct tempfile
*tempfile
, const char *path
)
132 prepare_tempfile_object(tempfile
);
134 strbuf_add_absolute_path(&tempfile
->filename
, path
);
135 tempfile
->fd
= open(tempfile
->filename
.buf
,
136 O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, 0666);
137 if (O_CLOEXEC
&& tempfile
->fd
< 0 && errno
== EINVAL
)
138 /* Try again w/o O_CLOEXEC: the kernel might not support it */
139 tempfile
->fd
= open(tempfile
->filename
.buf
,
140 O_RDWR
| O_CREAT
| O_EXCL
, 0666);
141 if (tempfile
->fd
< 0) {
142 deactivate_tempfile(tempfile
);
145 activate_tempfile(tempfile
);
146 if (adjust_shared_perm(tempfile
->filename
.buf
)) {
147 int save_errno
= errno
;
148 error("cannot fix permission bits on %s", tempfile
->filename
.buf
);
149 delete_tempfile(tempfile
);
156 void register_tempfile(struct tempfile
*tempfile
, const char *path
)
158 prepare_tempfile_object(tempfile
);
159 strbuf_add_absolute_path(&tempfile
->filename
, path
);
160 activate_tempfile(tempfile
);
163 int mks_tempfile_sm(struct tempfile
*tempfile
,
164 const char *template, int suffixlen
, int mode
)
166 prepare_tempfile_object(tempfile
);
168 strbuf_add_absolute_path(&tempfile
->filename
, template);
169 tempfile
->fd
= git_mkstemps_mode(tempfile
->filename
.buf
, suffixlen
, mode
);
170 if (tempfile
->fd
< 0) {
171 deactivate_tempfile(tempfile
);
174 activate_tempfile(tempfile
);
178 int mks_tempfile_tsm(struct tempfile
*tempfile
,
179 const char *template, int suffixlen
, int mode
)
183 prepare_tempfile_object(tempfile
);
185 tmpdir
= getenv("TMPDIR");
189 strbuf_addf(&tempfile
->filename
, "%s/%s", tmpdir
, 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 int xmks_tempfile_m(struct tempfile
*tempfile
, const char *template, int mode
)
202 struct strbuf full_template
= STRBUF_INIT
;
204 strbuf_add_absolute_path(&full_template
, template);
205 fd
= mks_tempfile_m(tempfile
, full_template
.buf
, mode
);
207 die_errno("Unable to create temporary file '%s'",
210 strbuf_release(&full_template
);
214 FILE *fdopen_tempfile(struct tempfile
*tempfile
, const char *mode
)
216 if (!is_tempfile_active(tempfile
))
217 BUG("fdopen_tempfile() called for inactive object");
219 BUG("fdopen_tempfile() called for open object");
221 tempfile
->fp
= fdopen(tempfile
->fd
, mode
);
225 const char *get_tempfile_path(struct tempfile
*tempfile
)
227 if (!is_tempfile_active(tempfile
))
228 BUG("get_tempfile_path() called for inactive object");
229 return tempfile
->filename
.buf
;
232 int get_tempfile_fd(struct tempfile
*tempfile
)
234 if (!is_tempfile_active(tempfile
))
235 BUG("get_tempfile_fd() called for inactive object");
239 FILE *get_tempfile_fp(struct tempfile
*tempfile
)
241 if (!is_tempfile_active(tempfile
))
242 BUG("get_tempfile_fp() called for inactive object");
246 int close_tempfile_gently(struct tempfile
*tempfile
)
252 if (!is_tempfile_active(tempfile
) || tempfile
->fd
< 0)
274 int reopen_tempfile(struct tempfile
*tempfile
)
276 if (!is_tempfile_active(tempfile
))
277 BUG("reopen_tempfile called for an inactive object");
278 if (0 <= tempfile
->fd
)
279 BUG("reopen_tempfile called for an open object");
280 tempfile
->fd
= open(tempfile
->filename
.buf
, O_WRONLY
);
284 int rename_tempfile(struct tempfile
*tempfile
, const char *path
)
286 if (!is_tempfile_active(tempfile
))
287 BUG("rename_tempfile called for inactive object");
289 if (close_tempfile_gently(tempfile
)) {
290 delete_tempfile(tempfile
);
294 if (rename(tempfile
->filename
.buf
, path
)) {
295 int save_errno
= errno
;
296 delete_tempfile(tempfile
);
301 deactivate_tempfile(tempfile
);
305 void delete_tempfile(struct tempfile
*tempfile
)
307 if (!is_tempfile_active(tempfile
))
310 close_tempfile_gently(tempfile
);
311 unlink_or_warn(tempfile
->filename
.buf
);
312 deactivate_tempfile(tempfile
);