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 struct tempfile *new_tempfile(void)
96 struct tempfile *tempfile = xmalloc(sizeof(*tempfile));
101 INIT_LIST_HEAD(&tempfile->list);
102 strbuf_init(&tempfile->filename, 0);
106 static void activate_tempfile(struct tempfile *tempfile)
108 static int initialized;
110 if (is_tempfile_active(tempfile))
111 BUG("activate_tempfile called for active object");
114 sigchain_push_common(remove_tempfiles_on_signal);
115 atexit(remove_tempfiles_on_exit);
119 volatile_list_add(&tempfile->list, &tempfile_list);
120 tempfile->owner = getpid();
121 tempfile->active = 1;
124 static void deactivate_tempfile(struct tempfile *tempfile)
126 tempfile->active = 0;
127 strbuf_release(&tempfile->filename);
128 volatile_list_del(&tempfile->list);
132 /* Make sure errno contains a meaningful value on error */
133 struct tempfile *create_tempfile(const char *path)
135 struct tempfile *tempfile = new_tempfile();
137 strbuf_add_absolute_path(&tempfile->filename, path);
138 tempfile->fd = open(tempfile->filename.buf,
139 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
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,
143 O_RDWR | O_CREAT | O_EXCL, 0666);
144 if (tempfile->fd < 0) {
145 deactivate_tempfile(tempfile);
148 activate_tempfile(tempfile);
149 if (adjust_shared_perm(tempfile->filename.buf)) {
150 int save_errno = errno;
151 error("cannot fix permission bits on %s", tempfile->filename.buf);
152 delete_tempfile(&tempfile);
160 struct tempfile *register_tempfile(const char *path)
162 struct tempfile *tempfile = new_tempfile();
163 strbuf_add_absolute_path(&tempfile->filename, path);
164 activate_tempfile(tempfile);
168 struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode)
170 struct tempfile *tempfile = new_tempfile();
172 strbuf_add_absolute_path(&tempfile->filename, filename_template);
173 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
174 if (tempfile->fd < 0) {
175 deactivate_tempfile(tempfile);
178 activate_tempfile(tempfile);
182 struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode)
184 struct tempfile *tempfile = new_tempfile();
187 tmpdir = getenv("TMPDIR");
191 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template);
192 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
193 if (tempfile->fd < 0) {
194 deactivate_tempfile(tempfile);
197 activate_tempfile(tempfile);
201 struct tempfile *xmks_tempfile_m(const char *filename_template, int mode)
203 struct tempfile *tempfile;
204 struct strbuf full_template = STRBUF_INIT;
206 strbuf_add_absolute_path(&full_template, filename_template);
207 tempfile = mks_tempfile_m(full_template.buf, mode);
209 die_errno("Unable to create temporary file '%s'",
212 strbuf_release(&full_template);
216 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
218 if (!is_tempfile_active(tempfile))
219 BUG("fdopen_tempfile() called for inactive object");
221 BUG("fdopen_tempfile() called for open object");
223 tempfile->fp = fdopen(tempfile->fd, mode);
227 const char *get_tempfile_path(struct tempfile *tempfile)
229 if (!is_tempfile_active(tempfile))
230 BUG("get_tempfile_path() called for inactive object");
231 return tempfile->filename.buf;
234 int get_tempfile_fd(struct tempfile *tempfile)
236 if (!is_tempfile_active(tempfile))
237 BUG("get_tempfile_fd() called for inactive object");
241 FILE *get_tempfile_fp(struct tempfile *tempfile)
243 if (!is_tempfile_active(tempfile))
244 BUG("get_tempfile_fp() called for inactive object");
248 int close_tempfile_gently(struct tempfile *tempfile)
254 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
276 int reopen_tempfile(struct tempfile *tempfile)
278 if (!is_tempfile_active(tempfile))
279 BUG("reopen_tempfile called for an inactive object");
280 if (0 <= tempfile->fd)
281 BUG("reopen_tempfile called for an open object");
282 tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC);
286 int rename_tempfile(struct tempfile **tempfile_p, const char *path)
288 struct tempfile *tempfile = *tempfile_p;
290 if (!is_tempfile_active(tempfile))
291 BUG("rename_tempfile called for inactive object");
293 if (close_tempfile_gently(tempfile)) {
294 delete_tempfile(tempfile_p);
298 if (rename(tempfile->filename.buf, path)) {
299 int save_errno = errno;
300 delete_tempfile(tempfile_p);
305 deactivate_tempfile(tempfile);
310 void delete_tempfile(struct tempfile **tempfile_p)
312 struct tempfile *tempfile = *tempfile_p;
314 if (!is_tempfile_active(tempfile))
317 close_tempfile_gently(tempfile);
318 unlink_or_warn(tempfile->filename.buf);
319 deactivate_tempfile(tempfile);