1 /*- 2 * Copyright (c) 2003-2004 Andrey Simonenko 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * @(#)$Id: ipa_mod.h,v 1.4 2011/01/23 18:42:34 simon Exp $ 27 */ 28 29 #ifndef IPA_MOD_H 30 #define IPA_MOD_H 31 32 #include <sys/types.h> 33 34 #include <regex.h> 35 #include <stdarg.h> 36 #include <time.h> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 #define IPA_MEMFUNC_API_VERSION 1 /* Version of ipa_memfunc API. */ 43 44 #define IPA_MEMFUNC_FLAG_PTHREAD 0x01 /* Sync access to memory object. */ 45 #define IPA_MEMFUNC_FLAG_OPTIMIZE 0x02 /* Optimize memory allocations. */ 46 47 typedef struct ipa_mem_type_struct ipa_mem_type; 48 typedef struct ipa_marray_struct ipa_marray; 49 typedef struct ipa_mzone_struct ipa_mzone; 50 51 /* 52 * Exported memory manipulation functions for module. 53 */ 54 typedef struct { 55 unsigned int api_ver; 56 int (*memfunc_init)(void); 57 void (*memfunc_deinit)(int); 58 ipa_mem_type *m_parser; 59 ipa_mem_type *(*mem_type_new)(const char *, const char *, 60 unsigned int); 61 void *(*mem_malloc)(size_t, ipa_mem_type *); 62 void *(*mem_calloc)(size_t, size_t, ipa_mem_type *); 63 void *(*mem_realloc)(void *, size_t, ipa_mem_type *); 64 char *(*mem_strdup)(const char *, ipa_mem_type *); 65 int (*mem_vasprintf)(ipa_mem_type *, char **, 66 const char *, va_list); 67 void (*mem_free)(void *, ipa_mem_type *); 68 ipa_marray *(*marray_init)(const char *, const char *, 69 unsigned int, void **, size_t, unsigned int, 70 unsigned int); 71 void (*marray_deinit)(ipa_marray *); 72 int (*marray_alloc)(ipa_marray *, unsigned int *, int); 73 void (*marray_free)(ipa_marray *, unsigned int); 74 void (*marray_minimize)(ipa_marray *); 75 int (*marray_check_index)(ipa_marray *, unsigned int); 76 unsigned int (*marray_nused)(ipa_marray *); 77 ipa_mzone *(*mzone_init)(const char *, const char *, 78 unsigned int, size_t, unsigned int, unsigned int); 79 void (*mzone_deinit)(ipa_mzone *); 80 void *(*mzone_alloc)(ipa_mzone *); 81 void (*mzone_free)(ipa_mzone *, void *); 82 unsigned int (*mzone_nused)(ipa_mzone *); 83 } ipa_memfunc; 84 85 /* 86 * Versions of ipa_mod(3) APIs. 87 */ 88 #define IPA_AC_MOD_API_VERSION 2 /* Version of ipa_ac_mod API. */ 89 #define IPA_DB_MOD_API_VERSION 2 /* Version of ipa_db_mod API. */ 90 #define IPA_ST_MOD_API_VERSION 2 /* Version of ipa_st_mod API. */ 91 92 /* 93 * Modules flags. 94 */ 95 #define IPA_MOD_FLAG_PTHREAD_SAFE 0x1 96 97 /* 98 * Log priority constants. 99 */ 100 enum { 101 IPA_LOG_INFO = 0, 102 IPA_LOG_WARNING, /* 1 */ 103 IPA_LOG_ERR /* 2 */ 104 }; 105 106 /* 107 * Type of parameter's argument. 108 */ 109 #define IPA_CONF_TYPE_INT32 0 /* int32_t */ 110 #define IPA_CONF_TYPE_UINT32 1 /* uint32_t */ 111 #define IPA_CONF_TYPE_INT64 2 /* int64_t */ 112 #define IPA_CONF_TYPE_UINT64 3 /* uint64_t */ 113 #define IPA_CONF_TYPE_STRING 4 /* "string" */ 114 #define IPA_CONF_TYPE_BYTES 5 /* bytes */ 115 #define IPA_CONF_TYPE_TIME 6 /* time */ 116 #define IPA_CONF_TYPE_VALUE 7 /* value */ 117 #define IPA_CONF_TYPE_PER_CENT 8 /* per cent */ 118 #define IPA_CONF_TYPE_BOOLEAN 9 /* int */ 119 #define IPA_CONF_TYPE_MISC 10 /* anything */ 120 121 /* 122 * IDs of section visible for modules. 123 */ 124 #define IPA_CONF_SECT_ROOT 1 125 #define IPA_CONF_SECT_GLOBAL 2 126 #define IPA_CONF_SECT_RULE 3 127 #define IPA_CONF_SECT_LIMIT 4 128 #define IPA_CONF_SECT_THRESHOLD 5 129 #define IPA_CONF_SECT_AUTORULE 6 130 #define IPA_CONF_SECT_RULEPAT 7 131 #define IPA_CONF_SECT_RESTART 8 132 #define IPA_CONF_SECT_REACH 9 133 #define IPA_CONF_SECT_EXPIRE 10 134 #define IPA_CONF_SECT_STARTUP 11 135 #define IPA_CONF_SECT_SHUTDOWN 12 136 #define IPA_CONF_SECT_IF_REACHED 13 137 #define IPA_CONF_SECT_IF_NOT_REACHED 14 138 #define IPA_CONF_SECT_IF_ALL_REACHED 15 139 #define IPA_CONF_SECT_IF_ANY_REACHED 16 140 #define IPA_CONF_SECT_IF_ALL_NOT_REACHED 17 141 #define IPA_CONF_SECT_IF_ANY_NOT_REACHED 18 142 #define IPA_CONF_SECT_BELOW_THRESHOLD 19 143 #define IPA_CONF_SECT_EQUAL_THRESHOLD 20 144 #define IPA_CONF_SECT_ABOVE_THRESHOLD 21 145 146 /* 147 * The offset for custom sections numbers. 148 */ 149 #define IPA_CONF_SECT_CUSTOM_OFFSET 0x80000000 150 151 /* 152 * A structure for describing a parameter. 153 */ 154 typedef struct { 155 const char *param_name; 156 int arg_nargs; 157 const char *arg_pattern; 158 regex_t *arg_regexp; 159 unsigned int arg_type; 160 const unsigned int *param_where; 161 int (*arg_parse)(void *); 162 } ipa_conf_param; 163 164 /* 165 * A structure for describing a section. 166 */ 167 typedef struct { 168 const char *sect_name; 169 unsigned int sect_id; 170 int arg_nargs; 171 const char *arg_pattern; 172 regex_t *arg_regexp; 173 unsigned int arg_type; 174 const unsigned int *sect_where; 175 int (*arg_parse)(void *); 176 } ipa_conf_sect; 177 178 /* 179 * Configuration events constants. 180 */ 181 #define IPA_CONF_EVENT_GLOBAL_BEGIN 0 182 #define IPA_CONF_EVENT_GLOBAL_END 1 183 #define IPA_CONF_EVENT_RULE_BEGIN 2 184 #define IPA_CONF_EVENT_RULE_END 3 185 #define IPA_CONF_EVENT_LIMIT_BEGIN 4 186 #define IPA_CONF_EVENT_LIMIT_END 5 187 #define IPA_CONF_EVENT_THRESHOLD_BEGIN 6 188 #define IPA_CONF_EVENT_THRESHOLD_END 7 189 #define IPA_CONF_EVENT_AUTORULE_BEGIN 8 190 #define IPA_CONF_EVENT_AUTORULE_END 9 191 #define IPA_CONF_EVENT_RULEPAT_BEGIN 10 192 #define IPA_CONF_EVENT_RULEPAT_END 11 193 #define IPA_CONF_EVENT_CUSTOM_SECT_BEGIN 12 194 #define IPA_CONF_EVENT_CUSTOM_SECT_END 13 195 #define IPA_CONF_EVENT_STARTUP_BEGIN 14 196 #define IPA_CONF_EVENT_STARTUP_END 15 197 #define IPA_CONF_EVENT_SHUTDOWN_BEGIN 16 198 #define IPA_CONF_EVENT_SHUTDOWN_END 17 199 #define IPA_CONF_EVENT_RESTART_BEGIN 18 200 #define IPA_CONF_EVENT_RESTART_END 19 201 #define IPA_CONF_EVENT_REACH_BEGIN 20 202 #define IPA_CONF_EVENT_REACH_END 21 203 #define IPA_CONF_EVENT_EXPIRE_BEGIN 22 204 #define IPA_CONF_EVENT_EXPIRE_END 23 205 #define IPA_CONF_EVENT_IF_REACHED_BEGIN 24 206 #define IPA_CONF_EVENT_IF_REACHED_END 25 207 #define IPA_CONF_EVENT_IF_NOT_REACHED_BEGIN 26 208 #define IPA_CONF_EVENT_IF_NOT_REACHED_END 27 209 #define IPA_CONF_EVENT_IF_ALL_REACHED_BEGIN 28 210 #define IPA_CONF_EVENT_IF_ALL_REACHED_END 29 211 #define IPA_CONF_EVENT_IF_ANY_REACHED_BEGIN 30 212 #define IPA_CONF_EVENT_IF_ANY_REACHED_END 31 213 #define IPA_CONF_EVENT_IF_ANY_NOT_REACHED_BEGIN 32 214 #define IPA_CONF_EVENT_IF_ANY_NOT_REACHED_END 33 215 #define IPA_CONF_EVENT_IF_ALL_NOT_REACHED_BEGIN 34 216 #define IPA_CONF_EVENT_IF_ALL_NOT_REACHED_END 35 217 #define IPA_CONF_EVENT_BELOW_THRESHOLD_BEGIN 36 218 #define IPA_CONF_EVENT_BELOW_THRESHOLD_END 37 219 #define IPA_CONF_EVENT_EQUAL_THRESHOLD_BEGIN 38 220 #define IPA_CONF_EVENT_EQUAL_THRESHOLD_END 39 221 #define IPA_CONF_EVENT_ABOVE_THRESHOLD_BEGIN 40 222 #define IPA_CONF_EVENT_ABOVE_THRESHOLD_END 41 223 224 /* 225 * Exported support functions for a module. 226 */ 227 typedef struct { 228 void (*print_string)(const char *); 229 void (*print_bytes)(const uint64_t *); 230 void (*print_time)(const uint64_t *); 231 void (*print_value)(const uint64_t *, unsigned int); 232 void (*print_boolean)(int); 233 void (*print_space)(void); 234 void (*print_param_name)(const char *, const char *); 235 void (*print_param_args)(const char *, va_list); 236 void (*print_param_end)(void); 237 void (*print_sect_name)(const char *, const char *); 238 void (*print_sect_args)(const char *, va_list); 239 void (*print_sect_begin)(void); 240 void (*print_sect_end)(void); 241 void (*open_log)(void); 242 void (*close_log)(void); 243 void (*logmsg)(const char *, int, int, const char *, va_list); 244 void (*logconferr)(const char *, int, const char *, va_list); 245 int (*local_sym_add)(char *, char *, int); 246 int (*local_sym_del)(const char *); 247 int (*global_sym_add)(char *, char *, int); 248 int (*global_sym_del)(const char *); 249 } ipa_suppfunc; 250 251 /* 252 * Limit's events constants. 253 */ 254 #define IPA_LIMIT_EVENT_START 0 255 #define IPA_LIMIT_EVENT_RESTART 1 256 #define IPA_LIMIT_EVENT_RESTART_EXEC 2 257 #define IPA_LIMIT_EVENT_REACH 3 258 #define IPA_LIMIT_EVENT_REACH_EXEC 4 259 #define IPA_LIMIT_EVENT_EXPIRE 5 260 #define IPA_LIMIT_EVENT_EXPIRE_EXEC 6 261 #define IPA_LIMIT_EVENT_UPDATED 7 262 #define IPA_LIMIT_EVENT_STARTUP_IF_REACHED 8 263 #define IPA_LIMIT_EVENT_STARTUP_IF_NOT_REACHED 9 264 #define IPA_LIMIT_EVENT_SHUTDOWN_IF_REACHED 10 265 #define IPA_LIMIT_EVENT_SHUTDOWN_IF_NOT_REACHED 11 266 267 /* 268 * Events bits for limit state. 269 */ 270 #define IPA_LIMIT_EVENT_START_SET (1 << IPA_LIMIT_EVENT_START) 271 #define IPA_LIMIT_EVENT_RESTART_SET (1 << IPA_LIMIT_EVENT_RESTART) 272 #define IPA_LIMIT_EVENT_RESTART_EXEC_SET (1 << IPA_LIMIT_EVENT_RESTART_EXEC) 273 #define IPA_LIMIT_EVENT_REACH_SET (1 << IPA_LIMIT_EVENT_REACH) 274 #define IPA_LIMIT_EVENT_REACH_EXEC_SET (1 << IPA_LIMIT_EVENT_REACH_EXEC) 275 #define IPA_LIMIT_EVENT_EXPIRE_SET (1 << IPA_LIMIT_EVENT_EXPIRE) 276 #define IPA_LIMIT_EVENT_EXPIRE_EXEC_SET (1 << IPA_LIMIT_EVENT_EXPIRE_EXEC) 277 #define IPA_LIMIT_EVENT_UPDATED_SET (1 << IPA_LIMIT_EVENT_UPDATED) 278 279 /* Number of limit's events. */ 280 #define IPA_LIMIT_EVENT_NUM 8 281 282 /* 283 * ipa_tm is almost the same as struct tm{}, but: only 284 * tm_{year,mon,mday,hour,min,sec} fields are used and 285 * tm_year and tm_mon are equal to real year and month values. 286 */ 287 typedef struct tm ipa_tm; 288 289 /* 290 * Threshold's events constants. 291 */ 292 #define IPA_THRESHOLD_EVENT_BELOW 0 293 #define IPA_THRESHOLD_EVENT_EQUAL 1 294 #define IPA_THRESHOLD_EVENT_ABOVE 2 295 296 /* 297 * Accounting module API. 298 */ 299 struct ipa_ac_mod { 300 unsigned int api_ver; 301 unsigned int mod_flags; 302 const char *ac_name; 303 const ipa_suppfunc *suppfunc; 304 const ipa_memfunc *memfunc; 305 const char *conf_prefix; 306 ipa_conf_sect *conf_sect_tbl; 307 ipa_conf_param *conf_param_tbl; 308 int (*conf_init)(void); 309 int (*conf_deinit)(void); 310 int (*conf_event)(unsigned int, unsigned int, const void *); 311 int (*conf_mimic_real)(void); 312 int (*conf_inherit)(unsigned int, unsigned int, 313 const char *); 314 void (*conf_show)(unsigned int, unsigned int); 315 int (*ac_pre_init)(void); 316 int (*ac_init_autorule)(unsigned int, const char *); 317 int (*ac_init_dynrule)(unsigned int, unsigned int, 318 const char *); 319 int (*ac_init_statrule)(unsigned int, const char *); 320 int (*ac_init)(void); 321 int (*ac_deinit_rule)(unsigned int); 322 int (*ac_deinit_autorule)(unsigned int); 323 int (*ac_deinit)(void); 324 int (*ac_get_stat)(const ipa_tm *); 325 int (*ac_get_rule_stat)(unsigned int, int, unsigned int, 326 int *, uint64_t *); 327 int (*ac_set_autorule_active)(unsigned int, int); 328 int (*ac_set_rule_active)(unsigned int, int); 329 int (*ac_set_limit_active)(unsigned int, unsigned int, int); 330 int (*ac_set_threshold_active)(unsigned int, unsigned int, 331 int); 332 int (*ac_limit_event)(unsigned int, unsigned int, 333 unsigned int); 334 int (*ac_threshold_event)(unsigned int, unsigned int, 335 unsigned int); 336 int (*ac_create_rule)(const char *, unsigned int, 337 const char *, const char *); 338 int (*ac_delete_rule)(const char *, unsigned int); 339 }; 340 341 #define IPA_AC_MOD_ENTRY(x) x ## _ac_mod 342 343 /* 344 * Limit's state. 345 */ 346 struct ipa_limit_state { 347 uint64_t lim; 348 uint64_t cnt; 349 unsigned int event_date_set; 350 ipa_tm event_date[IPA_LIMIT_EVENT_NUM]; 351 }; 352 353 /* 354 * Threshold's state. 355 */ 356 struct ipa_threshold_state { 357 uint64_t thr; 358 uint64_t cnt; 359 ipa_tm tm_from; 360 ipa_tm tm_updated; 361 }; 362 363 /* 364 * Database module API. 365 */ 366 struct ipa_db_mod { 367 unsigned int api_ver; 368 unsigned int mod_flags; 369 const char *db_name; 370 const ipa_suppfunc *suppfunc; 371 const ipa_memfunc *memfunc; 372 const char *conf_prefix; 373 ipa_conf_sect *conf_sect_tbl; 374 ipa_conf_param *conf_param_tbl; 375 int (*conf_init)(void); 376 int (*conf_deinit)(void); 377 int (*conf_event)(unsigned int, unsigned int, const void *); 378 int (*conf_mimic_real)(void); 379 int (*conf_inherit)(unsigned int, unsigned int, 380 const char *); 381 void (*conf_show)(unsigned int, unsigned int); 382 int (*db_pre_init)(void); 383 int (*db_init_dynrule)(unsigned int, unsigned int, 384 const char *, const char *); 385 int (*db_init_statrule)(unsigned int, const char *, 386 const char *); 387 int (*db_init_dynlimit)(unsigned int, unsigned int, 388 const char *, const char *, unsigned int, 389 const char *, const char *); 390 int (*db_init_statlimit)(unsigned int, const char *, 391 const char *, unsigned int, const char *, 392 const char *); 393 int (*db_init_dynthreshold)(unsigned int, unsigned int, 394 const char *, const char *, unsigned int, 395 const char *, const char *); 396 int (*db_init_statthreshold)(unsigned int, const char *, 397 const char *, unsigned int, const char *, 398 const char *); 399 int (*db_init)(void); 400 int (*db_get_limit_state)(unsigned int, unsigned int, 401 struct ipa_limit_state *); 402 int (*db_set_limit_state)(unsigned int, unsigned int, 403 const struct ipa_limit_state *, int); 404 int (*db_get_threshold_state)(unsigned int, unsigned int, 405 struct ipa_threshold_state *); 406 int (*db_set_threshold_state)(unsigned int, unsigned int, 407 const struct ipa_threshold_state *); 408 int (*db_deinit_threshold)(unsigned int, unsigned int); 409 int (*db_deinit_limit)(unsigned int, unsigned int); 410 int (*db_deinit_rule)(unsigned int); 411 int (*db_deinit)(void); 412 int (*db_append_rule)(unsigned int, const uint64_t *, 413 const ipa_tm *); 414 int (*db_update_rule)(unsigned int, const uint64_t *, 415 const ipa_tm *); 416 int (*db_update_limit)(unsigned int, unsigned int, 417 const uint64_t *, const ipa_tm *); 418 int (*db_limit_event)(unsigned int, unsigned int, 419 unsigned int, const ipa_tm *, const ipa_tm *); 420 int (*db_update_threshold)(unsigned int, unsigned int, 421 const uint64_t *, const ipa_tm *, const ipa_tm *); 422 int (*db_set_rule_active)(unsigned int, int); 423 int (*db_set_limit_active)(unsigned int, unsigned int, int); 424 int (*db_set_threshold_active)(unsigned int, unsigned int, 425 int); 426 }; 427 428 #define IPA_DB_MOD_ENTRY(x) x ## _db_mod 429 430 /* 431 * Statistics of one record from database. 432 */ 433 struct ipa_rule_stat { 434 uint64_t cnt; 435 unsigned int year; 436 unsigned char mon; 437 unsigned char mday; 438 unsigned char h1, m1, s1; 439 unsigned char h2, m2, s2; 440 }; 441 442 /* 443 * Description of one rule/limit/threshold. 444 */ 445 struct ipa_entity_desc { 446 char *name; 447 char *info; 448 }; 449 450 /* 451 * Statistics module API. 452 */ 453 struct ipa_st_mod { 454 unsigned int api_ver; 455 unsigned int mod_flags; 456 const char *st_name; 457 const ipa_suppfunc *suppfunc; 458 const ipa_memfunc *memfunc; 459 const char *conf_prefix; 460 ipa_conf_sect *conf_sect_tbl; 461 ipa_conf_param *conf_param_tbl; 462 int (*conf_init)(void); 463 int (*conf_deinit)(void); 464 int (*conf_event)(unsigned int, unsigned int, const void *); 465 int (*conf_mimic_real)(void); 466 int (*conf_inherit)(unsigned int, unsigned int, 467 const char *); 468 void (*conf_show)(unsigned int, unsigned int); 469 int (*st_pre_init)(void); 470 int (*st_init_rule)(unsigned int, const char *); 471 int (*st_init_limit)(unsigned int, const char *, 472 unsigned int, const char *); 473 int (*st_init_threshold)(unsigned int, const char *, 474 unsigned int, const char *); 475 int (*st_init)(void); 476 int (*st_deinit_threshold)(unsigned int, unsigned int); 477 int (*st_deinit_limit)(unsigned int, unsigned int); 478 int (*st_deinit_rule)(unsigned int); 479 int (*st_deinit)(void); 480 int (*st_get_rule_info)(unsigned int, ipa_mem_type *, 481 char **); 482 int (*st_get_limit_info)(unsigned int, unsigned int, 483 ipa_mem_type *, char **); 484 int (*st_get_threshold_info)(unsigned int, unsigned int, 485 ipa_mem_type *, char **); 486 int (*st_get_rules_list)(const char *, const regex_t *, 487 ipa_mem_type *, unsigned int*, 488 struct ipa_entity_desc **); 489 int (*st_get_limits_list)(unsigned int, const char *, 490 const regex_t *, ipa_mem_type *, unsigned int*, 491 struct ipa_entity_desc **); 492 int (*st_get_thresholds_list)(unsigned int, const char *, 493 const regex_t *, ipa_mem_type *, unsigned int *, 494 struct ipa_entity_desc **); 495 int (*st_get_rule_stat)(unsigned int, const ipa_tm *, 496 const ipa_tm *, int, ipa_mem_type *, unsigned int *, 497 struct ipa_rule_stat **); 498 int (*st_get_limit_stat)(unsigned int, unsigned int, 499 const ipa_tm *, const ipa_tm *, ipa_mem_type *, 500 unsigned int *, struct ipa_limit_state **); 501 int (*st_get_threshold_stat)(unsigned int, unsigned int, 502 struct ipa_threshold_state *); 503 }; 504 505 #define IPA_ST_MOD_ENTRY(x) x ## _st_mod 506 507 #ifdef __cplusplus 508 } 509 #endif 510 511 #endif /* !IPA_MOD_H */ 512