1 /*
2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
7 /** \file para.h global paraslash definitions */
9 #include "config.h"
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 <string.h>
23 #include <assert.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26 #include <sys/uio.h>
27 #include "gcc-compat.h"
29 /** used in various contexts */
30 #define MAXLINE 255
32 /** Compute the minimum of \a x and \a y. */
33 #define PARA_MIN(x, y) ({ \
34 typeof(x) _min1 = (x); \
35 typeof(y) _min2 = (y); \
36 (void) (&_min1 == &_min2); \
37 _min1 < _min2 ? _min1 : _min2; })
39 /** Compute the maximum of \a x and \a y. */
40 #define PARA_MAX(x, y) ({ \
41 typeof(x) _max1 = (x); \
42 typeof(y) _max2 = (y); \
43 (void) (&_max1 == &_max2); \
44 _max1 < _max2 ? _max2 : _max1; })
46 /** Compute the absolute value of \a x. */
47 #define PARA_ABS(x) ({ \
48 typeof(x) _x = (x); \
49 _x > 0? _x : -_x; })
52 extern __printf_2_3 void (*para_log)(int, const char*, ...);
53 /**
54 * Define a standard log function that always writes to stderr.
55 *
56 * \param funcname The name of the function to be defined.
57 *
58 * \param loglevel_barrier If the loglevel of the current message
59 * is less than that, the message is going to be ignored.
60 *
61 */
62 #define DEFINE_STDERR_LOGGER(funcname, loglevel_barrier) \
63 static __printf_2_3 void funcname(int ll, const char* fmt,...) \
64 { \
65 va_list argp; \
66 if (ll < loglevel_barrier) \
67 return; \
68 va_start(argp, fmt); \
69 vfprintf(stderr, fmt, argp); \
70 va_end(argp); \
71 }
72 /**
73 * Define the standard log function and activate it.
74 *
75 * \param loglevel_barrier See \ref DEFINE_STDERR_LOGGER.
76 */
77 #define INIT_STDERR_LOGGING(loglevel_barrier) \
78 DEFINE_STDERR_LOGGER(stderr_log, loglevel_barrier); \
79 __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
81 /** Sent by para_client to initiate the authentication procedure. */
82 #define AUTH_REQUEST_MSG "auth rsa "
84 /* exec */
85 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
87 /* time */
88 int tv_diff(const struct timeval *b, const struct timeval *a,
89 struct timeval *diff);
90 long unsigned tv2ms(const struct timeval *tv);
91 void tv_add(const struct timeval *a, const struct timeval *b,
92 struct timeval *sum);
93 void tv_scale(const unsigned long mult, const struct timeval *tv,
94 struct timeval *result);
95 void tv_divide(const unsigned long divisor, const struct timeval *tv,
96 struct timeval *result);
97 int tv_convex_combination(const long a, const struct timeval *tv1,
98 const long b, const struct timeval *tv2,
99 struct timeval *result);
100 void ms2tv(long unsigned n, struct timeval *tv);
101 void compute_chunk_time(long unsigned chunk_num,
102 struct timeval *chunk_tv, struct timeval *stream_start,
103 struct timeval *result);
104 struct timeval *clock_get_realtime(struct timeval *tv);
106 /** The enum of all status items. */
107 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
108 extern const char *status_item_list[];
109 /** Loop over each status item. */
110 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
111 int for_each_stat_item(char *item_buf, size_t num_bytes,
112 int (*item_handler)(int, char *));
115 /**
116 * Return a random non-negative integer in an interval.
117 *
118 * \param max Determines maximal possible return value.
119 *
120 * \return An integer between zero and \p max - 1, inclusively.
121 */
122 _static_inline_ long int para_random(unsigned max)
123 {
124 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
125 }
127 /**
128 * Simple sanity check for I/O vectors.
129 *
130 * \param iov Pointer to the I/O vector to check.
131 *
132 * \return True if \a iov points to a non-empty buffer.
133 */
134 _static_inline_ bool iov_valid(const struct iovec *iov)
135 {
136 return iov && iov->iov_len > 0 && iov->iov_base;
137 }
139 /** Round up x to next multiple of y. */
140 #define ROUND_UP(x, y) ({ \
141 const typeof(y) _divisor = y; \
142 ((x) + _divisor - 1) / _divisor * _divisor; })
144 /** Round down x to multiple of y. */
145 #define ROUND_DOWN(x, y) ({ \
146 const typeof(y) _divisor = y; \
147 (x) / _divisor * _divisor; })
149 /** Divide and round up to next integer. */
150 #define DIV_ROUND_UP(x, y) ({ \
151 typeof(y) _divisor = y; \
152 ((x) + _divisor - 1) / _divisor; })
154 /**
155 * Assert a build-time dependency, as an expression.
156 *
157 * \param cond The compile-time condition which must be true.
158 *
159 * Compilation will fail if the condition isn't true, or can't be evaluated by
160 * the compiler. This can be used in an expression: its value is "0".
161 *
162 * Taken from ccan.
163 */
164 #define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2 * !(cond)]) - 1)
166 /** &a[0] degrades to a pointer: a different type from an array */
167 #define _array_size_chk(arr) EXPR_BUILD_ASSERT(\
168 !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])))
169 /** Get the size of an array */
170 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
172 /**
173 * Wrapper for isspace.
174 * NetBSD needs this.
175 */
176 /*
177 * The values should be cast to an unsigned char first, then to int.
178 * Why? Because the isdigit (as do all other is/to functions/macros)
179 * expect a number from 0 upto and including 255 as their (int) argument.
180 * Because char is signed on most systems, casting it to int immediately
181 * gives the functions an argument between -128 and 127 (inclusive),
182 * which they will use as an array index, and which will thus fail
183 * horribly for characters which have their most significant bit set.
184 */
185 #define para_isspace(c) isspace((int)(unsigned char)(c))
187 /** Data that indicates an eof-condition for a fec-encoded stream. */
188 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
189 "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
190 /** The number of bytes of the \a FEC_EOF_PACKET. */
191 #define FEC_EOF_PACKET_LEN 32
193 /** Used to avoid a shortcoming in vim's syntax highlighting. */
194 #define EMBRACE(...) { __VA_ARGS__}
196 /** A nice cup of STFU for Mr gcc. */
197 #define do_nothing do {/* nothing */} while (0)
199 /**
200 * The sample formats supported by paraslash.
201 *
202 * It may be determined by one of the following sources:
203 *
204 * 1. The decoding filter (para_audiod only). In this case, it is always
205 * \p SF_S16_LE which is the canonical format used within decoders.
206 *
207 * 2. The wav header (para_write only).
208 *
209 * 3. The --sample-format option of para_write.
210 */
211 #define SAMPLE_FORMATS \
212 SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
213 SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
214 SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
215 SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
216 SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
217 SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
219 /** \cond sample_format */
220 #define SAMPLE_FORMAT(a, b) a
221 enum sample_format {SAMPLE_FORMATS};
222 #undef SAMPLE_FORMAT
223 #define SAMPLE_FORMAT(a, b) b
224 /** \endcond sample_format */
226 /** Debug, Info, etc. */
227 enum loglevels {LOGLEVELS, NUM_LOGLEVELS};
229 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
230 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
231 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
232 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
233 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
234 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
235 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)