nm-sd-adapt-shared.h 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
16
 * Copyright (C) 2014 - 2018 Red Hat, Inc.
17 18
 */

19 20
#ifndef __NM_SD_ADAPT_SHARED_H__
#define __NM_SD_ADAPT_SHARED_H__
21

22
#include "nm-default.h"
23 24

#include <syslog.h>
25

26
#include "nm-glib-aux/nm-logging-fwd.h"
27

28 29
/*****************************************************************************/

30 31 32 33 34
/* strerror() is not thread-safe. Patch systemd-sources via a define. */
#define strerror(errsv) nm_strerror_native (errsv)

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

35
static inline NMLogLevel
36 37
_slog_level_to_nm (int slevel)
{
38 39
	switch (LOG_PRI (slevel)) {
	case LOG_DEBUG:   return LOGL_DEBUG;
40
	case LOG_WARNING: return LOGL_WARN;
41
	case LOG_CRIT:
42 43 44 45 46 47 48
	case LOG_ERR:     return LOGL_ERR;
	case LOG_INFO:
	case LOG_NOTICE:
	default:          return LOGL_INFO;
	}
}

49 50 51 52 53 54 55
static inline int
_nm_log_get_max_level_realm (void)
{
	/* inline function, to avoid coverity warning about constant expression. */
	return LOG_DEBUG;
}
#define log_get_max_level_realm(realm) _nm_log_get_max_level_realm ()
56 57

#define log_internal_realm(level, error, file, line, func, format, ...) \
58
({ \
59 60 61
	const int _nm_e = (error); \
	const NMLogLevel _nm_l = _slog_level_to_nm ((level)); \
	\
62
	if (_nm_log_enabled_impl (!(NM_THREAD_SAFE_ON_MAIN_THREAD), _nm_l, LOGD_SYSTEMD)) { \
63
		const char *_nm_location = strrchr ((""file), '/'); \
64
		\
65
		_nm_log_impl (_nm_location ? _nm_location + 1 : (""file), (line), (func), !(NM_THREAD_SAFE_ON_MAIN_THREAD), _nm_l, LOGD_SYSTEMD, _nm_e, NULL, NULL, ("%s"format), "libsystemd: ", ## __VA_ARGS__); \
66
	} \
67 68
	(_nm_e > 0 ? -_nm_e : _nm_e); \
})
69

70
#define log_assert_failed(text, file, line, func) \
71
G_STMT_START { \
72 73
	log_internal (LOG_CRIT, 0, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.", text, file, line, func); \
	g_assert_not_reached (); \
74 75
} G_STMT_END

76 77 78 79 80 81
#define log_assert_failed_unreachable(text, file, line, func) \
G_STMT_START { \
	log_internal (LOG_CRIT, 0, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.", text, file, line, func); \
	g_assert_not_reached (); \
} G_STMT_END

82
#define log_assert_failed_return(text, file, line, func) \
83
({ \
84 85
	log_internal (LOG_DEBUG, 0, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.", text, file, line, func); \
	g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, text); \
86 87
	(void) 0; \
})
88

89 90 91 92 93 94
/*****************************************************************************/

#define VALGRIND 0

#define ENABLE_DEBUG_HASHMAP 0

95 96 97 98 99
/*****************************************************************************
 * The remainder of the header is only enabled when building the systemd code
 * itself.
 *****************************************************************************/

100
#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_SYSTEMD
101 102

#include <sys/syscall.h>
103
#include <sys/ioctl.h>
104

105 106 107 108 109 110 111 112 113
static inline pid_t
raw_getpid (void) {
#if defined(__alpha__)
	return (pid_t) syscall (__NR_getxpid);
#else
	return (pid_t) syscall (__NR_getpid);
#endif
}

114
static inline pid_t _nm_gettid(void) {
115
        return (pid_t) syscall(SYS_gettid);
116
}
117
#define gettid() _nm_gettid ()
118

119 120 121
/* we build with C11 and thus <uchar.h> provides char32_t,char16_t. */
#define HAVE_CHAR32_T 1
#define HAVE_CHAR16_T 1
122

123 124 125 126
#if defined(HAVE_DECL_REALLOCARRAY) && HAVE_DECL_REALLOCARRAY == 1
#define HAVE_REALLOCARRAY 1
#else
#define HAVE_REALLOCARRAY 0
127 128
#endif

129 130 131 132
#if defined(HAVE_DECL_EXPLICIT_BZERO) && HAVE_DECL_EXPLICIT_BZERO == 1
#define HAVE_EXPLICIT_BZERO 1
#else
#define HAVE_EXPLICIT_BZERO 0
133 134
#endif

135
#endif /* (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_SYSTEMD */
136

137
/*****************************************************************************/
138

139
#endif /* __NM_SD_ADAPT_SHARED_H__ */