/* independent printf adapter for the real libalpm reference's log callback. */ #ifndef V_ALPM_REFERENCE_LOG_H #define V_ALPM_REFERENCE_LOG_H #include #include #include #include #include #include #include typedef void (*v_alpm_reference_log_receiver)(void *, uint32_t, const char *); struct v_alpm_reference_log_state { v_alpm_reference_log_receiver receive; void *context; }; static void v_alpm_reference_log(void *context, alpm_loglevel_t level, const char *format, va_list args) { struct v_alpm_reference_log_state *state = context; va_list copy; va_copy(copy, args); int count = vsnprintf(NULL, 0, format, copy); va_end(copy); if(count < 0) { abort(); } char *message = malloc((size_t)count + 1); if(!message) { abort(); } if(vsnprintf(message, (size_t)count + 1, format, args) != count) { abort(); } state->receive(state->context, (uint32_t)level, message); free(message); } static void *v_alpm_reference_log_install(void *handle, v_alpm_reference_log_receiver receive, void *context) { struct v_alpm_reference_log_state *state = malloc(sizeof(*state)); if(!state) { abort(); } state->receive = receive; state->context = context; if(alpm_option_set_logcb(handle, v_alpm_reference_log, state) != 0) { abort(); } return state; } static void v_alpm_reference_log_remove(void *handle, void *state) { if(alpm_option_set_logcb(handle, NULL, NULL) != 0) { abort(); } free(state); } /* the probe has one command child; force the parent's later waitpid to fail. */ static void v_alpm_test_reap_command(void) { int status; pid_t child; do { child = waitpid(-1, &status, 0); } while(child < 0 && errno == EINTR); if(child < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { abort(); } } #endif