[196] | 1 | /* |
---|
| 2 | ** error.c |
---|
| 3 | ** |
---|
| 4 | ** Made by (mic) |
---|
| 5 | ** Login <mic@laptop> |
---|
| 6 | ** |
---|
| 7 | ** Started on Mon Mar 15 20:45:09 2010 mic |
---|
| 8 | ** Last update Sun May 12 01:17:25 2002 Speed Blue |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | #include <pthread.h> |
---|
| 12 | #include <stdio.h> |
---|
| 13 | #include <stdlib.h> |
---|
| 14 | #include <stdarg.h> |
---|
| 15 | #include <talloc.h> |
---|
| 16 | #include "aff4_errors.h" |
---|
| 17 | |
---|
| 18 | /** These slots carry the TLS error keys */ |
---|
| 19 | static pthread_key_t error_str_slot; |
---|
| 20 | static pthread_key_t error_value_slot; |
---|
| 21 | |
---|
| 22 | void error_dest(void *slot) { |
---|
| 23 | if(slot) talloc_free(slot); |
---|
[198] | 24 | } |
---|
[196] | 25 | |
---|
| 26 | void *raise_errors(enum _error_type t, char *reason, ...) { |
---|
| 27 | char *error_buffer; |
---|
| 28 | // This has to succeed: |
---|
| 29 | enum _error_type *type = aff4_get_current_error(&error_buffer); |
---|
| 30 | |
---|
| 31 | if(reason) { |
---|
| 32 | va_list ap; |
---|
| 33 | va_start(ap, reason); |
---|
| 34 | |
---|
| 35 | vsnprintf(error_buffer, ERROR_BUFFER_SIZE-1, reason,ap); |
---|
| 36 | error_buffer[ERROR_BUFFER_SIZE-1]=0; |
---|
| 37 | va_end(ap); |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | //update the error type |
---|
| 41 | *type = t; |
---|
| 42 | |
---|
| 43 | return NULL; |
---|
[198] | 44 | } |
---|
[196] | 45 | |
---|
| 46 | static int error_subsystem_initialised=0; |
---|
| 47 | |
---|
| 48 | enum _error_type *aff4_get_current_error(char **error_buffer) { |
---|
| 49 | enum _error_type *type; |
---|
| 50 | |
---|
| 51 | if(!error_subsystem_initialised) error_init(); |
---|
| 52 | |
---|
| 53 | type = pthread_getspecific(error_value_slot); |
---|
| 54 | |
---|
| 55 | // This is optional |
---|
| 56 | if(error_buffer) { |
---|
| 57 | *error_buffer = pthread_getspecific(error_str_slot); |
---|
| 58 | |
---|
| 59 | // If TLS buffers are not set we need to create them |
---|
| 60 | if(!*error_buffer) { |
---|
| 61 | *error_buffer =talloc_size(NULL, ERROR_BUFFER_SIZE); |
---|
| 62 | pthread_setspecific(error_str_slot, *error_buffer); |
---|
| 63 | }; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | if(!type) { |
---|
| 67 | type = talloc(NULL, enum _error_type); |
---|
| 68 | pthread_setspecific(error_value_slot, type); |
---|
| 69 | }; |
---|
| 70 | |
---|
| 71 | return type; |
---|
[198] | 72 | } |
---|
[196] | 73 | |
---|
| 74 | /** Initialise the error subsystem */ |
---|
| 75 | void error_init() { |
---|
| 76 | error_subsystem_initialised = 1; |
---|
| 77 | |
---|
| 78 | // We create the error buffer slots |
---|
| 79 | if(pthread_key_create(&error_str_slot, error_dest) || |
---|
| 80 | pthread_key_create(&error_value_slot, error_dest)) { |
---|
| 81 | printf("Unable to set up TLS variables\n"); |
---|
| 82 | abort(); |
---|
| 83 | }; |
---|
[198] | 84 | } |
---|