| 110 | } |
| 111 | |
| 112 | static void ha_open_crumb_log(int deadline_ms) { |
| 113 | const char *override = getenv("CBM_HOOK_TIMEOUT_LOG"); /* tests + power users */ |
| 114 | char path[CBM_SZ_1K]; |
| 115 | /* A path cut off at the buffer names some other file, and O_CREAT would |
| 116 | * create it. Such a path means no breadcrumb; the hook itself runs on. */ |
| 117 | if (override && override[0]) { |
| 118 | if (!ha_fits(snprintf(path, sizeof(path), "%s", override), sizeof(path))) { |
| 119 | return; |
| 120 | } |
| 121 | } else { |
| 122 | /* <cache_dir>/logs, where every cbm component logs: the cache dir is |
| 123 | * CBM_CACHE_DIR, else HOME (then USERPROFILE) + the default. */ |
| 124 | const char *cache = cbm_resolve_cache_dir(); |
| 125 | char dir[CBM_SZ_1K]; |
| 126 | if (!cache || !ha_fits(snprintf(dir, sizeof(dir), "%s/logs", cache), sizeof(dir)) || |
| 127 | !ha_fits(snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir), |
| 128 | sizeof(path))) { |
| 129 | return; |
| 130 | } |
| 131 | cbm_mkdir_p_ex(dir, 0755, CBM_MKDIR_FOLLOW_OWNED); |
| 132 | } |
| 133 | /* Startup may re-arm after resolving saved settings. Keep the original |
| 134 | * diagnostic descriptor and refresh only the deadline text below. */ |
| 135 | if (g_ha_crumb_fd < 0) { |
| 136 | g_ha_crumb_fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); |
| 137 | } |
| 138 | if (g_ha_crumb_fd < 0) { |
| 139 | return; |
| 140 | } |
| 141 | int n = snprintf(g_ha_crumb_msg, sizeof(g_ha_crumb_msg), |
| 142 | "hook-augment: deadline_exceeded ms=%d pid=%ld (raise via " |
| 143 | "CBM_HOOK_DEADLINE_MS)\n", |
| 144 | deadline_ms, (long)getpid()); |
| 145 | g_ha_crumb_len = (n > 0 && n < (int)sizeof(g_ha_crumb_msg)) ? (size_t)n : 0; |
| 146 | } |
| 147 | |
| 148 | int cbm_hook_augment_deadline_ms_for_testing(void) { |
| 149 | return ha_deadline_ms(); |
no test coverage detected