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