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 * A lock_file object can be in several states: 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, the lockfile exists, filename holds the filename of the 36 * lockfile, fd holds a file descriptor open for writing to the 37 * lockfile, and owner holds the PID of the process that locked the 38 * file. 39 * 40 * - Locked, lockfile closed (after close_lock_file()). Same as the 41 * previous state, except that the lockfile is closed and fd is -1. 42 * 43 * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a 44 * failed attempt to lock). In this state, filename[0] == '\0' and 45 * fd is -1. The object is left registered in the lock_file_list, 46 * and on_list is set. 47 */ 48 49static struct lock_file *lock_file_list; 50 51static voidremove_lock_file(void) 52{ 53 pid_t me =getpid(); 54 55while(lock_file_list) { 56if(lock_file_list->owner == me) 57rollback_lock_file(lock_file_list); 58 lock_file_list = lock_file_list->next; 59} 60} 61 62static voidremove_lock_file_on_signal(int signo) 63{ 64remove_lock_file(); 65sigchain_pop(signo); 66raise(signo); 67} 68 69/* 70 * p = absolute or relative path name 71 * 72 * Return a pointer into p showing the beginning of the last path name 73 * element. If p is empty or the root directory ("/"), just return p. 74 */ 75static char*last_path_elm(char*p) 76{ 77/* r starts pointing to null at the end of the string */ 78char*r =strchr(p,'\0'); 79 80if(r == p) 81return p;/* just return empty string */ 82 83 r--;/* back up to last non-null character */ 84 85/* back up past trailing slashes, if any */ 86while(r > p && *r =='/') 87 r--; 88 89/* 90 * then go backwards until I hit a slash, or the beginning of 91 * the string 92 */ 93while(r > p && *(r-1) !='/') 94 r--; 95return r; 96} 97 98 99/* We allow "recursive" symbolic links. Only within reason, though */ 100#define MAXDEPTH 5 101 102/* 103 * p = path that may be a symlink 104 * s = full size of p 105 * 106 * If p is a symlink, attempt to overwrite p with a path to the real 107 * file or directory (which may or may not exist), following a chain of 108 * symlinks if necessary. Otherwise, leave p unmodified. 109 * 110 * This is a best-effort routine. If an error occurs, p will either be 111 * left unmodified or will name a different symlink in a symlink chain 112 * that started with p's initial contents. 113 * 114 * Always returns p. 115 */ 116 117static char*resolve_symlink(char*p,size_t s) 118{ 119int depth = MAXDEPTH; 120 121while(depth--) { 122char link[PATH_MAX]; 123int link_len =readlink(p, link,sizeof(link)); 124if(link_len <0) { 125/* not a symlink anymore */ 126return p; 127} 128else if(link_len <sizeof(link)) 129/* readlink() never null-terminates */ 130 link[link_len] ='\0'; 131else{ 132warning("%s: symlink too long", p); 133return p; 134} 135 136if(is_absolute_path(link)) { 137/* absolute path simply replaces p */ 138if(link_len < s) 139strcpy(p, link); 140else{ 141warning("%s: symlink too long", p); 142return p; 143} 144}else{ 145/* 146 * link is a relative path, so I must replace the 147 * last element of p with it. 148 */ 149char*r = (char*)last_path_elm(p); 150if(r - p + link_len < s) 151strcpy(r, link); 152else{ 153warning("%s: symlink too long", p); 154return p; 155} 156} 157} 158return p; 159} 160 161/* Make sure errno contains a meaningful value on error */ 162static intlock_file(struct lock_file *lk,const char*path,int flags) 163{ 164/* 165 * subtract LOCK_SUFFIX_LEN from size to make sure there's 166 * room for adding ".lock" for the lock file name: 167 */ 168static const size_t max_path_len =sizeof(lk->filename) - 169 LOCK_SUFFIX_LEN; 170 171if(!lock_file_list) { 172/* One-time initialization */ 173sigchain_push_common(remove_lock_file_on_signal); 174atexit(remove_lock_file); 175} 176 177if(!lk->on_list) { 178/* Initialize *lk and add it to lock_file_list: */ 179 lk->fd = -1; 180 lk->owner =0; 181 lk->filename[0] =0; 182 lk->next = lock_file_list; 183 lock_file_list = lk; 184 lk->on_list =1; 185} 186 187if(strlen(path) >= max_path_len) { 188 errno = ENAMETOOLONG; 189return-1; 190} 191strcpy(lk->filename, path); 192if(!(flags & LOCK_NODEREF)) 193resolve_symlink(lk->filename, max_path_len); 194strcat(lk->filename, LOCK_SUFFIX); 195 lk->fd =open(lk->filename, O_RDWR | O_CREAT | O_EXCL,0666); 196if(lk->fd <0) { 197 lk->filename[0] =0; 198return-1; 199} 200 lk->owner =getpid(); 201if(adjust_shared_perm(lk->filename)) { 202int save_errno = errno; 203error("cannot fix permission bits on%s", lk->filename); 204rollback_lock_file(lk); 205 errno = save_errno; 206return-1; 207} 208return lk->fd; 209} 210 211voidunable_to_lock_message(const char*path,int err,struct strbuf *buf) 212{ 213if(err == EEXIST) { 214strbuf_addf(buf,"Unable to create '%s.lock':%s.\n\n" 215"If no other git process is currently running, this probably means a\n" 216"git process crashed in this repository earlier. Make sure no other git\n" 217"process is running and remove the file manually to continue.", 218absolute_path(path),strerror(err)); 219}else 220strbuf_addf(buf,"Unable to create '%s.lock':%s", 221absolute_path(path),strerror(err)); 222} 223 224intunable_to_lock_error(const char*path,int err) 225{ 226struct strbuf buf = STRBUF_INIT; 227 228unable_to_lock_message(path, err, &buf); 229error("%s", buf.buf); 230strbuf_release(&buf); 231return-1; 232} 233 234NORETURN voidunable_to_lock_die(const char*path,int err) 235{ 236struct strbuf buf = STRBUF_INIT; 237 238unable_to_lock_message(path, err, &buf); 239die("%s", buf.buf); 240} 241 242/* This should return a meaningful errno on failure */ 243inthold_lock_file_for_update(struct lock_file *lk,const char*path,int flags) 244{ 245int fd =lock_file(lk, path, flags); 246if(fd <0&& (flags & LOCK_DIE_ON_ERROR)) 247unable_to_lock_die(path, errno); 248return fd; 249} 250 251inthold_lock_file_for_append(struct lock_file *lk,const char*path,int flags) 252{ 253int fd, orig_fd; 254 255 fd =lock_file(lk, path, flags); 256if(fd <0) { 257if(flags & LOCK_DIE_ON_ERROR) 258unable_to_lock_die(path, errno); 259return fd; 260} 261 262 orig_fd =open(path, O_RDONLY); 263if(orig_fd <0) { 264if(errno != ENOENT) { 265if(flags & LOCK_DIE_ON_ERROR) 266die("cannot open '%s' for copying", path); 267rollback_lock_file(lk); 268returnerror("cannot open '%s' for copying", path); 269} 270}else if(copy_fd(orig_fd, fd)) { 271if(flags & LOCK_DIE_ON_ERROR) 272exit(128); 273rollback_lock_file(lk); 274return-1; 275} 276return fd; 277} 278 279intclose_lock_file(struct lock_file *lk) 280{ 281int fd = lk->fd; 282 283if(fd <0) 284return0; 285 286 lk->fd = -1; 287returnclose(fd); 288} 289 290intreopen_lock_file(struct lock_file *lk) 291{ 292if(0<= lk->fd) 293die(_("BUG: reopen a lockfile that is still open")); 294if(!lk->filename[0]) 295die(_("BUG: reopen a lockfile that has been committed")); 296 lk->fd =open(lk->filename, O_WRONLY); 297return lk->fd; 298} 299 300intcommit_lock_file(struct lock_file *lk) 301{ 302char result_file[PATH_MAX]; 303 304if(close_lock_file(lk)) 305return-1; 306 307strcpy(result_file, lk->filename); 308/* remove ".lock": */ 309 result_file[strlen(result_file) - LOCK_SUFFIX_LEN] =0; 310 311if(rename(lk->filename, result_file)) 312return-1; 313 lk->filename[0] =0; 314return0; 315} 316 317inthold_locked_index(struct lock_file *lk,int die_on_error) 318{ 319returnhold_lock_file_for_update(lk,get_index_file(), 320 die_on_error 321? LOCK_DIE_ON_ERROR 322:0); 323} 324 325voidrollback_lock_file(struct lock_file *lk) 326{ 327if(!lk->filename[0]) 328return; 329 330close_lock_file(lk); 331unlink_or_warn(lk->filename); 332 lk->filename[0] =0; 333}