summaryrefslogtreecommitdiff
path: root/web/src/cgi.tcl-1.10/example/kill.cgi
blob: f3ffe6a390bfd2beca4ec9f7e80dced3a55d8c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/depot/path/tclsh

package require cgi

cgi_eval {
    source example.tcl

    cgi_input

    cgi_title "Kill runaway CGI processes"

    cgi_body {
	if {0==[catch {cgi_import PidList}]} {
	    catch {cgi_import Refresh;set PidList {}}
	    cgi_import Sig
	    h4 "If this were not a demo, the following commands would be executed:"
	    foreach pid $PidList {
		# to undemoize this and allow processes to be killed,
		# change h5 to exec and remove the quotes
		if {[catch {h5 "kill -$Sig $pid"} msg]} {
		    h4 "$msg"
		}
	    }
	}

	cgi_form kill {
	    set ps /bin/ps
	    if {[file exists /usr/ucb/ps]} {
		set ps /usr/ucb/ps
	    }

	    set f [open "|$ps -auxww" r]
	    table border=2 {
		table_row {
		    table_data {puts kill}
		    table_data {cgi_preformatted {puts [gets $f]}}
		}
		while {-1 != [gets $f buf]} {
		    if {[regexp "$argv0" $buf]} continue
		    if {![regexp "^http" $buf]} continue
		    table_row {
			table_data {
			    scan $buf "%*s %d" pid
			    cgi_checkbox PidList=$pid
			}
			table_data {
			    cgi_preformatted {puts $buf}
			}
		    }
		}
		
	    }

	    submit_button "=Send signal to selected processes"
	    submit_button "Refresh=Refresh listing"
	    reset_button

	    br; radio_button "Sig=TERM" checked; puts "SIGTERM: terminate gracefully"
	    br; radio_button "Sig=KILL";	 puts "SIGKILL: terminate ungracefully"
	    br; radio_button "Sig=STOP";	 puts "SIGSTOP: suspend"
	    br; radio_button "Sig=CONT";	 puts "SIGCONT: continue"

	    p "SIGSTOP and SIGCONT are particularly useful if the
	    processes aren't yours and the owner isn't around to ask.
	    Suspend them and let the owner decide later whether to
	    continue or kill them."
	}
    }
}