| 1 | /* |
| 2 | * Copyright (c) 2005, Junio C Hamano |
| 3 | */ |
| 4 | |
| 5 | #include "git-compat-util.h" |
| 6 | #include "abspath.h" |
| 7 | #include "gettext.h" |
| 8 | #include "lockfile.h" |
| 9 | #include "parse.h" |
| 10 | #include "strbuf.h" |
| 11 | #include "wrapper.h" |
| 12 | |
| 13 | /* |
| 14 | * path = absolute or relative path name |
| 15 | * |
| 16 | * Remove the last path name element from path (leaving the preceding |
| 17 | * "/", if any). If path is empty or the root directory ("/"), set |
| 18 | * path to the empty string. |
| 19 | */ |
| 20 | static void trim_last_path_component(struct strbuf *path) |
| 21 | { |
| 22 | int i = path->len; |
| 23 | |
| 24 | /* back up past trailing slashes, if any */ |
| 25 | while (i && is_dir_sep(path->buf[i - 1])) |
| 26 | i--; |
| 27 | |
| 28 | /* |
| 29 | * then go backwards until a slash, or the beginning of the |
| 30 | * string |
| 31 | */ |
| 32 | while (i && !is_dir_sep(path->buf[i - 1])) |
| 33 | i--; |
| 34 | |
| 35 | strbuf_setlen(path, i); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /* We allow "recursive" symbolic links. Only within reason, though */ |
| 40 | #define MAXDEPTH 5 |
| 41 | |
| 42 | /* |
| 43 | * path contains a path that might be a symlink. |
| 44 | * |
| 45 | * If path is a symlink, attempt to overwrite it with a path to the |
| 46 | * real file or directory (which may or may not exist), following a |
| 47 | * chain of symlinks if necessary. Otherwise, leave path unmodified. |
| 48 | * |
| 49 | * This is a best-effort routine. If an error occurs, path will |
| 50 | * either be left unmodified or will name a different symlink in a |
| 51 | * symlink chain that started with the original path. |
| 52 | */ |
| 53 | static void resolve_symlink(struct strbuf *path) |
| 54 | { |
| 55 | int depth = MAXDEPTH; |
| 56 | static struct strbuf link = STRBUF_INIT; |
| 57 | |
| 58 | while (depth--) { |
| 59 | if (strbuf_readlink(&link, path->buf, path->len) < 0) |
| 60 | break; |
| 61 | |
| 62 | if (is_absolute_path(link.buf)) |
| 63 | /* absolute path simply replaces p */ |
| 64 | strbuf_reset(path); |
| 65 | else |
| 66 | /* |
| 67 | * link is a relative path, so replace the |
| 68 | * last element of p with it. |
| 69 | */ |
| 70 | trim_last_path_component(path); |
| 71 | |
| 72 | strbuf_addbuf(path, &link); |
| 73 | } |
| 74 | strbuf_reset(&link); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Lock PID file functions - write PID to a foo~pid.lock file alongside |
| 79 | * the lock file for debugging stale locks. The PID file is registered |
| 80 | * as a tempfile so it gets cleaned up by signal/atexit handlers. |
| 81 | * |
| 82 | * Naming: For "foo.lock", the PID file is "foo~pid.lock". The tilde is |
| 83 | * forbidden in refnames and allowed in Windows filenames, guaranteeing |
| 84 | * no collision with the refs namespace. |
| 85 | */ |
| 86 | |
| 87 | /* Global config variable, initialized from core.lockfilePid */ |
| 88 | int lockfile_pid_enabled; |
| 89 | |
| 90 | /* |
| 91 | * Path generation helpers. |
| 92 | * Given base path "foo", generate: |
| 93 | * - lock path: "foo.lock" |
| 94 | * - pid path: "foo-pid.lock" |
| 95 | */ |
| 96 | static void get_lock_path(struct strbuf *out, const char *path) |
| 97 | { |
| 98 | strbuf_addstr(out, path); |
| 99 | strbuf_addstr(out, LOCK_SUFFIX); |
| 100 | } |
| 101 | |
| 102 | static void get_pid_path(struct strbuf *out, const char *path) |
| 103 | { |
| 104 | strbuf_addstr(out, path); |
| 105 | strbuf_addstr(out, LOCK_PID_INFIX); |
| 106 | strbuf_addstr(out, LOCK_SUFFIX); |
| 107 | } |
| 108 | |
| 109 | static struct tempfile *create_lock_pid_file(const char *pid_path, int mode) |
| 110 | { |
| 111 | struct strbuf content = STRBUF_INIT; |
| 112 | struct tempfile *pid_tempfile = NULL; |
| 113 | int fd; |
| 114 | |
| 115 | if (!lockfile_pid_enabled) |
| 116 | goto out; |
| 117 | |
| 118 | fd = open(pid_path, O_WRONLY | O_CREAT | O_EXCL, mode); |
| 119 | if (fd < 0) |
| 120 | goto out; |
| 121 | |
| 122 | strbuf_addf(&content, "pid %" PRIuMAX "\n", (uintmax_t)getpid()); |
| 123 | if (write_in_full(fd, content.buf, content.len) < 0) { |
| 124 | warning_errno(_("could not write lock pid file '%s'"), pid_path); |
| 125 | close(fd); |
| 126 | unlink(pid_path); |
| 127 | goto out; |
| 128 | } |
| 129 | |
| 130 | close(fd); |
| 131 | pid_tempfile = register_tempfile(pid_path); |
| 132 | |
| 133 | out: |
| 134 | strbuf_release(&content); |
| 135 | return pid_tempfile; |
| 136 | } |
| 137 | |
| 138 | static int read_lock_pid(const char *pid_path, uintmax_t *pid_out) |
| 139 | { |
| 140 | struct strbuf content = STRBUF_INIT; |
| 141 | const char *val; |
| 142 | int ret = -1; |
| 143 | |
| 144 | if (strbuf_read_file(&content, pid_path, LOCK_PID_MAXLEN) <= 0) |
| 145 | goto out; |
| 146 | |
| 147 | strbuf_rtrim(&content); |
| 148 | |
| 149 | if (skip_prefix(content.buf, "pid ", &val)) { |
| 150 | char *endptr; |
| 151 | *pid_out = strtoumax(val, &endptr, 10); |
| 152 | if (*pid_out > 0 && !*endptr) |
| 153 | ret = 0; |
| 154 | } |
| 155 | |
| 156 | if (ret) |
| 157 | warning(_("malformed lock pid file '%s'"), pid_path); |
| 158 | |
| 159 | out: |
| 160 | strbuf_release(&content); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | /* Make sure errno contains a meaningful value on error */ |
| 165 | static int lock_file(struct lock_file *lk, const char *path, int flags, |
| 166 | int mode) |
| 167 | { |
| 168 | struct strbuf base_path = STRBUF_INIT; |
| 169 | struct strbuf lock_path = STRBUF_INIT; |
| 170 | struct strbuf pid_path = STRBUF_INIT; |
| 171 | |
| 172 | strbuf_addstr(&base_path, path); |
| 173 | if (!(flags & LOCK_NO_DEREF)) |
| 174 | resolve_symlink(&base_path); |
| 175 | |
| 176 | get_lock_path(&lock_path, base_path.buf); |
| 177 | get_pid_path(&pid_path, base_path.buf); |
| 178 | |
| 179 | lk->tempfile = create_tempfile_mode(lock_path.buf, mode); |
| 180 | if (lk->tempfile) |
| 181 | lk->pid_tempfile = create_lock_pid_file(pid_path.buf, mode); |
| 182 | |
| 183 | strbuf_release(&base_path); |
| 184 | strbuf_release(&lock_path); |
| 185 | strbuf_release(&pid_path); |
| 186 | return lk->tempfile ? lk->tempfile->fd : -1; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Constants defining the gaps between attempts to lock a file. The |
| 191 | * first backoff period is approximately INITIAL_BACKOFF_MS |
| 192 | * milliseconds. The longest backoff period is approximately |
| 193 | * (BACKOFF_MAX_MULTIPLIER * INITIAL_BACKOFF_MS) milliseconds. |
| 194 | */ |
| 195 | #define INITIAL_BACKOFF_MS 1L |
| 196 | #define BACKOFF_MAX_MULTIPLIER 1000 |
| 197 | |
| 198 | /* |
| 199 | * Try locking path, retrying with quadratic backoff for at least |
| 200 | * timeout_ms milliseconds. If timeout_ms is 0, try locking the file |
| 201 | * exactly once. If timeout_ms is -1, try indefinitely. |
| 202 | */ |
| 203 | static int lock_file_timeout(struct lock_file *lk, const char *path, |
| 204 | int flags, long timeout_ms, int mode) |
| 205 | { |
| 206 | int n = 1; |
| 207 | int multiplier = 1; |
| 208 | long remaining_ms = 0; |
| 209 | static int random_initialized = 0; |
| 210 | |
| 211 | if (timeout_ms == 0) |
| 212 | return lock_file(lk, path, flags, mode); |
| 213 | |
| 214 | if (!random_initialized) { |
| 215 | srand((unsigned int)getpid()); |
| 216 | random_initialized = 1; |
| 217 | } |
| 218 | |
| 219 | if (timeout_ms > 0) |
| 220 | remaining_ms = timeout_ms; |
| 221 | |
| 222 | while (1) { |
| 223 | long backoff_ms, wait_ms; |
| 224 | int fd; |
| 225 | |
| 226 | fd = lock_file(lk, path, flags, mode); |
| 227 | |
| 228 | if (fd >= 0) |
| 229 | return fd; /* success */ |
| 230 | else if (errno != EEXIST) |
| 231 | return -1; /* failure other than lock held */ |
| 232 | else if (timeout_ms > 0 && remaining_ms <= 0) |
| 233 | return -1; /* failure due to timeout */ |
| 234 | |
| 235 | backoff_ms = multiplier * INITIAL_BACKOFF_MS; |
| 236 | /* back off for between 0.75*backoff_ms and 1.25*backoff_ms */ |
| 237 | wait_ms = (750 + rand() % 500) * backoff_ms / 1000; |
| 238 | sleep_millisec(wait_ms); |
| 239 | remaining_ms -= wait_ms; |
| 240 | |
| 241 | /* Recursion: (n+1)^2 = n^2 + 2n + 1 */ |
| 242 | multiplier += 2*n + 1; |
| 243 | if (multiplier > BACKOFF_MAX_MULTIPLIER) |
| 244 | multiplier = BACKOFF_MAX_MULTIPLIER; |
| 245 | else |
| 246 | n++; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void unable_to_lock_message(const char *path, int err, struct strbuf *buf) |
| 251 | { |
| 252 | if (err == EEXIST) { |
| 253 | const char *abs_path = absolute_path(path); |
| 254 | struct strbuf lock_path = STRBUF_INIT; |
| 255 | struct strbuf pid_path = STRBUF_INIT; |
| 256 | uintmax_t pid; |
| 257 | int pid_status = 0; /* 0 = unknown, 1 = running, -1 = stale */ |
| 258 | |
| 259 | get_lock_path(&lock_path, abs_path); |
| 260 | get_pid_path(&pid_path, abs_path); |
| 261 | |
| 262 | strbuf_addf(buf, _("Unable to create '%s': %s.\n\n"), |
| 263 | lock_path.buf, strerror(err)); |
| 264 | |
| 265 | /* |
| 266 | * Try to read PID file unconditionally - it may exist if |
| 267 | * core.lockfilePid was enabled. |
| 268 | */ |
| 269 | if (!read_lock_pid(pid_path.buf, &pid)) { |
| 270 | if (kill((pid_t)pid, 0) == 0 || errno == EPERM) |
| 271 | pid_status = 1; /* running (or no permission to signal) */ |
| 272 | else if (errno == ESRCH) |
| 273 | pid_status = -1; /* no such process - stale lock */ |
| 274 | } |
| 275 | |
| 276 | if (pid_status == 1) |
| 277 | strbuf_addf(buf, _("Lock may be held by process %" PRIuMAX "; " |
| 278 | "if no git process is running, the lock file " |
| 279 | "may be stale (PIDs can be reused)"), |
| 280 | pid); |
| 281 | else if (pid_status == -1) |
| 282 | strbuf_addf(buf, _("Lock was held by process %" PRIuMAX ", " |
| 283 | "which is no longer running; the lock file " |
| 284 | "appears to be stale"), |
| 285 | pid); |
| 286 | else |
| 287 | strbuf_addstr(buf, _("Another git process seems to be running in this repository, " |
| 288 | "or the lock file may be stale")); |
| 289 | |
| 290 | strbuf_release(&lock_path); |
| 291 | strbuf_release(&pid_path); |
| 292 | } else { |
| 293 | strbuf_addf(buf, _("Unable to create '%s.lock': %s"), |
| 294 | absolute_path(path), strerror(err)); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | NORETURN void unable_to_lock_die(const char *path, int err) |
| 299 | { |
| 300 | struct strbuf buf = STRBUF_INIT; |
| 301 | |
| 302 | unable_to_lock_message(path, err, &buf); |
| 303 | die("%s", buf.buf); |
| 304 | } |
| 305 | |
| 306 | /* This should return a meaningful errno on failure */ |
| 307 | int hold_lock_file_for_update_timeout_mode(struct lock_file *lk, |
| 308 | const char *path, int flags, |
| 309 | long timeout_ms, int mode) |
| 310 | { |
| 311 | int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode); |
| 312 | if (fd < 0) { |
| 313 | if (flags & LOCK_DIE_ON_ERROR) |
| 314 | unable_to_lock_die(path, errno); |
| 315 | if (flags & LOCK_REPORT_ON_ERROR) { |
| 316 | struct strbuf buf = STRBUF_INIT; |
| 317 | unable_to_lock_message(path, errno, &buf); |
| 318 | error("%s", buf.buf); |
| 319 | strbuf_release(&buf); |
| 320 | } |
| 321 | } |
| 322 | return fd; |
| 323 | } |
| 324 | |
| 325 | char *get_locked_file_path(struct lock_file *lk) |
| 326 | { |
| 327 | struct strbuf ret = STRBUF_INIT; |
| 328 | |
| 329 | strbuf_addstr(&ret, get_tempfile_path(lk->tempfile)); |
| 330 | if (ret.len <= LOCK_SUFFIX_LEN || |
| 331 | strcmp(ret.buf + ret.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX)) |
| 332 | BUG("get_locked_file_path() called for malformed lock object"); |
| 333 | /* remove ".lock": */ |
| 334 | strbuf_setlen(&ret, ret.len - LOCK_SUFFIX_LEN); |
| 335 | return strbuf_detach(&ret, NULL); |
| 336 | } |
| 337 | |
| 338 | int commit_lock_file(struct lock_file *lk) |
| 339 | { |
| 340 | char *result_path = get_locked_file_path(lk); |
| 341 | |
| 342 | delete_tempfile(&lk->pid_tempfile); |
| 343 | |
| 344 | if (commit_lock_file_to(lk, result_path)) { |
| 345 | int save_errno = errno; |
| 346 | free(result_path); |
| 347 | errno = save_errno; |
| 348 | return -1; |
| 349 | } |
| 350 | free(result_path); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | int rollback_lock_file(struct lock_file *lk) |
| 355 | { |
| 356 | delete_tempfile(&lk->pid_tempfile); |
| 357 | return delete_tempfile(&lk->tempfile); |
| 358 | } |