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