1 /*
2  * Copyright (c) 2016-2018 NLNet Labs.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef JANITOR_H
28 #define JANITOR_H
29 
30 #include "config.h"
31 
32 #include <pthread.h>
33 
34 struct janitor_thread_struct;
35 typedef struct janitor_thread_struct* janitor_thread_t;
36 
37 typedef void (*janitor_runfn_t)(void *);
38 
39 typedef void (*janitor_alertfn_t)(const char *format, ...)
40 #ifdef HAVE___ATTRIBUTE__
41      __attribute__ ((format (printf, 1, 2)))
42 #endif
43      ;
44 
45 extern void janitor_initialize(janitor_alertfn_t fatalalertfn, janitor_alertfn_t problemalertfn);
46 
47 struct janitor_threadclass_struct;
48 typedef struct janitor_threadclass_struct* janitor_threadclass_t;
49 #define janitor_threadclass_DEFAULT (NULL)
50 
51 extern int janitor_threadclass_create(janitor_threadclass_t* threadclassptr, const char* name);
52 extern char* janitor_threadclass_name(janitor_threadclass_t threadclass);
53 extern void janitor_threadclass_destroy(janitor_threadclass_t threadclass);
54 extern void janitor_threadclass_setdetached(janitor_threadclass_t threadclass);
55 extern void janitor_threadclass_setautorun(janitor_threadclass_t threadclass);
56 extern void janitor_threadclass_setblockedsignals(janitor_threadclass_t threadclass);
57 extern void janitor_threadclass_setminstacksize(janitor_threadclass_t threadclass, size_t minstacksize);
58 
59 extern int janitor_thread_create(janitor_thread_t* thread, janitor_threadclass_t threadclass, janitor_runfn_t func, void*data);
60 extern void janitor_thread_start(janitor_thread_t thread);
61 extern int janitor_thread_join(janitor_thread_t thread);
62 extern int janitor_thread_tryjoinall(janitor_threadclass_t threadclass);
63 extern void janitor_thread_joinall(janitor_threadclass_t threadclass);
64 
65 extern int janitor_disablecoredump(void);
66 extern int janitor_trapsignals(char* argv0);
67 
68 extern void janitor_backtrace(void);
69 extern char* janitor_backtrace_string(void);
70 extern void janitor_backtrace_all(void);
71 
72 extern void janitor_thread_signal(janitor_thread_t thread);
73 
74 /* in case of missing pthread barrier calls */
75 #ifndef HAVE_PTHREAD_BARRIER_WAIT
76 # ifdef pthread_barrier_init
77 #  undef pthread_barrier_init
78 # endif
79 # define pthread_barrier_init janitor_pthread_barrier_init
80 # ifdef pthread_barrier_destroy
81 #  undef pthread_barrier_destroy
82 # endif
83 #  define pthread_barrier_destroy janitor_pthread_barrier_destroy
84 # ifdef pthread_barrier_wait
85 #  undef pthread_barrier_wait
86 # endif
87 # define pthread_barrier_wait janitor_pthread_barrier_wait
88 # ifndef PTHREAD_BARRIER_SERIAL_THREAD
89 #  define PTHREAD_BARRIER_SERIAL_THREAD 1
90 # endif
91 # ifdef pthread_barrier_t
92 #  undef pthread_barrier_t
93 # endif
94 # define pthread_barrier_t struct janitor_pthread_barrier_struct*
95 # ifdef pthread_barrierattr_t
96 #  undef pthread_barrierattr_t
97 # endif
98 # define pthread_barrierattr_t void*
99 #endif
100 
101 struct janitor_pthread_barrier_struct;
102 extern int janitor_pthread_barrier_init(pthread_barrier_t* barrier, const pthread_barrierattr_t* attr, unsigned int count);
103 extern int janitor_pthread_barrier_destroy(pthread_barrier_t* barrier);
104 extern int janitor_pthread_barrier_wait(pthread_barrier_t* barrier);
105 
106 #endif
107