summaryrefslogtreecommitdiff
path: root/tools.c
diff options
context:
space:
mode:
authorKlaus Schmidinger <vdr@tvdr.de>2000-04-24 13:54:23 +0200
committerKlaus Schmidinger <vdr@tvdr.de>2000-04-24 13:54:23 +0200
commit8a84f6b751f54b71f2476beef2eec10a9c919f36 (patch)
tree6ac7cba9fc3442720df93ad8232e6c1633d53981 /tools.c
parent61a20a36de5bf3b96c28a89d61f45ca58ec3d360 (diff)
downloadvdr-8a84f6b751f54b71f2476beef2eec10a9c919f36.tar.gz
vdr-8a84f6b751f54b71f2476beef2eec10a9c919f36.tar.bz2
Improved process handling
Diffstat (limited to 'tools.c')
-rw-r--r--tools.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/tools.c b/tools.c
index 68009b6f..c43e610f 100644
--- a/tools.c
+++ b/tools.c
@@ -4,13 +4,14 @@
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
- * $Id: tools.c 1.5 2000/04/24 09:46:02 kls Exp $
+ * $Id: tools.c 1.6 2000/04/24 13:54:23 kls Exp $
*/
#define _GNU_SOURCE
#include "tools.h"
#include <dirent.h>
#include <errno.h>
+#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -134,6 +135,41 @@ bool RemoveFileOrDir(const char *FileName)
return false;
}
+bool CheckProcess(pid_t pid)
+{
+ pid_t Pid2Check = pid;
+ int status;
+ pid = waitpid(Pid2Check, &status, WNOHANG);
+ if (pid < 0) {
+ if (errno != ECHILD)
+ LOG_ERROR;
+ return false;
+ }
+ return true;
+}
+
+void KillProcess(pid_t pid, int Timeout)
+{
+ pid_t Pid2Wait4 = pid;
+ for (time_t t0 = time(NULL); time(NULL) - t0 < Timeout; ) {
+ int status;
+ pid_t pid = waitpid(Pid2Wait4, &status, WNOHANG);
+ if (pid < 0) {
+ if (errno != ECHILD)
+ LOG_ERROR;
+ return;
+ }
+ if (pid == Pid2Wait4)
+ return;
+ }
+ esyslog(LOG_ERR, "ERROR: process %d won't end (waited %d seconds) - terminating it...", Pid2Wait4, Timeout);
+ if (kill(Pid2Wait4, SIGTERM) < 0) {
+ esyslog(LOG_ERR, "ERROR: process %d won't terminate (%s) - killing it...", Pid2Wait4, strerror(errno));
+ if (kill(Pid2Wait4, SIGKILL) < 0)
+ esyslog(LOG_ERR, "ERROR: process %d won't die (%s) - giving up", Pid2Wait4, strerror(errno));
+ }
+}
+
// --- cListObject -----------------------------------------------------------
cListObject::cListObject(void)