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