| 1 | /* |
| 2 | * File: Watchdog.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "Watchdog.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "SystemUtils.h" |
| 28 | |
| 29 | #include <iostream> |
| 30 | #include <sstream> |
| 31 | #ifndef hFramework |
| 32 | #include <sys/wait.h> |
| 33 | #endif |
| 34 | #include <cerrno> |
| 35 | #include <sys/stat.h> |
| 36 | |
| 37 | Watchdog::Watchdog(int herculesPid, bool child) |
| 38 | { |
| 39 | mHerculesPid = herculesPid; |
| 40 | mChild = child; |
| 41 | } |
| 42 | |
| 43 | Watchdog::Watchdog(QProcess * herculesProcess) |
| 44 | { |
| 45 | mHerculesPid = -1; |
| 46 | mChild = false; |
| 47 | mProcess = herculesProcess; |
| 48 | } |
| 49 | |
| 50 | Watchdog::~Watchdog() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void Watchdog::run() |
| 55 | { |
| 56 | if (mChild) |
| 57 | { |
| 58 | #ifndef hFramework |
| 59 | int stat_loc; |
| 60 | int rc = waitpid(mHerculesPid, &stat_loc, 0); |
| 61 | hOutDebug(1, "watchdog ended for pid:" << rc); |
| 62 | #endif |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | if (mHerculesPid > 0) |
| 67 | { |
| 68 | while(SystemUtils::processIsRunning(mHerculesPid)) |
| 69 | sleep(2); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | while (mProcess->state() != QProcess::NotRunning) |
| 74 | sleep(2); |
| 75 | } |
| 76 | } |
| 77 | emit HerculesEnded(); |
| 78 | } |