start moving system commands into its own file
[citadel.git] / citadel / modules / ctdlproto / serv_syscmds.c
1
2 /* 
3  * Main source module for the Citadel server
4  *
5  * Copyright (c) 1987-2011 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #if HAVE_BACKTRACE
37 #include <execinfo.h>
38 #endif
39
40 #include <ctype.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <netdb.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <libcitadel.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "sysdep_decls.h"
53 #include "threads.h"
54 #include "citserver.h"
55 #include "config.h"
56 #include "database.h"
57 #include "housekeeping.h"
58 #include "user_ops.h"
59 #include "msgbase.h"
60 #include "support.h"
61 #include "locate_host.h"
62 #include "room_ops.h"
63 #include "control.h"
64 #include "euidindex.h"
65 #include "context.h"
66 #include "svn_revision.h"
67 #include "ctdl_module.h"
68
69
70
71 /*
72  * Shut down the server
73  */
74 void cmd_down(char *argbuf) {
75         char *Reply ="%d Shutting down server.  Goodbye.\n";
76
77         if (CtdlAccessCheck(ac_aide)) return;
78
79         if (!IsEmptyStr(argbuf))
80         {
81                 int state = CIT_OK;
82                 restart_server = extract_int(argbuf, 0);
83                 
84                 if (restart_server > 0)
85                 {
86                         Reply = "%d citserver will now shut down and automatically restart.\n";
87                 }
88                 if ((restart_server > 0) && !running_as_daemon)
89                 {
90                         syslog(LOG_ERR, "The user requested restart, but not running as daemon! Geronimooooooo!\n");
91                         Reply = "%d Warning: citserver is not running in daemon mode and is therefore unlikely to restart automatically.\n";
92                         state = ERROR;
93                 }
94                 cprintf(Reply, state);
95         }
96         else
97         {
98                 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); 
99         }
100         CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
101         server_shutting_down = 1;
102 }
103
104
105 /*
106  * Halt the server without exiting the server process.
107  */
108 void cmd_halt(char *argbuf) {
109
110         if (CtdlAccessCheck(ac_aide)) return;
111
112         cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
113         server_shutting_down = 1;
114         shutdown_and_halt = 1;
115 }
116
117
118 /*
119  * Schedule or cancel a server shutdown
120  */
121 void cmd_scdn(char *argbuf)
122 {
123         int new_state;
124         int state = CIT_OK;
125         char *Reply = "%d %d\n";
126
127         if (CtdlAccessCheck(ac_aide)) return;
128
129         new_state = extract_int(argbuf, 0);
130         if ((new_state == 2) || (new_state == 3))
131         {
132                 restart_server = 1;
133                 if (!running_as_daemon)
134                 {
135                         syslog(LOG_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
136                         Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
137                         state = ERROR;
138                 }
139
140                 restart_server = extract_int(argbuf, 0);
141                 new_state -= 2;
142         }
143         if ((new_state == 0) || (new_state == 1)) {
144                 ScheduledShutdown = new_state;
145         }
146         cprintf(Reply, state, ScheduledShutdown);
147 }
148
149
150 /*****************************************************************************/
151 /*                      MODULE INITIALIZATION STUFF                          */
152 /*****************************************************************************/
153
154 CTDL_MODULE_INIT(syscmd)
155 {
156         if (!threading) {
157                 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
158                 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
159                 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
160         }
161         /* return our id for the Log */
162         return "syscmd";
163 }