summaryrefslogtreecommitdiff
path: root/src/util/common.h
blob: 15b1ad24eceb87ff469e7509c8ba47fbeddde721 (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
/*
 * This file is part of faultd.
 *
 * 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 FAULTD_COMMON_H
#define FAULTD_COMMON_H

#include <ejdb/bson.h>
#include <stdlib.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))

#define BSON_OID_IS_ZERO(o) (o.ints[0] == 0 && \
							 o.ints[1] == 0 && \
							 o.ints[2] == 0)

#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 faultd_type {
	TYPE_OID,
	TYPE_STRING,
	TYPE_INT,
	TYPE_TIMESPEC,
	TYPE_TIME_T,
	TYPE_UUID,
	TYPE_OBJECT,
};

typedef union {
	bson_oid_t bson;
	sd_id128_t uuid;
} faultd_oid_t;

#define FAULTD_OID_IS_ZERO(o) (o.bson.ints[0] == 0 && \
							 o.bson.ints[1] == 0 && \
							 o.bson.ints[2] == 0)

struct faultd_object {
	char *key;
	int type;
	union {
		faultd_oid_t oid;
		char *s;
		int i;
		struct timespec ts;
		time_t time;
		sd_id128_t uuid;
		struct list_head children;
	} val;

	struct uref uref;
	struct list_head node;
};

static inline void faultd_object_unref(struct faultd_object *obj)
{
	if (!obj)
		return;

	uref_put(&obj->uref);
}

static inline struct faultd_object *faultd_object_ref(struct faultd_object *obj)
{
	if (!obj)
		return NULL;

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

int faultd_object_new(struct faultd_object **obj);
void faultd_object_init(struct faultd_object *obj);
void faultd_object_destroy(struct faultd_object *obj);

int faultd_object_append_new(struct faultd_object *parent, const char *key,
							 int type, void *val);

int faultd_object_append_string(struct faultd_object *obj, const char *key,
								const char *val);
int faultd_object_append_object(struct faultd_object *obj, const char *key,
								struct faultd_object *val);

static inline int faultd_object_append_oid(struct faultd_object *obj,
										   const char *key, faultd_oid_t *oid)
{
	return faultd_object_append_new(obj, key, TYPE_OID, oid);
}

static inline int faultd_object_append_int(struct faultd_object *obj,
										   const char *key, int val)
{
	return faultd_object_append_new(obj, key, TYPE_INT, &val);
}

static inline int faultd_object_append_time_t(struct faultd_object *obj,
											  const char *key, time_t val)
{
	return faultd_object_append_new(obj, key, TYPE_TIME_T, &val);
}

static inline int faultd_object_append_timespec(struct faultd_object *obj,
												const char *key,
												struct timespec *val)
{
	return faultd_object_append_new(obj, key, TYPE_TIMESPEC, val);
}

static inline int faultd_object_append_uuid(struct faultd_object *obj,
											const char *key, sd_id128_t *val)
{
	return faultd_object_append_new(obj, key, TYPE_UUID, val);
}


int faultd_object_get_val(struct faultd_object *obj, const char *key,
						  int type, void *val);

static inline int faultd_object_get_string(struct faultd_object *obj,
										   const char *key, char **val)
{
	return faultd_object_get_val(obj, key, TYPE_STRING, val);
}

static inline int faultd_object_get_oid(struct faultd_object *obj,
										const char *key, faultd_oid_t *val)
{
	return faultd_object_get_val(obj, key, TYPE_OID, val);
}

static inline int faultd_object_get_int(struct faultd_object *obj,
										const char *key, int *val)
{
	return faultd_object_get_val(obj, key, TYPE_INT, val);
}

static inline int faultd_object_get_timespec(struct faultd_object *obj,
											 const char *key, struct timespec *val)
{
	return faultd_object_get_val(obj, key, TYPE_TIMESPEC, val);
}

static inline int faultd_object_get_time_t(struct faultd_object *obj,
											 const char *key, time_t *val)
{
	return faultd_object_get_val(obj, key, TYPE_TIME_T, val);
}

static inline int faultd_object_get_uuid(struct faultd_object *obj,
										 const char *key, sd_id128_t *val)
{
	return faultd_object_get_val(obj, key, TYPE_UUID, val);
}

int faultd_object_get_object(struct faultd_object *obj, const char *key, struct faultd_object **val);

int faultd_object_fill_empty(struct faultd_object *obj);

int faultd_oid_from_string(faultd_oid_t *oid, const char *s);

#endif /* FAULTD_COMMON_H */