A couple of minor speed ups and the beginings of using signals in
[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
18 #ifdef HAVE_DB_H
19 #include <db.h>
20 #elif defined(HAVE_DB4_DB_H)
21 #include <db4/db.h>
22 #else
23 #error Neither <db.h> nor <db4/db.h> was found by configure. Install db4-devel.
24 #endif
25
26
27 #if DB_VERSION_MAJOR < 4 || DB_VERSION_MINOR < 1
28 #error Citadel requires Berkeley DB v4.1 or newer.  Please upgrade.
29 #endif
30
31 #include "server.h"
32 #include "database.h"
33
34 #if SIZEOF_SIZE_T == SIZEOF_INT 
35 #define SIZE_T_FMT "%d"
36 #else
37 #define SIZE_T_FMT "%ld"
38 #endif
39
40
41 /* Logging levels - correspond to syslog(3) */
42 enum LogLevel {
43         /* When about to exit the server for an unrecoverable error */
44          CTDL_EMERG,    /* system is unusable */
45         /* Manual intervention is required to avoid an abnormal exit */
46          CTDL_ALERT,    /* action must be taken immediately */
47         /* The server can continue to run with degraded functionality */
48          CTDL_CRIT,     /* critical conditions */
49         /* An error occurs but the server continues to run normally */
50          CTDL_ERR,      /* error conditions */
51         /* An abnormal condition was detected; server will continue normally */
52          CTDL_WARNING,  /* warning conditions */
53         /* Normal messages (login/out, activity, etc.) */
54          CTDL_NOTICE,   /* normal but significant condition */
55         /* Unimportant progress messages, etc. */
56          CTDL_INFO,     /* informational */
57         /* Debugging messages */
58          CTDL_DEBUG     /* debug-level messages */
59 };
60
61 #ifdef __GNUC__
62 void lprintf (enum LogLevel loglevel, const char *format, ...) __attribute__((__format__(__printf__,2,3)));
63 void cprintf (const char *format, ...) __attribute__((__format__(__printf__,1,2)));
64 #else
65 void lprintf (enum LogLevel loglevel, const char *format, ...);
66 void cprintf (const char *format, ...);
67 #endif
68
69 void vlprintf (enum LogLevel loglevel, const char *format, va_list arg_ptr);
70
71 extern pthread_key_t MyConKey;                  /* TSD key for MyContext() */
72
73 extern int enable_syslog;
74
75 void init_sysdep (void);
76 void begin_critical_section (int which_one);
77 void end_critical_section (int which_one);
78 int ig_tcp_server (char *ip_addr, int port_number, int queue_len,char **errormessage);
79 int ig_uds_server(char *sockpath, int queue_len, char **errormessage);
80 struct CitContext *MyContext (void);
81 struct CitContext *CreateNewContext (void);
82 void InitMyContext (struct CitContext *con);
83 void buffer_output(void);
84 void unbuffer_output(void);
85 void flush_output(void);
86 void client_write (char *buf, int nbytes);
87 int client_read_to (char *buf, int bytes, int timeout);
88 int client_read (char *buf, int bytes);
89 int client_getln (char *buf, int maxbytes);
90 void sysdep_master_cleanup (void);
91 void kill_session (int session_to_kill);
92 void *sd_context_loop (struct CitContext *con);
93 void start_daemon (int do_close_stdio);
94 void cmd_nset (char *cmdbuf);
95 int convert_login (char *NameToConvert);
96 void *worker_thread (void *arg);
97 void *context_cleanup_thread (void *arg);
98 void become_session(struct CitContext *which_con);
99 void InitializeMasterCC(void);
100 void init_master_fdset(void);
101 void create_worker(void);
102 void InitialiseSemaphores(void);
103
104
105 extern int num_sessions;
106 extern volatile int exit_signal;
107 extern volatile int shutdown_and_halt;
108 extern volatile int running_as_daemon;
109 extern volatile int restart_server;
110
111 extern int verbosity;
112 extern int rescan[];
113
114
115
116 /*
117  * Thread stuff
118  */
119 #define CTDLTHREAD_BIGSTACK     0x0001
120 #define CTDLTHREAD_WORKER       0x0002
121
122 enum CtdlThreadState {
123         CTDL_THREAD_INVALID,
124         CTDL_THREAD_VALID,
125         CTDL_THREAD_CREATE,
126         CTDL_THREAD_CANCELLED,
127         CTDL_THREAD_EXITED,
128         CTDL_THREAD_STOPPING,
129         CTDL_THREAD_STOP_REQ,   /* Do NOT put any running states before this state */
130         CTDL_THREAD_SLEEPING,
131         CTDL_THREAD_BLOCKED,
132         CTDL_THREAD_RUNNING,
133         CTDL_THREAD_LAST_STATE
134 };
135
136 extern struct CtdlThreadNode {
137         pthread_t tid;                          /* id as returned by pthread_create() */
138         pid_t pid;                              /* pid, as best the OS will let us determine */
139         time_t when;                            /* When to start a scheduled thread */
140         struct CitConext *Context;              /* The session context that this thread mught be working on or NULL if none */
141         long number;                            /* A unigue number for this thread (not implimented yet) */
142         int wakefd_recv;                        /* An fd that this thread can sleep on (not implimented yet) */
143         int wakefd_send;                        /* An fd that this thread can send out on (Not implimented yet) */
144         int signal;                             /* A field to store a signal we caught. */
145         const char *name;                       /* A name for this thread */
146         void *(*thread_func) (void *arg);       /* The actual function that does this threads work */
147         void *user_args;                        /* Arguments passed to this threads work function */
148         long flags;                             /* Flags that describe this thread */
149         enum CtdlThreadState state;             /* Flag to show state of this thread */
150         pthread_mutex_t ThreadMutex;            /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
151         pthread_cond_t ThreadCond;              /* A condition variable to sync this thread with others */
152         pthread_mutex_t SleepMutex;             /* A mutex for sleeping */
153         pthread_cond_t SleepCond;               /* A condition variable for sleeping */
154         pthread_attr_t attr;                    /* Attributes of this thread */
155         struct timeval start_time;              /* Time this thread was started */
156         struct timeval last_state_change;       /* Time when this thread last changed state */
157         double avg_sleeping;                    /* Average sleeping time */
158         double avg_running;                     /* Average running time */
159         double avg_blocked;                     /* Average blocked time */
160         double load_avg;                        /* Load average for this thread */
161         struct CtdlThreadNode *prev;            /* Previous thread in the thread table */
162         struct CtdlThreadNode *next;            /* Next thread in the thread table */
163 } *CtdlThreadList;
164
165 typedef struct {
166         DB_TXN *tid;            /* Transaction handle */
167         DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
168         struct CtdlThreadNode *self;    /* Pointer to this threads control structure */
169 }ThreadTSD ;
170
171 extern double CtdlThreadLoadAvg;
172 extern double CtdlThreadWorkerAvg;
173 extern pthread_key_t ThreadKey;
174
175 void ctdl_thread_internal_init_tsd(void);
176 void ctdl_internal_thread_gc (void);
177 void ctdl_thread_internal_init(void);
178 void ctdl_thread_internal_cleanup(void);
179 void ctdl_thread_internal_calc_loadavg(void);
180 void ctdl_thread_internal_free_tsd(void);
181 struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
182 void ctdl_thread_internal_check_scheduled(void);
183
184
185
186 extern int SyslogFacility(char *name);
187 extern int syslog_facility;
188
189 #ifdef DEBUG_MEMORY_LEAKS
190 #define malloc(x) tracked_malloc(x, __FILE__, __LINE__)
191 #define realloc(x,y) tracked_realloc(x, y, __FILE__, __LINE__)
192 #undef strdup
193 #define strdup(x) tracked_strdup(x, __FILE__, __LINE__)
194 #define free(x) tracked_free(x)
195 void *tracked_malloc(size_t size, char *file, int line);
196 void *tracked_realloc(void *ptr, size_t size, char *file, int line);
197 void tracked_free(void *ptr);
198 char *tracked_strdup(const char *s, char *file, int line);
199 void dump_heap(void);
200 #endif
201
202 void create_maintenance_threads(void);
203
204 #endif /* SYSDEP_DECLS_H */