summaryrefslogtreecommitdiff
path: root/include/common.h
blob: b905f20074b8a524b8b5d677b06eda98b5ddbd10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * This file is part of epc.
 *
 * Copyright © 2017 Samsung Electronics
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EPC_COMMON_H
#define EPC_COMMON_H

#ifdef USE_EJDB
#include <ejdb/bson.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <systemd/sd-id128.h>

#include "list.h"
#include "uref.h"

/*
 * Value taken from linux kernel.
 * This value should be never returned to the caller so we may use
 * it for our internal purpouse
 */
#define EPROBE_DEFER	517

/* STR and CONCAT macros need two levels to accept macros as
   arguments */
#define _STR(A) #A
#define STR(A) _STR(A)

#define _CONCAT(A, B)  A ## B
#define CONCAT(A, B) _CONCAT(A, B)

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*a))

#ifndef offsetof
#define offsetof(type, member)  __builtin_offsetof(type, member)
#endif /* offsetof */

#ifndef container_of
#define container_of(ptr, type, field) ({							\
	const typeof(((type *)0)->field) *member = (ptr);				\
	(type *)((char *)member - offsetof(type, field));				\
})
#endif /* container_of */

#define _cleanup_(x) __attribute__((cleanup(x)))

static inline void freep(void *p)
{
	free(*(void **)p);
}
#define _cleanup_free_ _cleanup_(freep)

enum epc_object_type {
	TYPE_OID,
	TYPE_STRING,
	TYPE_INT,
	TYPE_TIMESPEC,
	TYPE_TIME_T,
	TYPE_BOOL,
	TYPE_DOUBLE,
	TYPE_UUID,
	TYPE_OBJECT,
};

enum epc_object_flags {
	EPC_FLAG_QUERY_MODE = 1
};

#define OID_NOP_SIZE 4
typedef union {
#ifdef USE_EJDB
	bson_oid_t bson;
#endif
	char nop[OID_NOP_SIZE];
	sd_id128_t uuid;
	struct {
		uint32_t table;
		uint32_t obj;
	} sqlite;
} epc_oid_t;

union epc_object_value {
	epc_oid_t oid;
	char *s;
	int i;
	struct timespec ts;
	time_t time;
	bool b;
	double d;
	sd_id128_t uuid;
	struct list_head children;
};

struct epc_object {
	char *key;
	int type;
	union epc_object_value val;
	int flags;
	struct uref uref;
	struct list_head node;
};

static inline void epc_object_unref(struct epc_object *obj)
{
	if (!obj)
		return;

	uref_put(&obj->uref);
}

static inline struct epc_object *epc_object_ref(struct epc_object *obj)
{
	if (!obj)
		return NULL;

	uref_get(&obj->uref);
	return obj;
}

int epc_object_new_flags(struct epc_object **obj, int flags);

static inline int epc_object_new(struct epc_object **obj)
{
	return epc_object_new_flags(obj, 0);
}

void epc_object_init(struct epc_object *obj);
void epc_object_destroy(struct epc_object *obj);

int epc_object_append_new(struct epc_object *parent, const char *key,
							 int type, void *val);

int epc_object_append_string(struct epc_object *obj, const char *key,
								const char *val);
int epc_object_append_object(struct epc_object *obj, const char *key,
								struct epc_object *val);

static inline int epc_object_append_oid(struct epc_object *obj,
										   const char *key, epc_oid_t *oid)
{
	return epc_object_append_new(obj, key, TYPE_OID, oid);
}

static inline int epc_object_append_int(struct epc_object *obj,
										   const char *key, int val)
{
	return epc_object_append_new(obj, key, TYPE_INT, &val);
}

static inline int epc_object_append_bool(struct epc_object *obj,
										   const char *key, bool val)
{
	return epc_object_append_new(obj, key, TYPE_BOOL, &val);
}

static inline int epc_object_append_double(struct epc_object *obj,
										   const char *key, double val)
{
	return epc_object_append_new(obj, key, TYPE_DOUBLE, &val);
}

static inline int epc_object_append_time_t(struct epc_object *obj,
											  const char *key, time_t val)
{
	return epc_object_append_new(obj, key, TYPE_TIME_T, &val);
}

static inline int epc_object_append_timespec(struct epc_object *obj,
												const char *key,
												struct timespec *val)
{
	return epc_object_append_new(obj, key, TYPE_TIMESPEC, val);
}

static inline int epc_object_append_uuid(struct epc_object *obj,
											const char *key, sd_id128_t *val)
{
	return epc_object_append_new(obj, key, TYPE_UUID, val);
}


int epc_object_get_val(struct epc_object *obj, const char *key,
						  int type, void *val);

static inline int epc_object_get_string(struct epc_object *obj,
										   const char *key, char **val)
{
	return epc_object_get_val(obj, key, TYPE_STRING, val);
}

static inline int epc_object_get_double(struct epc_object *obj,
										   const char *key, double *val)
{
	return epc_object_get_val(obj, key, TYPE_DOUBLE, val);
}

static inline int epc_object_get_bool(struct epc_object *obj,
										   const char *key, bool *val)
{
	return epc_object_get_val(obj, key, TYPE_BOOL, val);
}

static inline int epc_object_get_oid(struct epc_object *obj,
										const char *key, epc_oid_t *val)
{
	return epc_object_get_val(obj, key, TYPE_OID, val);
}

static inline int epc_object_get_int(struct epc_object *obj,
										const char *key, int *val)
{
	return epc_object_get_val(obj, key, TYPE_INT, val);
}

static inline int epc_object_get_timespec(struct epc_object *obj,
											 const char *key, struct timespec *val)
{
	return epc_object_get_val(obj, key, TYPE_TIMESPEC, val);
}

static inline int epc_object_get_time_t(struct epc_object *obj,
											 const char *key, time_t *val)
{
	return epc_object_get_val(obj, key, TYPE_TIME_T, val);
}

static inline int epc_object_get_uuid(struct epc_object *obj,
										 const char *key, sd_id128_t *val)
{
	return epc_object_get_val(obj, key, TYPE_UUID, val);
}

int epc_object_get_object(struct epc_object *obj, const char *key, struct epc_object **val);

int epc_object_to_string(char **str, struct epc_object *parent, int indent);

int epc_object_fill_empty(struct epc_object *obj);

int epc_oid_from_string(epc_oid_t *oid, const char *s);

int epc_oid_equal(const epc_oid_t *oid1, const epc_oid_t *oid2);

char *epc_oid_to_string_hex(const epc_oid_t *oid);

/**
 * Set callback to be called at epc exit
 */
void epc_set_exit_handler(int (*handler)(void));
int epc_call_at_exit(void);

char *fo_type_to_str(unsigned type);

#endif /* EPC_COMMON_H */