1 kumpf 1.1 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 kumpf 1.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include <Pegasus/Common/Config.h>
35 #include <Pegasus/Common/Constants.h>
36 #include <Pegasus/Common/Logger.h>
37 #include <Pegasus/Common/System.h>
38 #include <Pegasus/Common/Tracer.h>
39 #include <Pegasus/Client/CIMClient.h>
40 #include <Service/ServerShutdownClient.h>
41
42 PEGASUS_USING_STD;
43 kumpf 1.1
44 PEGASUS_NAMESPACE_BEGIN
45
46 ServerShutdownClient::ServerShutdownClient(
47 ServerRunStatus* serverRunStatus)
48 : _serverRunStatus(serverRunStatus)
49 {
50 }
51
52 ServerShutdownClient::~ServerShutdownClient()
53 {
54 }
55
56 //
57 // Waits until either the CIM Server has terminated, or the shutdown timeout
58 // has expired. If the shutdown timeout has expired, and the CIM Server is
59 // still running, kills the cimserver process.
60 //
61 void ServerShutdownClient::_waitForTerminationOrTimeout(Uint32 maxWaitTime)
62 {
63 //
64 kumpf 1.1 // If the CIM Server is still running, and the shutdown timeout has not
65 // expired, loop and wait one second until either the CIM Server has
66 // terminated, or the shutdown timeout has expired
67 //
68 Boolean running = _serverRunStatus->isServerRunning();
69 while (running && (maxWaitTime > 0))
70 {
71 System::sleep(1);
72 running = _serverRunStatus->isServerRunning();
73 maxWaitTime--;
74 }
75
76 //
77 // If the shutdown timeout has expired, and the CIM Server is still
78 // running, kill the cimserver process
79 //
80 if (running)
81 {
82 Boolean wasKilled = _serverRunStatus->kill();
83
84 #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) \
85 kumpf 1.1 || defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) || defined(PEGASUS_OS_SOLARIS) \
86 || defined (PEGASUS_OS_VMS)
87 if (wasKilled)
88 {
89 //l10n - TODO
90 Logger::put_l (Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
91 "src.Server.cimserver.TIMEOUT_EXPIRED_SERVER_KILLED",
92 "Shutdown timeout expired. Forced shutdown initiated.");
93 MessageLoaderParms parms
94 ("src.Server.cimserver.TIMEOUT_EXPIRED_SERVER_KILLED",
95 "Shutdown timeout expired. Forced shutdown initiated.");
96 cout << MessageLoader::getMessage(parms) << endl;
97 exit (0);
98 }
99 #endif
100 }
101 }
102
103 void ServerShutdownClient::shutdown(Uint32 timeoutValue)
104 {
105 //
106 kumpf 1.1 // Create CIMClient object
107 //
108 CIMClient client;
109
110 //
111 // Get local host name
112 //
113 String hostStr = System::getHostName();
114
115 //
116 // open connection to CIMOM
117 //
118 try
119 {
120 client.connectLocal();
121
122 //
123 // set client timeout to 2 seconds
124 //
125 client.setTimeout(2000);
126 }
127 kumpf 1.1 catch(Exception&)
128 {
129 MessageLoaderParms parms(
130 "src.Server.cimserver.UNABLE_CONNECT_SERVER_MAY_NOT_BE_RUNNING",
131 "Unable to connect to CIM Server. CIM Server may not be running.");
132 PEGASUS_STD(cerr) << MessageLoader::getMessage(parms) <<
133 PEGASUS_STD(endl);
134 exit(1);
135 }
136
137 try
138 {
139 //
140 // construct CIMObjectPath
141 //
142 String referenceStr = "//";
143 referenceStr.append(hostStr);
144 referenceStr.append("/");
145 referenceStr.append(PEGASUS_NAMESPACENAME_SHUTDOWN.getString());
146 referenceStr.append(":");
147 referenceStr.append(PEGASUS_CLASSNAME_SHUTDOWN.getString());
148 kumpf 1.1 CIMObjectPath reference(referenceStr);
149
150 //
151 // issue the invokeMethod request on the shutdown method
152 //
153 Array<CIMParamValue> inParams;
154 Array<CIMParamValue> outParams;
155
156 // set force option to true for now
157 inParams.append(CIMParamValue("force",
158 CIMValue(Boolean(true))));
159
160 inParams.append(CIMParamValue("timeout",
161 CIMValue(Uint32(timeoutValue))));
162
163 CIMValue retValue = client.invokeMethod(
164 PEGASUS_NAMESPACENAME_SHUTDOWN,
165 reference,
166 "shutdown",
167 inParams,
168 outParams);
169 kumpf 1.1 }
170 catch(CIMException& e)
171 {
172 //l10n - TODO
173 MessageLoaderParms parms("src.Server.cimserver.SHUTDOWN_FAILED",
174 "Error in server shutdown: ");
175 PEGASUS_STD(cerr) << MessageLoader::getMessage(parms);
176 if (e.getCode() == CIM_ERR_INVALID_NAMESPACE)
177 {
178 //
179 // Repository may be empty.
180 //
181 //l10n - TODO
182 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
183 "src.Server.cimserver.SHUTDOWN_FAILED_REPOSITORY_EMPTY",
184 "Error in server shutdown: The repository may be empty.");
185 MessageLoaderParms parms("src.Server.cimserver.REPOSITORY_EMPTY",
186 "The repository may be empty.");
187 PEGASUS_STD(cerr) << MessageLoader::getMessage(parms) <<
188 PEGASUS_STD(endl);
189 }
190 kumpf 1.1 else
191 {
192 //l10n - TODO
193 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
194 "src.Server.cimserver.SHUTDOWN_FAILED",
195 "Error in server shutdown: $0", e.getMessage());
196 PEGASUS_STD(cerr) << e.getMessage() << PEGASUS_STD(endl);
197 }
198
199 // Kill the cimserver process
200 if (_serverRunStatus->kill())
201 {
202 //l10n - TODO
203 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
204 "src.Server.cimserver.SERVER_FORCED_SHUTDOWN",
205 "Forced shutdown initiated.");
206 MessageLoaderParms parms(
207 "src.Server.cimserver.SERVER_FORCED_SHUTDOWN",
208 "Forced shutdown initiated.");
209 PEGASUS_STD(cerr) << MessageLoader::getMessage(parms) <<
210 PEGASUS_STD(endl);
211 kumpf 1.1 }
212 exit(1);
213 }
214 catch(Exception&)
215 {
216 //
217 // This may mean that the CIM Server has terminated, causing this
218 // client to get a "Empty HTTP response message" exception. It may
219 // also mean that the CIM Server is taking longer than 2 seconds
220 // (client timeout value) to terminate, causing this client to
221 // timeout with a "connection timeout" exception.
222 //
223 // Wait until either the CIM Server has terminated, or the shutdown
224 // timeout has expired. If the timeout has expired and the CIM Server
225 // is still running, kill the cimserver process.
226 //
227 _waitForTerminationOrTimeout(timeoutValue - 2);
228 return;
229 }
230
231 //
232 kumpf 1.1 // InvokeMethod succeeded.
233 // Wait until either the CIM Server has terminated, or the shutdown
234 // timeout has expired. If the timeout has expired and the CIM Server
235 // is still running, kill the cimserver process.
236 //
237 _waitForTerminationOrTimeout(timeoutValue);
238 }
239
240 PEGASUS_NAMESPACE_END
|