home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / bonobo-slay < prev    next >
Encoding:
Text File  |  2007-03-12  |  5.3 KB  |  240 lines

  1. #!/usr/bin/perl
  2. # bonobo-slay
  3. # Kills bonobo processes
  4. #
  5. # Return code of -1 (255) returned if usage error.
  6. # Return code of 1 indicates bonobo processes were running
  7. #   when script was run.  
  8. # Return code of 0 indicates no bonobo processes were
  9. #   running when script was run.
  10. #
  11. use         Getopt::Long;
  12.  
  13. Getopt::Long::Configure( "no_auto_abbrev" );    
  14. Getopt::Long::Configure( "bundling" );
  15.  
  16. $usage_error = 0;
  17. $opt_h = 0, $opt_i = 0, $opt_l = 0, $opt_s = 0; 
  18.  
  19. $rc = GetOptions("help" => \$usage_error, 
  20.          "h"  => \$opt_h, 
  21.          "i"  => \$opt_i, 
  22.          "l"  => \$opt_l, 
  23.          "s"  => \$opt_s);
  24.  
  25. # Usage errors
  26. #
  27. if ($rc != 1) {
  28.    $usage_error = 1;
  29. }
  30.  
  31. if ($opt_l && $opt_s) {
  32.    $usage_error = 1;
  33. }
  34.  
  35. if ($ARGV[0]) {
  36.     $regexp = $ARGV[0];
  37. }
  38.  
  39. # Print usage if necessary.
  40. #
  41. if ($usage_error == 1 || $opt_h) {
  42.     print "\n";
  43.     print "Usage : bonobo-slay [-hls] [regexp]\n";
  44.     print "\tKill Bonobo processes still running.\n";
  45.     print "\t -h,--help Print this help message.\n";
  46.     print "\t -i Ask before killing the processes.\n";
  47.     print "\t -l List processes running, but do not kill them.  Not valid with -s\n";
  48.     print "\t -s Silent.  Kill processes quietly\n";
  49.     print "\t [regexp] only kill proccesses matching this\n";
  50.     print "\n";
  51.     exit(-1);
  52. }
  53.  
  54. # Build ps command.
  55. #
  56. $username = $ENV{USER} || $ENV{LOGNAME} || `logname`;
  57. chomp($username);
  58.  
  59. # $ps_cmd = " -U $username -opid,args";
  60.    $ps_cmd = " -U $username -xww -opid,command";
  61. #   $ps_cmd = " -s -u $username | awk '{print \$1 \" \" \$4}'";
  62.  
  63. # get Bonobo files
  64. #
  65. @bonobo_dirs = ( "/usr/lib/bonobo/servers" );
  66. foreach $dir (split(':', $ENV{'BONOBO_ACTIVATION_INFO_PATH'})) {
  67.     if (-d $dir) {
  68.         push @bonobo_dirs, $dir;
  69.     }
  70. }
  71. foreach $dir (split(':', $ENV{'GNOME2_PATH'})) {
  72.     if (-d "$dir/lib/bonobo/servers") {
  73.         push @bonobo_dirs, "$dir/lib/bonobo/servers";
  74.     }
  75. }
  76.  
  77. foreach $dir (@bonobo_dirs) {
  78.     opendir(DIR, $dir) || die "\nCan not open directory $dir\n\t$!\n\n";
  79.     push @bonobo_files, map ("$dir/$_", readdir(DIR));
  80.     closedir DIR;
  81. }
  82.  
  83. # Initialize variables
  84. #
  85. $process_exists = 0;
  86. $first_time     = 1;
  87.  
  88. # Loop until no more processes are found.  This typically loops only once,
  89. # but if an Bonobo process is launched while this script is running an Bonobo
  90. # process can be left behind and be caught on the second loop, etc.
  91. #
  92. do {
  93.     # Initialize variables.
  94.     #
  95.     @files        = @bonobo_files;
  96.     @list_array   = ();
  97.     @process_pids = ();
  98.     @file_process = ();
  99.     $index        = 0;
  100.  
  101.     # Get process list.
  102.     #
  103.     @ps_result    = `$ps_cmd`;
  104.  
  105.     # Loop through files, and see if any Bonobo processes are running.  If
  106.     # so, then build the @list_array and @process_pids arrays.
  107.     # @list_array contains the process names
  108.     # @process_pids contains the process IDs
  109.     #
  110.     while ($fname = shift(@files)) {
  111.  
  112.         if ("$fname" =~ /\.server$/) {
  113.         
  114.             open(FILE, $fname);
  115.             while (<FILE>) {
  116.  
  117.                 $line = $_;
  118.                 if  ($line =~m/location[ \t]*\=/ && 
  119.                    !($line =~m/type=\"shlib\"/)) {
  120.     
  121.                     $line =~s/.*location[ \t]*\="//;
  122.                     $line =~s/".*//;
  123.                     chomp($line);
  124.                     $line =~s/\.\///;
  125.  
  126.                     # bonobo-activation-server needs to be last.
  127.                     #
  128.                     if ($line ne "bonobo-activation-server" and
  129.                         not ($line =~m/^OAFIID:/) and
  130.                         ($regexp and $line =~m/$regexp/) or (not $regexp)) {
  131.                         push @file_process, $line;
  132.                     }
  133.                 } #end while(<FILE>)
  134.             }
  135.             close(FILE);
  136.         }
  137.     }
  138.  
  139.     #   Add bonobo-activation-server so that it is last,
  140.     # but only if we're killing without a regexp
  141.     if (not $regexp) {
  142.          push @file_process, "bonobo-activation-server";
  143.     }
  144.  
  145.     foreach $filep (@file_process) {
  146.  
  147.             # Search through @ps_result and look for matches
  148.             #
  149.             foreach $el (@ps_result) {
  150.                 chomp $el;
  151.             @ps_array = split(' ', $el, 3);
  152.  
  153.             if ($ps_array[1] =~m/(\A|\/)$filep$/ ) {
  154.                     $list_array[$index]=$ps_array[0]."\t".$ps_array[1]."\n";
  155.                     $process_pids[$index]=$ps_array[0]."\n";
  156.                     $index++;
  157.                     }
  158.                 }
  159.             }
  160.  
  161.     # Do the killing.
  162.     #
  163.     if ($#list_array != -1) {
  164.  
  165.         # Print output if -s (silent) argument is not specified.
  166.         #
  167.         if(!$opt_s) {
  168.             if ($first_time == 1) {
  169.                 print "\n";
  170.                 print "The following processes are still running on the system.\n";
  171.  
  172.                 if (!$opt_l) {
  173.                     print "These processes will be terminated.\n";
  174.  
  175.                     print "\n";
  176.                     print "NOTE:  Killing these processes may affect other applications\n";
  177.                     print "on the system that use bonobo.\n"; 
  178.                 }
  179.                 print "\n";
  180.                 $first_time = 0;
  181.             } else {
  182.                 # Just some feedback to indicate it had to loop...
  183.                 #
  184.                 print "...and...\n";
  185.             }
  186.  
  187.             # Print list of processes...
  188.             #
  189.             print @list_array;
  190.             print "\n";
  191.         }
  192.  
  193.         # Kill if the -l argument is not specified.
  194.         #
  195.         if(!$opt_l) {
  196.                 if($opt_i) {
  197.                         print "Do you really want to continue (y/N) ? ";            
  198.                         $_=<STDIN>;
  199.                         chomp;
  200.                         if(not /^[Yy]$/) {
  201.                         exit(-1);
  202.                         }
  203.             }
  204.                         $killall = "/bin/kill";
  205.             $kill_params = ' -9 ';
  206.             foreach $proc (@process_pids) {
  207.                 chomp $proc;
  208.                 if($proc =~m/\d+/) {
  209.                     $cmd = $killall.$kill_params.$proc." 2>/dev/null";
  210.                     system($cmd);
  211.                 }
  212.             }    
  213.         }
  214.         $process_exists = 1;
  215.     }
  216.  
  217. # Only loop once if opt_l is used, otherwise loop until
  218. # no more processes are killed
  219. #
  220. } while ($#list_array != -1 && !opt_l);
  221.  
  222. # Exit
  223. #
  224. if ($process_exists == 0) {
  225.  
  226.     # Show feedback if -l argument is used
  227.     #
  228.     if ($opt_l) {
  229.         print "\n";
  230.         print "No processes.\n";
  231.         print "\n";
  232.     }
  233.     exit 1;
  234. } else {
  235.     exit 0;
  236. }
  237.  
  238.