| 1 | #ifndef UPSDCORE_H_ |
|---|
| 2 | #define UPSDCORE_H_ |
|---|
| 3 | |
|---|
| 4 | #include <time.h> |
|---|
| 5 | #include <sys/types.h> |
|---|
| 6 | #include <stdint.h> |
|---|
| 7 | #include "upsd.h" |
|---|
| 8 | #include "upsd_config.h" |
|---|
| 9 | |
|---|
| 10 | #if __GNUC__ >= 4 |
|---|
| 11 | #define UPSD_LOCAL __attribute__ ((visibility("hidden"))) |
|---|
| 12 | #else |
|---|
| 13 | #define UPSD_LOCAL |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | #define UPSD_LENGTHOF(array) (sizeof(array) / sizeof(*array)) |
|---|
| 17 | #define UPSD_MACRO_STRING(x) _UPSD_MACRO_STRING(x) |
|---|
| 18 | #define _UPSD_MACRO_STRING(x) #x |
|---|
| 19 | #define UPSD_LITERAL_PTRSIZE(x) x, (sizeof(x) - 1) |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | typedef struct upsd_listener upsd_listener_t; |
|---|
| 23 | typedef struct upsd_event upsd_event_t; |
|---|
| 24 | typedef struct upsd_inst upsd_inst_t; |
|---|
| 25 | typedef struct upsd_initlist upsd_initlist_t; |
|---|
| 26 | typedef struct upsd_handler upsd_handler_t; |
|---|
| 27 | typedef union upsd_handle upsd_handle_t; |
|---|
| 28 | |
|---|
| 29 | #define UPSD_ACTION_INTERNAL_CLEAN ((upsd_action_t) -1) |
|---|
| 30 | |
|---|
| 31 | /* Status handle for asynchronous init actions */ |
|---|
| 32 | union upsd_handle { |
|---|
| 33 | pid_t pid; |
|---|
| 34 | int descriptor; |
|---|
| 35 | void *pointer; |
|---|
| 36 | }; |
|---|
| 37 | |
|---|
| 38 | /* Dynamic state part of a service, changing throughout commands */ |
|---|
| 39 | struct upsd_handler { |
|---|
| 40 | uint16_t flags; |
|---|
| 41 | uint16_t awaiting; |
|---|
| 42 | int16_t status; |
|---|
| 43 | time_t timeout; |
|---|
| 44 | upsd_handle_t handle; |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | /* Service dataset */ |
|---|
| 48 | struct upsd_init { |
|---|
| 49 | uint16_t flags; |
|---|
| 50 | void *identifier; |
|---|
| 51 | char **options; |
|---|
| 52 | char **provides; |
|---|
| 53 | upsd_worker_t worker; |
|---|
| 54 | upsd_handler_t hndl; |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | /* Event listener instance */ |
|---|
| 58 | struct upsd_listener { |
|---|
| 59 | uint16_t flags; |
|---|
| 60 | upsd_action_t action; |
|---|
| 61 | char *emitter; |
|---|
| 62 | char *receiver; |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | /* Event dataset */ |
|---|
| 66 | struct upsd_event { |
|---|
| 67 | uint16_t receiving; |
|---|
| 68 | uint16_t emitting; |
|---|
| 69 | upsd_listener_t **receivers; |
|---|
| 70 | upsd_listener_t **emitters; |
|---|
| 71 | upsd_init_t *designated; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | /* Service list */ |
|---|
| 75 | struct upsd_initlist { |
|---|
| 76 | upsd_init_t *init; |
|---|
| 77 | upsd_initlist_t *next; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | /* Command action instance */ |
|---|
| 81 | struct upsd_inst { |
|---|
| 82 | uint16_t flags; |
|---|
| 83 | uint16_t remaining; |
|---|
| 84 | uint16_t active; |
|---|
| 85 | uint16_t lasterror; |
|---|
| 86 | time_t time; |
|---|
| 87 | upsd_initlist_t *actions; |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | #include "cbi2/hmap.h" |
|---|
| 92 | |
|---|
| 93 | struct upsd_state { |
|---|
| 94 | cbi2_hmap_t *events; |
|---|
| 95 | upsd_report_t *report; |
|---|
| 96 | size_t reportidx; |
|---|
| 97 | upsd_inst_t inst; |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | #define UPSD_INIT_LEGACY 0x01 |
|---|
| 102 | #define UPSD_INIT_INTERNAL 0x02 |
|---|
| 103 | |
|---|
| 104 | #if UPSD_LOGGING >= 4 |
|---|
| 105 | #define UPSD_LOG_INFO(...) syslog(LOG_INFO, __VA_ARGS__) |
|---|
| 106 | #else |
|---|
| 107 | #define UPSD_LOG_INFO(...) |
|---|
| 108 | #endif |
|---|
| 109 | |
|---|
| 110 | #if UPSD_LOGGING >= 3 |
|---|
| 111 | #define UPSD_LOG_NOTE(...) syslog(LOG_NOTICE, __VA_ARGS__) |
|---|
| 112 | #else |
|---|
| 113 | #define UPSD_LOG_NOTE(...) |
|---|
| 114 | #endif |
|---|
| 115 | |
|---|
| 116 | #if UPSD_LOGGING >= 2 |
|---|
| 117 | #define UPSD_LOG_WARN(...) syslog(LOG_WARNING, __VA_ARGS__) |
|---|
| 118 | #else |
|---|
| 119 | #define UPSD_LOG_WARN(...) |
|---|
| 120 | #endif |
|---|
| 121 | |
|---|
| 122 | #if UPSD_LOGGING >= 1 |
|---|
| 123 | #define UPSD_LOG_ERROR(...) syslog(LOG_ERR, __VA_ARGS__) |
|---|
| 124 | #include <syslog.h> |
|---|
| 125 | #else |
|---|
| 126 | #define UPSD_LOG_ERROR(...) |
|---|
| 127 | #endif |
|---|
| 128 | |
|---|
| 129 | #endif /* UPSDCORE_H_ */ |
|---|