Debian Codegraph @

src/OVAL/probes/probe/probe_main.c

raw symbols SCIP
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 * Tomas Heinrich <[email protected]>
22 */
23 #include <config.h>
24 #if !defined(_GNU_SOURCE)
25 # if defined(HAVE_PTHREAD_TIMEDJOIN_NP) && defined(HAVE_CLOCK_GETTIME)
26 # define _GNU_SOURCE
27 # endif
28 #endif
29 #include <stdio.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <pthread.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include "_seap.h"
36 #include "common/bfind.h"
37 #include "probe.h"
38 #include "ncache.h"
39 #include "rcache.h"
40 #include "icache.h"
41 #include "worker.h"
42 #include "input_handler.h"
43 #include "probe-api.h"
44 #include "option.h"
45 #include <oscap_debug.h>
46 #include "debug_priv.h"
47
48 #if defined(OS_FREEBSD)
49 #include <pthread_np.h>
50 #endif
51
52 #ifdef OS_WINDOWS
53 #define STDIN_FILENO _fileno(stdin)
54 #define STDOUT_FILENO _fileno(stdout)
55 #else
56 #include <unistd.h>
57 #endif
58
59 #include "probe_main.h"
60 #include "seap-descriptor.h"
61 #include "probe-table.h"
62
63 static int fail(int err, const char *who, int line)
64 {
65 fprintf(stderr, "FAIL: %d:%s: %d, %s\n", line, who, err, strerror(err));
66 exit(err);
67 }
68
69 void *OSCAP_GSYM(probe_arg) = NULL;
70 bool OSCAP_GSYM(varref_handling) = true;
71 char **OSCAP_GSYM(no_varref_ents) = NULL;
72 size_t OSCAP_GSYM(no_varref_ents_cnt) = 0;
73
74 extern probe_ncache_t *OSCAP_GSYM(ncache);
75
76 static int probe_optecmp(char **a, char **b)
77 {
78 return strcmp(*a, *b);
79 }
80
81 static SEXP_t *probe_reset(SEXP_t *arg0, void *arg1)
82 {
83 probe_t *probe = (probe_t *)arg1;
84 /*
85 * FIXME: implement main loop locking & worker waiting
86 */
87 probe_rcache_free(probe->rcache);
88
89 probe->rcache = probe_rcache_new();
90 probe_ncache_clear(OSCAP_GSYM(ncache));
91 probe->ncache = OSCAP_GSYM(ncache);
92
93 return(NULL);
94 }
95
96 static int probe_opthandler_varref(int option, int op, va_list args)
97 {
98 bool o_switch;
99 char *o_name;
100 char *o_temp;
101
102 if (op == PROBE_OPTION_GET)
103 return -1;
104
105 o_switch = va_arg(args, int);
106 o_name = va_arg(args, char *);
107
108 if (o_name == NULL) {
109 /* switch varref handling on/off globally */
110 OSCAP_GSYM(varref_handling) = o_switch;
111 return (0);
112 }
113
114 o_temp = oscap_bfind (OSCAP_GSYM(no_varref_ents), OSCAP_GSYM(no_varref_ents_cnt),
115 sizeof(char *), o_name, (int(*)(void *, void *)) &probe_optecmp);
116
117 if (o_temp != NULL)
118 return (0);
119
120 void *new_no_varref_ents = realloc(OSCAP_GSYM(no_varref_ents),
121 sizeof (char *) * (OSCAP_GSYM(no_varref_ents_cnt)+1));
122 if (new_no_varref_ents == NULL)
123 return -2;
124 OSCAP_GSYM(no_varref_ents_cnt)++;
125 OSCAP_GSYM(no_varref_ents) = new_no_varref_ents;
126 OSCAP_GSYM(no_varref_ents)[OSCAP_GSYM(no_varref_ents_cnt) - 1] = strdup(o_name);
127
128 qsort(OSCAP_GSYM(no_varref_ents), OSCAP_GSYM(no_varref_ents_cnt),
129 sizeof (char *), (int(*)(const void *, const void *))&probe_optecmp);
130
131 return (0);
132 }
133
134 static int probe_opthandler_rcache(int option, int op, va_list args)
135 {
136 return (0);
137 }
138
139 static void probe_common_main_cleanup(void *arg)
140 {
141 dD("probe_common_main_cleanup started");
142
143 probe_t *probe = (probe_t *)arg;
144 /* Cancel probe_input_handler thread */
145 if (pthread_cancel(probe->th_input) != 0) {
146 dE("Cannot cancel the probe input thread.");
147 return;
148 }
149
150 void *status;
151 int ret = pthread_join(probe->th_input, &status);
152 if (ret != 0) {
153 dE("pthread_join of probe_input_handler thread has failed: %d", ret);
154 }
155 dD("probe_input_handler thread has joined with status %ld", (long) status);
156
157 probe_fini_function_t fini_function = probe_table_get_fini_function(probe->subtype);
158 if (fini_function != NULL) {
159 fini_function(probe->probe_arg);
160 }
161
162 probe_rcache_free(probe->rcache);
163 probe_icache_free(probe->icache);
164 rbt_i32_free(probe->workers);
165 SEAP_CTX_free(probe->SEAP_ctx);
166 free(probe->option);
167
168 dD("probe_common_main_cleanup finished");
169 }
170
171 void *probe_common_main(void *arg)
172 {
173 pthread_attr_t th_attr;
174 probe_t probe;
175 struct probe_common_main_argument *probe_argument = (struct probe_common_main_argument *) arg;
176 sch_queuedata_t *data = probe_argument->queuedata;
177 oval_subtype_t subtype = probe_argument->subtype;
178 probe.subtype = subtype;
179 probe.real_root_fd = -1;
180 probe.real_cwd_fd = -1;
181
182 #if defined(HAVE_PTHREAD_SETNAME_NP)
183 # if defined(OS_APPLE)
184 pthread_setname_np("common_main");
185 # else
186 pthread_setname_np(pthread_self(), "common_main");
187 # endif
188 #endif
189
190 dD("probe_common_main started");
191
192 pthread_barrier_t barrier;
193
194 const unsigned thread_count = 2; // input and icache threads
195 if ((errno = pthread_barrier_init(&barrier, NULL, thread_count)) != 0) {
196 fail(errno, "pthread_barrier_init", __LINE__ - 6);
197 }
198
199 probe.offline_mode = false;
200 probe.selected_offline_mode = PROBE_OFFLINE_NONE;
201 probe.flags = 0;
202 probe.pid = getpid();
203 probe.probe_exitcode = 0;
204
205 /*
206 * Initialize SEAP stuff
207 */
208 probe.SEAP_ctx = SEAP_CTX_new();
209 probe.sd = SEAP_add_probe(probe.SEAP_ctx, data);
210
211 if (probe.sd < 0)
212 fail(errno, "SEAP_openfd2", __LINE__ - 3);
213
214 if (SEAP_cmd_register(probe.SEAP_ctx, PROBECMD_RESET, SEAP_CMDREG_USEARG, &probe_reset, &probe) != 0)
215 fail(errno, "SEAP_cmd_register", __LINE__ - 1);
216
217 /*
218 * Initialize result & name caching
219 */
220 probe.rcache = probe_rcache_new();
221 probe.icache = probe_icache_new(&barrier);
222 probe_ncache_clear(OSCAP_GSYM(ncache));
223 probe.ncache = OSCAP_GSYM(ncache);
224 probe.th_barrier = &barrier;
225
226 /*
227 * Initialize probe option handlers
228 */
229 #define PROBE_OPTION_INITCOUNT 2
230
231 probe.option = malloc(sizeof(probe_option_t) * PROBE_OPTION_INITCOUNT);
232 probe.optcnt = PROBE_OPTION_INITCOUNT;
233
234 probe.option[0].option = PROBEOPT_VARREF_HANDLING;
235 probe.option[0].handler = &probe_opthandler_varref;
236 probe.option[1].option = PROBEOPT_RESULT_CACHING;
237 probe.option[1].handler = &probe_opthandler_rcache;
238
239 OSCAP_GSYM(probe_optdef) = probe.option;
240 OSCAP_GSYM(probe_optdef_count) = probe.optcnt;
241
242
243 /*
244 * Create input handler (detached)
245 */
246 probe.workers = rbt_i32_new();
247
248 probe_init_function_t init_function = probe_table_get_init_function(probe.subtype);
249 if (init_function != NULL) {
250 probe.probe_arg = init_function();
251 }
252
253 pthread_cleanup_push(probe_common_main_cleanup, (void *) &probe);
254
255 pthread_attr_init(&th_attr);
256
257 if (pthread_create(&probe.th_input, &th_attr, &probe_input_handler, &probe))
258 fail(errno, "pthread_create(probe_input_handler)", __LINE__ - 1);
259
260 pthread_attr_destroy(&th_attr);
261
262 void *status;
263 if (pthread_join(probe.th_input, &status) != 0) {
264 dD("pthread_join of probe_input_handler thread has failed");
265 }
266 dD("probe_input_handler thread has joined with status %ld", (long) status);
267
268 pthread_cleanup_pop(1);
269
270 pthread_barrier_destroy(&barrier);
271
272 return NULL;
273 }