eb99ec68e6f2609da70f8d75d3dc038c1c4ea59e
[paraslash.git] / para.h
1 /*
2 * Copyright (C) 1997-2007 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 a and \a b */
34 #define PARA_MIN(a,b) ((a) < (b) ? (a) : (b))
35 /** compute the maximum of \a a and \a b */
36 #define PARA_MAX(a,b) ((a) > (b) ? (a) : (b))
37 /** compute the absolute value of \a a */
38 #define PARA_ABS(a) ((a) > 0 ? (a) : -(a))
39
40 /** debug loglevel, gets really noisy */
41 #define DEBUG 1
42 /** still noisy, but won't fill your disk */
43 #define INFO 2
44 /** normal, but significant event */
45 #define NOTICE 3
46 /** unexpected event that can be handled */
47 #define WARNING 4
48 /** unhandled error condition */
49 #define ERROR 5
50 /** system might be unreliable */
51 #define CRIT 6
52 /** last message before exit */
53 #define EMERG 7
54
55 /** log messages with lower proirity than that will not be compiled in*/
56 #define COMPILE_TIME_LOGLEVEL 0
57
58 #if DEBUG > COMPILE_TIME_LOGLEVEL
59 #define PARA_DEBUG_LOG(f,...) para_log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
60 #else
61 #define PARA_DEBUG_LOG(...) do {;} while (0)
62 #endif
63
64 #if INFO > COMPILE_TIME_LOGLEVEL
65 #define PARA_INFO_LOG(f,...) para_log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
66 #else
67 #define PARA_INFO_LOG(...) do {;} while (0)
68 #endif
69
70 #if NOTICE > COMPILE_TIME_LOGLEVEL
71 #define PARA_NOTICE_LOG(f,...) para_log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
72 #else
73 #define PARA_NOTICE_LOG(...) do {;} while (0)
74 #endif
75
76 #if WARNING > COMPILE_TIME_LOGLEVEL
77 #define PARA_WARNING_LOG(f,...) para_log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
78 #else
79 #define PARA_WARNING_LOG(...) do {;} while (0)
80 #endif
81
82 #if ERROR > COMPILE_TIME_LOGLEVEL
83 #define PARA_ERROR_LOG(f,...) para_log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
84 #else
85 #define PARA_ERROR_LOG(...) do {;} while (0)
86 #endif
87
88 #if CRIT > COMPILE_TIME_LOGLEVEL
89 #define PARA_CRIT_LOG(f,...) para_log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
90 #else
91 #define PARA_CRIT_LOG(...) do {;} while (0)
92 #endif
93
94 #if EMERG > COMPILE_TIME_LOGLEVEL
95 #define PARA_EMERG_LOG(f,...) para_log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
96 #else
97 #define PARA_EMERG_LOG(...)
98 #endif
99
100 /**
101 * define a standard log function that always writes to stderr
102 *
103 * \param loglevel_barrier If the loglevel of the current message
104 * is less than that, the message is going to be ignored.
105 *
106 */
107 #define INIT_STDERR_LOGGING(loglevel_barrier) \
108 __printf_2_3 void para_log(int ll, const char* fmt,...) \
109 { \
110 va_list argp; \
111 if (ll < loglevel_barrier) \
112 return; \
113 va_start(argp, fmt); \
114 vfprintf(stderr, fmt, argp); \
115 va_end(argp); \
116 }
117
118 /** version text used by various commands if -V switch was given */
119 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION "\n" \
120 "Copyright (C) 2007 Andre Noll\n" \
121 "This is free software with ABSOLUTELY NO WARRANTY." \
122 " See COPYING for details.\n" \
123 "Written by Andre Noll.\n" \
124 "Report bugs to <maan@systemlinux.org>.\n"
125
126 /** print out \p VERSION_TEXT and exit if version flag was given */
127 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
128 if (_args_info_struct.version_given) { \
129 printf("%s", VERSION_TEXT(_prefix)); \
130 exit(EXIT_SUCCESS); \
131 }
132 /** sent by para_server for commands that expect a data file */
133 #define AWAITING_DATA_MSG "\nAwaiting Data."
134 /** sent by para_server if authentication was successful */
135 #define PROCEED_MSG "\nProceed.\n"
136 /** length of the \p PROCEED_MSG string */
137 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
138 /** sent by para_client to indicate the end of the command line */
139 #define EOC_MSG "\nEnd of Command."
140 /** sent by para_client, followed by the decrypted challenge number */
141 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
142
143 /* exec */
144 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
145
146 /* time */
147 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
148 long unsigned tv2ms(const struct timeval*);
149 void d2tv(double, struct timeval*);
150 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
151 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
152 void tv_divide(const unsigned long divisor, const struct timeval *tv,
153 struct timeval *result);
154 int tv_convex_combination(const long a, const struct timeval *tv1,
155 const long b, const struct timeval *tv2,
156 struct timeval *result);
157 void ms2tv(const long unsigned n, struct timeval *tv);
158
159 /* stat */
160 enum {
161 SI_STATUS_BAR, SI_STATUS, SI_NUM_PLAYED,
162 SI_MTIME, SI_LENGTH_MIN, SI_LENGTH_SEC,
163 SI_FILE_SIZE, SI_STATUS_FLAGS, SI_FORMAT,
164 SI_SCORE, SI_AUDIO_INFO1, SI_AUDIO_INFO2,
165 SI_AUDIO_INFO3, SI_DBINFO1, SI_DBINFO2,
166 SI_DBINFO3, SI_DECODER_FLAGS, SI_AUDIOD_STATUS,
167 SI_PLAY_TIME, SI_UPTIME, SI_OFFSET,
168 SI_LENGTH, SI_STREAM_START, SI_CURRENT_TIME,
169 SI_AUDIOD_UPTIME, SI_SELECTOR, NUM_STAT_ITEMS
170 };
171
172 int stat_item_valid(const char *item);
173 int stat_line_valid(const char *);
174 void stat_client_write(const char *msg, int itemnum);
175 int stat_client_add(int fd, long unsigned mask);
176 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
177
178 __printf_2_3 void para_log(int, const char*, ...);
179
180 /* taken from printf man page */
181 #define PARA_VSPRINTF(fmt, p) \
182 { \
183 int n; \
184 size_t size = 100; \
185 p = para_malloc(size); \
186 while (1) { \
187 va_list ap; \
188 /* Try to print in the allocated space. */ \
189 va_start(ap, fmt); \
190 n = vsnprintf(p, size, fmt, ap); \
191 va_end(ap); \
192 /* If that worked, return the string. */ \
193 if (n > -1 && n < size) \
194 break; \
195 /* Else try again with more space. */ \
196 if (n > -1) /* glibc 2.1 */ \
197 size = n + 1; /* precisely what is needed */ \
198 else /* glibc 2.0 */ \
199 size *= 2; /* twice the old size */ \
200 p = para_realloc(p, size); \
201 } \
202 }
203
204 static inline int para_random(unsigned max)
205 {
206 return ((max + 0.0) * (rand() / (RAND_MAX + 1.0)));
207 }
208