2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file para.h global paraslash definitions */
16 #include <time.h> /* time(), localtime() */
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 */
28 #include "gcc-compat.h"
30 /** used in various contexts */
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; })
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; })
47 /** Compute the absolute value of \a x. */
48 #define PARA_ABS(x) ({ \
52 /** debug loglevel, gets really noisy */
54 /** still noisy, but won't fill your disk */
56 /** normal, but significant event */
58 /** unexpected event that can be handled */
60 /** unhandled error condition */
62 /** system might be unreliable */
64 /** last message before exit */
67 /** Log messages with lower priority than that will not be compiled in. */
68 #define COMPILE_TIME_LOGLEVEL 0
71 #if DEBUG > COMPILE_TIME_LOGLEVEL
72 #define PARA_DEBUG_LOG(f,...) para_log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
74 #define PARA_DEBUG_LOG(...) do {;} while (0)
77 #if INFO > COMPILE_TIME_LOGLEVEL
78 #define PARA_INFO_LOG(f,...) para_log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
80 #define PARA_INFO_LOG(...) do {;} while (0)
83 #if NOTICE > COMPILE_TIME_LOGLEVEL
84 #define PARA_NOTICE_LOG(f,...) para_log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
86 #define PARA_NOTICE_LOG(...) do {;} while (0)
89 #if WARNING > COMPILE_TIME_LOGLEVEL
90 #define PARA_WARNING_LOG(f,...) para_log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
92 #define PARA_WARNING_LOG(...) do {;} while (0)
95 #if ERROR > COMPILE_TIME_LOGLEVEL
96 #define PARA_ERROR_LOG(f,...) para_log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
98 #define PARA_ERROR_LOG(...) do {;} while (0)
101 #if CRIT > COMPILE_TIME_LOGLEVEL
102 #define PARA_CRIT_LOG(f,...) para_log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
104 #define PARA_CRIT_LOG(...) do {;} while (0)
107 #if EMERG > COMPILE_TIME_LOGLEVEL
108 #define PARA_EMERG_LOG(f,...) para_log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
110 #define PARA_EMERG_LOG(...)
115 * define a standard log function that always writes to stderr
117 * \param loglevel_barrier If the loglevel of the current message
118 * is less than that, the message is going to be ignored.
121 #define INIT_STDERR_LOGGING(loglevel_barrier) \
122 __printf_2_3 void para_log(int ll, const char* fmt,...) \
125 if (ll < loglevel_barrier) \
127 va_start(argp, fmt); \
128 vfprintf(stderr, fmt, argp); \
132 /** version text used by various commands if -V switch was given */
133 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
134 "Copyright (C) 2008 Andre Noll\n" \
135 "This is free software with ABSOLUTELY NO WARRANTY." \
136 " See COPYING for details.\n" \
137 "Written by Andre Noll.\n" \
138 "Report bugs to <maan@systemlinux.org>.\n"
140 /** print out \p VERSION_TEXT and exit if version flag was given */
141 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
142 if (_args_info_struct.version_given) { \
143 printf("%s", VERSION_TEXT(_prefix)); \
144 exit(EXIT_SUCCESS); \
146 /** sent by para_server for commands that expect a data file */
147 #define AWAITING_DATA_MSG "\nAwaiting Data."
148 /** sent by para_server if authentication was successful */
149 #define PROCEED_MSG "\nProceed.\n"
150 /** length of the \p PROCEED_MSG string */
151 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
152 /** sent by para_client to indicate the end of the command line */
153 #define EOC_MSG "\nEnd of Command."
154 /** sent by para_client, followed by the decrypted challenge number */
155 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
158 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
161 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
162 long unsigned tv2ms(const struct timeval*);
163 void d2tv(double, struct timeval*);
164 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
165 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
166 void tv_divide(const unsigned long divisor, const struct timeval *tv,
167 struct timeval *result);
168 int tv_convex_combination(const long a, const struct timeval *tv1,
169 const long b, const struct timeval *tv2,
170 struct timeval *result);
171 void ms2tv(const long unsigned n, struct timeval *tv);
172 void compute_chunk_time(long unsigned chunk_num,
173 struct timeval *chunk_tv, struct timeval *stream_start,
174 struct timeval *result);
176 /** The enum of all status items. */
177 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
178 extern const char *status_item_list[];
179 /** Loop over each status item. */
180 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
181 int stat_item_valid(const char *item);
182 int stat_line_valid(const char *);
183 void stat_client_write(const char *msg, int itemnum);
184 int stat_client_add(int fd, long unsigned mask);
186 __printf_2_3 void para_log(int, const char*, ...);
189 * Write a log message to a dynamically allocated string.
191 * \param fmt Usual format string.
192 * \param p Result pointer.
195 #define PARA_VSPRINTF(fmt, p) \
199 p = para_malloc(size); \
202 /* Try to print in the allocated space. */ \
204 n = vsnprintf(p, size, fmt, ap); \
206 /* If that worked, return the string. */ \
207 if (n > -1 && n < size) \
209 /* Else try again with more space. */ \
210 if (n > -1) /* glibc 2.1 */ \
211 size = n + 1; /* precisely what is needed */ \
212 else /* glibc 2.0 */ \
213 size *= 2; /* twice the old size */ \
214 p = para_realloc(p, size); \
219 * Return a random non-negative integer in an interval.
221 * \param max Determines maximal possible return value.
223 * \return An integer between zero and \p max - 1, inclusively.
225 static inline long int para_random(unsigned max)
227 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
230 /** Round up x to a multiple of y */
231 #define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
233 /** Get the size of an array */
234 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
237 * Wrapper for isspace.
241 * The values should be cast to an unsigned char first, then to int.
242 * Why? Because the isdigit (as do all other is/to functions/macros)
243 * expect a number from 0 upto and including 255 as their (int) argument.
244 * Because char is signed on most systems, casting it to int immediately
245 * gives the functions an argument between -128 and 127 (inclusive),
246 * which they will use as an array index, and which will thus fail
247 * horribly for characters which have their most significant bit set.
249 #define para_isspace(c) isspace((int)(unsigned char)(c))