summaryrefslogtreecommitdiff
path: root/shared/nm-default.h
blob: 8bcc5c7043891d4f93bd2f161b763b371087e07e (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * (C) Copyright 2015 Red Hat, Inc.
 */

#ifndef __NM_DEFAULT_H__
#define __NM_DEFAULT_H__

#define NM_NETWORKMANAGER_COMPILATION_WITH_GLIB                 (1 <<  0)
#define NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB        (1 <<  1)
#define NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG       (1 <<  2)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM                (1 <<  3)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_PRIVATE        (1 <<  4)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE           (1 <<  5)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_INTERNAL  (1 <<  6)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_PRIVATE   (1 <<  7)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_UTIL           (1 <<  8)
#define NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_GLIB           (1 <<  9)
#define NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON               (1 << 10)
#define NM_NETWORKMANAGER_COMPILATION_WITH_SYSTEMD              (1 << 11)

#define NM_NETWORKMANAGER_COMPILATION_LIBNM_CORE     ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_PRIVATE \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_INTERNAL \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_LIBNM          ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_PRIVATE \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_INTERNAL \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_LIBNM_UTIL     ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_UTIL \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_LIBNM_GLIB     ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_LIBNM_UTIL \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_GLIB \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_CLIENT         ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_DAEMON         ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_INTERNAL \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_SYSTEMD        ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_DAEMON \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_SYSTEMD \
                                                     )

#define NM_NETWORKMANAGER_COMPILATION_GLIB           ( 0 \
                                                     | NM_NETWORKMANAGER_COMPILATION_WITH_GLIB \
                                                     )

#ifndef NETWORKMANAGER_COMPILATION
#error Define NETWORKMANAGER_COMPILATION accordingly
#endif

#ifndef G_LOG_DOMAIN
#if defined(NETWORKMANAGER_COMPILATION_TEST)
#define G_LOG_DOMAIN "test"
#elif NETWORKMANAGER_COMPILATION & NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON
#define G_LOG_DOMAIN "NetworkManager"
#else
#error Need to define G_LOG_DOMAIN
#endif
#elif defined (NETWORKMANAGER_COMPILATION_TEST) || (NETWORKMANAGER_COMPILATION & NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON)
#error Do not define G_LOG_DOMAIN with NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON
#endif

/*****************************************************************************/

/* always include these headers for our internal source files. */

#ifndef ___CONFIG_H__
#define ___CONFIG_H__
#include <config.h>
#endif

#include "config-extra.h"

/* for internal compilation we don't want the deprecation macros
 * to be in effect. Define the widest range of versions to effectively
 * disable deprecation checks */
#define NM_VERSION_MIN_REQUIRED  NM_VERSION_0_9_8

#ifndef NM_MORE_ASSERTS
#define NM_MORE_ASSERTS 0
#endif

#if NM_MORE_ASSERTS == 0
/* The cast macros like NM_TYPE() are implemented via G_TYPE_CHECK_INSTANCE_CAST()
 * and _G_TYPE_CIC(). The latter, by default performs runtime checks of the type
 * by calling g_type_check_instance_cast().
 * This check has a certain overhead without being helpful.
 *
 * Example 1:
 *     static void foo (NMType *obj)
 *     {
 *         access_obj_without_check (obj);
 *     }
 *     foo ((NMType *) obj);
 *     // There is no runtime check and passing an invalid pointer
 *     // leads to a crash.
 *
 * Example 2:
 *     static void foo (NMType *obj)
 *     {
 *         access_obj_without_check (obj);
 *     }
 *     foo (NM_TYPE (obj));
 *     // There is a runtime check which prints a g_warning(), but that doesn't
 *     // avoid the crash as NM_TYPE() cannot do anything then passing on the
 *     // invalid pointer.
 *
 * Example 3:
 *     static void foo (NMType *obj)
 *     {
 *         g_return_if_fail (NM_IS_TYPE (obj));
 *         access_obj_without_check (obj);
 *     }
 *     foo ((NMType *) obj);
 *     // There is a runtime check which prints a g_critical() which also avoids
 *     // the crash. That is actually helpful to catch bugs and avoid crashes.
 *
 * Example 4:
 *     static void foo (NMType *obj)
 *     {
 *         g_return_if_fail (NM_IS_TYPE (obj));
 *         access_obj_without_check (obj);
 *     }
 *     foo (NM_TYPE (obj));
 *     // The runtime check is performed twice, with printing a g_warning() and
 *     // a g_critical() and avoiding the crash.
 *
 * Example 3 is how it should be done. Type checks in NM_TYPE() are pointless.
 * Disable them for our production builds.
 */
#ifndef G_DISABLE_CAST_CHECKS
#define G_DISABLE_CAST_CHECKS
#endif
#endif

#if NM_MORE_ASSERTS == 0
#ifndef G_DISABLE_CAST_CHECKS
/* Unless compiling with G_DISABLE_CAST_CHECKS, glib performs type checking
 * during G_VARIANT_TYPE() via g_variant_type_checked_(). This is not necesary
 * because commonly this cast is needed during something like
 *
 *   g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}"));
 *
 * Note that in if the variant type would be invalid, the check still
 * wouldn't make the buggy code magically work. Instead of passing a
 * bogus type string (bad), it would pass %NULL to g_variant_builder_init()
 * (also bad).
 *
 * Also, a function like g_variant_builder_init() already validates
 * the input type via something like
 *
 *   g_return_if_fail (g_variant_type_is_container (type));
 *
 * So, by having G_VARIANT_TYPE() also validate the type, we validate
 * twice, whereas the first validation is rather pointless because it
 * doesn't prevent the function to be called with invalid arguments.
 *
 * Just patch G_VARIANT_TYPE() to perform no check.
 */
#undef G_VARIANT_TYPE
#define G_VARIANT_TYPE(type_string) ((const GVariantType *) (type_string))
#endif
#endif

#include <stdlib.h>

/*****************************************************************************/

#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_GLIB

#include <glib.h>

#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG
#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB
#error Cannot define NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG and NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB
#endif
#include <glib/gi18n.h>
#elif (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_LIB
#include <glib/gi18n-lib.h>
#endif

/*****************************************************************************/

#if NM_MORE_ASSERTS == 0

/* glib assertions (g_return_*(), g_assert*()) contain a textual representation
 * of the checked statement. This part of the assertion blows up the size of the
 * binary. Unless we compile a debug-build with NM_MORE_ASSERTS, drop these
 * parts. Note that the failed assertion still prints the file and line where the
 * assertion fails. That shall suffice. */

static inline void
_nm_g_return_if_fail_warning (const char *log_domain,
                              const char *file,
                              int line)
{
	char file_buf[256 + 15];

	g_snprintf (file_buf, sizeof (file_buf), "((%s:%d))", file, line);
	g_return_if_fail_warning (log_domain, file_buf, "<dropped>");
}

#define g_return_if_fail_warning(log_domain, pretty_function, expression) \
	_nm_g_return_if_fail_warning (log_domain, __FILE__, __LINE__)

#define g_assertion_message_expr(domain, file, line, func, expr) \
	g_assertion_message_expr(domain, file, line, "<unknown-fcn>", (expr) ? "<dropped>" : NULL)

#undef g_return_val_if_reached
#define g_return_val_if_reached(val) \
    G_STMT_START { \
        g_log (G_LOG_DOMAIN, \
               G_LOG_LEVEL_CRITICAL, \
               "file %s: line %d (%s): should not be reached", \
               __FILE__, \
               __LINE__, \
               "<dropped>"); \
        return (val); \
    } G_STMT_END

#undef g_return_if_reached
#define g_return_if_reached() \
    G_STMT_START { \
        g_log (G_LOG_DOMAIN, \
               G_LOG_LEVEL_CRITICAL, \
               "file %s: line %d (%s): should not be reached", \
               __FILE__, \
               __LINE__, \
               "<dropped>"); \
        return; \
    } G_STMT_END

#define NM_ASSERT_G_RETURN_EXPR(expr) "<dropped>"
#define NM_ASSERT_NO_MSG 1

#else

#define NM_ASSERT_G_RETURN_EXPR(expr) ""expr""
#define NM_ASSERT_NO_MSG 0

#endif

/*****************************************************************************/

#include "nm-utils/nm-macros-internal.h"
#include "nm-utils/nm-shared-utils.h"

#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_UTIL
/* no hash-utils in legacy code. */
#else
#include "nm-utils/nm-hash-utils.h"
#endif

/*****************************************************************************/

#if (NETWORKMANAGER_COMPILATION) & (NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_UTIL)
#include "nm-version.h"
#endif

/*****************************************************************************/

#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON
#include "nm-types.h"
#include "nm-logging.h"
#endif

#if ((NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM) && !((NETWORKMANAGER_COMPILATION) & (NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_PRIVATE | NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_CORE_INTERNAL))
#include "NetworkManager.h"
#endif

#endif /* NM_NETWORKMANAGER_COMPILATION_WITH_GLIB */

/*****************************************************************************/

#endif /* __NM_DEFAULT_H__ */