blob: ed88cf84314753443c8c42f0043320957d65d165 [file] [log] [blame]
Michael Haggerty1a9d15d2015-08-10 11:47:41 +02001/*
2 * State diagram and cleanup
3 * -------------------------
4 *
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
9 * temporary files.
10 *
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.
14 *
15 * The possible states of a `tempfile` object are as follows:
16 *
Jeff King320fa572022-08-30 15:46:01 -040017 * - 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.
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020020 *
21 * - Active, file open (after `create_tempfile()` or
22 * `reopen_tempfile()`). In this state:
23 *
24 * - the temporary file exists
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020025 * - `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
30 *
Jeff King49bd0fc2017-09-05 08:14:30 -040031 * - Active, file closed (after `close_tempfile_gently()`). Same
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020032 * as the previous state, except that the temporary file is closed,
33 * `fd` is -1, and `fp` is `NULL`.
34 *
Jeff King49bd0fc2017-09-05 08:14:30 -040035 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
Jeff King320fa572022-08-30 15:46:01 -040036 * failed attempt to create a temporary file). The struct is removed from
37 * the global tempfile_list and deallocated.
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020038 *
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.
43 */
44
Elijah Newrena64acf72023-03-21 06:26:02 +000045#include "git-compat-util.h"
Calvin Wan5d1344b2023-06-06 19:48:39 +000046#include "abspath.h"
Elijah Newrena64acf72023-03-21 06:26:02 +000047#include "path.h"
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020048#include "tempfile.h"
49#include "sigchain.h"
50
Jeff King24d82182017-09-05 08:15:00 -040051static VOLATILE_LIST_HEAD(tempfile_list);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020052
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +010053static int remove_template_directory(struct tempfile *tempfile,
René Scharfe2c2db192022-04-20 22:26:09 +020054 int in_signal_handler)
55{
René Scharfebabe2e02022-08-27 00:46:29 +020056 if (tempfile->directory) {
René Scharfe2c2db192022-04-20 22:26:09 +020057 if (in_signal_handler)
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +010058 return rmdir(tempfile->directory);
René Scharfe2c2db192022-04-20 22:26:09 +020059 else
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +010060 return rmdir_or_warn(tempfile->directory);
René Scharfe2c2db192022-04-20 22:26:09 +020061 }
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +010062
63 return 0;
René Scharfe2c2db192022-04-20 22:26:09 +020064}
65
Jeff King6b935062017-09-05 08:14:53 -040066static void remove_tempfiles(int in_signal_handler)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020067{
68 pid_t me = getpid();
Jeff King24d82182017-09-05 08:15:00 -040069 volatile struct volatile_list_head *pos;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020070
Jeff King24d82182017-09-05 08:15:00 -040071 list_for_each(pos, &tempfile_list) {
72 struct tempfile *p = list_entry(pos, struct tempfile, list);
73
Jeff King6b935062017-09-05 08:14:53 -040074 if (!is_tempfile_active(p) || p->owner != me)
75 continue;
76
77 if (p->fd >= 0)
78 close(p->fd);
79
80 if (in_signal_handler)
81 unlink(p->filename.buf);
82 else
83 unlink_or_warn(p->filename.buf);
René Scharfe2c2db192022-04-20 22:26:09 +020084 remove_template_directory(p, in_signal_handler);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +020085 }
86}
87
88static void remove_tempfiles_on_exit(void)
89{
90 remove_tempfiles(0);
91}
92
93static void remove_tempfiles_on_signal(int signo)
94{
95 remove_tempfiles(1);
96 sigchain_pop(signo);
97 raise(signo);
98}
99
Jeff King076aa2c2017-09-05 08:15:08 -0400100static struct tempfile *new_tempfile(void)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200101{
Jeff King076aa2c2017-09-05 08:15:08 -0400102 struct tempfile *tempfile = xmalloc(sizeof(*tempfile));
Jeff King422a21c2017-09-05 08:15:04 -0400103 tempfile->fd = -1;
104 tempfile->fp = NULL;
Jeff King422a21c2017-09-05 08:15:04 -0400105 tempfile->owner = 0;
106 INIT_LIST_HEAD(&tempfile->list);
107 strbuf_init(&tempfile->filename, 0);
René Scharfebabe2e02022-08-27 00:46:29 +0200108 tempfile->directory = NULL;
Jeff King076aa2c2017-09-05 08:15:08 -0400109 return tempfile;
Michael Haggerty7eba6ce2015-08-10 11:47:42 +0200110}
111
Jeff King2933ebb2017-09-05 08:14:47 -0400112static void activate_tempfile(struct tempfile *tempfile)
113{
Jeff King422a21c2017-09-05 08:15:04 -0400114 static int initialized;
115
Jeff King422a21c2017-09-05 08:15:04 -0400116 if (!initialized) {
117 sigchain_push_common(remove_tempfiles_on_signal);
118 atexit(remove_tempfiles_on_exit);
119 initialized = 1;
120 }
121
122 volatile_list_add(&tempfile->list, &tempfile_list);
Jeff King2933ebb2017-09-05 08:14:47 -0400123 tempfile->owner = getpid();
Jeff King2933ebb2017-09-05 08:14:47 -0400124}
125
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400126static void deactivate_tempfile(struct tempfile *tempfile)
127{
Jeff King77a42b32022-08-30 15:45:06 -0400128 volatile_list_del(&tempfile->list);
Jeff King102cf7a2017-09-05 08:14:56 -0400129 strbuf_release(&tempfile->filename);
René Scharfebabe2e02022-08-27 00:46:29 +0200130 free(tempfile->directory);
Jeff King076aa2c2017-09-05 08:15:08 -0400131 free(tempfile);
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400132}
133
Michael Haggerty7eba6ce2015-08-10 11:47:42 +0200134/* Make sure errno contains a meaningful value on error */
Taylor Blaubef04132020-04-27 10:27:54 -0600135struct tempfile *create_tempfile_mode(const char *path, int mode)
Michael Haggerty7eba6ce2015-08-10 11:47:42 +0200136{
Jeff King076aa2c2017-09-05 08:15:08 -0400137 struct tempfile *tempfile = new_tempfile();
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200138
139 strbuf_add_absolute_path(&tempfile->filename, path);
Ben Wijen05d1ed62016-08-22 14:47:55 +0200140 tempfile->fd = open(tempfile->filename.buf,
Taylor Blaubef04132020-04-27 10:27:54 -0600141 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Ben Wijen05d1ed62016-08-22 14:47:55 +0200142 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
143 /* Try again w/o O_CLOEXEC: the kernel might not support it */
144 tempfile->fd = open(tempfile->filename.buf,
Taylor Blaubef04132020-04-27 10:27:54 -0600145 O_RDWR | O_CREAT | O_EXCL, mode);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200146 if (tempfile->fd < 0) {
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400147 deactivate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400148 return NULL;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200149 }
Jeff King2933ebb2017-09-05 08:14:47 -0400150 activate_tempfile(tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200151 if (adjust_shared_perm(tempfile->filename.buf)) {
152 int save_errno = errno;
153 error("cannot fix permission bits on %s", tempfile->filename.buf);
Jeff King076aa2c2017-09-05 08:15:08 -0400154 delete_tempfile(&tempfile);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200155 errno = save_errno;
Jeff King076aa2c2017-09-05 08:15:08 -0400156 return NULL;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200157 }
Jeff King076aa2c2017-09-05 08:15:08 -0400158
159 return tempfile;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200160}
161
Jeff King076aa2c2017-09-05 08:15:08 -0400162struct tempfile *register_tempfile(const char *path)
Michael Haggerty99397152015-08-10 11:47:44 +0200163{
Jeff King076aa2c2017-09-05 08:15:08 -0400164 struct tempfile *tempfile = new_tempfile();
Michael Haggerty99397152015-08-10 11:47:44 +0200165 strbuf_add_absolute_path(&tempfile->filename, path);
Jeff King2933ebb2017-09-05 08:14:47 -0400166 activate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400167 return tempfile;
Michael Haggerty99397152015-08-10 11:47:44 +0200168}
169
Brandon Williamsea8ace42018-02-14 10:59:57 -0800170struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode)
Michael Haggerty354ab112015-08-10 11:47:43 +0200171{
Jeff King076aa2c2017-09-05 08:15:08 -0400172 struct tempfile *tempfile = new_tempfile();
Michael Haggerty354ab112015-08-10 11:47:43 +0200173
Brandon Williamsea8ace42018-02-14 10:59:57 -0800174 strbuf_add_absolute_path(&tempfile->filename, filename_template);
Michael Haggerty354ab112015-08-10 11:47:43 +0200175 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
176 if (tempfile->fd < 0) {
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400177 deactivate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400178 return NULL;
Michael Haggerty354ab112015-08-10 11:47:43 +0200179 }
Jeff King2933ebb2017-09-05 08:14:47 -0400180 activate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400181 return tempfile;
Michael Haggerty354ab112015-08-10 11:47:43 +0200182}
183
Brandon Williamsea8ace42018-02-14 10:59:57 -0800184struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode)
Michael Haggerty354ab112015-08-10 11:47:43 +0200185{
Jeff King076aa2c2017-09-05 08:15:08 -0400186 struct tempfile *tempfile = new_tempfile();
Michael Haggerty354ab112015-08-10 11:47:43 +0200187 const char *tmpdir;
188
Michael Haggerty354ab112015-08-10 11:47:43 +0200189 tmpdir = getenv("TMPDIR");
190 if (!tmpdir)
191 tmpdir = "/tmp";
192
Brandon Williamsea8ace42018-02-14 10:59:57 -0800193 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template);
Michael Haggerty354ab112015-08-10 11:47:43 +0200194 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
195 if (tempfile->fd < 0) {
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400196 deactivate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400197 return NULL;
Michael Haggerty354ab112015-08-10 11:47:43 +0200198 }
Jeff King2933ebb2017-09-05 08:14:47 -0400199 activate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400200 return tempfile;
Michael Haggerty354ab112015-08-10 11:47:43 +0200201}
202
René Scharfe2c2db192022-04-20 22:26:09 +0200203struct tempfile *mks_tempfile_dt(const char *directory_template,
204 const char *filename)
205{
206 struct tempfile *tempfile;
207 const char *tmpdir;
208 struct strbuf sb = STRBUF_INIT;
209 int fd;
210 size_t directorylen;
211
212 if (!ends_with(directory_template, "XXXXXX")) {
213 errno = EINVAL;
214 return NULL;
215 }
216
217 tmpdir = getenv("TMPDIR");
218 if (!tmpdir)
219 tmpdir = "/tmp";
220
221 strbuf_addf(&sb, "%s/%s", tmpdir, directory_template);
222 directorylen = sb.len;
223 if (!mkdtemp(sb.buf)) {
224 int orig_errno = errno;
225 strbuf_release(&sb);
226 errno = orig_errno;
227 return NULL;
228 }
229
230 strbuf_addf(&sb, "/%s", filename);
231 fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600);
232 if (fd < 0) {
233 int orig_errno = errno;
234 strbuf_setlen(&sb, directorylen);
235 rmdir(sb.buf);
236 strbuf_release(&sb);
237 errno = orig_errno;
238 return NULL;
239 }
240
241 tempfile = new_tempfile();
242 strbuf_swap(&tempfile->filename, &sb);
René Scharfebabe2e02022-08-27 00:46:29 +0200243 tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen);
René Scharfe2c2db192022-04-20 22:26:09 +0200244 tempfile->fd = fd;
245 activate_tempfile(tempfile);
246 return tempfile;
247}
248
Brandon Williamsea8ace42018-02-14 10:59:57 -0800249struct tempfile *xmks_tempfile_m(const char *filename_template, int mode)
Michael Haggerty354ab112015-08-10 11:47:43 +0200250{
Jeff King076aa2c2017-09-05 08:15:08 -0400251 struct tempfile *tempfile;
Michael Haggerty354ab112015-08-10 11:47:43 +0200252 struct strbuf full_template = STRBUF_INIT;
253
Brandon Williamsea8ace42018-02-14 10:59:57 -0800254 strbuf_add_absolute_path(&full_template, filename_template);
Jeff King076aa2c2017-09-05 08:15:08 -0400255 tempfile = mks_tempfile_m(full_template.buf, mode);
256 if (!tempfile)
Michael Haggerty354ab112015-08-10 11:47:43 +0200257 die_errno("Unable to create temporary file '%s'",
258 full_template.buf);
259
260 strbuf_release(&full_template);
Jeff King076aa2c2017-09-05 08:15:08 -0400261 return tempfile;
Michael Haggerty354ab112015-08-10 11:47:43 +0200262}
263
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200264FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
265{
Jeff Kinge6fc2672017-09-05 08:14:36 -0400266 if (!is_tempfile_active(tempfile))
Jeff King9b028aa2017-09-05 08:14:43 -0400267 BUG("fdopen_tempfile() called for inactive object");
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200268 if (tempfile->fp)
Jeff King9b028aa2017-09-05 08:14:43 -0400269 BUG("fdopen_tempfile() called for open object");
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200270
271 tempfile->fp = fdopen(tempfile->fd, mode);
272 return tempfile->fp;
273}
274
275const char *get_tempfile_path(struct tempfile *tempfile)
276{
Jeff Kinge6fc2672017-09-05 08:14:36 -0400277 if (!is_tempfile_active(tempfile))
Jeff King9b028aa2017-09-05 08:14:43 -0400278 BUG("get_tempfile_path() called for inactive object");
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200279 return tempfile->filename.buf;
280}
281
282int get_tempfile_fd(struct tempfile *tempfile)
283{
Jeff Kinge6fc2672017-09-05 08:14:36 -0400284 if (!is_tempfile_active(tempfile))
Jeff King9b028aa2017-09-05 08:14:43 -0400285 BUG("get_tempfile_fd() called for inactive object");
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200286 return tempfile->fd;
287}
288
289FILE *get_tempfile_fp(struct tempfile *tempfile)
290{
Jeff Kinge6fc2672017-09-05 08:14:36 -0400291 if (!is_tempfile_active(tempfile))
Jeff King9b028aa2017-09-05 08:14:43 -0400292 BUG("get_tempfile_fp() called for inactive object");
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200293 return tempfile->fp;
294}
295
Jeff King49bd0fc2017-09-05 08:14:30 -0400296int close_tempfile_gently(struct tempfile *tempfile)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200297{
Jeff Kingf5b4dc72017-09-05 08:14:40 -0400298 int fd;
299 FILE *fp;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200300 int err;
301
Jeff Kingf5b4dc72017-09-05 08:14:40 -0400302 if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200303 return 0;
304
Jeff Kingf5b4dc72017-09-05 08:14:40 -0400305 fd = tempfile->fd;
306 fp = tempfile->fp;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200307 tempfile->fd = -1;
308 if (fp) {
309 tempfile->fp = NULL;
Jeff King7e8c9352017-02-17 16:07:49 -0500310 if (ferror(fp)) {
311 err = -1;
312 if (!fclose(fp))
313 errno = EIO;
314 } else {
315 err = fclose(fp);
316 }
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200317 } else {
318 err = close(fd);
319 }
320
Jeff King49bd0fc2017-09-05 08:14:30 -0400321 return err ? -1 : 0;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200322}
323
324int reopen_tempfile(struct tempfile *tempfile)
325{
Jeff Kinge6fc2672017-09-05 08:14:36 -0400326 if (!is_tempfile_active(tempfile))
Jeff King9b028aa2017-09-05 08:14:43 -0400327 BUG("reopen_tempfile called for an inactive object");
Jeff Kingf5b4dc72017-09-05 08:14:40 -0400328 if (0 <= tempfile->fd)
Jeff King9b028aa2017-09-05 08:14:43 -0400329 BUG("reopen_tempfile called for an open object");
Jeff King6c003d62018-09-04 19:36:43 -0400330 tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200331 return tempfile->fd;
332}
333
Jeff King076aa2c2017-09-05 08:15:08 -0400334int rename_tempfile(struct tempfile **tempfile_p, const char *path)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200335{
Jeff King076aa2c2017-09-05 08:15:08 -0400336 struct tempfile *tempfile = *tempfile_p;
337
Jeff Kinge6fc2672017-09-05 08:14:36 -0400338 if (!is_tempfile_active(tempfile))
Jeff King9b028aa2017-09-05 08:14:43 -0400339 BUG("rename_tempfile called for inactive object");
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200340
Jeff King49bd0fc2017-09-05 08:14:30 -0400341 if (close_tempfile_gently(tempfile)) {
Jeff King076aa2c2017-09-05 08:15:08 -0400342 delete_tempfile(tempfile_p);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200343 return -1;
Jeff King49bd0fc2017-09-05 08:14:30 -0400344 }
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200345
346 if (rename(tempfile->filename.buf, path)) {
347 int save_errno = errno;
Jeff King076aa2c2017-09-05 08:15:08 -0400348 delete_tempfile(tempfile_p);
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200349 errno = save_errno;
350 return -1;
351 }
352
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400353 deactivate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400354 *tempfile_p = NULL;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200355 return 0;
356}
357
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +0100358int delete_tempfile(struct tempfile **tempfile_p)
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200359{
Jeff King076aa2c2017-09-05 08:15:08 -0400360 struct tempfile *tempfile = *tempfile_p;
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +0100361 int err = 0;
Jeff King076aa2c2017-09-05 08:15:08 -0400362
Jeff Kinge6fc2672017-09-05 08:14:36 -0400363 if (!is_tempfile_active(tempfile))
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +0100364 return 0;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200365
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +0100366 err |= close_tempfile_gently(tempfile);
367 err |= unlink_or_warn(tempfile->filename.buf);
368 err |= remove_template_directory(tempfile, 0);
Jeff Kingb5f4dcb2017-09-05 08:14:50 -0400369 deactivate_tempfile(tempfile);
Jeff King076aa2c2017-09-05 08:15:08 -0400370 *tempfile_p = NULL;
Patrick Steinhardt4ae540d2024-03-07 14:10:31 +0100371
372 return err ? -1 : 0;
Michael Haggerty1a9d15d2015-08-10 11:47:41 +0200373}