Begun implimentation of a really good thread control interface.
[citadel.git] / citadel / sysdep_decls.h
1 /* $Id$ */
2
3 #ifndef SYSDEP_DECLS_H
4 #define SYSDEP_DECLS_H
5
6 /*
7  * Uncomment this #define if you are a Citadel developer tracking
8  * down memory leaks in the server.  Do NOT do this on a production
9  * system because it definitely incurs a lot of additional overhead.
10 #define DEBUG_MEMORY_LEAKS
11  */
12
13
14 #include <pthread.h>
15 #include <stdarg.h>
16 #include "sysdep.h"
17 #include "server.h"
18
19 #if SIZEOF_SIZE_T == SIZEOF_INT 
20 #define SIZE_T_FMT "%d"
21 #else
22 #define SIZE_T_FMT "%ld"
23 #endif
24
25
26 /* Logging levels - correspond to syslog(3) */
27 enum LogLevel {
28         /* When about to exit the server for an unrecoverable error */
29          CTDL_EMERG,    /* system is unusable */
30         /* Manual intervention is required to avoid an abnormal exit */
31          CTDL_ALERT,    /* action must be taken immediately */
32         /* The server can continue to run with degraded functionality */
33          CTDL_CRIT,     /* critical conditions */
34         /* An error occurs but the server continues to run normally */
35          CTDL_ERR,      /* error conditions */
36         /* An abnormal condition was detected; server will continue normally */
37          CTDL_WARNING,  /* warning conditions */
38         /* Normal messages (login/out, activity, etc.) */
39          CTDL_NOTICE,   /* normal but significant condition */
40         /* Unimportant progress messages, etc. */
41          CTDL_INFO,     /* informational */
42         /* Debugging messages */
43          CTDL_DEBUG     /* debug-level messages */
44 };
45
46 #ifdef __GNUC__
47 void lprintf (enum LogLevel loglevel, const char *format, ...) __attribute__((__format__(__printf__,2,3)));
48 void cprintf (const char *format, ...) __attribute__((__format__(__printf__,1,2)));
49 #else
50 void lprintf (enum LogLevel loglevel, const char *format, ...);
51 void cprintf (const char *format, ...);
52 #endif
53
54 void vlprintf (enum LogLevel loglevel, const char *format, va_list arg_ptr);
55
56 extern pthread_key_t MyConKey;                  /* TSD key for MyContext() */
57
58 extern int enable_syslog;
59
60 void init_sysdep (void);
61 void begin_critical_section (int which_one);
62 void end_critical_section (int which_one);
63 int ig_tcp_server (char *ip_addr, int port_number, int queue_len,char **errormessage);
64 int ig_uds_server(char *sockpath, int queue_len, char **errormessage);
65 struct CitContext *MyContext (void);
66 struct CitContext *CreateNewContext (void);
67 void InitMyContext (struct CitContext *con);
68 void buffer_output(void);
69 void unbuffer_output(void);
70 void flush_output(void);
71 void client_write (char *buf, int nbytes);
72 int client_read_to (char *buf, int bytes, int timeout);
73 int client_read (char *buf, int bytes);
74 int client_getln (char *buf, int maxbytes);
75 void sysdep_master_cleanup (void);
76 void kill_session (int session_to_kill);
77 void *sd_context_loop (struct CitContext *con);
78 void start_daemon (int do_close_stdio);
79 void cmd_nset (char *cmdbuf);
80 int convert_login (char *NameToConvert);
81 void *worker_thread (void *arg);
82 void become_session(struct CitContext *which_con);
83 void InitializeMasterCC(void);
84 void init_master_fdset(void);
85 void create_worker(void);
86 void InitialiseSemaphores(void);
87 void ctdl_internal_thread_gc (int shutdown);
88
89
90 extern int num_sessions;
91 extern volatile int time_to_die;
92 extern volatile int shutdown_and_halt;
93 extern volatile int running_as_daemon;
94 extern volatile int restart_server;
95
96 extern int verbosity;
97 extern int rescan[];
98
99 extern struct worker_node {
100         pthread_t tid;
101         struct worker_node *next;
102 } *worker_list;
103
104
105 extern struct CtdlThreadNode {
106         pthread_t tid;
107         char *name;
108         void *(*thread_func) (void *arg);
109         void *user_args;
110         int flags;
111         int running;
112         int valid;
113         struct CtdlThreadNode *prev;
114         struct CtdlThreadNode *next;
115 } *CtdlThreadList;
116
117 extern int SyslogFacility(char *name);
118 extern int syslog_facility;
119
120 #ifdef DEBUG_MEMORY_LEAKS
121 #define malloc(x) tracked_malloc(x, __FILE__, __LINE__)
122 #define realloc(x,y) tracked_realloc(x, y, __FILE__, __LINE__)
123 #undef strdup
124 #define strdup(x) tracked_strdup(x, __FILE__, __LINE__)
125 #define free(x) tracked_free(x)
126 void *tracked_malloc(size_t size, char *file, int line);
127 void *tracked_realloc(void *ptr, size_t size, char *file, int line);
128 void tracked_free(void *ptr);
129 char *tracked_strdup(const char *s, char *file, int line);
130 void dump_heap(void);
131 #endif
132
133 void create_maintenance_threads(void);
134
135 #endif /* SYSDEP_DECLS_H */