~ubuntu-branches/ubuntu/quantal/libbonobo/quantal-201207170711

« back to all changes in this revision

Viewing changes to utils/bonobo-slay.in

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-02-18 14:40:51 UTC
  • mto: (3.1.1 etch) (1.1.25 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050218144051-fo4h9qh2gim8x3wt
Tags: upstream-2.8.1
ImportĀ upstreamĀ versionĀ 2.8.1

Show diffs side-by-side

added added

removed removed

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