|
1 | 1 | /*
|
2 | 2 | * Copyright (c) 2005, Junio C Hamano
|
3 | 3 | */
|
| 4 | + |
| 5 | +/* |
| 6 | + * State diagram and cleanup |
| 7 | + * ------------------------- |
| 8 | + * |
| 9 | + * This module keeps track of all locked files in `lock_file_list` for |
| 10 | + * use at cleanup. This list and the `lock_file` objects that comprise |
| 11 | + * it must be kept in self-consistent states at all time, because the |
| 12 | + * program can be interrupted any time by a signal, in which case the |
| 13 | + * signal handler will walk through the list attempting to clean up |
| 14 | + * any open lock files. |
| 15 | + * |
| 16 | + * The possible states of a `lock_file` object are as follows: |
| 17 | + * |
| 18 | + * - Uninitialized. In this state the object's `on_list` field must be |
| 19 | + * zero but the rest of its contents need not be initialized. As |
| 20 | + * soon as the object is used in any way, it is irrevocably |
| 21 | + * registered in `lock_file_list`, and `on_list` is set. |
| 22 | + * |
| 23 | + * - Locked, lockfile open (after `hold_lock_file_for_update()`, |
| 24 | + * `hold_lock_file_for_append()`, or `reopen_lock_file()`). In this |
| 25 | + * state: |
| 26 | + * |
| 27 | + * - the lockfile exists |
| 28 | + * - `active` is set |
| 29 | + * - `filename` holds the filename of the lockfile |
| 30 | + * - `fd` holds a file descriptor open for writing to the lockfile |
| 31 | + * - `fp` holds a pointer to an open `FILE` object if and only if |
| 32 | + * `fdopen_lock_file()` has been called on the object |
| 33 | + * - `owner` holds the PID of the process that locked the file |
| 34 | + * |
| 35 | + * - Locked, lockfile closed (after successful `close_lock_file()`). |
| 36 | + * Same as the previous state, except that the lockfile is closed |
| 37 | + * and `fd` is -1. |
| 38 | + * |
| 39 | + * - Unlocked (after `commit_lock_file()`, `commit_lock_file_to()`, |
| 40 | + * `rollback_lock_file()`, a failed attempt to lock, or a failed |
| 41 | + * `close_lock_file()`). In this state: |
| 42 | + * |
| 43 | + * - `active` is unset |
| 44 | + * - `filename` is empty (usually, though there are transitory |
| 45 | + * states in which this condition doesn't hold). Client code should |
| 46 | + * *not* rely on the filename being empty in this state. |
| 47 | + * - `fd` is -1 |
| 48 | + * - the object is left registered in the `lock_file_list`, and |
| 49 | + * `on_list` is set. |
| 50 | + * |
| 51 | + * A lockfile is owned by the process that created it. The `lock_file` |
| 52 | + * has an `owner` field that records the owner's PID. This field is |
| 53 | + * used to prevent a forked process from closing a lockfile created by |
| 54 | + * its parent. |
| 55 | + */ |
| 56 | + |
4 | 57 | #include "cache.h"
|
5 | 58 | #include "lockfile.h"
|
6 | 59 | #include "sigchain.h"
|
|
0 commit comments