Ævar Arnfjörð Bjarmason | 5e3aba3 | 2021-09-26 21:03:26 +0200 | [diff] [blame] | 1 | #ifndef HOOK_H |
| 2 | #define HOOK_H |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 3 | #include "strvec.h" |
| 4 | |
| 5 | struct run_hooks_opt |
| 6 | { |
| 7 | /* Environment vars to be set for each hook */ |
| 8 | struct strvec env; |
| 9 | |
| 10 | /* Args to be passed to each hook */ |
| 11 | struct strvec args; |
| 12 | |
| 13 | /* Emit an error if the hook is missing */ |
| 14 | unsigned int error_if_missing:1; |
Emily Shaffer | 1a3017d | 2021-12-22 04:59:36 +0100 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * An optional initial working directory for the hook, |
| 18 | * translates to "struct child_process"'s "dir" member. |
| 19 | */ |
| 20 | const char *dir; |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * A pointer which if provided will be set to 1 or 0 depending |
| 24 | * on if a hook was started, regardless of whether or not that |
| 25 | * was successful. I.e. if the underlying start_command() was |
| 26 | * successful this will be set to 1. |
| 27 | * |
| 28 | * Used for avoiding TOCTOU races in code that would otherwise |
| 29 | * call hook_exist() after a "maybe hook run" to see if a hook |
| 30 | * was invoked. |
| 31 | */ |
| 32 | int *invoked_hook; |
Emily Shaffer | 917e080 | 2023-02-08 20:21:13 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Path to file which should be piped to stdin for each hook. |
| 36 | */ |
| 37 | const char *path_to_stdin; |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | #define RUN_HOOKS_OPT_INIT { \ |
| 41 | .env = STRVEC_INIT, \ |
| 42 | .args = STRVEC_INIT, \ |
| 43 | } |
| 44 | |
| 45 | struct hook_cb_data { |
| 46 | /* rc reflects the cumulative failure state */ |
| 47 | int rc; |
| 48 | const char *hook_name; |
| 49 | const char *hook_path; |
| 50 | struct run_hooks_opt *options; |
| 51 | }; |
Ævar Arnfjörð Bjarmason | 5e3aba3 | 2021-09-26 21:03:26 +0200 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Returns the path to the hook file, or NULL if the hook is missing |
| 55 | * or disabled. Note that this points to static storage that will be |
| 56 | * overwritten by further calls to find_hook and run_hook_*. |
| 57 | */ |
| 58 | const char *find_hook(const char *name); |
| 59 | |
Emily Shaffer | 330155e | 2021-09-26 21:03:27 +0200 | [diff] [blame] | 60 | /** |
| 61 | * A boolean version of find_hook() |
| 62 | */ |
| 63 | int hook_exists(const char *hookname); |
| 64 | |
Emily Shaffer | 96e7225 | 2021-12-22 04:59:27 +0100 | [diff] [blame] | 65 | /** |
| 66 | * Takes a `hook_name`, resolves it to a path with find_hook(), and |
| 67 | * runs the hook for you with the options specified in "struct |
| 68 | * run_hooks opt". Will free memory associated with the "struct run_hooks_opt". |
| 69 | * |
| 70 | * Returns the status code of the run hook, or a negative value on |
| 71 | * error(). |
| 72 | */ |
| 73 | int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options); |
Ævar Arnfjörð Bjarmason | 474c119 | 2021-12-22 04:59:28 +0100 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * A wrapper for run_hooks_opt() which provides a dummy "struct |
| 77 | * run_hooks_opt" initialized with "RUN_HOOKS_OPT_INIT". |
| 78 | */ |
| 79 | int run_hooks(const char *hook_name); |
Ævar Arnfjörð Bjarmason | ab81cf2 | 2021-12-22 04:59:31 +0100 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Like run_hooks(), a wrapper for run_hooks_opt(). |
| 83 | * |
| 84 | * In addition to the wrapping behavior provided by run_hooks(), this |
| 85 | * wrapper takes a list of strings terminated by a NULL |
| 86 | * argument. These things will be used as positional arguments to the |
| 87 | * hook. This function behaves like the old run_hook_le() API. |
| 88 | */ |
| 89 | int run_hooks_l(const char *hook_name, ...); |
Ævar Arnfjörð Bjarmason | 5e3aba3 | 2021-09-26 21:03:26 +0200 | [diff] [blame] | 90 | #endif |