1 kumpf 1.2 /*
2 //%2006////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
5 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
6 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
7 // IBM Corp.; EMC Corporation, The Open Group.
8 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
9 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
10 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
11 // EMC Corporation; VERITAS Software Corporation; The Open Group.
12 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
13 // EMC Corporation; Symantec Corporation; The Open Group.
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to
17 // deal in the Software without restriction, including without limitation the
18 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 // sell copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
23 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
24 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32 */
33 #include <stdio.h>
34 #include <string.h>
35 #include "Process.h"
36 #include "Strlcpy.h"
37
38 #if defined(PEGASUS_OS_HPUX)
39 # include <sys/pstat.h>
40 #endif
41
42 /*
43 kumpf 1.2 **==============================================================================
44 **
45 ** GetProcessName()
46 **
47 **==============================================================================
48 */
49
50 #if defined(PEGASUS_OS_HPUX)
51
52 int GetProcessName(int pid, char name[EXECUTOR_BUFFER_SIZE])
53 {
54 struct pst_status psts;
55
56 if (pstat_getproc(&psts, sizeof(psts), 0, pid) == -1)
57 return -1;
58
59 Strlcpy(name, psts.pst_ucomm, EXECUTOR_BUFFER_SIZE);
60
61 return 0;
62 }
63
64 kumpf 1.2 #elif defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
65
66 int GetProcessName(int pid, char name[EXECUTOR_BUFFER_SIZE])
67 {
68 FILE* is;
69 char* start;
70 char* end;
71
72 /* Read the process name from the file. */
73
74 static char buffer[1024];
75 sprintf(buffer, "/proc/%d/stat", pid);
76
77 if ((is = fopen(buffer, "r")) == NULL)
78 return -1;
79
80 /* Read the first line of the file. */
81
82 if (fgets(buffer, sizeof(buffer), is) == NULL)
83 {
84 fclose(is);
85 kumpf 1.2 return -1;
86 }
87
88 fclose(is);
89
90 /* Extract the PID enclosed in parentheses. */
91
92 start = strchr(buffer, '(');
93
94 if (!start)
95 return -1;
96
97 start++;
98
99 end = strchr(start, ')');
100
101 if (!end)
102 return -1;
103
104 if (start == end)
105 return -1;
106 kumpf 1.2
107 *end = '\0';
108
109 Strlcpy(name, start, EXECUTOR_BUFFER_SIZE);
110
111 return 0;
112 }
113
114 #else
115 # error "not implemented on this platform."
116 #endif /* PEGASUS_PLATFORM_LINUX_GENERIC_GNU */
|
117 kumpf 1.3
118 /*
119 **==============================================================================
120 **
121 ** ReadPidFile()
122 **
123 **==============================================================================
124 */
125
126 int ReadPidFile(const char* pidFilePath, int* pid)
127 {
128 FILE* is = fopen(pidFilePath, "r");
129
130 if (!is)
131 return -1;
132
133 *pid = 0;
134
135 fscanf(is, "%d\n", pid);
136 fclose(is);
137
138 kumpf 1.3 if (*pid == 0)
139 return -1;
140
141 return 0;
142 }
143
144 /*
145 **==============================================================================
146 **
147 ** TestProcessRunning()
148 **
149 ** Returns 0 if a process is running with the ID in the specified PID file
150 ** and with the specified process name.
151 **
152 **==============================================================================
153 */
154
155 int TestProcessRunning(
156 const char* pidFilePath,
157 const char* processName)
158 {
159 kumpf 1.3 int pid;
160 char name[EXECUTOR_BUFFER_SIZE];
161
162 if (ReadPidFile(pidFilePath, &pid) != 0)
163 return -1;
164
165 if (GetProcessName(pid, name) != 0 || strcmp(name, processName) != 0)
166 return -1;
167
168 return 0;
169 }
|