summaryrefslogtreecommitdiff
path: root/net-mgmt/net-snmp-devel/files/patch-local:fixproc
blob: 5d6e3515467d3f5075119994f96b4ae1df8ea42b (plain) (blame)
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
--- local/fixproc.orig	Sat Apr 20 09:30:13 2002
+++ local/fixproc	Sat Mar  6 01:59:59 2004
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!%%PERL%%
 # 
 # fixproc [-min n] [-max n] [-check | -kill | -restart | -exist | -fix] proc ...
 # 
@@ -129,7 +129,7 @@
 #
 # Timothy Kong		3/1995
 
-$database_file = '/local/etc/fixproc.conf';
+$database_file = '%%PREFIX%%/etc/fixproc.conf';
 
 $debug = 0;			# specify debug level using -dN
 				# currently defined: -d1
@@ -155,6 +155,14 @@
 $shell_header = "#!/bin/sh\n";
 $shell_end_marker = 'shell_end_marker';
 
+open(command, "/bin/ps -p $$ |") || die "$0: can't run ps command\n";
+if (split(' ', <command>) > 4) {
+        $ps_opts = 'ax';
+} else {
+	$ps_opts = '-e';
+}
+close command;
+
 &read_args();
 &read_database();
 # &dump_database();		# debug only
@@ -203,7 +211,9 @@
       $i++;
     }
   close (file);
-  system "chmod +x $file";
+  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
+   $atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
+  chmod $mode | 0111, $file;
   return file;
 }
 
@@ -237,8 +247,8 @@
 
       	# return code is number divided by 256
       $error_code = (system "$tmpfile") / 256;
-      system "rm $tmpfile";
-      return ($fix_failed_error) if ($error_code != 0);
+      unlink $tmpfile;
+      return ($cannot_fix_error) if ($error_code != 0);
         # sleep needed here?
       return &do_exist ($proc);
     }
@@ -268,7 +278,7 @@
 
       	# return code is number divided by 256
       $error_code = (system "$tmpfile") / 256;
-      system "rm $tmpfile";
+      unlink $tmpfile;
       return ($check_failed_error) if ($error_code != 0);
 
       # check passed, continue
@@ -285,10 +295,12 @@
 
   # do ps, check to see if min <= no. of processes <= max
   $! = $fixproc_error;
-  open (command, "/bin/ps -e | /bin/grep $proc | /bin/wc -l |")
+  open (command, "/bin/ps $ps_opts |")
     || die "$0: can't run ps-grep-wc command\n";
-  $proc_count = <command>;
-  if (($proc_count < $min{$proc}) || ($proc_count > $max{$proc}))
+  @allprocs = <command>;
+  close command;
+  @procs = grep(/$proc/, @allprocs);
+  if (($#procs < $min{$proc}) || ($#procs > $max{$proc}))
     {
       return $check_failed_error;
     }
@@ -305,41 +317,48 @@
 
   # first try kill
   $! = $fixproc_error;
-  open (command, "/bin/ps -e | /bin/grep $proc |")
+  open (command, "/bin/ps $ps_opts |")
     || die "$0: can't run ps-grep-awk command\n";
   while (<command>)
     {
-      # match the first field of ps -e
+     if /$proc/ {
+      # match the first field of ps $ps_opts
       $! = $fixproc_error;
-      /^\s*(\d+)\s/ || die "$0: can't match ps -e output\n";
-      system "kill $1";
+      /^\s*(\d+)\s/ || die "$0: can't match ps $ps_opts output\n";
+      kill 15, $1;
+     }
     }
+  close command;
 
   # if process still exist, try kill -9
   sleep 2;
   $! = $fixproc_error;
-  open (command, "/bin/ps -e | /bin/grep $proc |")
+  open (command, "/bin/ps $ps_opts |")
     || die "$0: can't run ps-grep-awk command\n";
   $second_kill_needed = 0;
   while (<command>)
     {
-      # match the first field of ps -e
+     if /$proc/ {
+      # match the first field of ps $ps_opts
       $! = $fixproc_error;
-      /^\s*(\d+)\s/ || die "$0: can't match ps -e output\n";
-      system "kill -9 $1";
+      /^\s*(\d+)\s/ || die "$0: can't match ps $ps_opts output\n";
+      kill 9, $1;
       $second_kill_needed = 1;
+     }
     }
+  close command;
   return ($no_error) if ($second_kill_needed == 0);
 
   # see if kill -9 worked
   sleep 2;
   $! = $fixproc_error;
-  open (command, "/bin/ps -e | /bin/grep $proc |")
+  open (command, "/bin/ps $ps_opts |")
     || die "$0: can't run ps-grep-awk command\n";
   while (<command>)
     {				# a process still exist, return error
-      return $cannot_kill_error;
+      return $cannot_kill_error if /$proc/;
     }
+  close command;
   return $no_error;		# good, all dead
 }