Now do garbage collection every second.
[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 *context_cleanup_thread (void *arg);
83 void become_session(struct CitContext *which_con);
84 void InitializeMasterCC(void);
85 void init_master_fdset(void);
86 void create_worker(void);
87 void InitialiseSemaphores(void);
88
89
90 extern int num_sessions;
91 extern volatile int exit_signal;
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
106 /*
107  * Thread stuff
108  */
109 #define CTDLTHREAD_BIGSTACK     0x0001
110 #define CTDLTHREAD_WORKER       0x0002
111 extern double CtdlThreadLoadAvg;
112 extern double CtdlThreadWorkerAvg;
113
114 void ctdl_internal_thread_gc (void);
115 void ctdl_thread_internal_init(void);
116 void ctdl_thread_internal_cleanup(void);
117 void ctdl_thread_internal_calc_loadavg(void);
118 struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
119
120 enum CtdlThreadState {
121         CTDL_THREAD_INVALID,
122         CTDL_THREAD_VALID,
123         CTDL_THREAD_CREATE,
124         CTDL_THREAD_CANCELLED,
125         CTDL_THREAD_EXITED,
126         CTDL_THREAD_STOPPING,
127         CTDL_THREAD_STOP_REQ,   /* Do NOT put any running states before this state */
128         CTDL_THREAD_SLEEPING,
129         CTDL_THREAD_BLOCKED,
130         CTDL_THREAD_RUNNING,
131         CTDL_THREAD_LAST_STATE
132 };
133
134 extern struct CtdlThreadNode {
135         pthread_t tid;                          /* id as returned by pthread_create() */
136         pid_t pid;                              /* pid, as best the OS will let us determine */
137         struct CitConext *Context;              /* The session context that this thread mught be working on or NULL if none */
138         long number;                            /* A unigue number for this thread (not implimented yet) */
139         int wakefd_recv;                        /* An fd that this thread can sleep on (not implimented yet) */
140         int wakefd_send;                        /* An fd that this thread can send out on (Not implimented yet) */
141         char *name;                             /* A name for this thread */
142         void *(*thread_func) (void *arg);       /* The actual function that does this threads work */
143         void *user_args;                        /* Arguments passed to this threads work function */
144         long flags;                             /* Flags that describe this thread */
145         enum CtdlThreadState state;                             /* Flag to show state of this thread */
146         pthread_mutex_t ThreadMutex;            /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
147         pthread_cond_t ThreadCond;              /* A condition variable to sync this thread with others (also used for sleeping) */
148         pthread_attr_t attr;                    /* Attributes of this thread */
149         struct timeval start_time;              /* Time this thread was started */
150         struct timeval last_state_change;       /* Time when this thread last changed state */
151         double avg_sleeping;                    /* Average sleeping time */
152         double avg_running;                     /* Average running time */
153         double avg_blocked;                     /* Average blocked time */
154         double load_avg;                        /* Load average for this thread */
155         struct CtdlThreadNode *prev;            /* Previous thread in the thread table */
156         struct CtdlThreadNode *next;            /* Next thread in the thread table */
157 } *CtdlThreadList;
158
159 extern int SyslogFacility(char *name);
160 extern int syslog_facility;
161
162 #ifdef DEBUG_MEMORY_LEAKS
163 #define malloc(x) tracked_malloc(x, __FILE__, __LINE__)
164 #define realloc(x,y) tracked_realloc(x, y, __FILE__, __LINE__)
165 #undef strdup
166 #define strdup(x) tracked_strdup(x, __FILE__, __LINE__)
167 #define free(x) tracked_free(x)
168 void *tracked_malloc(size_t size, char *file, int line);
169 void *tracked_realloc(void *ptr, size_t size, char *file, int line);
170 void tracked_free(void *ptr);
171 char *tracked_strdup(const char *s, char *file, int line);
172 void dump_heap(void);
173 #endif
174
175 void create_maintenance_threads(void);
176
177 #endif /* SYSDEP_DECLS_H */