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
20 #elif defined(HAVE_DB4_DB_H)
23 #error Neither <db.h> nor <db4/db.h> was found by configure. Install db4-devel.
27 #if DB_VERSION_MAJOR < 4 || DB_VERSION_MINOR < 1
28 #error Citadel requires Berkeley DB v4.1 or newer. Please upgrade.
34 #if SIZEOF_SIZE_T == SIZEOF_INT
35 #define SIZE_T_FMT "%d"
37 #define SIZE_T_FMT "%ld"
41 /* Logging levels - correspond to syslog(3) */
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 */
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)));
65 void lprintf (enum LogLevel loglevel, const char *format, ...);
66 void cprintf (const char *format, ...);
69 void vlprintf (enum LogLevel loglevel, const char *format, va_list arg_ptr);
71 extern pthread_key_t MyConKey; /* TSD key for MyContext() */
73 extern int enable_syslog;
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);
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;
111 extern int verbosity;
119 #define CTDLTHREAD_BIGSTACK 0x0001
120 #define CTDLTHREAD_WORKER 0x0002
122 enum CtdlThreadState {
126 CTDL_THREAD_CANCELLED,
128 CTDL_THREAD_STOPPING,
129 CTDL_THREAD_STOP_REQ, /* Do NOT put any running states before this state */
130 CTDL_THREAD_SLEEPING,
133 CTDL_THREAD_LAST_STATE
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 const char *name; /* A name for this thread */
145 void *(*thread_func) (void *arg); /* The actual function that does this threads work */
146 void *user_args; /* Arguments passed to this threads work function */
147 long flags; /* Flags that describe this thread */
148 enum CtdlThreadState state; /* Flag to show state of this thread */
149 pthread_mutex_t ThreadMutex; /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */
150 pthread_cond_t ThreadCond; /* A condition variable to sync this thread with others */
151 pthread_mutex_t SleepMutex; /* A mutex for sleeping */
152 pthread_cond_t SleepCond; /* A condition variable for sleeping */
153 pthread_attr_t attr; /* Attributes of this thread */
154 struct timeval start_time; /* Time this thread was started */
155 struct timeval last_state_change; /* Time when this thread last changed state */
156 double avg_sleeping; /* Average sleeping time */
157 double avg_running; /* Average running time */
158 double avg_blocked; /* Average blocked time */
159 double load_avg; /* Load average for this thread */
160 struct CtdlThreadNode *prev; /* Previous thread in the thread table */
161 struct CtdlThreadNode *next; /* Next thread in the thread table */
165 DB_TXN *tid; /* Transaction handle */
166 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
167 struct CtdlThreadNode *self; /* Pointer to this threads control structure */
170 extern double CtdlThreadLoadAvg;
171 extern double CtdlThreadWorkerAvg;
172 extern pthread_key_t ThreadKey;
174 void ctdl_thread_internal_init_tsd(void);
175 void ctdl_internal_thread_gc (void);
176 void ctdl_thread_internal_init(void);
177 void ctdl_thread_internal_cleanup(void);
178 void ctdl_thread_internal_calc_loadavg(void);
179 void ctdl_thread_internal_free_tsd(void);
180 struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args);
181 void ctdl_thread_internal_check_scheduled(void);
185 extern int SyslogFacility(char *name);
186 extern int syslog_facility;
188 #ifdef DEBUG_MEMORY_LEAKS
189 #define malloc(x) tracked_malloc(x, __FILE__, __LINE__)
190 #define realloc(x,y) tracked_realloc(x, y, __FILE__, __LINE__)
192 #define strdup(x) tracked_strdup(x, __FILE__, __LINE__)
193 #define free(x) tracked_free(x)
194 void *tracked_malloc(size_t size, char *file, int line);
195 void *tracked_realloc(void *ptr, size_t size, char *file, int line);
196 void tracked_free(void *ptr);
197 char *tracked_strdup(const char *s, char *file, int line);
198 void dump_heap(void);
201 void create_maintenance_threads(void);
203 #endif /* SYSDEP_DECLS_H */