7341ad238c0e6dc82225b18f56358b3a7fa26f60
[citadel.git] / citadel / modules / ctdlproto / serv_syscmds.c
1
2 /* 
3  * Main source module for the Citadel server
4  *
5  * Copyright (c) 1987-2018 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                 restart_server = 1;
84                 restart_server = extract_int(argbuf, 0);
85                 new_state -= 2;
86         }
87         if ((new_state == 0) || (new_state == 1)) {
88                 ScheduledShutdown = new_state;
89         }
90         cprintf(Reply, state, ScheduledShutdown);
91 }
92
93
94 /*****************************************************************************/
95 /*                      MODULE INITIALIZATION STUFF                          */
96 /*****************************************************************************/
97
98 CTDL_MODULE_INIT(syscmd)
99 {
100         if (!threading) {
101                 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
102                 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
103                 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
104         }
105         /* return our id for the Log */
106         return "syscmd";
107 }