1/* 2 * Copyright (c) 2005, Junio C Hamano 3 */ 4#include"cache.h" 5#include"sigchain.h" 6 7/* 8 * File write-locks as used by Git. 9 * 10 * For an overview of how to use the lockfile API, please see 11 * 12 * Documentation/technical/api-lockfile.txt 13 * 14 * This module keeps track of all locked files in lock_file_list for 15 * use at cleanup. This list and the lock_file objects that comprise 16 * it must be kept in self-consistent states at all time, because the 17 * program can be interrupted any time by a signal, in which case the 18 * signal handler will walk through the list attempting to clean up 19 * any open lock files. 20 * 21 * A lockfile is owned by the process that created it. The lock_file 22 * object has an "owner" field that records its owner. This field is 23 * used to prevent a forked process from closing a lockfile created by 24 * its parent. 25 * 26 * The possible states of a lock_file object are as follows: 27 * 28 * - Uninitialized. In this state the object's on_list field must be 29 * zero but the rest of its contents need not be initialized. As 30 * soon as the object is used in any way, it is irrevocably 31 * registered in the lock_file_list, and on_list is set. 32 * 33 * - Locked, lockfile open (after hold_lock_file_for_update(), 34 * hold_lock_file_for_append(), or reopen_lock_file()). In this 35 * state: 36 * - the lockfile exists 37 * - active is set 38 * - filename holds the filename of the lockfile 39 * - fd holds a file descriptor open for writing to the lockfile 40 * - owner holds the PID of the process that locked the file 41 * 42 * - Locked, lockfile closed (after successful close_lock_file()). 43 * Same as the previous state, except that the lockfile is closed 44 * and fd is -1. 45 * 46 * - Unlocked (after commit_lock_file(), rollback_lock_file(), a 47 * failed attempt to lock, or a failed close_lock_file()). In this 48 * state: 49 * - active is unset 50 * - filename[0] == '\0' (usually, though there are transitory states 51 * in which this condition doesn't hold). Client code should *not* 52 * rely on this fact! 53 * - fd is -1 54 * - the object is left registered in the lock_file_list, and 55 * on_list is set. 56 */ 57 58static struct lock_file *volatile lock_file_list; 59 60static voidremove_lock_file(void) 61{ 62 pid_t me =getpid(); 63 64while(lock_file_list) { 65if(lock_file_list->owner == me) 66rollback_lock_file(lock_file_list); 67 lock_file_list = lock_file_list->next; 68} 69} 70 71static voidremove_lock_file_on_signal(int signo) 72{ 73remove_lock_file(); 74sigchain_pop(signo); 75raise(signo); 76} 77 78/* 79 * p = absolute or relative path name 80 * 81 * Return a pointer into p showing the beginning of the last path name 82 * element. If p is empty or the root directory ("/"), just return p. 83 */ 84static char*last_path_elm(char*p) 85{ 86/* r starts pointing to null at the end of the string */ 87char*r =strchr(p,'\0'); 88 89if(r == p) 90return p;/* just return empty string */ 91 92 r--;/* back up to last non-null character */ 93 94/* back up past trailing slashes, if any */ 95while(r > p && *r =='/') 96 r--; 97 98/* 99 * then go backwards until I hit a slash, or the beginning of 100 * the string 101 */ 102while(r > p && *(r-1) !='/') 103 r--; 104return r; 105} 106 107 108/* We allow "recursive" symbolic links. Only within reason, though */ 109#define MAXDEPTH 5 110 111/* 112 * p = path that may be a symlink 113 * s = full size of p 114 * 115 * If p is a symlink, attempt to overwrite p with a path to the real 116 * file or directory (which may or may not exist), following a chain of 117 * symlinks if necessary. Otherwise, leave p unmodified. 118 * 119 * This is a best-effort routine. If an error occurs, p will either be 120 * left unmodified or will name a different symlink in a symlink chain 121 * that started with p's initial contents. 122 * 123 * Always returns p. 124 */ 125 126static char*resolve_symlink(char*p,size_t s) 127{ 128int depth = MAXDEPTH; 129 130while(depth--) { 131char link[PATH_MAX]; 132int link_len =readlink(p, link,sizeof(link)); 133if(link_len <0) { 134/* not a symlink anymore */ 135return p; 136} 137else if(link_len <sizeof(link)) 138/* readlink() never null-terminates */ 139 link[link_len] ='\0'; 140else{ 141warning("%s: symlink too long", p); 142return p; 143} 144 145if(is_absolute_path(link)) { 146/* absolute path simply replaces p */ 147if(link_len < s) 148strcpy(p, link); 149else{ 150warning("%s: symlink too long", p); 151return p; 152} 153}else{ 154/* 155 * link is a relative path, so I must replace the 156 * last element of p with it. 157 */ 158char*r = (char*)last_path_elm(p); 159if(r - p + link_len < s) 160strcpy(r, link); 161else{ 162warning("%s: symlink too long", p); 163return p; 164} 165} 166} 167return p; 168} 169 170/* Make sure errno contains a meaningful value on error */ 171static intlock_file(struct lock_file *lk,const char*path,int flags) 172{ 173/* 174 * subtract LOCK_SUFFIX_LEN from size to make sure there's 175 * room for adding ".lock" for the lock file name: 176 */ 177static const size_t max_path_len =sizeof(lk->filename) - 178 LOCK_SUFFIX_LEN; 179 180if(!lock_file_list) { 181/* One-time initialization */ 182sigchain_push_common(remove_lock_file_on_signal); 183atexit(remove_lock_file); 184} 185 186if(lk->active) 187die("BUG: cannot lock_file(\"%s\") using active struct lock_file", 188 path); 189if(!lk->on_list) { 190/* Initialize *lk and add it to lock_file_list: */ 191 lk->fd = -1; 192 lk->active =0; 193 lk->owner =0; 194 lk->filename[0] =0; 195 lk->next = lock_file_list; 196 lock_file_list = lk; 197 lk->on_list =1; 198} 199 200if(strlen(path) >= max_path_len) { 201 errno = ENAMETOOLONG; 202return-1; 203} 204strcpy(lk->filename, path); 205if(!(flags & LOCK_NODEREF)) 206resolve_symlink(lk->filename, max_path_len); 207strcat(lk->filename, LOCK_SUFFIX); 208 lk->fd =open(lk->filename, O_RDWR | O_CREAT | O_EXCL,0666); 209if(lk->fd <0) { 210 lk->filename[0] =0; 211return-1; 212} 213 lk->owner =getpid(); 214 lk->active =1; 215if(adjust_shared_perm(lk->filename)) { 216int save_errno = errno; 217error("cannot fix permission bits on%s", lk->filename); 218rollback_lock_file(lk); 219 errno = save_errno; 220return-1; 221} 222return lk->fd; 223} 224 225voidunable_to_lock_message(const char*path,int err,struct strbuf *buf) 226{ 227if(err == EEXIST) { 228strbuf_addf(buf,"Unable to create '%s.lock':%s.\n\n" 229"If no other git process is currently running, this probably means a\n" 230"git process crashed in this repository earlier. Make sure no other git\n" 231"process is running and remove the file manually to continue.", 232absolute_path(path),strerror(err)); 233}else 234strbuf_addf(buf,"Unable to create '%s.lock':%s", 235absolute_path(path),strerror(err)); 236} 237 238intunable_to_lock_error(const char*path,int err) 239{ 240struct strbuf buf = STRBUF_INIT; 241 242unable_to_lock_message(path, err, &buf); 243error("%s", buf.buf); 244strbuf_release(&buf); 245return-1; 246} 247 248NORETURN voidunable_to_lock_die(const char*path,int err) 249{ 250struct strbuf buf = STRBUF_INIT; 251 252unable_to_lock_message(path, err, &buf); 253die("%s", buf.buf); 254} 255 256/* This should return a meaningful errno on failure */ 257inthold_lock_file_for_update(struct lock_file *lk,const char*path,int flags) 258{ 259int fd =lock_file(lk, path, flags); 260if(fd <0&& (flags & LOCK_DIE_ON_ERROR)) 261unable_to_lock_die(path, errno); 262return fd; 263} 264 265inthold_lock_file_for_append(struct lock_file *lk,const char*path,int flags) 266{ 267int fd, orig_fd; 268 269 fd =lock_file(lk, path, flags); 270if(fd <0) { 271if(flags & LOCK_DIE_ON_ERROR) 272unable_to_lock_die(path, errno); 273return fd; 274} 275 276 orig_fd =open(path, O_RDONLY); 277if(orig_fd <0) { 278if(errno != ENOENT) { 279if(flags & LOCK_DIE_ON_ERROR) 280die("cannot open '%s' for copying", path); 281rollback_lock_file(lk); 282returnerror("cannot open '%s' for copying", path); 283} 284}else if(copy_fd(orig_fd, fd)) { 285if(flags & LOCK_DIE_ON_ERROR) 286exit(128); 287rollback_lock_file(lk); 288return-1; 289} 290return fd; 291} 292 293intclose_lock_file(struct lock_file *lk) 294{ 295int fd = lk->fd; 296 297if(fd <0) 298return0; 299 300 lk->fd = -1; 301if(close(fd)) { 302int save_errno = errno; 303rollback_lock_file(lk); 304 errno = save_errno; 305return-1; 306} 307return0; 308} 309 310intreopen_lock_file(struct lock_file *lk) 311{ 312if(0<= lk->fd) 313die(_("BUG: reopen a lockfile that is still open")); 314if(!lk->active) 315die(_("BUG: reopen a lockfile that has been committed")); 316 lk->fd =open(lk->filename, O_WRONLY); 317return lk->fd; 318} 319 320intcommit_lock_file(struct lock_file *lk) 321{ 322char result_file[PATH_MAX]; 323 324if(!lk->active) 325die("BUG: attempt to commit unlocked object"); 326 327if(close_lock_file(lk)) 328return-1; 329 330strcpy(result_file, lk->filename); 331/* remove ".lock": */ 332 result_file[strlen(result_file) - LOCK_SUFFIX_LEN] =0; 333 334if(rename(lk->filename, result_file)) { 335int save_errno = errno; 336rollback_lock_file(lk); 337 errno = save_errno; 338return-1; 339} 340 341 lk->active =0; 342 lk->filename[0] =0; 343return0; 344} 345 346inthold_locked_index(struct lock_file *lk,int die_on_error) 347{ 348returnhold_lock_file_for_update(lk,get_index_file(), 349 die_on_error 350? LOCK_DIE_ON_ERROR 351:0); 352} 353 354voidrollback_lock_file(struct lock_file *lk) 355{ 356if(!lk->active) 357return; 358 359if(!close_lock_file(lk)) { 360unlink_or_warn(lk->filename); 361 lk->active =0; 362 lk->filename[0] =0; 363} 364}