2 * Tvheadend - structures
3 * Copyright (C) 2007 Andreas Ă–man
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <netinet/in.h>
34 #include "tvh_thread.h"
35 #include "tvh_string.h"
36 #include "hts_strtab.h"
43 #include "tvh_locale.h"
45 #define ERRNO_AGAIN(e) ((e) == EAGAIN || (e) == EINTR || (e) == EWOULDBLOCK)
51 const uint32_t *enabled;
54 extern int tvheadend_running;
55 extern const char *tvheadend_version;
56 extern const char *tvheadend_cwd;
57 extern const char *tvheadend_webroot;
58 extern const tvh_caps_t tvheadend_capabilities[];
60 static inline int tvheadend_is_running(void)
62 return atomic_get(&tvheadend_running);
65 htsmsg_t *tvheadend_capabilities_list(int check);
67 typedef struct str_list
74 #define PTS_UNSET INT64_C(0x8000000000000000)
75 #define PTS_MASK INT64_C(0x00000001ffffffff)
77 extern tvh_mutex_t global_lock;
78 extern tvh_mutex_t tasklet_lock;
79 extern tvh_mutex_t fork_lock;
81 extern int tvheadend_webui_port;
82 extern int tvheadend_webui_debug;
83 extern int tvheadend_htsp_port;
85 #if defined(__has_feature)
86 #if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer)
87 #define CLANG_SANITIZER 1
94 #define UILEVEL_DEFAULT (-1)
95 #define UILEVEL_BASIC 0
96 #define UILEVEL_ADVANCED 1
97 #define UILEVEL_EXPERT 2
102 #define CHICON_NONE 0
103 #define CHICON_LOWERCASE 1
104 #define CHICON_SVCNAME 2
109 #define PICON_STANDARD 0
110 #define PICON_ISVCTYPE 1
113 * timer support functions
116 #if ENABLE_GTIMER_CHECK
117 #define GTIMER_TRACEID_ const char *id,
118 #define GTIMER_FCN(n) check_##n
120 #define GTIMER_TRACEID_
121 #define GTIMER_FCN(n) n
125 * global timer - monotonic
128 typedef void (mti_callback_t)(void *opaque);
130 #define MTIMER_MAGIC1 0x0d62a9de
132 typedef struct mtimer {
133 LIST_ENTRY(mtimer) mti_link;
137 mti_callback_t *mti_callback;
140 #if ENABLE_GTIMER_CHECK
145 void GTIMER_FCN(mtimer_arm_rel)
146 (GTIMER_TRACEID_ mtimer_t *mti, mti_callback_t *callback, void *opaque, int64_t delta);
147 void GTIMER_FCN(mtimer_arm_abs)
148 (GTIMER_TRACEID_ mtimer_t *mti, mti_callback_t *callback, void *opaque, int64_t when);
150 #if ENABLE_GTIMER_CHECK
151 #define mtimer_arm_rel(a, b, c, d) GTIMER_FCN(mtimer_arm_rel)(SRCLINEID(), a, b, c, d)
152 #define mtimer_arm_abs(a, b, c, d) GTIMER_FCN(mtimer_arm_abs)(SRCLINEID(), a, b, c, d)
155 void mtimer_disarm(mtimer_t *mti);
160 * global timer (based on the current system time - time())
163 #define GTIMER_MAGIC1 0x8a6f238f
165 typedef void (gti_callback_t)(void *opaque);
167 typedef struct gtimer {
168 LIST_ENTRY(gtimer) gti_link;
172 gti_callback_t *gti_callback;
175 #if ENABLE_GTIMER_CHECK
180 void GTIMER_FCN(gtimer_arm_rel)
181 (GTIMER_TRACEID_ gtimer_t *gti, gti_callback_t *callback, void *opaque, time_t delta);
182 void GTIMER_FCN(gtimer_arm_absn)
183 (GTIMER_TRACEID_ gtimer_t *gti, gti_callback_t *callback, void *opaque, time_t when);
185 #if ENABLE_GTIMER_CHECK
186 #define gtimer_arm_rel(a, b, c, d) GTIMER_FCN(gtimer_arm_rel)(SRCLINEID(), a, b, c, d)
187 #define gtimer_arm_absn(a, b, c, d) GTIMER_FCN(gtimer_arm_absn)(SRCLINEID(), a, b, c, d)
190 void gtimer_disarm(gtimer_t *gti);
197 typedef void (tsk_callback_t)(void *opaque, int disarmed);
199 typedef struct tasklet {
200 TAILQ_ENTRY(tasklet) tsk_link;
201 tsk_callback_t *tsk_callback;
203 void (*tsk_free)(void *);
206 tasklet_t *tasklet_arm_alloc(tsk_callback_t *callback, void *opaque);
207 void tasklet_arm(tasklet_t *tsk, tsk_callback_t *callback, void *opaque);
208 void tasklet_disarm(tasklet_t *gti);
213 #define TVH_KILL_KILL 0
214 #define TVH_KILL_TERM 1
215 #define TVH_KILL_INT 2
216 #define TVH_KILL_HUP 3
217 #define TVH_KILL_USR1 4
218 #define TVH_KILL_USR2 5
220 int tvh_kill_to_sig(int tvh_kill);
222 #define MIN(a,b) ((a) < (b) ? (a) : (b))
223 #define MAX(a,b) ((a) > (b) ? (a) : (b))
224 #define MINMAX(a,mi,ma) MAX(mi, MIN(ma, a))
225 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
227 int sri_to_rate(int sri);
228 int rate_to_sri(int rate);
230 typedef struct th_pipe
238 int tvh_open(const char *pathname, int flags, mode_t mode);
240 int tvh_socket(int domain, int type, int protocol);
242 int tvh_pipe(int flags, th_pipe_t *pipe);
244 void tvh_pipe_close(th_pipe_t *pipe);
246 int tvh_write(int fd, const void *buf, size_t len);
248 int tvh_write_in_chunks(int fd, const void *buf, size_t len, size_t chunkSize);
250 int tvh_nonblock_write(int fd, const void *buf, size_t len);
252 FILE *tvh_fopen(const char *filename, const char *mode);
254 void hexdump(const char *pfx, const uint8_t *data, int len);
256 uint32_t tvh_crc32(const uint8_t *data, size_t datalen, uint32_t crc);
258 int base64_decode(uint8_t *out, const char *in, int out_size);
260 char *base64_encode(char *out, int out_size, const uint8_t *in, int in_size);
262 /* Calculate the output size needed to base64-encode x bytes. */
263 #define BASE64_SIZE(x) (((x)+2) / 3 * 4 + 1)
265 static inline int64_t ts_rescale(int64_t ts, int tb)
267 // return (ts * tb + (tb / 2)) / 90000LL;
268 return (ts * tb ) / 90000LL;
271 static inline int64_t ts_rescale_inv(int64_t ts, int tb)
273 return (ts * 90000LL) / tb;
276 char *md5sum ( const char *str, int lowercase );
277 char *sha256sum ( const char *str, int lowercase );
278 char *sha512sum256 ( const char *str, int lowercase );
279 char *sha256sum_base64 ( const char *str );
281 int makedirs ( int subsys, const char *path, int mode, int mstrict, gid_t gid, uid_t uid );
283 int rmtree ( const char *path );
285 char *regexp_escape ( const char *str );
288 uint8_t *tvh_gzip_inflate ( const uint8_t *data, size_t size, size_t orig );
289 uint8_t *tvh_gzip_deflate ( const uint8_t *data, size_t orig, size_t *size );
290 int tvh_gzip_deflate_fd ( int fd, const uint8_t *data, size_t orig, size_t *size, int speed );
291 int tvh_gzip_deflate_fd_header ( int fd, const uint8_t *data, size_t orig, size_t *size, int speed , const char *signature);
295 char to_hex(char code);
296 char *url_encode(const char *str);
297 void http_deescape(char *str);
299 int mpegts_word_count(const uint8_t *tsb, int len, uint32_t mask);
301 int deferred_unlink(const char *filename, const char *rootdir);
302 void dvr_cutpoint_delete_files (const char *s);
304 void sha1_calc(uint8_t *dst, const uint8_t *d1, size_t d1_len, const uint8_t *d2, size_t d2_len);
306 uint32_t gcdU32(uint32_t a, uint32_t b);
307 static inline int32_t deltaI32(int32_t a, int32_t b) { return (a > b) ? (a - b) : (b - a); }
308 static inline uint32_t deltaU32(uint32_t a, uint32_t b) { return (a > b) ? (a - b) : (b - a); }
310 #define SKEL_DECLARE(name, type) type *name;
311 #define SKEL_ALLOC(name) do { if (!name) name = calloc(1, sizeof(*name)); } while (0)
312 #define SKEL_USED(name) do { name = NULL; } while (0)
313 #define SKEL_FREE(name) do { free(name); name = NULL; } while (0)
315 htsmsg_t *network_interfaces_enum(void *obj, const char *lang);
317 const char *gmtime2local(time_t gmt, char *buf, size_t buflen);
322 qsort_r(void *base, size_t nmemb, size_t size,
323 int (*cmp)(const void *, const void *, void *), void *aux);
324 #endif /* ENABLE_QSORT_R */
326 void tvh_qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg);
330 # if ULONG_MAX == 0xffffffffffffffff
331 # define __WORDSIZE 64
332 # elif ULONG_MAX == 0xffffffff
333 # define __WORDSIZE 32
334 # endif /* ULONG_MAX */
335 #endif /* __WORDSIZE */
338 # define PRItime_t "lld"
339 #elif __WORDSIZE == 64
340 # define PRItime_t "ld"
342 # error "__WORDSIZE not properly defined"
346 #define TVH_NAME_LEN 32
347 #define TVH_TITLE_LEN 256
349 /* sanitizer helpers */
350 #if ENABLE_CCLANG_THREADSAN
351 void *blacklisted_memcpy(void *dest, const void *src, size_t n);
352 int blacklisted_close(int fildes);
354 #define blacklisted_memcpy memcpy
355 #define blacklisted_close close
358 #endif /* TVHEADEND_H */