| 1 | /* |
| 2 | * Copyright 2011 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Daniel Kopecek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <pthread.h> |
| 29 | #include <string.h> |
| 30 | #include "_seap.h" |
| 31 | #include <probe-api.h> |
| 32 | |
| 33 | #if defined(OS_FREEBSD) |
| 34 | #include <pthread_np.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "common/debug_priv.h" |
| 38 | #include "../SEAP/generic/rbt/rbt.h" |
| 39 | #include "probe.h" |
| 40 | #include "worker.h" |
| 41 | #include "rcache.h" |
| 42 | #include "input_handler.h" |
| 43 | #include "common/compat_pthread_barrier.h" |
| 44 | |
| 45 | static void pthread_attr_cleanup_handler(void *arg) |
| 46 | { |
| 47 | pthread_attr_destroy(arg); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * The input handler waits for incomming eval requests and either returns |
| 52 | * a result immediately if it is found in the result cache or spawns a new |
| 53 | * worker thread which takes care of evaluating the request, caching the |
| 54 | * result and sending it to the requestee. |
| 55 | */ |
| 56 | void *probe_input_handler(void *arg) |
| 57 | { |
| 58 | pthread_attr_t pth_attr; |
| 59 | probe_t *probe = (probe_t *)arg; |
| 60 | |
| 61 | int probe_ret, cstate; /* XXX */ |
| 62 | SEAP_msg_t *seap_request, *seap_reply; |
| 63 | SEXP_t *probe_in, *probe_out, *oid; |
| 64 | |
| 65 | #if defined(HAVE_PTHREAD_SETNAME_NP) |
| 66 | # if defined(OS_APPLE) |
| 67 | pthread_setname_np("input_handler"); |
| 68 | # else |
| 69 | pthread_setname_np(pthread_self(), "input_handler"); |
| 70 | # endif |
| 71 | #endif |
| 72 | |
| 73 | #define TH_CANCEL_ON pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cstate) |
| 74 | #define TH_CANCEL_OFF pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cstate) |
| 75 | |
| 76 | TH_CANCEL_OFF; |
| 77 | |
| 78 | if (pthread_attr_init(&pth_attr)) |
| 79 | return (NULL); |
| 80 | |
| 81 | if (pthread_attr_setdetachstate(&pth_attr, PTHREAD_CREATE_JOINABLE)) { |
| 82 | pthread_attr_destroy(&pth_attr); |
| 83 | return (NULL); |
| 84 | } |
| 85 | |
| 86 | pthread_cleanup_push(pthread_attr_cleanup_handler, (void *)&pth_attr); |
| 87 | |
| 88 | switch (errno = pthread_barrier_wait(probe->th_barrier)) |
| 89 | { |
| 90 | case 0: |
| 91 | case PTHREAD_BARRIER_SERIAL_THREAD: |
| 92 | break; |
| 93 | default: |
| 94 | dE("pthread_barrier_wait: %d, %s.", |
| 95 | errno, strerror(errno)); |
| 96 | return (NULL); |
| 97 | } |
| 98 | |
| 99 | while(1) { |
| 100 | TH_CANCEL_ON; |
| 101 | |
| 102 | if (SEAP_recvmsg(probe->SEAP_ctx, probe->sd, &seap_request) == -1) { |
| 103 | dE("An error ocured while receiving SEAP message. errno=%u, %s.", errno, strerror(errno)); |
| 104 | |
| 105 | /* |
| 106 | * TODO: check for abort request |
| 107 | */ |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | TH_CANCEL_OFF; |
| 112 | |
| 113 | probe_in = SEAP_msg_get(seap_request); |
| 114 | |
| 115 | if (probe_in == NULL) |
| 116 | abort(); |
| 117 | |
| 118 | SEXP_VALIDATE(probe_in); |
| 119 | |
| 120 | /* |
| 121 | * Get a reference to the `id' attribute of the input object. The value |
| 122 | * of this attribute is the OVAL object ID and serves as a key in the |
| 123 | * result cache. |
| 124 | */ |
| 125 | oid = probe_obj_getattrval(probe_in, "id"); |
| 126 | |
| 127 | if (oid != NULL) { |
| 128 | SEXP_VALIDATE(oid); |
| 129 | probe_out = probe_rcache_sexp_get(probe->rcache, oid); |
| 130 | |
| 131 | if (probe_out == NULL) { /* cache miss */ |
| 132 | SEXP_t *skip_flag, *obj_mask; |
| 133 | |
| 134 | skip_flag = probe_obj_getattrval(probe_in, "skip_eval"); |
| 135 | obj_mask = probe_obj_getmask(probe_in); |
| 136 | SEXP_free(probe_in); |
| 137 | probe_in = NULL; |
| 138 | |
| 139 | if (skip_flag != NULL) { |
| 140 | oval_syschar_collection_flag_t cobj_flag; |
| 141 | |
| 142 | cobj_flag = SEXP_number_geti_32(skip_flag); |
| 143 | probe_out = probe_cobj_new(cobj_flag, NULL, NULL, obj_mask); |
| 144 | |
| 145 | if (probe_rcache_sexp_add(probe->rcache, oid, probe_out) != 0) { |
| 146 | /* TODO */ |
| 147 | abort(); |
| 148 | } |
| 149 | |
| 150 | probe_ret = 0; |
| 151 | SEXP_free(oid); |
| 152 | SEXP_free(skip_flag); |
| 153 | SEXP_free(obj_mask); |
| 154 | } else { |
| 155 | |
| 156 | SEXP_free(oid); |
| 157 | SEXP_free(skip_flag); |
| 158 | SEXP_free(obj_mask); |
| 159 | |
| 160 | probe_pwpair_t *pair = malloc(sizeof(probe_pwpair_t)); |
| 161 | pair->probe = probe; |
| 162 | pair->pth = probe_worker_new(); |
| 163 | pair->pth->sid = SEAP_msg_id(seap_request); |
| 164 | pair->pth->msg = seap_request; |
| 165 | pair->pth->msg_handler = &probe_worker; |
| 166 | |
| 167 | if (rbt_i32_add(probe->workers, pair->pth->sid, pair->pth, NULL) != 0) { |
| 168 | /* |
| 169 | * Getting here means that there is already a |
| 170 | * thread handling the message with the given |
| 171 | * ID. |
| 172 | */ |
| 173 | dW("Attempt to evaluate an object " |
| 174 | "(ID=%u) " // TODO: 64b IDs |
| 175 | "which is already being evaluated by another thread.", pair->pth->sid); |
| 176 | |
| 177 | free(pair->pth); |
| 178 | free(pair); |
| 179 | SEAP_msg_free(seap_request); |
| 180 | } else { |
| 181 | /* OK */ |
| 182 | |
| 183 | if (pthread_create(&pair->pth->tid, &pth_attr, &probe_worker_runfn, pair)) |
| 184 | { |
| 185 | dE("Cannot start a new worker thread: %d, %s.", errno, strerror(errno)); |
| 186 | |
| 187 | if (rbt_i32_del(probe->workers, pair->pth->sid, NULL) != 0) |
| 188 | dE("rbt_i32_del: failed to remove worker thread (ID=%u)", pair->pth->sid); |
| 189 | |
| 190 | SEAP_msg_free(pair->pth->msg); |
| 191 | free(pair->pth); |
| 192 | free(pair); |
| 193 | |
| 194 | probe_ret = PROBE_EUNKNOWN; |
| 195 | probe_out = NULL; |
| 196 | |
| 197 | goto __error_reply; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | seap_request = NULL; |
| 202 | continue; |
| 203 | } |
| 204 | } else { |
| 205 | /* cache hit */ |
| 206 | SEXP_free(oid); |
| 207 | SEXP_free(probe_in); |
| 208 | probe_ret = 0; |
| 209 | } |
| 210 | } else { |
| 211 | /* the `id' was not found in the input object */ |
| 212 | dE("No `id' attribute"); |
| 213 | probe_ret = PROBE_ENOATTR; |
| 214 | probe_out = NULL; |
| 215 | } |
| 216 | |
| 217 | if (probe_out == NULL || probe_ret != 0) { |
| 218 | __error_reply: |
| 219 | if (SEAP_replyerr(probe->SEAP_ctx, probe->sd, seap_request, probe_ret) == -1) |
| 220 | { |
| 221 | dE("An error ocured while sending error status. errno=%u, %s.", |
| 222 | errno, strerror(errno)); |
| 223 | |
| 224 | SEAP_msg_free(seap_request); |
| 225 | SEXP_free(probe_in); |
| 226 | |
| 227 | break; |
| 228 | } |
| 229 | SEXP_free(probe_in); |
| 230 | } else { |
| 231 | SEXP_VALIDATE(probe_out); |
| 232 | |
| 233 | seap_reply = SEAP_msg_new(); |
| 234 | SEAP_msg_set(seap_reply, probe_out); |
| 235 | SEXP_free(probe_out); |
| 236 | |
| 237 | if (SEAP_reply(probe->SEAP_ctx, probe->sd, seap_reply, seap_request) == -1) { |
| 238 | dE("An error ocured while sending SEAP message. errno=%u, %s.", |
| 239 | errno, strerror(errno)); |
| 240 | |
| 241 | /* TODO: check for abort request */ |
| 242 | SEAP_msg_free(seap_reply); |
| 243 | SEAP_msg_free(seap_request); |
| 244 | |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | SEAP_msg_free(seap_reply); |
| 249 | } |
| 250 | |
| 251 | SEAP_msg_free(seap_request); |
| 252 | } /* main loop */ |
| 253 | |
| 254 | pthread_cleanup_pop(1); |
| 255 | return (NULL); |
| 256 | } |