9a11deca6c3aa3b0c641952cc6e5d7fe7dc31a21
[citadel.git] / citadel / modules / ctdlproto / serv_syscmds.c
1
2 /* 
3  * Main source module for the Citadel server
4  *
5  * Copyright (c) 1987-2017 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 <stdio.h>
17 #include <libcitadel.h>
18
19 #include "serv_extensions.h"
20 #include "ctdl_module.h"
21
22
23 /*
24  * Shut down the server
25  */
26 void cmd_down(char *argbuf) {
27         char *Reply ="%d Shutting down server.  Goodbye.\n";
28
29         if (CtdlAccessCheck(ac_aide)) return;
30
31         if (!IsEmptyStr(argbuf))
32         {
33                 int state = CIT_OK;
34                 restart_server = extract_int(argbuf, 0);
35                 
36                 if (restart_server > 0)
37                 {
38                         Reply = "%d citserver will now shut down and automatically restart.\n";
39                 }
40                 if ((restart_server > 0) && !running_as_daemon)
41                 {
42                         syslog(LOG_ERR, "The user requested restart, but not running as daemon! Geronimooooooo!\n");
43                         Reply = "%d Warning: citserver is not running in daemon mode and is therefore unlikely to restart automatically.\n";
44                         state = ERROR;
45                 }
46                 cprintf(Reply, state);
47         }
48         else
49         {
50                 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); 
51         }
52         CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
53         server_shutting_down = 1;
54 }
55
56
57 /*
58  * Halt the server without exiting the server process.
59  */
60 void cmd_halt(char *argbuf) {
61
62         if (CtdlAccessCheck(ac_aide)) return;
63
64         cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
65         server_shutting_down = 1;
66         shutdown_and_halt = 1;
67 }
68
69
70 /*
71  * Schedule or cancel a server shutdown
72  */
73 void cmd_scdn(char *argbuf)
74 {
75         int new_state;
76         int state = CIT_OK;
77         char *Reply = "%d %d\n";
78
79         if (CtdlAccessCheck(ac_aide)) return;
80
81         new_state = extract_int(argbuf, 0);
82         if ((new_state == 2) || (new_state == 3))
83         {
84                 restart_server = 1;
85                 if (!running_as_daemon)
86                 {
87                         syslog(LOG_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
88                         Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
89                         state = ERROR;
90                 }
91
92                 restart_server = extract_int(argbuf, 0);
93                 new_state -= 2;
94         }
95         if ((new_state == 0) || (new_state == 1)) {
96                 ScheduledShutdown = new_state;
97         }
98         cprintf(Reply, state, ScheduledShutdown);
99 }
100
101 /*
102  * Manually initiate log file cull.
103  */
104 void cmd_cull(char *argbuf) {
105         if (CtdlAccessCheck(ac_internal)) return;
106         cdb_cull_logs();
107         cprintf("%d Database log file cull completed.\n", CIT_OK);
108 }
109
110
111
112 /*****************************************************************************/
113 /*                      MODULE INITIALIZATION STUFF                          */
114 /*****************************************************************************/
115
116 CTDL_MODULE_INIT(syscmd)
117 {
118         if (!threading) {
119                 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
120                 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
121                 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
122                 CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
123         }
124         /* return our id for the Log */
125         return "syscmd";
126 }