1/* 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 * 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. 21 * 22 * - Active, file open (after `create_tempfile()` or 23 * `reopen_tempfile()`). In this state: 24 * 25 * - the temporary file exists 26 * - `active` is set 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 32 * 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`. 36 * 37 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a 38 * failed attempt to create a temporary file). In this state: 39 * 40 * - `active` is unset 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 46 * `on_list` is set. 47 * 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. 52 */ 53 54#include "cache.h" 55#include "tempfile.h" 56#include "sigchain.h" 57 58static struct tempfile *volatile tempfile_list; 59 60static void remove_tempfiles(int skip_fclose) 61{ 62 pid_t me = getpid(); 63 64 while (tempfile_list) { 65 if (tempfile_list->owner == me) { 66 /* fclose() is not safe to call in a signal handler */ 67 if (skip_fclose) 68 tempfile_list->fp = NULL; 69 delete_tempfile(tempfile_list); 70 } 71 tempfile_list = tempfile_list->next; 72 } 73} 74 75static void remove_tempfiles_on_exit(void) 76{ 77 remove_tempfiles(0); 78} 79 80static void remove_tempfiles_on_signal(int signo) 81{ 82 remove_tempfiles(1); 83 sigchain_pop(signo); 84 raise(signo); 85} 86 87/* 88 * Initialize *tempfile if necessary and add it to tempfile_list. 89 */ 90static void prepare_tempfile_object(struct tempfile *tempfile) 91{ 92 if (!tempfile_list) { 93 /* One-time initialization */ 94 sigchain_push_common(remove_tempfiles_on_signal); 95 atexit(remove_tempfiles_on_exit); 96 } 97 98 if (is_tempfile_active(tempfile)) 99 BUG("prepare_tempfile_object called for active object"); 100 if (!tempfile->on_list) { 101 /* Initialize *tempfile and add it to tempfile_list: */ 102 tempfile->fd = -1; 103 tempfile->fp = NULL; 104 tempfile->active = 0; 105 tempfile->owner = 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 BUG("prepare_tempfile_object called for improperly-reset object"); 113 } 114} 115 116static void activate_tempfile(struct tempfile *tempfile) 117{ 118 tempfile->owner = getpid(); 119 tempfile->active = 1; 120} 121 122/* Make sure errno contains a meaningful value on error */ 123int create_tempfile(struct tempfile *tempfile, const char *path) 124{ 125 prepare_tempfile_object(tempfile); 126 127 strbuf_add_absolute_path(&tempfile->filename, path); 128 tempfile->fd = open(tempfile->filename.buf, 129 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666); 130 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) 131 /* Try again w/o O_CLOEXEC: the kernel might not support it */ 132 tempfile->fd = open(tempfile->filename.buf, 133 O_RDWR | O_CREAT | O_EXCL, 0666); 134 if (tempfile->fd < 0) { 135 strbuf_reset(&tempfile->filename); 136 return -1; 137 } 138 activate_tempfile(tempfile); 139 if (adjust_shared_perm(tempfile->filename.buf)) { 140 int save_errno = errno; 141 error("cannot fix permission bits on %s", tempfile->filename.buf); 142 delete_tempfile(tempfile); 143 errno = save_errno; 144 return -1; 145 } 146 return tempfile->fd; 147} 148 149void register_tempfile(struct tempfile *tempfile, const char *path) 150{ 151 prepare_tempfile_object(tempfile); 152 strbuf_add_absolute_path(&tempfile->filename, path); 153 activate_tempfile(tempfile); 154} 155 156int mks_tempfile_sm(struct tempfile *tempfile, 157 const char *template, int suffixlen, int mode) 158{ 159 prepare_tempfile_object(tempfile); 160 161 strbuf_add_absolute_path(&tempfile->filename, template); 162 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); 163 if (tempfile->fd < 0) { 164 strbuf_reset(&tempfile->filename); 165 return -1; 166 } 167 activate_tempfile(tempfile); 168 return tempfile->fd; 169} 170 171int mks_tempfile_tsm(struct tempfile *tempfile, 172 const char *template, int suffixlen, int mode) 173{ 174 const char *tmpdir; 175 176 prepare_tempfile_object(tempfile); 177 178 tmpdir = getenv("TMPDIR"); 179 if (!tmpdir) 180 tmpdir = "/tmp"; 181 182 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template); 183 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); 184 if (tempfile->fd < 0) { 185 strbuf_reset(&tempfile->filename); 186 return -1; 187 } 188 activate_tempfile(tempfile); 189 return tempfile->fd; 190} 191 192int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode) 193{ 194 int fd; 195 struct strbuf full_template = STRBUF_INIT; 196 197 strbuf_add_absolute_path(&full_template, template); 198 fd = mks_tempfile_m(tempfile, full_template.buf, mode); 199 if (fd < 0) 200 die_errno("Unable to create temporary file '%s'", 201 full_template.buf); 202 203 strbuf_release(&full_template); 204 return fd; 205} 206 207FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) 208{ 209 if (!is_tempfile_active(tempfile)) 210 BUG("fdopen_tempfile() called for inactive object"); 211 if (tempfile->fp) 212 BUG("fdopen_tempfile() called for open object"); 213 214 tempfile->fp = fdopen(tempfile->fd, mode); 215 return tempfile->fp; 216} 217 218const char *get_tempfile_path(struct tempfile *tempfile) 219{ 220 if (!is_tempfile_active(tempfile)) 221 BUG("get_tempfile_path() called for inactive object"); 222 return tempfile->filename.buf; 223} 224 225int get_tempfile_fd(struct tempfile *tempfile) 226{ 227 if (!is_tempfile_active(tempfile)) 228 BUG("get_tempfile_fd() called for inactive object"); 229 return tempfile->fd; 230} 231 232FILE *get_tempfile_fp(struct tempfile *tempfile) 233{ 234 if (!is_tempfile_active(tempfile)) 235 BUG("get_tempfile_fp() called for inactive object"); 236 return tempfile->fp; 237} 238 239int close_tempfile_gently(struct tempfile *tempfile) 240{ 241 int fd; 242 FILE *fp; 243 int err; 244 245 if (!is_tempfile_active(tempfile) || tempfile->fd < 0) 246 return 0; 247 248 fd = tempfile->fd; 249 fp = tempfile->fp; 250 tempfile->fd = -1; 251 if (fp) { 252 tempfile->fp = NULL; 253 if (ferror(fp)) { 254 err = -1; 255 if (!fclose(fp)) 256 errno = EIO; 257 } else { 258 err = fclose(fp); 259 } 260 } else { 261 err = close(fd); 262 } 263 264 return err ? -1 : 0; 265} 266 267int reopen_tempfile(struct tempfile *tempfile) 268{ 269 if (!is_tempfile_active(tempfile)) 270 BUG("reopen_tempfile called for an inactive object"); 271 if (0 <= tempfile->fd) 272 BUG("reopen_tempfile called for an open object"); 273 tempfile->fd = open(tempfile->filename.buf, O_WRONLY); 274 return tempfile->fd; 275} 276 277int rename_tempfile(struct tempfile *tempfile, const char *path) 278{ 279 if (!is_tempfile_active(tempfile)) 280 BUG("rename_tempfile called for inactive object"); 281 282 if (close_tempfile_gently(tempfile)) { 283 delete_tempfile(tempfile); 284 return -1; 285 } 286 287 if (rename(tempfile->filename.buf, path)) { 288 int save_errno = errno; 289 delete_tempfile(tempfile); 290 errno = save_errno; 291 return -1; 292 } 293 294 tempfile->active = 0; 295 strbuf_reset(&tempfile->filename); 296 return 0; 297} 298 299void delete_tempfile(struct tempfile *tempfile) 300{ 301 if (!is_tempfile_active(tempfile)) 302 return; 303 304 close_tempfile_gently(tempfile); 305 unlink_or_warn(tempfile->filename.buf); 306 tempfile->active = 0; 307 strbuf_reset(&tempfile->filename); 308}