| 1 | /** |
| 2 | * @file oval_probe_handler.c |
| 3 | * @brief OVAL probe handler API implementation |
| 4 | * @author "Daniel Kopecek" <[email protected]> |
| 5 | * |
| 6 | * @addtogroup PROBEHANDLERS |
| 7 | * @{ |
| 8 | */ |
| 9 | /* |
| 10 | * Copyright 2010 Red Hat Inc., Durham, North Carolina. |
| 11 | * All Rights Reserved. |
| 12 | * |
| 13 | * This library is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU Lesser General Public |
| 15 | * License as published by the Free Software Foundation; either |
| 16 | * version 2.1 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This library is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * Lesser General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU Lesser General Public |
| 24 | * License along with this library; if not, write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | * |
| 27 | * Authors: |
| 28 | * "Daniel Kopecek" <[email protected]> |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include "common/bfind.h" |
| 37 | #include "_oval_probe_handler.h" |
| 38 | |
| 39 | oval_phtbl_t *oval_phtbl_new(void) |
| 40 | { |
| 41 | oval_phtbl_t *phtbl = malloc(sizeof(oval_phtbl_t)); |
| 42 | phtbl->ph = NULL; |
| 43 | phtbl->sz = 0; |
| 44 | |
| 45 | return(phtbl); |
| 46 | } |
| 47 | |
| 48 | void oval_phtbl_free(oval_phtbl_t *phtbl) |
| 49 | { |
| 50 | register uint32_t i; |
| 51 | |
| 52 | for (i = 0; i < phtbl->sz; ++i) { |
| 53 | if (phtbl->ph[i]->func) |
| 54 | phtbl->ph[i]->func(phtbl->ph[i]->type, phtbl->ph[i]->uptr, PROBE_HANDLER_ACT_FREE); |
| 55 | free(phtbl->ph[i]); |
| 56 | } |
| 57 | |
| 58 | free(phtbl->ph); |
| 59 | free(phtbl); |
| 60 | } |
| 61 | |
| 62 | static int oval_handler_subtype_cmp(oval_subtype_t *a, oval_ph_t **b) |
| 63 | { |
| 64 | return(*a - (*b)->type); |
| 65 | } |
| 66 | |
| 67 | static int oval_phtbl_subtype_cmp(oval_ph_t **a, oval_ph_t **b) |
| 68 | { |
| 69 | return((*a)->type - (*b)->type); |
| 70 | } |
| 71 | |
| 72 | oval_ph_t *oval_probe_handler_get(oval_phtbl_t *phtbl, oval_subtype_t type) |
| 73 | { |
| 74 | oval_ph_t **ph = oscap_bfind((void *)phtbl->ph, phtbl->sz, sizeof(oval_ph_t *), &type, |
| 75 | (int(*)(void *, void *))oval_handler_subtype_cmp); |
| 76 | return (ph != NULL ? *ph : NULL); |
| 77 | } |
| 78 | |
| 79 | int oval_probe_handler_set(oval_phtbl_t *phtbl, oval_subtype_t type, oval_probe_handler_t *handler, void *uptr) |
| 80 | { |
| 81 | int ret = 0; |
| 82 | bool sort = false; |
| 83 | oval_ph_t *phrec; |
| 84 | |
| 85 | if (phtbl->sz > 0) { |
| 86 | phrec = oval_probe_handler_get(phtbl, type); |
| 87 | |
| 88 | if (phrec != NULL) { |
| 89 | if ((ret = phrec->func(type, phrec->uptr, PROBE_HANDLER_ACT_CLOSE)) != 0 || |
| 90 | (ret = phrec->func(type, phrec->uptr, PROBE_HANDLER_ACT_FREE)) != 0) |
| 91 | { |
| 92 | return(ret); |
| 93 | } |
| 94 | |
| 95 | goto fillrec; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void *new_ph = realloc(phtbl->ph, sizeof(oval_ph_t *) * ++phtbl->sz); |
| 100 | if (new_ph == NULL) |
| 101 | return -1; |
| 102 | phtbl->ph = new_ph; |
| 103 | |
| 104 | phrec = phtbl->ph[phtbl->sz - 1] = malloc(sizeof(oval_ph_t)); |
| 105 | sort = true; |
| 106 | fillrec: |
| 107 | if (phrec == NULL) { |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | phrec->type = type; |
| 112 | phrec->func = handler; |
| 113 | phrec->uptr = uptr; |
| 114 | |
| 115 | if ((ret = phrec->func(type, phrec->uptr, PROBE_HANDLER_ACT_INIT)) != 0) { |
| 116 | phrec->func = PROBE_HANDLER_IGNORE; |
| 117 | phrec->uptr = NULL; |
| 118 | } |
| 119 | |
| 120 | if (sort) |
| 121 | qsort(phtbl->ph, phtbl->sz, sizeof(oval_ph_t *), |
| 122 | (int(*)(const void *, const void *))oval_phtbl_subtype_cmp); |
| 123 | |
| 124 | return(ret); |
| 125 | } |
| 126 | |
| 127 | /// @} |