cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

misc_lib.h
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_MISC_LIB_H
26 #define CFENGINE_MISC_LIB_H
27 
28 #include <platform.h>
29 
30 #include <compiler.h>
31 
32 
33 #define ProgrammingError(...) __ProgrammingError(__FILE__, __LINE__, __VA_ARGS__)
34 
35 /* TODO UnexpectedError() needs
36  * to be rate limited to avoid spamming the console. */
37 #ifndef NDEBUG
38 # define UnexpectedError(...) __ProgrammingError(__FILE__, __LINE__, __VA_ARGS__)
39 #else
40 # define UnexpectedError(...) __UnexpectedError(__FILE__, __LINE__, __VA_ARGS__)
41 #endif
42 
43 
44 /**
45  * CF_ASSERT(condition, message...)
46  *
47  * If NDEBUG is defined then the #message is printed and execution continues,
48  * else execution aborts.
49  */
50 
51 # define CF_ASSERT(condition, ...) \
52  do { \
53  if (!(condition)) \
54  UnexpectedError(__VA_ARGS__); \
55  } while(0)
56 
57 /**
58  * CF_ASSERT_FIX(condition, fix, message...)
59  *
60  * If NDEBUG is defined then the #message is printed, the #fix is executed,
61  * and execution continues. If not NDEBUG, the #fix is ignored and execution
62  * is aborted.
63  */
64 # define CF_ASSERT_FIX(condition, fix, ...) \
65  { \
66  if (!(condition)) \
67  { \
68  UnexpectedError(__VA_ARGS__); \
69  (fix); \
70  } \
71  }
72 
73 
74 #define ISPOW2(n) ( (n)>0 && ((((n) & ((n)-1)) == 0)) )
75 
76 
77 /*
78  In contrast to the standard C modulus operator (%), this gives
79  you an unsigned modulus. So where -1 % 3 => -1,
80  UnsignedModulus(-1, 3) => 2.
81 */
82 unsigned long UnsignedModulus(long dividend, long divisor);
83 
84 size_t UpperPowerOfTwo(size_t v);
85 
86 
87 void __ProgrammingError(const char *file, int lineno, const char *format, ...) \
88  FUNC_ATTR_PRINTF(3, 4) FUNC_ATTR_NORETURN;
89 
90 void __UnexpectedError(const char *file, int lineno, const char *format, ...) \
91  FUNC_ATTR_PRINTF(3, 4);
92 
93 
94 /**
95  * Unchecked versions of common functions, i.e. functions that no longer
96  * return anything, but try to continue in case of failure.
97  *
98  * @NOTE call these only with arguments that will always succeed!
99  */
100 
101 
102 void xclock_gettime(clockid_t clk_id, struct timespec *ts);
103 void xsnprintf(char *str, size_t str_size, const char *format, ...);
104 
105 int setenv_wrapper(const char *name, const char *value, int overwrite);
106 int putenv_wrapper(const char *str);
107 
108 #endif
#define FUNC_ATTR_PRINTF(string_index, first_to_check)
Definition: compiler.h:52
#define FUNC_ATTR_NORETURN
Definition: compiler.h:51
#define const
Definition: getopt.c:38
void xsnprintf(char *str, size_t str_size, const char *format,...)
Definition: misc_lib.c:114
void __ProgrammingError(const char *file, int lineno, const char *format,...)
Definition: misc_lib.c:55
size_t UpperPowerOfTwo(size_t v)
Definition: misc_lib.c:41
void xclock_gettime(clockid_t clk_id, struct timespec *ts)
Definition: misc_lib.c:94
int putenv_wrapper(const char *str)
Definition: misc_lib.c:169
int setenv_wrapper(const char *name, const char *value, int overwrite)
Definition: misc_lib.c:140
unsigned long UnsignedModulus(long dividend, long divisor)
Definition: misc_lib.c:36
void __UnexpectedError(const char *file, int lineno, const char *format,...)
Log unexpected runtime error to stderr, do not exit program.
Definition: misc_lib.c:77
int clockid_t
Definition: platform.h:415