| 1 | /* |
| 2 | * Copyright 2018 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 | * "Jan Černý" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | #include "_sexp-types.h" |
| 30 | #include "_seap-types.h" |
| 31 | #include "sch_queue.h" |
| 32 | #include "seap-descriptor.h" |
| 33 | #include "common/debug_priv.h" |
| 34 | #include "../probe/probe_main.h" |
| 35 | #include "oval_definitions.h" |
| 36 | |
| 37 | |
| 38 | int sch_queue_connect(SEAP_desc_t *desc) |
| 39 | { |
| 40 | sch_queuedata_t *data = malloc(sizeof(sch_queuedata_t)); |
| 41 | |
| 42 | data->from_probe_queue = oscap_queue_new(); |
| 43 | data->from_probe_cnt = 0; |
| 44 | pthread_cond_init(&data->from_probe_cond, NULL); |
| 45 | pthread_mutex_init(&data->from_probe_mutex, NULL); |
| 46 | |
| 47 | data->to_probe_queue = oscap_queue_new(); |
| 48 | data->to_probe_cnt = 0; |
| 49 | pthread_cond_init(&data->to_probe_cond, NULL); |
| 50 | pthread_mutex_init(&data->to_probe_mutex, NULL); |
| 51 | |
| 52 | data->parent_thread_id = pthread_self(); |
| 53 | |
| 54 | struct probe_common_main_argument *arg = malloc(sizeof(struct probe_common_main_argument)); |
| 55 | arg->subtype = desc->subtype; |
| 56 | arg->queuedata = data; |
| 57 | desc->arg = arg; |
| 58 | |
| 59 | pthread_attr_t attr; |
| 60 | pthread_attr_init(&attr); |
| 61 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 62 | if (pthread_create(&data->probe_thread_id, &attr, &probe_common_main, arg)) { |
| 63 | dE("Cannot create probe thread"); |
| 64 | return -1; |
| 65 | } |
| 66 | pthread_attr_destroy(&attr); |
| 67 | |
| 68 | desc->scheme_data = (void *) data; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | SEXP_t *sch_queue_recvsexp(SEAP_desc_t *desc) |
| 73 | { |
| 74 | sch_queuedata_t *data = (sch_queuedata_t *)desc->scheme_data; |
| 75 | struct oscap_queue *queue; |
| 76 | pthread_mutex_t *mutex; |
| 77 | pthread_cond_t *cond; |
| 78 | int *cnt; |
| 79 | if (pthread_equal(pthread_self(), data->parent_thread_id)) { |
| 80 | queue = data->from_probe_queue; |
| 81 | mutex = &data->from_probe_mutex; |
| 82 | cond = &data->from_probe_cond; |
| 83 | cnt = &data->from_probe_cnt; |
| 84 | } else { |
| 85 | queue = data->to_probe_queue; |
| 86 | mutex = &data->to_probe_mutex; |
| 87 | cond = &data->to_probe_cond; |
| 88 | cnt = &data->to_probe_cnt; |
| 89 | } |
| 90 | pthread_mutex_lock(mutex); |
| 91 | while (*cnt == 0) { |
| 92 | pthread_cond_wait(cond, mutex); |
| 93 | } |
| 94 | SEXP_t *sexp = oscap_queue_remove(queue); |
| 95 | (*cnt)--; |
| 96 | pthread_mutex_unlock(mutex); |
| 97 | return sexp; |
| 98 | } |
| 99 | |
| 100 | ssize_t sch_queue_sendsexp(SEAP_desc_t *desc, SEXP_t *sexp, uint32_t flags) |
| 101 | { |
| 102 | sch_queuedata_t *data = (sch_queuedata_t *) desc->scheme_data; |
| 103 | struct oscap_queue *queue; |
| 104 | pthread_mutex_t *mutex; |
| 105 | pthread_cond_t *cond; |
| 106 | int *cnt; |
| 107 | if (pthread_equal(pthread_self(), data->parent_thread_id)) { |
| 108 | queue = data->to_probe_queue; |
| 109 | mutex = &data->to_probe_mutex; |
| 110 | cond = &data->to_probe_cond; |
| 111 | cnt = &data->to_probe_cnt; |
| 112 | } else { |
| 113 | queue = data->from_probe_queue; |
| 114 | mutex = &data->from_probe_mutex; |
| 115 | cond = &data->from_probe_cond; |
| 116 | cnt = &data->from_probe_cnt; |
| 117 | } |
| 118 | /* We want to send a SEXP, but the receiver expects a list of SEXPs. */ |
| 119 | SEXP_t *sexp_list = SEXP_list_new(sexp, NULL); |
| 120 | pthread_mutex_lock(mutex); |
| 121 | oscap_queue_add(queue, (void *) sexp_list); |
| 122 | (*cnt)++; |
| 123 | pthread_cond_broadcast(cond); |
| 124 | pthread_mutex_unlock(mutex); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int sch_queue_close(SEAP_desc_t *desc, uint32_t flags) |
| 129 | { |
| 130 | int ret = 0; |
| 131 | sch_queuedata_t *data = (sch_queuedata_t *) desc->scheme_data; |
| 132 | if (pthread_cancel(data->probe_thread_id) != 0) { |
| 133 | dE("Could not cancel %s_probe main thread.", oval_subtype_get_text(desc->subtype)); |
| 134 | ret = -1; |
| 135 | goto cleanup; |
| 136 | } |
| 137 | void *status; |
| 138 | const char *subtype_str = oval_subtype_get_text(desc->subtype); |
| 139 | ret = pthread_join(data->probe_thread_id, &status); |
| 140 | if (ret != 0) { |
| 141 | dE("Return code of %s_probe main thread is %d.", subtype_str, ret); |
| 142 | } |
| 143 | cleanup: |
| 144 | oscap_queue_free(data->to_probe_queue, NULL); |
| 145 | oscap_queue_free(data->from_probe_queue, NULL); |
| 146 | free(data); |
| 147 | free(desc->arg); |
| 148 | return ret; |
| 149 | } |