Server restart via command no longer requires running as a daemon , uses execv()...
[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                         Reply = "%d citserver will now shut down and automatically restart.\n";
38                 }
39                 cprintf(Reply, state);
40         }
41         else {
42                 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); 
43         }
44         CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
45         server_shutting_down = 1;
46 }
47
48
49 /*
50  * Halt the server without exiting the server process.
51  */
52 void cmd_halt(char *argbuf) {
53
54         if (CtdlAccessCheck(ac_aide)) return;
55
56         cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
57         server_shutting_down = 1;
58         shutdown_and_halt = 1;
59 }
60
61
62 /*
63  * Schedule or cancel a server shutdown
64  */
65 void cmd_scdn(char *argbuf)
66 {
67         int new_state;
68         int state = CIT_OK;
69         char *Reply = "%d %d\n";
70
71         if (CtdlAccessCheck(ac_aide)) return;
72
73         new_state = extract_int(argbuf, 0);
74         if ((new_state == 2) || (new_state == 3)) {
75                 restart_server = 1;
76                 restart_server = extract_int(argbuf, 0);
77                 new_state -= 2;
78         }
79         if ((new_state == 0) || (new_state == 1)) {
80                 ScheduledShutdown = new_state;
81         }
82         cprintf(Reply, state, ScheduledShutdown);
83 }
84
85
86 /*****************************************************************************/
87 /*                      MODULE INITIALIZATION STUFF                          */
88 /*****************************************************************************/
89
90 CTDL_MODULE_INIT(syscmd)
91 {
92         if (!threading) {
93                 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
94                 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
95                 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
96         }
97         /* return our id for the Log */
98         return "syscmd";
99 }