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