1 #ifndef LIB_H
2 #define LIB_H
3 
4 /* default lib includes */
5 #ifdef HAVE_CONFIG_H
6 #  include "config.h"
7 #endif
8 
9 /* default system includes - keep these at minimum.. */
10 #include <stddef.h> /* Solaris defines NULL wrong unless this is used */
11 #include <stdlib.h>
12 #include <string.h> /* strcmp() etc. */
13 #ifdef HAVE_STRINGS_H
14 #  include <strings.h> /* strcasecmp() etc. */
15 #endif
16 #include <stdarg.h> /* va_list is used everywhere */
17 #include <limits.h> /* INT_MAX, etc. */
18 #include <errno.h> /* error checking is good */
19 #include <sys/types.h> /* many other includes want this */
20 #include <inttypes.h> /* PRI* macros */
21 
22 #ifdef HAVE_STDINT_H
23 #  include <stdint.h> /* C99 int types, we mostly need uintmax_t */
24 #endif
25 
26 #include "compat.h"
27 #include "macros.h"
28 #include "failures.h"
29 
30 #include "malloc-overflow.h"
31 #include "data-stack.h"
32 #include "mempool.h"
33 #include "imem.h"
34 #include "byteorder.h"
35 #include "fd-util.h"
36 
37 typedef struct buffer buffer_t;
38 typedef struct buffer string_t;
39 
40 struct istream;
41 struct ostream;
42 
43 typedef void lib_atexit_callback_t(void);
44 
45 #include "array-decl.h" /* ARRAY*()s may exist in any header */
46 #include "bits.h"
47 #include "hash-decl.h" /* HASH_TABLE*()s may exist in any header */
48 #include "strfuncs.h"
49 #include "strnum.h"
50 #include "event-log.h"
51 
52 #define LIB_ATEXIT_PRIORITY_HIGH -10
53 #define LIB_ATEXIT_PRIORITY_DEFAULT 0
54 #define LIB_ATEXIT_PRIORITY_LOW 10
55 
56 /* /dev/null opened as O_WRONLY. Opened at lib_init(), so it can be accessed
57    also inside chroots. */
58 extern int dev_null_fd;
59 
60 /* Call unlink(). If it fails, log an error including the source filename
61    and line number. */
62 int i_unlink(const char *path, const char *source_fname,
63 	     unsigned int source_linenum);
64 #define i_unlink(path) i_unlink(path, __FILE__, __LINE__)
65 /* Same as i_unlink(), but don't log an error if errno=ENOENT. Returns 1 on
66    unlink() success, 0 if errno=ENOENT, -1 on other errors. */
67 int i_unlink_if_exists(const char *path, const char *source_fname,
68 		       unsigned int source_linenum);
69 #define i_unlink_if_exists(path) i_unlink_if_exists(path, __FILE__, __LINE__)
70 /* Reset getopt() so it can be used for the next args. */
71 void i_getopt_reset(void);
72 
73 /* Call the given callback at the beginning of lib_deinit(). The main
74    difference to atexit() is that liblib's memory allocation and logging
75    functions are still available. Also if lib_atexit() is called multiple times
76    to the same callback, it's added only once. */
77 void lib_atexit(lib_atexit_callback_t *callback);
78 /* Specify the order in which the callback is called. Lowest numbered
79    priorities are called first. lib_atexit() is called with priority=0. */
80 void lib_atexit_priority(lib_atexit_callback_t *callback, int priority);
81 /* Manually run the atexit callbacks. lib_deinit() also does this if not
82    explicitly called. */
83 void lib_atexit_run(void);
84 /* Unless this or lib_deinit() is called, any unexpected exit() will result
85    in abort(). This can be helpful in catching unexpected exits. */
86 void lib_set_clean_exit(bool set);
87 /* Same as lib_set_clean_exit(TRUE) followed by exit(status). */
88 void lib_exit(int status) ATTR_NORETURN;
89 
90 void lib_init(void);
91 bool lib_is_initialized(void);
92 void lib_deinit(void);
93 
94 uint32_t i_rand(void);
95 /* Returns a random integer < upper_bound. */
96 uint32_t i_rand_limit(uint32_t upper_bound);
97 
i_rand_ushort(void)98 static inline unsigned short i_rand_ushort(void)
99 {
100         return i_rand_limit(USHRT_MAX + 1);
101 }
102 
i_rand_uchar(void)103 static inline unsigned char i_rand_uchar(void)
104 {
105         return i_rand_limit(UCHAR_MAX + 1);
106 }
107 
108 /* Returns a random integer >= min_val, and <= max_val. */
i_rand_minmax(uint32_t min_val,uint32_t max_val)109 static inline uint32_t i_rand_minmax(uint32_t min_val, uint32_t max_val)
110 {
111 	i_assert(min_val <= max_val);
112 	return min_val + i_rand_limit(max_val - min_val + 1);
113 }
114 
115 #endif
116