Open SCAP Library

seap-descriptor.h
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <dkopecek@redhat.com>
21 */
22
23 #pragma once
24 #ifndef _SEAP_DESCRIPTOR_H
25 #define _SEAP_DESCRIPTOR_H
26
27 #include <errno.h>
28 #include <pthread.h>
29 #include <stdint.h>
30 #include "generic/bitmap.h"
31 #include "generic/rbt/rbt.h"
32 #include "_sexp-types.h"
33 #include "_seap-packetq.h"
34 #include "_sexp-parser.h"
35 #include "_sexp-output.h"
36 #include "_seap-command.h"
37 #include "public/seap-scheme.h"
38 #include "public/seap-message.h"
39 #include "public/seap-command.h"
40 #include "public/seap-error.h"
41 #include "../../../common/util.h"
42
43 OSCAP_HIDDEN_START;
44
45 /*
46 * Descriptor table + related stuff
47 */
48 typedef struct {
49 SEAP_msgid_t next_id;
50 SEXP_ostate_t *ostate; /* Output state */
51 SEXP_pstate_t *pstate; /* Parser state */
52 SEAP_scheme_t scheme; /* Protocol/Scheme used for this descriptor */
53 void *scheme_data; /* Protocol/Scheme related data */
54
55 SEXP_t *msg_queue;
56 rbt_t *err_queue;
57 SEXP_t *cmd_queue;
58
59 SEAP_packetq_t pck_queue;
60
61 pthread_mutex_t w_lock;
62 pthread_mutex_t r_lock;
63
64 SEAP_cmdid_t next_cid;
65 SEAP_cmdtbl_t *cmd_c_table; /* Local SEAP commands */
66 SEAP_cmdtbl_t *cmd_w_table; /* Waiting SEAP commands */
67 } SEAP_desc_t;
68
69 #define SEAP_DESC_FDIN 0x00000001
70 #define SEAP_DESC_FDOUT 0x00000002
71 #define SEAP_DESC_SELF -1
72
73 typedef struct {
74 rbt_t *tree;
75 bitmap_t *bmap;
76 } SEAP_desctable_t;
77
78 #define SEAP_DESCTBL_INITIALIZER { NULL, NULL }
79
80 #define SEAP_BUFFER_SIZE 2*4096
81 #define SEAP_MAX_OPENDESC 128
82 #define SDTABLE_REALLOC_ADD 4
83
84 int SEAP_desc_add (SEAP_desctable_t *sd_table, SEXP_pstate_t *pstate, SEAP_scheme_t scheme, void *scheme_data);
85 int SEAP_desc_del (SEAP_desctable_t *sd_table, int sd);
86 SEAP_desc_t *SEAP_desc_get (SEAP_desctable_t *sd_table, int sd);
87
88 SEAP_desctable_t *SEAP_desctable_new (void);
89 void SEAP_desctable_free(SEAP_desctable_t *sd_table);
90 void SEAP_desc_free(SEAP_desc_t *dsc);
91
92 static inline int SEAP_desc_trylock (pthread_mutex_t *m)
93 {
94 switch (pthread_mutex_trylock (m)) {
95 case 0:
96 return (1);
97 case EBUSY:
98 return (0);
99 case EINVAL:
100 errno = EINVAL;
101 /* FALLTHROUGH */
102 default:
103 return (-1);
104 }
105 }
106
107 static inline int SEAP_desc_lock (pthread_mutex_t *m)
108 {
109 switch (pthread_mutex_lock (m)) {
110 case 0:
111 return (1);
112 default:
113 return (-1);
114 }
115 }
116
117 static inline int SEAP_desc_unlock (pthread_mutex_t *m)
118 {
119 switch (pthread_mutex_unlock (m)) {
120 case 0:
121 return (1);
122 default:
123 return (-1);
124 }
125 }
126
127 #define DESC_TRYRLOCK(d) SEAP_desc_trylock (&((d)->r_lock))
128 #define DESC_RLOCK(d) SEAP_desc_lock (&((d)->r_lock))
129 #define DESC_RUNLOCK(d) SEAP_desc_unlock (&((d)->r_lock))
130
131 #define DESC_TRYWLOCK(d) SEAP_desc_trylock (&((d)->w_lock))
132 #define DESC_WLOCK(d) SEAP_desc_lock (&((d)->w_lock))
133 #define DESC_WUNLOCK(d) SEAP_desc_unlock (&((d)->w_lock))
134
135 SEAP_msgid_t SEAP_desc_genmsgid (SEAP_desctable_t *sd_table, int sd);
136 SEAP_cmdid_t SEAP_desc_gencmdid (SEAP_desctable_t *sd_table, int sd);
137
138 OSCAP_HIDDEN_END;
139
140 #endif /* _SEAP_DESCRIPTOR_H */
SEXP_pstate
Definition: _sexp-parser.h:48
SEAP_packetq_t
Definition: _seap-packetq.h:16
bitmap_t
Definition: bitmap.h:36
SEXP_ostate
Definition: _sexp-output.h:36
SEAP_desc_t
Definition: seap-descriptor.h:48
SEAP_cmdtbl_t
Definition: _seap-command.h:68
SEAP_desctable_t
Definition: seap-descriptor.h:73
rbt
Definition: rbt_common.h:129
SEXP
Definition: sexp-types.h:82
err_queue
Definition: err_queue.c:30