2 Unix SMB/CIFS implementation.
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define PIDMAP struct PidMap
31 static PIDMAP *pidmap;
32 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
34 static pid_t smbd_pid;
36 /* from 2nd call on, remove old list */
37 static void initPid2Machine (void)
39 /* show machine name rather PID on table "Open Files"? */
43 for (p = pidmap; p != NULL; ) {
44 DLIST_REMOVE(pidmap, p);
45 SAFE_FREE(p->machine);
53 /* add new PID <-> Machine name mapping */
54 static void addPid2Machine (pid_t pid, char *machine)
56 /* show machine name rather PID on table "Open Files"? */
60 if ((newmap = (PIDMAP *) malloc (sizeof (PIDMAP))) == NULL) {
61 /* XXX need error message for this?
62 if malloc fails, PID is always shown */
67 newmap->machine = strdup (machine);
69 DLIST_ADD(pidmap, newmap);
73 /* lookup PID <-> Machine name mapping */
74 static char *mapPid2Machine (pid_t pid)
76 static char pidbuf [64];
79 /* show machine name rather PID on table "Open Files"? */
81 for (map = pidmap; map != NULL; map = map->next) {
82 if (pid == map->pid) {
83 if (map->machine == NULL) /* no machine name */
91 /* PID not in list or machine name NULL? return pid as string */
92 snprintf (pidbuf, sizeof (pidbuf) - 1, "%d", pid);
96 static char *tstring(time_t t)
99 pstrcpy(buf, asctime(LocalTime(&t)));
100 all_string_sub(buf," "," ",sizeof(buf));
104 static void print_share_mode(share_mode_entry *e, char *fname)
106 d_printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
108 switch ((e->share_mode>>4)&0xF) {
109 case DENY_NONE: d_printf("DENY_NONE"); break;
110 case DENY_ALL: d_printf("DENY_ALL "); break;
111 case DENY_DOS: d_printf("DENY_DOS "); break;
112 case DENY_READ: d_printf("DENY_READ "); break;
113 case DENY_WRITE:d_printf("DENY_WRITE "); break;
118 switch (e->share_mode&0xF) {
119 case 0: d_printf("RDONLY "); break;
120 case 1: d_printf("WRONLY "); break;
121 case 2: d_printf("RDWR "); break;
127 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
128 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
129 d_printf("EXCLUSIVE+BATCH ");
130 else if (e->op_type & EXCLUSIVE_OPLOCK)
131 d_printf("EXCLUSIVE ");
132 else if (e->op_type & BATCH_OPLOCK)
134 else if (e->op_type & LEVEL_II_OPLOCK)
135 d_printf("LEVEL_II ");
140 d_printf("<td>%s</td><td>%s</td></tr>\n",
141 fname,tstring(e->time.tv_sec));
145 /* kill off any connections chosen by the user */
146 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
148 struct connections_data crec;
150 if (dbuf.dsize != sizeof(crec))
153 memcpy(&crec, dbuf.dptr, sizeof(crec));
155 if (crec.cnum == -1 && process_exists(crec.pid)) {
157 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
158 if (cgi_variable(buf)) {
165 /* traversal fn for showing machine connections */
166 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
168 struct connections_data crec;
170 if (dbuf.dsize != sizeof(crec))
173 memcpy(&crec, dbuf.dptr, sizeof(crec));
175 if (crec.cnum != -1 || !process_exists(crec.pid) || (crec.pid == smbd_pid))
178 addPid2Machine (crec.pid, crec.machine);
180 d_printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
182 crec.machine,crec.addr,
183 tstring(crec.start));
184 if (geteuid() == 0) {
185 d_printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
193 /* traversal fn for showing share connections */
194 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
196 struct connections_data crec;
198 if (dbuf.dsize != sizeof(crec))
201 memcpy(&crec, dbuf.dptr, sizeof(crec));
203 if (crec.cnum == -1 || !process_exists(crec.pid))
206 d_printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
207 crec.name,uidtoname(crec.uid),
208 gidtoname(crec.gid),(int)crec.pid,
210 tstring(crec.start));
215 /* show the current server status */
216 void status_page(void)
220 int refresh_interval=30;
223 smbd_pid = pidfile_pid("smbd");
225 if (cgi_variable("smbd_restart")) {
230 if (cgi_variable("smbd_start")) {
234 if (cgi_variable("smbd_stop")) {
238 if (cgi_variable("nmbd_restart")) {
242 if (cgi_variable("nmbd_start")) {
246 if (cgi_variable("nmbd_stop")) {
250 if (cgi_variable("autorefresh")) {
252 } else if (cgi_variable("norefresh")) {
254 } else if (cgi_variable("refresh")) {
258 if ((v=cgi_variable("refresh_interval"))) {
259 refresh_interval = atoi(v);
262 if (cgi_variable("show_client_in_col_1")) {
266 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
267 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
271 d_printf("<H2>%s</H2>\n", _("Server Status"));
273 d_printf("<FORM method=post>\n");
276 d_printf("<input type=submit value=\"%s\" name=autorefresh>\n", _("Auto Refresh"));
277 d_printf("<br>%s", _("Refresh Interval: "));
278 d_printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n",
281 d_printf("<input type=submit value=\"%s\" name=norefresh>\n", _("Stop Refreshing"));
282 d_printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
283 d_printf("<input type=hidden name=refresh value=1>\n");
289 /* open failure either means no connections have been
294 d_printf("<table>\n");
296 d_printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), VERSION);
299 d_printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
300 if (geteuid() == 0) {
301 if (smbd_running()) {
302 d_printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
304 d_printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
306 d_printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
311 d_printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
312 if (geteuid() == 0) {
313 if (nmbd_running()) {
314 d_printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
316 d_printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
318 d_printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
322 d_printf("</table>\n");
325 d_printf("<p><h3>%s</h3>\n", _("Active Connections"));
326 d_printf("<table border=1>\n");
327 d_printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
328 if (geteuid() == 0) {
329 d_printf("<th>%s</th>\n", _("Kill"));
333 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
335 d_printf("</table><p>\n");
337 d_printf("<p><h3>%s</h3>\n", _("Active Shares"));
338 d_printf("<table border=1>\n");
339 d_printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
340 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
342 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
344 d_printf("</table><p>\n");
346 d_printf("<h3>%s</h3>\n", _("Open Files"));
347 d_printf("<table border=1>\n");
348 d_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"));
351 share_mode_forall(print_share_mode);
353 d_printf("</table>\n");
355 if (tdb) tdb_close(tdb);
357 d_printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"Show Client in col 1\">\n");
358 d_printf("<input type=submit name=\"show_pid_in_col_1\" value=\"Show PID in col 1\">\n");
360 d_printf("</FORM>\n");
363 /* this little JavaScript allows for automatic refresh
364 of the page. There are other methods but this seems
365 to be the best alternative */
366 d_printf("<script language=\"JavaScript\">\n");
367 d_printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
370 refresh_interval*1000);
371 d_printf("//-->\n</script>\n");