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 left registered in the `tempfile_list`, and
48 * A temporary file is owned by the process that created it. The
49 * `tempfile` has an `owner` field that records the owner's PID. This
50 * field is used to prevent a forked process from deleting a temporary
51 * file created by its parent.
58 static struct tempfile *volatile tempfile_list;
60 static void remove_tempfiles(int skip_fclose)
64 while (tempfile_list) {
65 if (tempfile_list->owner == me) {
66 /* fclose() is not safe to call in a signal handler */
68 tempfile_list->fp = NULL;
69 delete_tempfile(tempfile_list);
71 tempfile_list = tempfile_list->next;
75 static void remove_tempfiles_on_exit(void)
80 static void remove_tempfiles_on_signal(int signo)
88 * Initialize *tempfile if necessary and add it to tempfile_list.
90 static void prepare_tempfile_object(struct tempfile *tempfile)
93 /* One-time initialization */
94 sigchain_push_common(remove_tempfiles_on_signal);
95 atexit(remove_tempfiles_on_exit);
99 die("BUG: prepare_tempfile_object called for active object");
100 if (!tempfile->on_list) {
101 /* Initialize *tempfile and add it to tempfile_list: */
104 tempfile->active = 0;
106 strbuf_init(&tempfile->filename, 0);
107 tempfile->next = tempfile_list;
108 tempfile_list = tempfile;
109 tempfile->on_list = 1;
110 } else if (tempfile->filename.len) {
111 /* This shouldn't happen, but better safe than sorry. */
112 die("BUG: prepare_tempfile_object called for improperly-reset object");
116 /* Make sure errno contains a meaningful value on error */
117 int create_tempfile(struct tempfile *tempfile, const char *path)
119 prepare_tempfile_object(tempfile);
121 strbuf_add_absolute_path(&tempfile->filename, path);
122 tempfile->fd = open(tempfile->filename.buf,
123 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
124 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
125 /* Try again w/o O_CLOEXEC: the kernel might not support it */
126 tempfile->fd = open(tempfile->filename.buf,
127 O_RDWR | O_CREAT | O_EXCL, 0666);
128 if (tempfile->fd < 0) {
129 strbuf_reset(&tempfile->filename);
132 tempfile->owner = getpid();
133 tempfile->active = 1;
134 if (adjust_shared_perm(tempfile->filename.buf)) {
135 int save_errno = errno;
136 error("cannot fix permission bits on %s", tempfile->filename.buf);
137 delete_tempfile(tempfile);
144 void register_tempfile(struct tempfile *tempfile, const char *path)
146 prepare_tempfile_object(tempfile);
147 strbuf_add_absolute_path(&tempfile->filename, path);
148 tempfile->owner = getpid();
149 tempfile->active = 1;
152 int mks_tempfile_sm(struct tempfile *tempfile,
153 const char *template, int suffixlen, int mode)
155 prepare_tempfile_object(tempfile);
157 strbuf_add_absolute_path(&tempfile->filename, template);
158 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
159 if (tempfile->fd < 0) {
160 strbuf_reset(&tempfile->filename);
163 tempfile->owner = getpid();
164 tempfile->active = 1;
168 int mks_tempfile_tsm(struct tempfile *tempfile,
169 const char *template, int suffixlen, int mode)
173 prepare_tempfile_object(tempfile);
175 tmpdir = getenv("TMPDIR");
179 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
180 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
181 if (tempfile->fd < 0) {
182 strbuf_reset(&tempfile->filename);
185 tempfile->owner = getpid();
186 tempfile->active = 1;
190 int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
193 struct strbuf full_template = STRBUF_INIT;
195 strbuf_add_absolute_path(&full_template, template);
196 fd = mks_tempfile_m(tempfile, full_template.buf, mode);
198 die_errno("Unable to create temporary file '%s'",
201 strbuf_release(&full_template);
205 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
207 if (!tempfile->active)
208 die("BUG: fdopen_tempfile() called for inactive object");
210 die("BUG: fdopen_tempfile() called for open object");
212 tempfile->fp = fdopen(tempfile->fd, mode);
216 const char *get_tempfile_path(struct tempfile *tempfile)
218 if (!tempfile->active)
219 die("BUG: get_tempfile_path() called for inactive object");
220 return tempfile->filename.buf;
223 int get_tempfile_fd(struct tempfile *tempfile)
225 if (!tempfile->active)
226 die("BUG: get_tempfile_fd() called for inactive object");
230 FILE *get_tempfile_fp(struct tempfile *tempfile)
232 if (!tempfile->active)
233 die("BUG: get_tempfile_fp() called for inactive object");
237 int close_tempfile_gently(struct tempfile *tempfile)
239 int fd = tempfile->fd;
240 FILE *fp = tempfile->fp;
263 int reopen_tempfile(struct tempfile *tempfile)
265 if (0 <= tempfile->fd)
266 die("BUG: reopen_tempfile called for an open object");
267 if (!tempfile->active)
268 die("BUG: reopen_tempfile called for an inactive object");
269 tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
273 int rename_tempfile(struct tempfile *tempfile, const char *path)
275 if (!tempfile->active)
276 die("BUG: rename_tempfile called for inactive object");
278 if (close_tempfile_gently(tempfile)) {
279 delete_tempfile(tempfile);
283 if (rename(tempfile->filename.buf, path)) {
284 int save_errno = errno;
285 delete_tempfile(tempfile);
290 tempfile->active = 0;
291 strbuf_reset(&tempfile->filename);
295 void delete_tempfile(struct tempfile *tempfile)
297 if (!tempfile->active)
300 close_tempfile_gently(tempfile);
301 unlink_or_warn(tempfile->filename.buf);
302 tempfile->active = 0;
303 strbuf_reset(&tempfile->filename);