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 successful `close_tempfile()`). 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()`, a 38 * failed attempt to create a temporary file, or a failed 39 * `close_tempfile()`). In this state: 40 * 41 * - `active` is unset 42 * - `filename` is empty (usually, though there are transitory 43 * states in which this condition doesn't hold). Client code should 44 * *not* rely on the filename being empty in this state. 45 * - `fd` is -1 and `fp` is `NULL` 46 * - the object is left registered in the `tempfile_list`, and 47 * `on_list` is set. 48 * 49 * A temporary file is owned by the process that created it. The 50 * `tempfile` has an `owner` field that records the owner's PID. This 51 * field is used to prevent a forked process from deleting a temporary 52 * file created by its parent. 53 */ 54 55#include"cache.h" 56#include"tempfile.h" 57#include"sigchain.h" 58 59static struct tempfile *volatile tempfile_list; 60 61static voidremove_tempfiles(int skip_fclose) 62{ 63 pid_t me =getpid(); 64 65while(tempfile_list) { 66if(tempfile_list->owner == me) { 67/* fclose() is not safe to call in a signal handler */ 68if(skip_fclose) 69 tempfile_list->fp = NULL; 70delete_tempfile(tempfile_list); 71} 72 tempfile_list = tempfile_list->next; 73} 74} 75 76static voidremove_tempfiles_on_exit(void) 77{ 78remove_tempfiles(0); 79} 80 81static voidremove_tempfiles_on_signal(int signo) 82{ 83remove_tempfiles(1); 84sigchain_pop(signo); 85raise(signo); 86} 87 88/* 89 * Initialize *tempfile if necessary and add it to tempfile_list. 90 */ 91static voidprepare_tempfile_object(struct tempfile *tempfile) 92{ 93if(!tempfile_list) { 94/* One-time initialization */ 95sigchain_push_common(remove_tempfiles_on_signal); 96atexit(remove_tempfiles_on_exit); 97} 98 99if(tempfile->active) 100die("BUG: prepare_tempfile_object called for active object"); 101if(!tempfile->on_list) { 102/* Initialize *tempfile and add it to tempfile_list: */ 103 tempfile->fd = -1; 104 tempfile->fp = NULL; 105 tempfile->active =0; 106 tempfile->owner =0; 107strbuf_init(&tempfile->filename,0); 108 tempfile->next = tempfile_list; 109 tempfile_list = tempfile; 110 tempfile->on_list =1; 111}else if(tempfile->filename.len) { 112/* This shouldn't happen, but better safe than sorry. */ 113die("BUG: prepare_tempfile_object called for improperly-reset object"); 114} 115} 116 117/* Make sure errno contains a meaningful value on error */ 118intcreate_tempfile(struct tempfile *tempfile,const char*path) 119{ 120prepare_tempfile_object(tempfile); 121 122strbuf_add_absolute_path(&tempfile->filename, path); 123 tempfile->fd =open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL,0666); 124if(tempfile->fd <0) { 125strbuf_reset(&tempfile->filename); 126return-1; 127} 128 tempfile->owner =getpid(); 129 tempfile->active =1; 130if(adjust_shared_perm(tempfile->filename.buf)) { 131int save_errno = errno; 132error("cannot fix permission bits on%s", tempfile->filename.buf); 133delete_tempfile(tempfile); 134 errno = save_errno; 135return-1; 136} 137return tempfile->fd; 138} 139 140voidregister_tempfile(struct tempfile *tempfile,const char*path) 141{ 142prepare_tempfile_object(tempfile); 143strbuf_add_absolute_path(&tempfile->filename, path); 144 tempfile->owner =getpid(); 145 tempfile->active =1; 146} 147 148intmks_tempfile_sm(struct tempfile *tempfile, 149const char*template,int suffixlen,int mode) 150{ 151prepare_tempfile_object(tempfile); 152 153strbuf_add_absolute_path(&tempfile->filename,template); 154 tempfile->fd =git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); 155if(tempfile->fd <0) { 156strbuf_reset(&tempfile->filename); 157return-1; 158} 159 tempfile->owner =getpid(); 160 tempfile->active =1; 161return tempfile->fd; 162} 163 164intmks_tempfile_tsm(struct tempfile *tempfile, 165const char*template,int suffixlen,int mode) 166{ 167const char*tmpdir; 168 169prepare_tempfile_object(tempfile); 170 171 tmpdir =getenv("TMPDIR"); 172if(!tmpdir) 173 tmpdir ="/tmp"; 174 175strbuf_addf(&tempfile->filename,"%s/%s", tmpdir,template); 176 tempfile->fd =git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); 177if(tempfile->fd <0) { 178strbuf_reset(&tempfile->filename); 179return-1; 180} 181 tempfile->owner =getpid(); 182 tempfile->active =1; 183return tempfile->fd; 184} 185 186intxmks_tempfile_m(struct tempfile *tempfile,const char*template,int mode) 187{ 188int fd; 189struct strbuf full_template = STRBUF_INIT; 190 191strbuf_add_absolute_path(&full_template,template); 192 fd =mks_tempfile_m(tempfile, full_template.buf, mode); 193if(fd <0) 194die_errno("Unable to create temporary file '%s'", 195 full_template.buf); 196 197strbuf_release(&full_template); 198return fd; 199} 200 201FILE*fdopen_tempfile(struct tempfile *tempfile,const char*mode) 202{ 203if(!tempfile->active) 204die("BUG: fdopen_tempfile() called for inactive object"); 205if(tempfile->fp) 206die("BUG: fdopen_tempfile() called for open object"); 207 208 tempfile->fp =fdopen(tempfile->fd, mode); 209return tempfile->fp; 210} 211 212const char*get_tempfile_path(struct tempfile *tempfile) 213{ 214if(!tempfile->active) 215die("BUG: get_tempfile_path() called for inactive object"); 216return tempfile->filename.buf; 217} 218 219intget_tempfile_fd(struct tempfile *tempfile) 220{ 221if(!tempfile->active) 222die("BUG: get_tempfile_fd() called for inactive object"); 223return tempfile->fd; 224} 225 226FILE*get_tempfile_fp(struct tempfile *tempfile) 227{ 228if(!tempfile->active) 229die("BUG: get_tempfile_fp() called for inactive object"); 230return tempfile->fp; 231} 232 233intclose_tempfile(struct tempfile *tempfile) 234{ 235int fd = tempfile->fd; 236FILE*fp = tempfile->fp; 237int err; 238 239if(fd <0) 240return0; 241 242 tempfile->fd = -1; 243if(fp) { 244 tempfile->fp = NULL; 245 246/* 247 * Note: no short-circuiting here; we want to fclose() 248 * in any case! 249 */ 250 err =ferror(fp) |fclose(fp); 251}else{ 252 err =close(fd); 253} 254 255if(err) { 256int save_errno = errno; 257delete_tempfile(tempfile); 258 errno = save_errno; 259return-1; 260} 261 262return0; 263} 264 265intreopen_tempfile(struct tempfile *tempfile) 266{ 267if(0<= tempfile->fd) 268die("BUG: reopen_tempfile called for an open object"); 269if(!tempfile->active) 270die("BUG: reopen_tempfile called for an inactive object"); 271 tempfile->fd =open(tempfile->filename.buf, O_WRONLY); 272return tempfile->fd; 273} 274 275intrename_tempfile(struct tempfile *tempfile,const char*path) 276{ 277if(!tempfile->active) 278die("BUG: rename_tempfile called for inactive object"); 279 280if(close_tempfile(tempfile)) 281return-1; 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 300if(!close_tempfile(tempfile)) { 301unlink_or_warn(tempfile->filename.buf); 302 tempfile->active =0; 303strbuf_reset(&tempfile->filename); 304} 305}