1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: terminate.cxx,v $
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"
40 #include <sys/types.h>
48 #include "testshl/getopt.hxx"
50 #if (defined UNX) || (defined OS2)
51 #include <unistd.h> /* usleep */
52 #include <sys/types.h>
57 # include "testshl/winstuff.hxx"
62 // -----------------------------------------------------------------------------
65 std::string m_sProcessIDFilename
;
69 int readPIDFromFile();
70 void sendSignal(int _nPID
);
74 void setName(std::string
const& _sFilename
);
78 void waitForPIDFile(int);
79 void waitForTimeout(int);
82 void my_sleep(int sec
)
87 usleep(sec
* 1000000); // 10 ms
91 // ------------------------------- ProcessHelper -------------------------------
92 ProcessHandler::ProcessHandler():m_nPID(0) {}
94 void ProcessHandler::setName(std::string
const& _sPIDFilename
)
96 m_sProcessIDFilename
= _sPIDFilename
;
99 int ProcessHandler::getPID()
104 int ProcessHandler::readPIDFromFile()
108 if (m_sProcessIDFilename
.size() > 0)
111 in
= fopen(m_sProcessIDFilename
.c_str(), "r");
114 // fprintf(stderr, "warning: (testshl.cxx) can't read own pid.\n");
118 // if file exist, wait short, maybe the other tool writes it down.
119 fscanf(in
, "%d", &nPID
);
124 fprintf(stderr
, "error: (terminate.cxx) PID Filename empty, must set.\n");
130 ProcessHandler::~ProcessHandler()
134 void ProcessHandler::sendSignal(int _nPID
)
139 WinTerminateApp(_nPID
, 100);
141 kill(_nPID
, SIGKILL
);
146 // -----------------------------------------------------------------------------
147 void ProcessHandler::waitForPIDFile(int _nTimeout
)
149 int nWaitforTimeout
= _nTimeout
;
150 while (getPID() == 0 && nWaitforTimeout
> 0)
152 int nPID
= readPIDFromFile();
160 fprintf(stderr
, "wait for pid file\n");
164 if (nWaitforTimeout
<= 0)
166 fprintf(stderr
, "No PID found, time runs out\n");
170 // -----------------------------------------------------------------------------
171 void ProcessHandler::waitForTimeout(int _nTimeout
)
173 int nTimeout
= _nTimeout
;
177 fprintf(stderr
, "%d \r", nTimeout
);
179 int nNewPID
= readPIDFromFile();
180 if ( nNewPID
!= getPID() )
182 fprintf(stderr
, "PID has changed.\n");
185 fprintf(stderr
, "new PID is not 0, maybe forgotten to delete old PID file, restart timeout.\n");
187 nTimeout
= _nTimeout
;
198 fprintf(stderr
, "PID: %d\n", getPID());
199 sendSignal(getPID());
204 void ProcessHandler::write(int _nPID
)
208 if (m_sProcessIDFilename
.size() > 0)
211 out
= fopen(m_sProcessIDFilename
.c_str(), "w");
214 fprintf(stderr
, "warning: (testshl.cxx) can't write own pid.\n");
218 fprintf(out
, "%d", _nPID
);
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
[] )
231 int _cdecl
main( int, char* argv
[] )
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",
243 ProcessHandler aCurrentProcess
;
245 GetOpt
opt( argv
, optionSet
);
246 if ( opt
.hasOpt("-pid") )
248 aCurrentProcess
.setName(opt
.getOpt("-pid").getStr());
252 if ( opt
.hasOpt("-time"))
255 nTimeout
= opt
.getOpt("-time").toInt32();
262 if ( opt
.hasOpt("-version") )
264 fprintf(stderr
, "testshl2_timeout $Revision: 1.9 $\n");
268 // someone indicates that he needs help
269 if ( opt
.hasOpt( "-h" ) || opt
.hasOpt( "-help" ) )
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
);