| 1 | /* independent printf adapter for the real libalpm reference's log callback. */ |
| 2 | #ifndef V_ALPM_REFERENCE_LOG_H |
| 3 | #define V_ALPM_REFERENCE_LOG_H |
| 4 | #include <alpm.h> |
| 5 | #include <errno.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdarg.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <sys/wait.h> |
| 11 | |
| 12 | typedef void (*v_alpm_reference_log_receiver)(void *, uint32_t, const char *); |
| 13 | struct v_alpm_reference_log_state { |
| 14 | v_alpm_reference_log_receiver receive; |
| 15 | void *context; |
| 16 | }; |
| 17 | static void v_alpm_reference_log(void *context, alpm_loglevel_t level, |
| 18 | const char *format, va_list args) { |
| 19 | struct v_alpm_reference_log_state *state = context; |
| 20 | va_list copy; |
| 21 | va_copy(copy, args); |
| 22 | int count = vsnprintf(NULL, 0, format, copy); |
| 23 | va_end(copy); |
| 24 | if(count < 0) { abort(); } |
| 25 | char *message = malloc((size_t)count + 1); |
| 26 | if(!message) { abort(); } |
| 27 | if(vsnprintf(message, (size_t)count + 1, format, args) != count) { abort(); } |
| 28 | state->receive(state->context, (uint32_t)level, message); |
| 29 | free(message); |
| 30 | } |
| 31 | static void *v_alpm_reference_log_install(void *handle, |
| 32 | v_alpm_reference_log_receiver receive, void *context) { |
| 33 | struct v_alpm_reference_log_state *state = malloc(sizeof(*state)); |
| 34 | if(!state) { abort(); } |
| 35 | state->receive = receive; |
| 36 | state->context = context; |
| 37 | if(alpm_option_set_logcb(handle, v_alpm_reference_log, state) != 0) { abort(); } |
| 38 | return state; |
| 39 | } |
| 40 | static void v_alpm_reference_log_remove(void *handle, void *state) { |
| 41 | if(alpm_option_set_logcb(handle, NULL, NULL) != 0) { abort(); } |
| 42 | free(state); |
| 43 | } |
| 44 | /* the probe has one command child; force the parent's later waitpid to fail. */ |
| 45 | static void v_alpm_test_reap_command(void) { |
| 46 | int status; |
| 47 | pid_t child; |
| 48 | do { child = waitpid(-1, &status, 0); } while(child < 0 && errno == EINTR); |
| 49 | if(child < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { abort(); } |
| 50 | } |
| 51 | #endif |
| 52 | |