Tools/ProcessList.pm untested

filter: Statementstmt 0.0% 0 / 252 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 475 CC 61 SCAR 50.0
CC
61
Cov
0%
get_process_by_pid
342.0
get_other_daemon_process
210.0
get_process_table
156.0
CRAP 148.9
1 #!/usr/bin/perl
2 #
3 #
4 #
5  
6 use strict;
7 use warnings;
8 use Yandex::Tools;
9  
10 package Yandex::Tools::ProcessList;
11  
12 package psProcess;
13 sub pid { my ($self) = @_; return $self->{'pid'}; }
14 sub ppid { my ($self) = @_; return $self->{'ppid'}; }
15 sub pgrp { my ($self) = @_; return $self->{'pgid'}; }
16 sub pgid { my ($self) = @_; return $self->{'pgid'}; }
17 sub cmndline { my ($self) = @_; return $self->{'cmd'}; }
18 sub cmd { my ($self) = @_; return $self->{'cmd'}; }
19 sub start_time { my ($self) = @_; return $self->{'start_time'}; }
20  
21 package Yandex::Tools::ProcessList;
22  
23 my $runtime = {};
24 my $config = {};
25  
26 sub set_options {
27 my ($opts) = @_;
28  
29 Yandex::Tools::die("set_options expects program cmd line regexp")
30 unless $opts->{'daemon_match'} && ref($opts->{'daemon_match'}) eq 'ARRAY';
31  
32 Yandex::Tools::die("set_options expects program cmd line regexp during startup")
33 unless $opts->{'daemon_match_startup'} && ref($opts->{'daemon_match_startup'}) eq 'ARRAY';
34  
35 $config = $opts;
36 }
37  
38 sub code_may_fail {
39 my ($code, $opts) = @_;
40  
41 die("Need coderef (something to execute)")
42 unless $code && ref($code) eq 'CODE';
43
44 $opts = {} unless $opts;
45 $opts->{'tries'} = 1 unless $opts->{'tries'};
46 $opts->{'sleep_between_tries'} = 1 unless $opts->{'sleep_between_tries'};
47  
48 my $i = 0;
49 my $lastwarn = "";
50 my $code_result;
51  
52 while ($i < $opts->{'tries'}) {
53 $i = $i + 1;
54  
55 $SIG{'__WARN__'} = sub { $lastwarn = join("\n", @_); };
56 eval { $code_result = $code->(); };
57 delete($SIG{'__WARN__'});
58  
59 if ($lastwarn && $i < $opts->{'tries'}) {
60 $lastwarn = "";
61 sleep $opts->{'sleep_between_tries'};
62 }
63 }
64  
65 return {
66 'result' => $code_result,
67 'warn' => $lastwarn,
68 'try' => $i,
69 };
70 }
71  
72 # returns
73 # * 0 if process does not exist at all (no /proc/PID directory),
74 # * undef if its /proc/PID entry is in some inconsistent state
75 # * psProcess object if pid was found and successfully read with (pid, ppid, pgid, cmd) methods
76 #
77 sub get_process_by_pid {
78 my ($pid) = @_;
79  
80 my $read_may_fail = sub {
81 my ($filename) = @_;
82 my $filecontent;
83 if (open F, $filename) {
84 { local $/ = undef; $filecontent = <F>; }
85 close F;
86 }
87 return $filecontent;
88 };
89  
90 my $pid_dir = "/proc/$pid";
91  
92 return 0 if ! -d $pid_dir;
93  
94 my $cmd = $read_may_fail->("$pid_dir/cmdline");
95 $cmd =~ s/\0/ /goi if $cmd;
96  
97 my $ppid;
98 my $pgid;
99 my $seconds_since_boot = 0;
100  
101 if (-e "$pid_dir/stat") { # linux path
102 my $stat = $read_may_fail->("$pid_dir/stat");
103 return undef unless $stat;
104  
105 my @stat_arr = split(" ", $stat);
106 return undef if ! scalar(@stat_arr) > 5;
107  
108 if (!$cmd) {
109 $cmd = $stat_arr[1];
110
111 if ($cmd) {
112 $cmd =~ s/[\(\)]//goi;
113 $cmd = "[" . $cmd . "]";
114 }
115 }
116  
117 $ppid = $stat_arr[3];
118 $pgid = $stat_arr[4];
119 $seconds_since_boot = int($stat_arr[21] / 100);
120 }
121 elsif (-e "$pid_dir/status") { # bsd path
122 my $stat = $read_may_fail->("$pid_dir/status");
123 return undef unless $stat;
124  
125 my @stat_arr = split(" ", $stat);
126 return undef if ! scalar(@stat_arr) > 5;
127  
128 if (!$cmd) {
129 $cmd = $stat_arr[0];
130
131 if ($cmd) {
132 $cmd =~ s/[\(\)]//goi;
133 $cmd = "[" . $cmd . "]";
134 }
135 }
136  
137 $ppid = $stat_arr[2];
138 $pgid = $stat_arr[3];
139 }
140  
141 # Get btime
142 my $start_time;
143 my $stat = $read_may_fail->("/proc/stat");
144 $start_time = undef;
145 my @stat = split("\n",$stat);
146 for (@stat) {
147 if (/^btime (\d+)/) {
148 $start_time = $seconds_since_boot + $1;
149 last;
150 }
151 }
152  
153 return undef if ! $cmd;
154 return undef if $ppid !~ /^[0-9]+$/o;
155 return undef if $pgid !~ /^[0-9]+$/o;
156  
157 my $p = {
158 'pid' => $pid,
159 'ppid' => $ppid,
160 'pgid' => $pgid,
161 'cmd' => $cmd,
162 'start_time' => $start_time,
163 };
164  
165 bless ($p, 'psProcess');
166 return $p;
167 }
168  
169 sub get_process_table {
170 my $ptable;
171  
172 my @all_entries;
173 if (-d "/proc") {
174 my $i = 0;
175  
176 my $dummy;
177 my $open_res;
178 while (!($open_res = opendir($dummy, "/proc")) && $i < 2) {
179 sleep 1;
180 $i++;
181 }
182 if (!$open_res) {
183 Yandex::Tools::die("unable to read /proc");
184 }
185  
186 $i = 0;
187 while (scalar(@all_entries) < 3 && $i < 2) {
188 @all_entries = readdir($dummy);
189 sleep 1 if $i > 0;
190 $i++;
191 }
192 close($dummy);
193 }
194 else {
195 # try to use Proc::ProcessTable on BSD when /proc is not available
196 if ($^O ne 'linux') {
197 require Proc::ProcessTable;
198 }
199 }
200  
201 # Proc::ProcessTable has some leaks on linux
202 # which leads to process dying
203 #
204 # use procfs if available
205 #
206 if ($^O eq 'linux' || scalar(@all_entries) > 3) {
207 $ptable = [];
208
209 # . + .. eq 2
210 if (scalar(@all_entries) < 3) {
211 Yandex::Tools::die("/proc is not mounted");
212 }
213  
214 foreach my $e (sort @all_entries) {
215 next if $e eq '.' || $e eq '..';
216 next if $e !~ /^\d+$/o;
217  
218 my $p = get_process_by_pid($e);
219
220 if ($p) {
221 push (@{$ptable}, $p);
222 }
223 }
224  
225 return $ptable;
226 }
227 else {
228 my $r = code_may_fail(sub {return Proc::ProcessTable->new()->table}, {'tries' => 3});
229  
230 if (!$r->{'result'}) {
231 Yandex::Tools::die("unable to get process table: " . $r->{'warn'});
232 }
233
234 $ptable = $r->{'result'};
235 }
236  
237 my $i = 0;
238 while (scalar(@{$ptable}) < 2 && $i < 3) {
239 $i++;
240 sleep 1;
241 $ptable = get_process_table();
242 }
243  
244 if (scalar(@{$ptable}) < 2) {
245 Yandex::Tools::die("unable to read process table");
246 }
247  
248 return $ptable;
249 }
250  
251 sub get_process_by_id {
252 my ($pid, $opts) = @_;
253  
254 $opts = {} unless $opts;
255  
256 my $processes;
257 if ($opts->{'processes'}) {
258 $processes = $opts->{'processes'};
259 }
260 else {
261 if (!$runtime->{'startup_processes'}) {
262 $runtime->{'startup_processes'} = get_process_table();
263 }
264 $processes = $runtime->{'startup_processes'};
265 }
266  
267 foreach my $p (@$processes) {
268 my $r = code_may_fail(sub {return $p->pid});
269  
270 if (!$r->{'result'}) {
271 # print STDERR
272 # "empty pid: " . $p->cmndline . "; " .
273 # ((-f $p->cmndline) ? "file exists" : "file does not exist") .
274 # "; my pid [" . $$ . "]" .
275 # "\n";
276 #
277 # dvina: empty pid: /proc/23263/cmdline; file does not exist; my pid [23348]
278 # dunai: empty pid: /proc/31978/cmdline; file does not exist; my pid [32072]
279  
280 next;
281 }
282
283 return $p if $r->{'result'} eq $pid;
284 }
285  
286 return undef;
287 }
288  
289 # get the pid of my parent process (by command line)
290 #
291 sub get_my_process {
292 my ($pid) = @_;
293  
294 my $orig_pid = $pid;
295  
296 # trying to find daemon with the same --cfg option
297 #
298 while ($pid ne 1) {
299 my $pid_p = get_process_by_id($pid);
300  
301 if (!$pid_p) {
302 Yandex::Tools::die("unable to find [$pid] in process list");
303 }
304  
305 if ($pid_p->cmndline &&
306 Yandex::Tools::matches_with_one_of_regexps($pid_p->cmndline, $config->{'daemon_match'})
307 ) {
308  
309 return $pid_p;
310 }
311  
312 $pid = $pid_p->ppid;
313 }
314  
315 # backward compatibility: trying to find
316 # any daemon without --cfg option
317 $pid = $orig_pid;
318 while ($pid ne 1) {
319 my $pid_p = get_process_by_id($pid);
320 if (!$pid_p) {
321 Yandex::Tools::die("unable to find [$pid] in process list");
322 }
323  
324 if ($pid_p->cmndline &&
325 Yandex::Tools::matches_with_one_of_regexps($pid_p->cmndline, $config->{'daemon_match_startup'}) &&
326 !Yandex::Tools::matches_with_one_of_regexps($pid_p->cmndline, $config->{'daemon_match'})) {
327  
328 return $pid_p;
329 }
330  
331 $pid = $pid_p->ppid;
332 }
333  
334 return undef;
335 }
336  
337 # get pid of other daemon started with the same --cfg option
338 #
339 sub get_other_daemon_process {
340 my ($opts) = @_;
341 $opts = {} unless $opts;
342  
343 my $processes;
344 if (!$runtime->{'startup_processes'} || $opts->{'refresh_startup_processes'}) {
345 $runtime->{'startup_processes'} = get_process_table();
346 }
347 $processes = $runtime->{'startup_processes'};
348  
349 # this doesn't mean "always find my process",
350 # name of the sub is not consistent!!!
351 #
352 # it usually returns undef (during --stop for example)
353 #
354 my $my_process = get_my_process($$);
355  
356 my $r;
357  
358 # trying to find other daemon with the same --cfg option
359 #
360 foreach my $p (@$processes) {
361 my $p_pid;
362 my $p_cmndline;
363 my $p_pgrp;
364 $r = code_may_fail(sub {$p_pid = $p->pid});
365 $r = code_may_fail(sub {$p_cmndline = $p->cmndline});
366 $r = code_may_fail(sub {$p_pgrp = $p->pgrp});
367  
368 next unless $p_cmndline;
369 next if !Yandex::Tools::matches_with_one_of_regexps($p_cmndline, $config->{'daemon_match'});
370  
371 # find process with given command line
372 # from other process group
373 if ($my_process) {
374 next if $p->pgrp eq $my_process->pgrp;
375 }
376  
377 # if we are looking for daemon then its parent should be init
378 if ($p_cmndline !~ /--debug/o && $p->ppid ne 1) {
379 next;
380 }
381  
382 my $real_daemon = get_process_by_id($p_pgrp);
383
384 # found a process for which group leader doesn't exist
385 # (shouldn't happen but just in case of some error)
386 #
387 #
388 # real world situation:
389 #
390 # pechora:~# ps -eo pid,ppid,pgrp,cmd | grep snak | grep -v grep
391 # 5674 1 5674 /usr/bin/perl /usr/local/ps-snake/bin/snaked --watchdog --cfg /etc/ps-farm/options/ps-snaked
392 # 26550 1 25742 /usr/bin/perl /usr/local/ps-snake/bin/snaked --daemon --cfg /etc/ps-farm/options/ps-snaked
393 # 29634 1 25742 /usr/bin/perl /usr/local/ps-snake/bin/snaked --daemon --cfg /etc/ps-farm/options/ps-snaked
394 #
395 # corresponding log message about parent pid:
396 # Sat Apr 10 16:37:52 2010 [/usr/local/ps-snake/bin/snaked] [25742] started
397 #
398 # both 26550 and 29634 were not snaked daemons
399 # but were forks doing some work (actually locked
400 # during log operation or something) but
401 # watchdog doesn't detect difference between
402 # snaked daemon and its children forks
403 # (it should and will do it one day probably)
404 #
405 # and manual `snaked --daemon` also didn't detect them,
406 # now it kills them (their process group)
407 # before spawning new daemon
408 #
409 if (!$real_daemon) {
410 print STDERR "cleaning up stuck process group [$p_pgrp]\n";
411 kill(-9, $p_pgrp);
412 }
413
414 return $real_daemon;
415 }
416  
417 # backward compatibility: trying to find
418 # any other daemon without --cfg option
419 #
420 foreach my $p (@$processes) {
421 my $p_pid;
422 my $p_cmndline;
423 my $p_pgrp;
424 $r = code_may_fail(sub {$p_pid = $p->pid});
425 $r = code_may_fail(sub {$p_cmndline = $p->cmndline});
426 $r = code_may_fail(sub {$p_pgrp = $p->pgrp});
427  
428 next unless $p_cmndline;
429 next if !Yandex::Tools::matches_with_one_of_regexps($p_cmndline, $config->{'daemon_match_startup'});
430 next if Yandex::Tools::matches_with_one_of_regexps($p_cmndline, $config->{'daemon_match'});
431  
432 if ($my_process) {
433 next if $p->pgrp eq $my_process->pgrp;
434 }
435  
436 my $real_daemon = get_process_by_id($p_pgrp);
437 return $real_daemon;
438 }
439  
440 return undef;
441 }
442  
443 sub get_my_path_commandline {
444 my ($opts) = @_;
445 my $my_path;
446 my $my_command_line;
447  
448 my $me = Yandex::Tools::ProcessList::get_process_by_id($$, $opts);
449 Yandex::Tools::die("[$$]: unable to find myself in process list") unless $me;
450 $my_path = $FindBin::Bin;
451 Yandex::Tools::die("[$$]: unable to find my path") unless $my_path;
452 $my_command_line = $me->cmndline;
453 Yandex::Tools::die("[$$] unable to determine my command line") unless $my_command_line;
454  
455 return ($my_path, $my_command_line);
456 }
457  
458  
459 1;