Debian Codegraph @

src/OVAL/probes/SEAP/seap.c

raw symbols SCIP
1 /*
2 * Copyright 2009 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 <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <pthread.h>
32 #include <errno.h>
33
34 #if defined(OS_FREEBSD)
35 #include <pthread_np.h>
36 #endif
37
38 #include "_seap.h"
39 #include "generic/common.h"
40 #include "_sexp-types.h"
41 #include "_seap-types.h"
42 #include "_seap-message.h"
43 #include "_seap-command.h"
44 #include "_seap-error.h"
45 #include "_seap-packet.h"
46 #include "_seap.h"
47 #include "seap-descriptor.h"
48 #include "debug_priv.h"
49
50 #define SCH_QUEUE 4
51
52 static void SEAP_CTX_initdefault (SEAP_CTX_t *ctx)
53 {
54 _A(ctx != NULL);
55
56 ctx->fmt_in = SEXP_FMT_CANONICAL;
57 ctx->fmt_out = SEXP_FMT_CANONICAL;
58
59 /* Initialize descriptor table */
60 ctx->sd_table = SEAP_desctable_new();
61 ctx->cmd_c_table = SEAP_cmdtbl_new ();
62
63 ctx->recv_timeout = 5;
64 ctx->send_timeout = 5;
65 ctx->cflags = 0;
66
67 return;
68 }
69
70 SEAP_CTX_t *SEAP_CTX_new (void)
71 {
72 SEAP_CTX_t *ctx = malloc(sizeof(SEAP_CTX_t));
73 SEAP_CTX_initdefault (ctx);
74
75 return (ctx);
76 }
77
78 void SEAP_CTX_init (SEAP_CTX_t *ctx)
79 {
80 _A(ctx != NULL);
81 SEAP_CTX_initdefault (ctx);
82 return;
83 }
84
85 void SEAP_CTX_free (SEAP_CTX_t *ctx)
86 {
87 _A(ctx != NULL);
88 SEAP_desctable_free(ctx->sd_table);
89 SEAP_cmdtbl_free (ctx->cmd_c_table);
90 free(ctx);
91
92 return;
93 }
94
95 int SEAP_connect(SEAP_CTX_t *ctx)
96 {
97 SEAP_desc_t *dsc;
98 int sd;
99
100
101 sd = SEAP_desc_add(ctx->sd_table, SCH_QUEUE, NULL);
102
103 if (sd < 0) {
104 dD("Can't create/add new SEAP descriptor");
105 return (-1);
106 }
107
108 dsc = SEAP_desc_get (ctx->sd_table, sd);
109
110 if (dsc == NULL) {
111 errno = ESRCH;
112 return(-1);
113 }
114 dsc->subtype = ctx->subtype;
115
116 if (sch_queue_connect(dsc) != 0) {
117 dD("FAIL: errno=%u, %s.", errno, strerror (errno));
118 SEAP_desc_del(ctx->sd_table, sd);
119
120 return (-1);
121 }
122
123 return (sd);
124 }
125
126 int SEAP_open (SEAP_CTX_t *ctx, const char *path, uint32_t flags)
127 {
128 errno = EOPNOTSUPP;
129 return (-1);
130 }
131
132 int SEAP_openfd (SEAP_CTX_t *ctx, int fd, uint32_t flags)
133 {
134 errno = EOPNOTSUPP;
135 return (-1);
136 }
137
138 int SEAP_openfd2 (SEAP_CTX_t *ctx, int ifd, int ofd, uint32_t flags)
139 {
140 SEAP_desc_t *dsc;
141 int sd;
142
143 sd = SEAP_desc_add(ctx->sd_table, SCH_QUEUE, NULL);
144
145 if (sd < 0) {
146 dD("Can't create/add new SEAP descriptor");
147 return (-1);
148 }
149
150 dsc = SEAP_desc_get (ctx->sd_table, sd);
151
152 if (dsc == NULL) {
153 errno = ESRCH;
154 return(-1);
155 }
156
157 return (sd);
158 }
159
160 int SEAP_add_probe (SEAP_CTX_t *ctx, sch_queuedata_t *data)
161 {
162 int sd = SEAP_desc_add(ctx->sd_table, SCH_QUEUE, data);
163 dD("SEAP_add_probe");
164 if (sd < 0) {
165 dD("Can't create/add new SEAP descriptor");
166 return (-1);
167 }
168 SEAP_desc_t *dsc = SEAP_desc_get (ctx->sd_table, sd);
169
170 if (dsc == NULL) {
171 dD("dsc == NULL");
172 }
173 return sd;
174 }
175
176 int SEAP_recvsexp (SEAP_CTX_t *ctx, int sd, SEXP_t **sexp)
177 {
178 SEAP_msg_t *msg = NULL;
179
180 if (SEAP_recvmsg (ctx, sd, &msg) == 0) {
181 *sexp = SEAP_msg_get (msg);
182 SEAP_msg_free (msg);
183
184 return (0);
185 }
186
187 *sexp = NULL;
188 return (-1);
189 }
190
191 static int __SEAP_cmdexec_reply (SEAP_CTX_t *ctx, int sd, SEAP_cmd_t *cmd)
192 {
193 SEAP_desc_t *dsc;
194 SEXP_t *res;
195 SEAP_packet_t *packet;
196 SEAP_cmd_t *cmdrep;
197
198 dsc = SEAP_desc_get (ctx->sd_table, sd);
199
200 if (dsc == NULL)
201 return (-1);
202
203 res = SEAP_cmd_exec (ctx, sd, SEAP_EXEC_LOCAL,
204 cmd->code, cmd->args,
205 SEAP_CMDCLASS_USR, NULL, NULL);
206
207 packet = SEAP_packet_new ();
208 cmdrep = SEAP_packet_settype (packet, SEAP_PACKET_CMD);
209
210 cmdrep->id = SEAP_desc_gencmdid (ctx->sd_table, sd);
211 cmdrep->rid = cmd->id;
212 cmdrep->flags |= SEAP_CMDFLAG_REPLY;
213 cmdrep->args = res;
214 cmdrep->class = cmd->class;
215 cmdrep->code = cmd->code;
216
217 if (SEAP_packet_send (ctx, sd, packet) != 0) {
218 protect_errno {
219 dD("FAIL: errno=%u, %s.", errno, strerror (errno));
220 SEAP_packet_free (packet);
221 }
222 return (-1);
223 }
224
225 if (res != NULL && res != cmd->args)
226 SEXP_free(res);
227
228 SEAP_packet_free (packet);
229
230 return (0);
231 }
232
233 static void *__SEAP_cmdexec_worker (void *arg)
234 {
235 SEAP_cmdjob_t *job = (SEAP_cmdjob_t *)arg;
236
237 _A(job != NULL);
238 _A(job->cmd != NULL);
239 #if defined(HAVE_PTHREAD_SETNAME_NP)
240 # if defined(OS_APPLE)
241 pthread_setname_np("command_worker");
242 # else
243 pthread_setname_np(pthread_self(), "command_worker");
244 # endif
245 #endif
246
247 if (job->cmd->flags & SEAP_CMDFLAG_REPLY) {
248 (void) SEAP_cmd_exec (job->ctx, job->sd, SEAP_EXEC_WQUEUE,
249 job->cmd->rid, job->cmd->args,
250 SEAP_CMDCLASS_USR, NULL, NULL);
251 } else {
252 (void)__SEAP_cmdexec_reply (job->ctx, job->sd, job->cmd);
253 }
254
255 return (NULL);
256 }
257
258 int __SEAP_recvmsg_process_cmd (SEAP_CTX_t *ctx, int sd, SEAP_cmd_t *cmd)
259 {
260 _A(ctx != NULL);
261 _A(cmd != NULL);
262
263 if (ctx->cflags & SEAP_CFLG_THREAD) {
264 /*
265 * Create a new thread for processing this command
266 */
267 pthread_t th;
268 pthread_attr_t th_attrs;
269
270 SEAP_cmdjob_t *job;
271
272 /* Initialize thread stuff */
273 pthread_attr_init (&th_attrs);
274 pthread_attr_setdetachstate (&th_attrs, PTHREAD_CREATE_DETACHED);
275
276 /* Prepare the job */
277 job = SEAP_cmdjob_new ();
278 job->ctx = ctx;
279 job->sd = sd;
280 job->cmd = cmd;
281
282 /* Create the worker */
283 if (pthread_create (&th, &th_attrs,
284 &__SEAP_cmdexec_worker, (void *)job) != 0)
285 {
286 dD("Can't create worker thread: %u, %s.", errno, strerror (errno));
287 SEAP_cmdjob_free (job);
288 pthread_attr_destroy (&th_attrs);
289
290 return (-1);
291 }
292
293 pthread_attr_destroy (&th_attrs);
294 } else {
295 if (cmd->flags & SEAP_CMDFLAG_REPLY) {
296 (void) SEAP_cmd_exec (ctx, sd, SEAP_EXEC_WQUEUE,
297 cmd->rid, cmd->args,
298 SEAP_CMDCLASS_USR, NULL, NULL);
299 } else {
300 int ret;
301
302 ret = __SEAP_cmdexec_reply (ctx, sd, cmd);
303
304 if (cmd->args != NULL)
305 SEXP_free(cmd->args);
306
307 return (ret);
308 }
309 }
310
311 return (0);
312 }
313
314 static int __SEAP_recvmsg_process_err (SEAP_CTX_t *ctx, int sd, SEAP_err_t *err)
315 {
316 SEAP_desc_t *sd_desc;
317 SEAP_err_t *cloned_err;
318 void *prev_err;
319
320 sd_desc = SEAP_desc_get(ctx->sd_table, sd);
321
322 if (sd_desc == NULL) {
323 errno = EBADF;
324 return (-1);
325 }
326
327 cloned_err = SEAP_error_clone(err);
328 prev_err = NULL;
329
330 /* XXX: handle 64bit message ids */
331 if (rbt_i32_add(sd_desc->err_queue,
332 (uint32_t)(err->id), cloned_err, (void **)&prev_err) != 0)
333 {
334 SEAP_error_free(cloned_err);
335 errno = EINVAL;
336 return (-1);
337 }
338
339 if (prev_err != NULL) {
340 /* XXX: log this */
341 SEAP_error_free((SEAP_err_t*)prev_err);
342 }
343
344 return (0);
345 }
346
347 int SEAP_recvmsg (SEAP_CTX_t *ctx, int sd, SEAP_msg_t **seap_msg)
348 {
349 SEAP_packet_t *packet;
350
351 _A(ctx != NULL);
352 _A(seap_msg != NULL);
353
354 (*seap_msg) = NULL;
355
356 /*
357 * Packet loop
358 */
359 for (;;) {
360 if (SEAP_packet_recv (ctx, sd, &packet) != 0) {
361 protect_errno {
362 dD("FAIL: ctx=%p, sd=%d, errno=%u, %s.",
363 ctx, sd, errno, strerror (errno));
364 }
365 return (-1);
366 }
367
368 switch (SEAP_packet_gettype (packet)) {
369 case SEAP_PACKET_MSG:
370
371 (*seap_msg) = malloc(sizeof(SEAP_msg_t));
372 memcpy ((*seap_msg), SEAP_packet_msg (packet), sizeof (SEAP_msg_t));
373
374 SEAP_packet_free (packet);
375 return (0);
376 case SEAP_PACKET_CMD:
377 switch (__SEAP_recvmsg_process_cmd (ctx, sd, SEAP_packet_cmd (packet))) {
378 case 0:
379 SEAP_packet_free (packet);
380 continue;
381 default:
382 errno = EDOOFUS;
383 return (-1);
384 }
385 case SEAP_PACKET_ERR:
386 switch (__SEAP_recvmsg_process_err (ctx, sd, SEAP_packet_err (packet))) {
387 case 0:
388 break;
389 case -1:
390 SEAP_packet_free (packet);
391 return (-1);
392 default:
393 errno = EDOOFUS;
394 return (-1);
395 }
396
397 SEAP_packet_free (packet);
398 errno = ECANCELED;
399 return (-1);
400 default:
401 abort ();
402 }
403
404 SEAP_packet_free (packet);
405 }
406
407 /* NOTREACHED */
408 errno = EDOOFUS;
409 return (-1);
410 }
411
412 int SEAP_sendmsg (SEAP_CTX_t *ctx, int sd, SEAP_msg_t *seap_msg)
413 {
414 int ret;
415 SEAP_packet_t *packet;
416 SEAP_msg_t *msg;
417
418 packet = SEAP_packet_new ();
419 msg = (SEAP_msg_t *)SEAP_packet_settype (packet, SEAP_PACKET_MSG);
420
421 seap_msg->id = SEAP_desc_genmsgid (ctx->sd_table, sd);
422 memcpy (msg, seap_msg, sizeof (SEAP_msg_t));
423
424 ret = SEAP_packet_send (ctx, sd, packet);
425
426 protect_errno {
427 SEAP_packet_free (packet);
428 }
429 return (ret);
430 }
431
432 int SEAP_sendsexp (SEAP_CTX_t *ctx, int sd, SEXP_t *sexp)
433 {
434 SEAP_msg_t *msg;
435 int ret;
436
437 msg = SEAP_msg_new ();
438
439 if (SEAP_msg_set (msg, sexp) == 0)
440 ret = SEAP_sendmsg (ctx, sd, msg);
441 else
442 ret = -1;
443
444 protect_errno {
445 SEAP_msg_free (msg);
446 }
447 return (ret);
448 }
449
450 int SEAP_reply (SEAP_CTX_t *ctx, int sd, SEAP_msg_t *rep_msg, SEAP_msg_t *req_msg)
451 {
452 SEXP_t *r0;
453
454 _A(ctx != NULL);
455 _A(rep_msg != NULL);
456 _A(req_msg != NULL);
457
458 if (SEAP_msgattr_set (rep_msg, "reply-id",
459 #if SEAP_MSGID_BITS == 64
460 r0 = SEXP_number_newu_64 (req_msg->id)
461 #else
462 r0 = SEXP_number_newu_32 (req_msg->id)
463 #endif
464 ) == 0)
465 {
466 SEXP_free (r0);
467
468 if (SEAP_msgattr_exists (req_msg, "no-reply")) {
469 SEAP_msg_t *nil_msg;
470 int ret;
471
472 dD("no-reply set: sending empty reply");
473
474 nil_msg = SEAP_msg_clone (rep_msg);
475 SEAP_msg_unset (nil_msg);
476 ret = SEAP_sendmsg (ctx, sd, nil_msg);
477 SEAP_msg_free (nil_msg);
478
479 return (ret);
480 } else {
481 dD("no-reply not set: sending full reply");
482 return SEAP_sendmsg (ctx, sd, rep_msg);
483 }
484 } else {
485 SEXP_free (r0);
486 return (-1);
487 }
488 }
489
490 static int __SEAP_senderr (SEAP_CTX_t *ctx, int sd, SEAP_err_t *err, unsigned int type)
491 {
492 SEAP_packet_t *packet;
493 SEAP_err_t *errptr;
494
495 _A(ctx != NULL);
496 _A(err != NULL);
497
498 packet = SEAP_packet_new ();
499 errptr = SEAP_packet_settype (packet, SEAP_PACKET_ERR);
500
501 memcpy (errptr, err, sizeof (SEAP_err_t));
502 errptr->type = type;
503
504 if (SEAP_packet_send (ctx, sd, packet) != 0) {
505 protect_errno {
506 dD("FAIL: errno=%u, %s.", errno, strerror (errno));
507 SEAP_packet_free (packet);
508 }
509
510 return (-1);
511 }
512
513 SEAP_packet_free (packet);
514 return (0);
515 }
516
517 int SEAP_senderr (SEAP_CTX_t *ctx, int sd, SEAP_err_t *err)
518 {
519 return (__SEAP_senderr (ctx, sd, err, SEAP_ETYPE_USER));
520 }
521
522 int SEAP_replyerr (SEAP_CTX_t *ctx, int sd, SEAP_msg_t *rep_msg, uint32_t e)
523 {
524 SEAP_err_t err;
525
526 _A(ctx != NULL);
527 _A(rep_msg != NULL);
528
529 err.type = 0;
530 err.code = e;
531 err.id = rep_msg->id;
532 err.data = NULL; /* FIXME: Attach original message */
533
534 return (__SEAP_senderr (ctx, sd, &err, SEAP_ETYPE_USER));
535 }
536
537 int SEAP_recverr (SEAP_CTX_t *ctx, int sd, SEAP_err_t **err)
538 {
539 errno = EOPNOTSUPP;
540 return (-1);
541 }
542
543 /**
544 * Receive the last error that happened on `sd' and was caused by a message
545 * with ID `id'.
546 * If an error is in the queue, it's saved at `*err' and zero is returned.
547 * The caller takes care of freeing the memory used by the returned SEAP_err_t
548 * structure. If there is not an error, 1 is returned. In case of an internal
549 * error, -1 is returned an errno is set.
550 */
551 int SEAP_recverr_byid (SEAP_CTX_t *ctx, int sd, SEAP_err_t **err, SEAP_msgid_t id)
552 {
553 SEAP_desc_t *sd_desc;
554 void *data;
555
556 if (err == NULL) {
557 errno = EINVAL;
558 return (-1);
559 }
560
561 sd_desc = SEAP_desc_get(ctx->sd_table, sd);
562
563 if (sd_desc == NULL) {
564 errno = EBADF;
565 return (-1);
566 }
567
568 data = NULL;
569
570 if (rbt_i32_del(sd_desc->err_queue, (uint32_t)id, &data) != 0)
571 return (1);
572 else {
573 if (data == NULL) {
574 return -1;
575 }
576 *err = (SEAP_err_t *)data;
577 }
578
579 return (0);
580 }
581
582 SEXP_t *SEAP_read (SEAP_CTX_t *ctx, int sd)
583 {
584 errno = EOPNOTSUPP;
585 return (NULL);
586 }
587
588 int SEAP_write (SEAP_CTX_t *ctx, int sd, SEXP_t *sexp)
589 {
590 errno = EOPNOTSUPP;
591 return (-1);
592 }
593
594 int SEAP_close (SEAP_CTX_t *ctx, int sd)
595 {
596 SEAP_desc_t *dsc;
597 int ret;
598
599 _A(ctx != NULL);
600
601 if (sd < 0) {
602 errno = EBADF;
603 return (-1);
604 }
605
606 dsc = SEAP_desc_get (ctx->sd_table, sd);
607
608 if (dsc == NULL) {
609 errno = EBADF;
610 return (-1);
611 }
612
613 ret = sch_queue_close(dsc, 0); /* TODO: Are flags usable here? */
614
615 protect_errno {
616 if (SEAP_desc_del (ctx->sd_table, sd) != 0) {
617 /* something very bad happened */
618 dD("SEAP_desc_del failed");
619 return(-1);
620 }
621 }
622
623 return(ret);
624 }