]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/ctdlproto/serv_syscmds.c
If you can't handle me at my worst, you don't deserve me at my best.
[citadel.git] / citadel / server / modules / ctdlproto / serv_syscmds.c
1 // System management commands for Citadel server
2 //
3 // Copyright (c) 1987-2022 by the citadel.org team
4 //
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 3.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13 #include <stdio.h>
14 #include <libcitadel.h>
15
16 #include "../../serv_extensions.h"
17 #include "../../ctdl_module.h"
18
19
20 // Shut down or restart the server
21 void cmd_down(char *argbuf) {
22         char *Reply ="%d Shutting down server.  Goodbye.\n";
23
24         if (CtdlAccessCheck(ac_aide)) return;
25
26         if (!IsEmptyStr(argbuf)) {
27                 int state = CIT_OK;
28                 restart_server = extract_int(argbuf, 0);
29                 
30                 if (restart_server > 0) {
31                         Reply = "%d citserver will now shut down and automatically restart.\n";
32                 }
33                 cprintf(Reply, state);
34         }
35         else {
36                 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); 
37         }
38         CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
39         server_shutting_down = 1;
40 }
41
42
43 // Halt the server without exiting the server process.
44 void cmd_halt(char *argbuf) {
45         if (CtdlAccessCheck(ac_aide)) return;
46
47         cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
48         server_shutting_down = 1;
49         shutdown_and_halt = 1;
50 }
51
52
53 // Schedule or cancel a server shutdown
54 void cmd_scdn(char *argbuf) {
55         int new_state;
56         int state = CIT_OK;
57         char *Reply = "%d %d\n";
58
59         if (CtdlAccessCheck(ac_aide)) return;
60
61         new_state = extract_int(argbuf, 0);
62         if ((new_state == 2) || (new_state == 3)) {
63                 restart_server = 1;
64                 restart_server = extract_int(argbuf, 0);
65                 new_state -= 2;
66         }
67         if ((new_state == 0) || (new_state == 1)) {
68                 ScheduledShutdown = new_state;
69         }
70         cprintf(Reply, state, ScheduledShutdown);
71 }
72
73
74 // Initialization function, called from modules_init.c
75 char *ctdl_module_init_syscmd(void) {
76         if (!threading) {
77                 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
78                 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
79                 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
80         }
81         // return our id for the log
82         return "syscmd";
83 }