Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[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 <stdio.h>
17 #include <libcitadel.h>
18
19 #include "serv_extensions.h"
20 #include "ctdl_module.h"
21
22 void cmd_log_get(char *argbuf)
23 {
24         long HKLen;
25         const char *ch;
26         HashPos *Pos;
27         void *vptr;
28
29         if (CtdlAccessCheck(ac_aide)) return;
30
31         cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
32
33         Pos = GetNewHashPos(LogDebugEntryTable, 0);
34
35         while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
36                 LogDebugEntry *E = (LogDebugEntry*)vptr;
37                 cprintf("%s|%d\n", ch, *E->LogP);
38         }
39         
40         DeleteHashPos(&Pos);
41         cprintf("000\n");
42 }
43 void cmd_log_set(char *argbuf)
44 {
45         void *vptr;
46         int lset;
47         int wlen;
48         char which[SIZ] = "";
49
50         if (CtdlAccessCheck(ac_aide)) return;
51
52         wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
53         if (wlen < 0) wlen = 0;
54         lset = extract_int(argbuf, 1);
55         if (lset != 0) lset = 1;
56         if (GetHash(LogDebugEntryTable, which, wlen, &vptr) && 
57             (vptr != NULL))
58         {
59                 LogDebugEntry *E = (LogDebugEntry*)vptr;
60                 E->F(lset);
61                 cprintf("%d %s|%d\n", CIT_OK, which, lset);
62         }
63         else {
64                 cprintf("%d Log setting %s not known\n", 
65                         ERROR, which);
66         }
67 }
68
69
70 /*
71  * Shut down the server
72  */
73 void cmd_down(char *argbuf) {
74         char *Reply ="%d Shutting down server.  Goodbye.\n";
75
76         if (CtdlAccessCheck(ac_aide)) return;
77
78         if (!IsEmptyStr(argbuf))
79         {
80                 int state = CIT_OK;
81                 restart_server = extract_int(argbuf, 0);
82                 
83                 if (restart_server > 0)
84                 {
85                         Reply = "%d citserver will now shut down and automatically restart.\n";
86                 }
87                 if ((restart_server > 0) && !running_as_daemon)
88                 {
89                         syslog(LOG_ERR, "The user requested restart, but not running as daemon! Geronimooooooo!\n");
90                         Reply = "%d Warning: citserver is not running in daemon mode and is therefore unlikely to restart automatically.\n";
91                         state = ERROR;
92                 }
93                 cprintf(Reply, state);
94         }
95         else
96         {
97                 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); 
98         }
99         CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
100         server_shutting_down = 1;
101 }
102
103
104 /*
105  * Halt the server without exiting the server process.
106  */
107 void cmd_halt(char *argbuf) {
108
109         if (CtdlAccessCheck(ac_aide)) return;
110
111         cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
112         server_shutting_down = 1;
113         shutdown_and_halt = 1;
114 }
115
116
117 /*
118  * Schedule or cancel a server shutdown
119  */
120 void cmd_scdn(char *argbuf)
121 {
122         int new_state;
123         int state = CIT_OK;
124         char *Reply = "%d %d\n";
125
126         if (CtdlAccessCheck(ac_aide)) return;
127
128         new_state = extract_int(argbuf, 0);
129         if ((new_state == 2) || (new_state == 3))
130         {
131                 restart_server = 1;
132                 if (!running_as_daemon)
133                 {
134                         syslog(LOG_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
135                         Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
136                         state = ERROR;
137                 }
138
139                 restart_server = extract_int(argbuf, 0);
140                 new_state -= 2;
141         }
142         if ((new_state == 0) || (new_state == 1)) {
143                 ScheduledShutdown = new_state;
144         }
145         cprintf(Reply, state, ScheduledShutdown);
146 }
147
148 /*
149  * Manually initiate log file cull.
150  */
151 void cmd_cull(char *argbuf) {
152         if (CtdlAccessCheck(ac_internal)) return;
153         cdb_cull_logs();
154         cprintf("%d Database log file cull completed.\n", CIT_OK);
155 }
156
157
158
159 /*****************************************************************************/
160 /*                      MODULE INITIALIZATION STUFF                          */
161 /*****************************************************************************/
162
163 CTDL_MODULE_INIT(syscmd)
164 {
165         if (!threading) {
166                 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
167                 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");
168
169                 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
170                 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
171                 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
172
173                 CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
174         }
175         /* return our id for the Log */
176         return "syscmd";
177 }