Attempt to fix the darwin build
[Samba.git] / source / web / statuspage.c
blobeda53b66ab307a4188b07290a707128b07a6c582
1 /*
2 Unix SMB/CIFS implementation.
3 web status page
4 Copyright (C) Andrew Tridgell 1997-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "web/swat_proto.h"
23 #define _(x) lang_msg_rotate(talloc_tos(),x)
25 #define PIDMAP struct PidMap
27 /* how long to wait for start/stops to take effect */
28 #define SLEEP_TIME 3
30 PIDMAP {
31 PIDMAP *next, *prev;
32 struct server_id pid;
33 char *machine;
36 static PIDMAP *pidmap;
37 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
39 static struct server_id smbd_pid;
41 /* from 2nd call on, remove old list */
42 static void initPid2Machine (void)
44 /* show machine name rather PID on table "Open Files"? */
45 if (PID_or_Machine) {
46 PIDMAP *p;
48 for (p = pidmap; p != NULL; ) {
49 DLIST_REMOVE(pidmap, p);
50 SAFE_FREE(p->machine);
51 SAFE_FREE(p);
54 pidmap = NULL;
58 /* add new PID <-> Machine name mapping */
59 static void addPid2Machine (struct server_id pid, const char *machine)
61 /* show machine name rather PID on table "Open Files"? */
62 if (PID_or_Machine) {
63 PIDMAP *newmap;
65 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
66 /* XXX need error message for this?
67 if malloc fails, PID is always shown */
68 return;
71 newmap->pid = pid;
72 newmap->machine = SMB_STRDUP(machine);
74 DLIST_ADD(pidmap, newmap);
78 /* lookup PID <-> Machine name mapping */
79 static char *mapPid2Machine (struct server_id pid)
81 static char pidbuf [64];
82 PIDMAP *map;
84 /* show machine name rather PID on table "Open Files"? */
85 if (PID_or_Machine) {
86 for (map = pidmap; map != NULL; map = map->next) {
87 if (procid_equal(&pid, &map->pid)) {
88 if (map->machine == NULL) /* no machine name */
89 break; /* show PID */
91 return map->machine;
96 /* PID not in list or machine name NULL? return pid as string */
97 snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
98 procid_str_static(&pid));
99 return pidbuf;
102 static const char *tstring(TALLOC_CTX *ctx, time_t t)
104 char *buf;
105 buf = talloc_strdup(ctx, time_to_asc(t));
106 if (!buf) {
107 return "";
109 buf = talloc_all_string_sub(ctx,
110 buf,
111 " ",
112 "&nbsp;");
113 if (!buf) {
114 return "";
116 return buf;
119 static void print_share_mode(const struct share_mode_entry *e,
120 const char *sharepath,
121 const char *fname,
122 void *dummy)
124 char *utf8_fname;
125 int deny_mode;
127 if (!is_valid_share_mode_entry(e)) {
128 return;
131 deny_mode = map_share_mode_to_deny_mode(e->share_access,
132 e->private_options);
134 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
135 printf("<td>%u</td>",(unsigned int)e->uid);
136 printf("<td>");
137 switch ((deny_mode>>4)&0xF) {
138 case DENY_NONE: printf("DENY_NONE"); break;
139 case DENY_ALL: printf("DENY_ALL "); break;
140 case DENY_DOS: printf("DENY_DOS "); break;
141 case DENY_FCB: printf("DENY_FCB "); break;
142 case DENY_READ: printf("DENY_READ "); break;
143 case DENY_WRITE:printf("DENY_WRITE "); break;
145 printf("</td>");
147 printf("<td>");
148 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
149 printf("%s", _("RDWR "));
150 } else if (e->access_mask & FILE_WRITE_DATA) {
151 printf("%s", _("WRONLY "));
152 } else {
153 printf("%s", _("RDONLY "));
155 printf("</td>");
157 printf("<td>");
158 if((e->op_type &
159 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
160 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
161 printf("EXCLUSIVE+BATCH ");
162 else if (e->op_type & EXCLUSIVE_OPLOCK)
163 printf("EXCLUSIVE ");
164 else if (e->op_type & BATCH_OPLOCK)
165 printf("BATCH ");
166 else if (e->op_type & LEVEL_II_OPLOCK)
167 printf("LEVEL_II ");
168 else
169 printf("NONE ");
170 printf("</td>");
172 push_utf8_allocate(&utf8_fname, fname);
173 printf("<td>%s</td><td>%s</td></tr>\n",
174 utf8_fname,tstring(talloc_tos(),e->time.tv_sec));
175 SAFE_FREE(utf8_fname);
179 /* kill off any connections chosen by the user */
180 static int traverse_fn1(struct db_record *rec,
181 const struct connections_key *key,
182 const struct connections_data *crec,
183 void *private_data)
185 if (crec->cnum == -1 && process_exists(crec->pid)) {
186 char buf[30];
187 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
188 if (cgi_variable(buf)) {
189 kill_pid(crec->pid);
190 sleep(SLEEP_TIME);
193 return 0;
196 /* traversal fn for showing machine connections */
197 static int traverse_fn2(struct db_record *rec,
198 const struct connections_key *key,
199 const struct connections_data *crec,
200 void *private_data)
202 if (crec->cnum == -1 || !process_exists(crec->pid) ||
203 procid_equal(&crec->pid, &smbd_pid))
204 return 0;
206 addPid2Machine (crec->pid, crec->machine);
208 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
209 procid_str_static(&crec->pid),
210 crec->machine, crec->addr,
211 tstring(talloc_tos(),crec->start));
212 if (geteuid() == 0) {
213 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
214 procid_str_static(&crec->pid));
216 printf("</tr>\n");
218 return 0;
221 /* traversal fn for showing share connections */
222 static int traverse_fn3(struct db_record *rec,
223 const struct connections_key *key,
224 const struct connections_data *crec,
225 void *private_data)
227 if (crec->cnum == -1 || !process_exists(crec->pid))
228 return 0;
230 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
231 crec->servicename, uidtoname(crec->uid),
232 gidtoname(crec->gid),procid_str_static(&crec->pid),
233 crec->machine,
234 tstring(talloc_tos(),crec->start));
235 return 0;
239 /* show the current server status */
240 void status_page(void)
242 const char *v;
243 int autorefresh=0;
244 int refresh_interval=30;
245 int nr_running=0;
246 bool waitup = False;
247 TALLOC_CTX *ctx = talloc_stackframe();
249 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
251 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
252 stop_smbd();
253 start_smbd();
254 waitup=True;
257 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
258 start_smbd();
259 waitup=True;
262 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
263 stop_smbd();
264 waitup=True;
267 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
268 stop_nmbd();
269 start_nmbd();
270 waitup=True;
272 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
273 start_nmbd();
274 waitup=True;
277 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
278 stop_nmbd();
279 waitup=True;
282 #ifdef WITH_WINBIND
283 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
284 stop_winbindd();
285 start_winbindd();
286 waitup=True;
289 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
290 start_winbindd();
291 waitup=True;
294 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
295 stop_winbindd();
296 waitup=True;
298 #endif
299 /* wait for daemons to start/stop */
300 if (waitup)
301 sleep(SLEEP_TIME);
303 if (cgi_variable("autorefresh")) {
304 autorefresh = 1;
305 } else if (cgi_variable("norefresh")) {
306 autorefresh = 0;
307 } else if (cgi_variable("refresh")) {
308 autorefresh = 1;
311 if ((v=cgi_variable("refresh_interval"))) {
312 refresh_interval = atoi(v);
315 if (cgi_variable("show_client_in_col_1")) {
316 PID_or_Machine = 1;
319 if (cgi_variable("show_pid_in_col_1")) {
320 PID_or_Machine = 0;
323 connections_forall(traverse_fn1, NULL);
325 initPid2Machine ();
327 printf("<H2>%s</H2>\n", _("Server Status"));
329 printf("<FORM method=post>\n");
331 if (!autorefresh) {
332 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
333 printf("<br>%s", _("Refresh Interval: "));
334 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
335 refresh_interval);
336 } else {
337 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
338 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
339 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
342 printf("<p>\n");
344 printf("<table>\n");
346 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
348 fflush(stdout);
349 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
350 if (geteuid() == 0) {
351 if (smbd_running()) {
352 nr_running++;
353 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
354 } else {
355 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
357 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
359 printf("</tr>\n");
361 fflush(stdout);
362 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
363 if (geteuid() == 0) {
364 if (nmbd_running()) {
365 nr_running++;
366 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
367 } else {
368 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
370 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
372 printf("</tr>\n");
374 #ifdef WITH_WINBIND
375 fflush(stdout);
376 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
377 if (geteuid() == 0) {
378 if (winbindd_running()) {
379 nr_running++;
380 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
381 } else {
382 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
384 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
386 printf("</tr>\n");
387 #endif
389 if (geteuid() == 0) {
390 printf("<tr><td></td><td></td>\n");
391 if (nr_running >= 1) {
392 /* stop, restart all */
393 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
394 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
396 else if (nr_running == 0) {
397 /* start all */
398 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
400 printf("</tr>\n");
402 printf("</table>\n");
403 fflush(stdout);
405 printf("<p><h3>%s</h3>\n", _("Active Connections"));
406 printf("<table border=1>\n");
407 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
408 if (geteuid() == 0) {
409 printf("<th>%s</th>\n", _("Kill"));
411 printf("</tr>\n");
413 connections_forall(traverse_fn2, NULL);
415 printf("</table><p>\n");
417 printf("<p><h3>%s</h3>\n", _("Active Shares"));
418 printf("<table border=1>\n");
419 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
420 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
422 connections_forall(traverse_fn3, NULL);
424 printf("</table><p>\n");
426 printf("<h3>%s</h3>\n", _("Open Files"));
427 printf("<table border=1>\n");
428 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
430 locking_init_readonly();
431 share_mode_forall(print_share_mode, NULL);
432 locking_end();
433 printf("</table>\n");
435 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
436 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
438 printf("</FORM>\n");
440 if (autorefresh) {
441 /* this little JavaScript allows for automatic refresh
442 of the page. There are other methods but this seems
443 to be the best alternative */
444 printf("<script language=\"JavaScript\">\n");
445 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
446 cgi_baseurl(),
447 refresh_interval,
448 refresh_interval*1000);
449 printf("//-->\n</script>\n");
451 TALLOC_FREE(ctx);