File: kill.tcl

package info (click to toggle)
tkworld 1.4.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 948 kB
  • ctags: 336
  • sloc: tcl: 10,189; makefile: 36
file content (660 lines) | stat: -rw-r--r-- 16,650 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# kill.tcl - a tcl dialog to the kill command
# version 1.3, Feb 15, 1999
# Author: Samy Zafrany  (samy@netanya.ac.il // samy@math.technion.ac.il)
#         www.netanya.ac.il/~samy
#
namespace eval kill {
    variable kill

    # Define the kill array structure so that all variables are
    # defined for the callbacks in the radiobuttons and checkbuttons.
    array set kill {
       dialog ""
       user_procs_text ""
       all_procs_text ""
       jobs_text ""
       signal TERM
       pids:list {}
       signal_names 0
       num_to_sig:entry ""
       num_to_sig:textvar ""
       sig_to_num:entry ""
       sig_to_num:textvar ""
    }
}

# kill::create --
#
#   Method to create the dialog box for the kill command.
#
# Note
#
#   This dialog will not grab focus so the user can keep it open
#   and run other tkWorld dialogs.  Imagine how tedious it would be
#   if you had to close the dialog to run your command, then reopen
#   it to modify it.  By not making this a modal dialog, we do not
#   have to implement any last command saving characteristics since
#   the user can just leave the dialog open.
#
# Args
#
#   None.
#
# Returns
#
#   None.

proc kill::create { } {
    global tkWorld
    variable kill

    # Put the focus on the kill dialog if it is already open.
    if [winfo exists $kill(dialog)] {
        wm deiconify $kill(dialog)
	focus $kill(dialog)
        raise $kill(dialog)
	return
    } else {
	set kill(dialog) [dialog::create .kill Kill]
    }

    ############################### FIRST TAB

    # The first tab deals with the standard kill command line options.
    set tab1 [tabnotebook::page [dialog::interior $kill(dialog)] "Signal"]
    set Signals {
      HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV
      USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN
      TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR UNUSED   
    }

    # Now build the individual radio buttons for the -s (signal) option
    #
    set f1 [frame $tab1.f1 -class TabnotebookFrame]
    set off [frame $tab1.off -class TabnotebookFrame]

    set n 0
    foreach sig $Signals {
        set rb $f1.[string tolower $sig]
        radiobutton $rb \
	    -text "$sig ([expr $n+1])" \
	    -variable kill::kill(signal) \
	    -value "$sig"
        set i [expr $n/4]
        set j [expr $n%4]
        incr n
        grid config $rb -row $i -column $j -padx 2 -pady 2 -sticky "w"

    }
    set rb $off.off
    radiobutton $rb \
        -text "Off" \
        -variable kill::kill(signal) \
        -value ""
    grid $rb -padx 2 -pady 2 -sticky "w"

    # Build the first tab.
    pack $f1 $off \
        -side top \
        -fill x   \
        -padx 5   \
        -pady 5   \
        -ipadx 5  \
        -ipady 5

    ############################### SECOND TAB

    # The second tab has to do with the list of user processes to kill.
    # Only processes that belong to the current user (who runs tkworld)
    # are presented.
    # We use a text widget for presenting this list of processes.
    # The user can select any number of them.
    #
    set tab2 [tabnotebook::page [dialog::interior .kill] "User Processes"]

    set f2 [frame $tab2.f2 -class TabnotebookFrame]
    set t $f2.user_procs_text
    set kill(user_procs_text) $t

    scrollbar $f2.sb -command "$t yview"
    pack $f2.sb -side right -fill y

    text $t -background white \
            -cursor left_ptr  \
            -tabs {2c left 2.5c right} \
            -yscrollcommand "$f2.sb set" \
            -height 20 \
            -width 61
    pack $t -side left -expand yes -fill both
    
    # Now we clear out all default text bindings
    #
    foreach event [bind Text] {
       bind $t $event {break}
    }

    bind $t <Enter> {focus %W}
    bind $t <Button-1> {kill::pid_select %W}
    bind $t <Next>  "$t yview scroll  1 pages"
    bind $t <space> "$t yview scroll  1 pages"
    bind $t <Prior> "$t yview scroll -1 pages"
    bind $t <Up>    "$t yview scroll -1 units"
    bind $t <Down>  "$t yview scroll  1 units"

    pack $f2 \
       -side  top \
       -fill  x   \
       -padx  5   \
       -pady  5   \
       -ipadx 5   \
       -ipady 5

    ############################### THIRD TAB

    # The third tab has to do with the list of all current processes
    # This is useful only for the root user, since normal users cannot
    # terminate processes that do not belong to them.
    # We use a text widget for presenting the available list of processes.
    # The user can select any number of them.
    #
    set tab3 [tabnotebook::page [dialog::interior .kill] "All Processes"]

    set f3 [frame $tab3.f3 -class TabnotebookFrame]
    set t $f3.all_procs_text
    set kill(all_procs_text) $t

    scrollbar $f3.sb -command "$t yview"
    pack $f3.sb -side right -fill y

    text $t -background white \
            -cursor left_ptr  \
            -tabs {2c left 4.5c left} \
            -yscrollcommand "$f3.sb set" \
            -height 20 \
            -width 61
    pack $t -side left -expand yes -fill both
    
    # Now we clear out all default text bindings
    #
    foreach event [bind Text] {
       bind $t $event {break}
    }
    
    bind $t <Enter> {focus %W}
    bind $t <Button-1> {kill::pid_select %W}
    bind $t <Next>  "$t yview scroll  1 pages"
    bind $t <space> "$t yview scroll  1 pages"
    bind $t <Prior> "$t yview scroll -1 pages"
    bind $t <Up>    "$t yview scroll -1 units"
    bind $t <Down>  "$t yview scroll  1 units"

    pack $f3 \
	    -side top \
	    -fill x \
	    -padx 5 \
	    -pady 5 \
	    -ipadx 5 \
	    -ipady 5

    ############################### FOURTH TAB

    # The fourth tab has to do with the list of user jobs.
    # We use a text widget for presenting the available list of jobs.
    # The user can select any number of them.
    #
    set tab4 [tabnotebook::page [dialog::interior .kill] "Jobs"]

    set f4 [frame $tab4.f4 -class TabnotebookFrame]
    set t $f4.jobs_text
    set kill(jobs_text) $t

    scrollbar $f4.sb -command "$t yview"
    pack $f4.sb -side right -fill y

    text $t -background white \
            -cursor left_ptr  \
            -tabs {2c left 4c left} \
            -yscrollcommand "$f4.sb set" \
            -height 20 \
            -width 61
    pack $t -side left -expand yes -fill both
    
    # Now we clear out all default text bindings
    #
    foreach event [bind Text] {
       bind $t $event {break}
    }
    

    bind $t <Enter> {focus %W}
    bind $t <Button-1> {kill::pid_select %W}
    bind $t <Next>  "$t yview scroll  1 pages"
    bind $t <space> "$t yview scroll  1 pages"
    bind $t <Prior> "$t yview scroll -1 pages"
    bind $t <Up>    "$t yview scroll -1 units"
    bind $t <Down>  "$t yview scroll  1 units"

    pack $f4 \
	    -side top \
	    -fill x \
	    -padx 5 \
	    -pady 5 \
	    -ipadx 5 \
	    -ipady 5

    # Now we fill those text widgets with the process info
    # The info is updated every 10 seconds:
    #
    kill::update

    ############################### FIFTH TAB

    # Processing the -l option of the kill command
    #
    set tab5 \
        [tabnotebook::page [dialog::interior $kill(dialog)] "Other Options"]

    set f5 [frame $tab5.f5 -class TabnotebookFrame]
    radiobutton $f5.signames \
	    -text "Print a list of all signal names" \
	    -variable kill::kill(signal_names) \
	    -value 1
    radiobutton $f5.num_to_sig \
	    -text "Convert a signal number to signal name: " \
	    -variable kill::kill(signal_names) \
	    -value 2
    set kill(num_to_sig:entry) [entry $f5.num_to_sig:entry \
	    -width 12 \
	    -background #d9d9d9 \
	    -state disabled \
	    -textvariable kill::kill(num_to_sig:textvar)]
    radiobutton $f5.sig_to_num \
	    -text "Convert a signal name to signal number: " \
	    -variable kill::kill(signal_names) \
	    -value 3
    set kill(sig_to_num:entry) [entry $f5.sig_to_num:entry \
	    -width 12 \
	    -background #d9d9d9 \
	    -state disabled \
	    -textvariable kill::kill(sig_to_num:textvar)]
    radiobutton $f5.off \
	    -text "Off" \
	    -variable kill::kill(signal_names) \
	    -value 0


    grid $f5.signames \
	    -padx 2 \
	    -pady 2 \
	    -sticky w
    grid $f5.sig_to_num $f5.sig_to_num:entry \
	    -padx 2 \
	    -pady 2 \
	    -sticky w
    grid $f5.num_to_sig $f5.num_to_sig:entry \
	    -padx 2 \
	    -pady 2 \
	    -sticky w
    grid $f5.off - \
	    -padx 2 \
	    -pady 2 \
	    -sticky w

    pack $f5 \
	    -side top \
	    -fill x \
	    -padx 5 \
	    -pady 5 \
	    -ipadx 5 \
	    -ipady 5
    bind $f5.off        <ButtonPress-1> {kill::toggle_entries 0}
    bind $f5.signames   <ButtonPress-1> {kill::toggle_entries 1}
    bind $f5.num_to_sig <ButtonPress-1> {kill::toggle_entries 2}
    bind $f5.sig_to_num <ButtonPress-1> {kill::toggle_entries 3}

    set mf [frame $tab5.mf -class TabnotebookFrame]
    set m $mf.message
    message $m -aspect 400 -font fixed -text "
NOTE: \
These options are not guaranteed to work in any case. There are different\
versions of the \"kill\" command which depend on the shell (for example\
they work under zsh but not under the standard Bourne shell or even bash).\
There is also an external \"/bin/kill\" command that may differ from the
internal shell \"kill\" commands."

    pack $mf \
	    -side top \
	    -fill x \
	    -padx 5 \
	    -pady 5 \
	    -ipadx 5 \
	    -ipady 5
    pack $m -side top -fill x
}


############################### PROCEDURES

proc kill::refreshUserProcs {} {
   variable kill

   set t $kill(user_procs_text)
   set yv [lindex [$t yview] 0]
   $t configure -state normal
   $t delete 1.0 end
   $t insert end "PID\tCOMMAND\n" title

   foreach line [split [exec ps uxw] \n] {
      if [regexp {[ ]*USER[ ]+PID[ ]+%CPU} $line] {
        set cmd_start [string first COMMAND $line]
        set cut [string first SIZE $line]
        continue
      }
      if [regexp {ps uxw[ ]*$} $line] {continue}
      set short_line [string range $line 0 $cut]
      set pid [lindex $short_line 1]
      set cmd [string range $line $cmd_start [expr $cmd_start+42]]
      $t insert end "$pid\t\t$cmd\n" [list body "pid:$pid"]
   }

   $t tag configure title -font {Helvetica 12 bold}
   $t tag configure title -underline 1

   $t configure -state disabled
   $t yview moveto $yv
}

proc kill::refreshAllProcs {} {
   variable kill

   set t $kill(all_procs_text)
   set yv [lindex [$t yview] 0]
   $t configure -state normal
   $t delete 1.0 end
   $t insert end "PID\tOWNER\tCOMMAND\n" title

   set all_pids {}
   foreach line [split [exec ps auxw] \n] {
      if [regexp {[ ]*USER[ ]+PID[ ]+%CPU} $line] {
        set cut [string first SIZE $line]
        set cmd_start [string first COMMAND $line]
        continue
      }
      if [regexp {ps auxw[ ]*$} $line] {continue}
      set short_line [string range $line 0 $cut]
      set owner [lindex $short_line 0]
      set pid [lindex $short_line 1]
      set cmd [string range $line $cmd_start [expr $cmd_start+42]]
      $t insert end "$pid\t$owner\t$cmd\n" [list body "pid:$pid"]
      lappend all_pids $pid
   }

   $t tag configure title -font {Helvetica 12 bold}
   $t tag configure title -underline 1

   $t configure -state disabled
   $t yview moveto $yv
   
   foreach p $kill(pids:list) {
      if {[lsearch $all_pids $p]==-1} {
        set i [lsearch $kill(pids:list) $p]
        set kill(pids:list) [lreplace $kill(pids:list) $i $i]
      }
   }
}

proc kill::refreshJobs {} {
   variable kill

   set t $kill(jobs_text)
   set yv [lindex [$t yview] 0]
   $t configure -state normal
   $t delete 1.0 end
   $t insert end "JOB\tPID\tCOMMAND\n" title

   set job 0
   set pgid_list {}
   foreach line [split [exec ps jxw] \n] {
      if [regexp {[ ]*PPID[ ]+PID} $line] {
        set cmd_start [string first COMMAND $line]
        set cut [string first TTY $line]
        continue
      }
      if [regexp {ps jxw[ ]*$} $line] {continue}
      set short_line [string range $line 0 $cut]
      set pgid [lindex $short_line 2]
      if {[lsearch $pgid_list $pgid]>=0} {continue}
      lappend pgid_list $pgid
      incr job
      set pid [lindex $short_line 1]
      set cmd [string range $line $cmd_start [expr $cmd_start+42]]
      $t insert end "\[$job\]\t$pid\t$cmd\n" [list body "pid:$pid"]
   }

   $t tag configure title -font {Helvetica 12 bold}
   $t tag configure title -underline 1

   $t configure -state disabled
   $t yview moveto $yv
}

proc kill::pid_select {w} {
   variable kill
   set t1 $kill(user_procs_text)
   set t2 $kill(all_procs_text)
   set t3 $kill(jobs_text)
   set tags [$w tag names current]
   set i [lsearch -glob $tags pid:*]
   if {$i==-1} {return}
   set tg [lindex $tags $i]
   set pid [lindex [split $tg :] 1]
   set i [lsearch $kill(pids:list) $pid]
   if {$i >= 0} {
     $t1 tag configure $tg -background [$t1 cget -background]
     $t2 tag configure $tg -background [$t2 cget -background]
     $t3 tag configure $tg -background [$t3 cget -background]
     set kill(pids:list) [lreplace $kill(pids:list) $i $i]
     return
   }
   $t1 tag configure $tg -background grey75
   $t2 tag configure $tg -background grey75
   $t3 tag configure $tg -background grey75
   lappend kill(pids:list) $pid
}

proc kill::toggle_entries { int } {
    variable kill

    switch $int {
        0 -
	1 {
	    $kill(num_to_sig:entry) delete 0 end
	    $kill(num_to_sig:entry) configure \
		    -bg #d9d9d9 \
		    -state disabled
	    $kill(sig_to_num:entry) delete 0 end
	    $kill(sig_to_num:entry) configure \
		    -bg #d9d9d9 \
		    -state disabled
	}
	2 {
	    $kill(num_to_sig:entry) configure \
		    -bg #ffffff \
		    -state normal
	    $kill(sig_to_num:entry) delete 0 end
	    $kill(sig_to_num:entry) configure \
		    -bg #d9d9d9 \
		    -state disabled
	}
	3 {
	    $kill(num_to_sig:entry) delete 0 end
	    $kill(num_to_sig:entry) configure \
		    -bg #d9d9d9 \
		    -state disabled
	    $kill(sig_to_num:entry) configure \
		    -bg #ffffff \
		    -state normal
	}
    }
}


# kill::ok --
#
#   Method to insert the command the user has created into the CC
#   as a Tcl list.
#
# Args
#
#   None.
#
# Returns
#
#   None.

proc kill::ok { } {
   global tkWorld
   variable kill

   # Build the full command line
   #
   set cmd "kill "
   set cmd_done 0

   switch $kill(signal_names) {
      1 {
         append cmd "-l"
         set cmd_done 1
      }
      2 {
         append cmd "-l $kill(num_to_sig:textvar)"
         set cmd_done 1
      }
      3 {
         append cmd "-l $kill(sig_to_num:textvar)"
         set cmd_done 1
      }
   }

   if {$cmd_done==0} {
      set pids [lsort -integer $kill(pids:list)]
      if [string length $kill(signal)] {
         set cmd [eval list kill -s $kill(signal) $pids]
      } else {
         set cmd [eval list kill $pids]
      }
   }



   # Insert the Tcl command list in the Command Center with the
   # proper formatting of a space between each argument on the
   # command line.  If there are no options given by the user,
   # then don't display it in the CC.

   $tkWorld(cmd_center) insert insert $cmd

   # Activate the buttons in the toolbar for the command center.
   toolbar::group_state cmd_center active
   toolbar::button_state $toolbar::toolbar(stop) disabled
}

# kill::reset --
#
#   Method to reset the radio and checkbuttons in the dialog.
#
# Args
#
#   None.
#
# Returns
#
#   None.

proc kill::reset { } {
   variable kill

   set kill(signal) TERM
   set kill(signal_names) 0
   kill::toggle_entries 0
   kill::clear
}

# kill::clear --
#
#   Method to clear entry items of their text and reset the
#   background and foreground properties.
#
# Args
#
#   None.
#
# Returns
#
#   None.

proc kill::clear { } {
   variable kill

   set kill(sig_to_num:textvar) ""
   set kill(num_to_sig:textvar) ""
   set kill(pids:list) {}
   set t1 $kill(user_procs_text)
   set t2 $kill(all_procs_text)
   set t3 $kill(jobs_text)
   foreach t [list $t1 $t2 $t3] {
      foreach tg [$t tag names] {
         $t tag delete $tg
      }
   }
   kill::refreshUserProcs
   kill::refreshAllProcs
   kill::refreshJobs
}

proc kill::update {} {
   variable kill

   if ![winfo exists $kill(dialog)] {return}
   kill::refreshUserProcs
   kill::refreshAllProcs
   kill::refreshJobs
   after 10000 {kill::update}
}

# kill::help --
#
#   Method to invoke the Kill Command Help.
#
# Args
#
#   None.
#
# Returns
#
#   None.

proc kill::help { } {
   global tkWorld

   help::create "help/kill.html" "Kill Command Help"
}

# kill::close --
#
#   Close the dialog up.
#
# Args
#
#   None.
#
# Returns
#
#   None.

proc kill::close { } {
   variable kill
   
   balloonhelp::cancel
   destroy $kill(dialog)
}