Merge commit 'fml/master' into next
[paraslash.git] / para.h
1 /*
2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file para.h global paraslash definitions */
8
9 #include "config.h"
10
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h> /* time(), localtime() */
17 #include <unistd.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <sys/un.h> /* needed by create_pf_socket */
26 #include <string.h>
27 #include <assert.h>
28 #include "gcc-compat.h"
29
30 /** used in various contexts */
31 #define MAXLINE 255
32
33 /** Compute the minimum of \a x and \a y. */
34 #define PARA_MIN(x, y) ({ \
35 typeof(x) _min1 = (x); \
36 typeof(y) _min2 = (y); \
37 (void) (&_min1 == &_min2); \
38 _min1 < _min2 ? _min1 : _min2; })
39
40 /** Compute the maximum of \a x and \a y. */
41 #define PARA_MAX(x, y) ({ \
42 typeof(x) _max1 = (x); \
43 typeof(y) _max2 = (y); \
44 (void) (&_max1 == &_max2); \
45 _max1 < _max2 ? _max2 : _max1; })
46
47 /** Compute the absolute value of \a x. */
48 #define PARA_ABS(x) ({ \
49 typeof(x) _x = (x); \
50 _x > 0? _x : -_x; })
51
52 /** Debug loglevel, gets really noisy. */
53 #define LL_DEBUG 0
54 /** Still noisy, but won't fill your disk. */
55 #define LL_INFO 1
56 /** Normal, but significant event. */
57 #define LL_NOTICE 2
58 /** Unexpected event that can be handled. */
59 #define LL_WARNING 3
60 /** Unhandled error condition. */
61 #define LL_ERROR 4
62 /** System might be unreliable. */
63 #define LL_CRIT 5
64 /** Last message before exit. */
65 #define LL_EMERG 6
66 /** Number of all loglevels. */
67 #define NUM_LOGLEVELS 7
68
69 /** Log messages with lower priority than that will not be compiled in. */
70 #define COMPILE_TIME_LOGLEVEL 0
71
72 /** \cond */
73 #if LL_DEBUG >= COMPILE_TIME_LOGLEVEL
74 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
75 #else
76 #define PARA_DEBUG_LOG(...) do {;} while (0)
77 #endif
78
79 #if LL_INFO >= COMPILE_TIME_LOGLEVEL
80 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
81 #else
82 #define PARA_INFO_LOG(...) do {;} while (0)
83 #endif
84
85 #if LL_NOTICE >= COMPILE_TIME_LOGLEVEL
86 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
87 #else
88 #define PARA_NOTICE_LOG(...) do {;} while (0)
89 #endif
90
91 #if LL_WARNING >= COMPILE_TIME_LOGLEVEL
92 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
93 #else
94 #define PARA_WARNING_LOG(...) do {;} while (0)
95 #endif
96
97 #if LL_ERROR >= COMPILE_TIME_LOGLEVEL
98 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
99 #else
100 #define PARA_ERROR_LOG(...) do {;} while (0)
101 #endif
102
103 #if LL_CRIT >= COMPILE_TIME_LOGLEVEL
104 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
105 #else
106 #define PARA_CRIT_LOG(...) do {;} while (0)
107 #endif
108
109 #if LL_EMERG >= COMPILE_TIME_LOGLEVEL
110 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
111 #else
112 #define PARA_EMERG_LOG(...)
113 #endif
114 /** \endcond */
115
116 /**
117 * define a standard log function that always writes to stderr
118 *
119 * \param loglevel_barrier If the loglevel of the current message
120 * is less than that, the message is going to be ignored.
121 *
122 */
123 #define INIT_STDERR_LOGGING(loglevel_barrier) \
124 __printf_2_3 void para_log(int ll, const char* fmt,...) \
125 { \
126 va_list argp; \
127 if (ll < loglevel_barrier) \
128 return; \
129 va_start(argp, fmt); \
130 vfprintf(stderr, fmt, argp); \
131 va_end(argp); \
132 }
133
134 /** version text used by various commands if -V switch was given */
135 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
136 "Copyright (C) 2009 Andre Noll\n" \
137 "This is free software with ABSOLUTELY NO WARRANTY." \
138 " See COPYING for details.\n" \
139 "Written by Andre Noll.\n" \
140 "Report bugs to <maan@systemlinux.org>.\n"
141
142 /** print out \p VERSION_TEXT and exit if version flag was given */
143 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
144 if (_args_info_struct.version_given) { \
145 printf("%s", VERSION_TEXT(_prefix)); \
146 exit(EXIT_SUCCESS); \
147 }
148
149 /* Sent by para_client to initiate the authentication procedure. */
150 #define AUTH_REQUEST_MSG "auth rsa "
151 /** sent by para_server for commands that expect a data file */
152 #define AWAITING_DATA_MSG "\nAwaiting Data."
153 /** sent by para_server if authentication was successful */
154 #define PROCEED_MSG "Proceed."
155 /** length of the \p PROCEED_MSG string */
156 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
157 /** sent by para_client to indicate the end of the command line */
158 #define EOC_MSG "\nEnd of Command."
159
160 /* exec */
161 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
162
163 /* time */
164 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
165 long unsigned tv2ms(const struct timeval*);
166 void d2tv(double, struct timeval*);
167 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
168 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
169 void tv_divide(const unsigned long divisor, const struct timeval *tv,
170 struct timeval *result);
171 int tv_convex_combination(const long a, const struct timeval *tv1,
172 const long b, const struct timeval *tv2,
173 struct timeval *result);
174 void ms2tv(const long unsigned n, struct timeval *tv);
175 void compute_chunk_time(long unsigned chunk_num,
176 struct timeval *chunk_tv, struct timeval *stream_start,
177 struct timeval *result);
178
179 /** The enum of all status items. */
180 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
181 extern const char *status_item_list[];
182 /** Loop over each status item. */
183 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
184 int stat_item_valid(const char *item);
185 void stat_client_write_item(int item_num);
186 int stat_client_add(int fd, uint64_t mask, int parser_friendly);
187 int for_each_stat_item(char *item_buf, size_t num_bytes,
188 int (*item_handler)(int, char *));
189 void clear_and_dump_items(void);
190
191 __printf_2_3 void para_log(int, const char*, ...);
192
193 /**
194 * Write a log message to a dynamically allocated string.
195 *
196 * \param fmt Usual format string.
197 * \param p Result pointer.
198 *
199 * \sa printf(3). */
200 #define PARA_VSPRINTF(fmt, p) \
201 { \
202 int n; \
203 size_t size = 100; \
204 p = para_malloc(size); \
205 while (1) { \
206 va_list ap; \
207 /* Try to print in the allocated space. */ \
208 va_start(ap, fmt); \
209 n = vsnprintf(p, size, fmt, ap); \
210 va_end(ap); \
211 /* If that worked, return the string. */ \
212 if (n > -1 && n < size) \
213 break; \
214 /* Else try again with more space. */ \
215 if (n > -1) /* glibc 2.1 */ \
216 size = n + 1; /* precisely what is needed */ \
217 else /* glibc 2.0 */ \
218 size *= 2; /* twice the old size */ \
219 p = para_realloc(p, size); \
220 } \
221 }
222
223 /**
224 * Return a random non-negative integer in an interval.
225 *
226 * \param max Determines maximal possible return value.
227 *
228 * \return An integer between zero and \p max - 1, inclusively.
229 */
230 _static_inline_ long int para_random(unsigned max)
231 {
232 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
233 }
234
235 /** Round up x to a multiple of y */
236 #define ROUND_UP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
237
238
239 /** Get the size of an array */
240 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
241
242 /**
243 * Wrapper for isspace.
244 * NetBSD needs this.
245 */
246 /*
247 * The values should be cast to an unsigned char first, then to int.
248 * Why? Because the isdigit (as do all other is/to functions/macros)
249 * expect a number from 0 upto and including 255 as their (int) argument.
250 * Because char is signed on most systems, casting it to int immediately
251 * gives the functions an argument between -128 and 127 (inclusive),
252 * which they will use as an array index, and which will thus fail
253 * horribly for characters which have their most significant bit set.
254 */
255 #define para_isspace(c) isspace((int)(unsigned char)(c))
256
257 /** Data that indicates an eof-condition for a fec-encoded stream. */
258 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
259 "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
260 #define FEC_EOF_PACKET_LEN 32