update dev300-m58
[ooovba.git] / testshl2 / source / terminate.cxx
blob2c85bcc6035c87a36cf765d8378f0a386d6d0cf2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: terminate.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_testshl2.hxx"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
38 #ifdef SOLARIS
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #endif
43 #include <signal.h>
45 #include <iostream>
46 #include <string>
48 #include "testshl/getopt.hxx"
50 #if (defined UNX) || (defined OS2)
51 #include <unistd.h> /* usleep */
52 #include <sys/types.h>
53 #include <signal.h>
54 #endif
56 #ifdef WNT
57 # include "testshl/winstuff.hxx"
58 #endif
60 using namespace std;
62 // -----------------------------------------------------------------------------
63 class ProcessHandler
65 std::string m_sProcessIDFilename;
66 int m_nPID;
68 int getPID();
69 int readPIDFromFile();
70 void sendSignal(int _nPID);
71 void write(int);
72 public:
73 ProcessHandler();
74 void setName(std::string const& _sFilename);
76 ~ProcessHandler();
78 void waitForPIDFile(int);
79 void waitForTimeout(int);
82 void my_sleep(int sec)
84 #ifdef WNT
85 WinSleep(sec * 1000);
86 #else
87 usleep(sec * 1000000); // 10 ms
88 #endif
91 // ------------------------------- ProcessHelper -------------------------------
92 ProcessHandler::ProcessHandler():m_nPID(0) {}
94 void ProcessHandler::setName(std::string const& _sPIDFilename)
96 m_sProcessIDFilename = _sPIDFilename;
99 int ProcessHandler::getPID()
101 return m_nPID;
104 int ProcessHandler::readPIDFromFile()
106 // get own PID
107 int nPID = 0;
108 if (m_sProcessIDFilename.size() > 0)
110 FILE* in;
111 in = fopen(m_sProcessIDFilename.c_str(), "r");
112 if (!in)
114 // fprintf(stderr, "warning: (testshl.cxx) can't read own pid.\n");
115 return 0;
116 // exit(0);
118 // if file exist, wait short, maybe the other tool writes it down.
119 fscanf(in, "%d", &nPID);
120 fclose(in);
122 else
124 fprintf(stderr, "error: (terminate.cxx) PID Filename empty, must set.\n");
125 exit(0);
127 return nPID;
130 ProcessHandler::~ProcessHandler()
134 void ProcessHandler::sendSignal(int _nPID)
136 if (_nPID != 0)
138 #ifdef WNT
139 WinTerminateApp(_nPID, 100);
140 #else
141 kill(_nPID, SIGKILL);
142 #endif
146 // -----------------------------------------------------------------------------
147 void ProcessHandler::waitForPIDFile(int _nTimeout)
149 int nWaitforTimeout = _nTimeout;
150 while (getPID() == 0 && nWaitforTimeout > 0)
152 int nPID = readPIDFromFile();
153 if (nPID != 0)
155 m_nPID = nPID;
156 break;
159 my_sleep(1);
160 fprintf(stderr, "wait for pid file\n");
161 nWaitforTimeout--;
164 if (nWaitforTimeout <= 0)
166 fprintf(stderr, "No PID found, time runs out\n");
167 exit(1);
170 // -----------------------------------------------------------------------------
171 void ProcessHandler::waitForTimeout(int _nTimeout)
173 int nTimeout = _nTimeout;
174 while (nTimeout > 0)
176 my_sleep(1);
177 fprintf(stderr, "%d \r", nTimeout);
179 int nNewPID = readPIDFromFile();
180 if ( nNewPID != getPID() )
182 fprintf(stderr, "PID has changed.\n");
183 if ( nNewPID != 0)
185 fprintf(stderr, "new PID is not 0, maybe forgotten to delete old PID file, restart timeout.\n");
186 m_nPID = nNewPID;
187 nTimeout = _nTimeout;
189 else
191 break;
194 nTimeout --;
196 if (nTimeout <= 0)
198 fprintf(stderr, "PID: %d\n", getPID());
199 sendSignal(getPID());
200 write(0);
204 void ProcessHandler::write(int _nPID)
206 // get own PID
208 if (m_sProcessIDFilename.size() > 0)
210 FILE* out;
211 out = fopen(m_sProcessIDFilename.c_str(), "w");
212 if (!out)
214 fprintf(stderr, "warning: (testshl.cxx) can't write own pid.\n");
215 return;
216 // exit(0);
218 fprintf(out, "%d", _nPID);
219 fclose(out);
221 else
223 fprintf(stderr, "warning: (testshl.cxx) PID Filename empty, must set.\n");
227 // ----------------------------------- Main -----------------------------------
228 #if (defined UNX) || (defined OS2)
229 int main( int, char* argv[] )
230 #else
231 int _cdecl main( int, char* argv[] )
232 #endif
234 static char const * optionSet[] = {
235 "-version, shows current program version and exit.",
236 "-pid=s, write current process id to file",
237 "-time=s, timeout [default is 10 sec]",
238 "-h:s, display help or help on option",
239 "-help:s, see -h",
240 NULL
243 ProcessHandler aCurrentProcess;
245 GetOpt opt( argv, optionSet );
246 if ( opt.hasOpt("-pid") )
248 aCurrentProcess.setName(opt.getOpt("-pid").getStr());
251 int nTimeout = 10;
252 if ( opt.hasOpt("-time"))
255 nTimeout = opt.getOpt("-time").toInt32();
256 if (nTimeout == 0)
258 nTimeout = 10;
262 if ( opt.hasOpt("-version") )
264 fprintf(stderr, "testshl2_timeout $Revision: 1.9 $\n");
265 exit(0);
268 // someone indicates that he needs help
269 if ( opt.hasOpt( "-h" ) || opt.hasOpt( "-help" ) )
271 opt.showUsage();
272 exit(0);
275 // wait until pid file exist ==============================
277 aCurrentProcess.waitForPIDFile(10);
279 printf("Found PID file, wait for timeout %d sec.\n", nTimeout);
281 // timeout ==================================================
282 aCurrentProcess.waitForTimeout(nTimeout);
284 return 0;