2000-10-08 09:25:20 +02:00
|
|
|
/*
|
|
|
|
* thread.c: A simple thread base class
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2002-06-16 12:57:31 +02:00
|
|
|
* $Id: thread.c 1.21 2002/06/10 16:30:00 kls Exp $
|
2000-10-08 09:25:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "thread.h"
|
2000-12-08 16:23:32 +01:00
|
|
|
#include <errno.h>
|
2000-10-08 09:25:20 +02:00
|
|
|
#include <signal.h>
|
2001-09-15 13:00:58 +02:00
|
|
|
#include <sys/resource.h>
|
2000-12-08 16:23:32 +01:00
|
|
|
#include <sys/wait.h>
|
2000-10-08 09:25:20 +02:00
|
|
|
#include <unistd.h>
|
2000-12-08 16:23:32 +01:00
|
|
|
#include "tools.h"
|
2000-10-08 09:25:20 +02:00
|
|
|
|
2001-08-03 14:18:08 +02:00
|
|
|
// --- cCondVar --------------------------------------------------------------
|
|
|
|
|
|
|
|
cCondVar::cCondVar(void)
|
|
|
|
{
|
|
|
|
pthread_cond_init(&cond, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
cCondVar::~cCondVar()
|
|
|
|
{
|
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
bool cCondVar::Wait(cMutex &Mutex)
|
2001-08-03 14:18:08 +02:00
|
|
|
{
|
2001-08-05 12:23:24 +02:00
|
|
|
return pthread_cond_wait(&cond, &Mutex.mutex);
|
2001-08-03 14:18:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-08-05 12:23:24 +02:00
|
|
|
bool cCondVar::TimedWait(cMutex &Mutex, unsigned long tmout)
|
2001-08-03 14:18:08 +02:00
|
|
|
{
|
2001-08-05 12:23:24 +02:00
|
|
|
return pthread_cond_timedwait(&cond, &Mutex.mutex, tmout);
|
2001-08-03 14:18:08 +02:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
void cCondVar::Broadcast(void)
|
|
|
|
{
|
|
|
|
pthread_cond_broadcast(&cond);
|
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
/*
|
2001-08-03 14:18:08 +02:00
|
|
|
void cCondVar::Signal(void)
|
|
|
|
{
|
|
|
|
pthread_cond_signal(&cond);
|
|
|
|
}
|
2001-08-05 12:23:24 +02:00
|
|
|
*/
|
2001-08-03 14:18:08 +02:00
|
|
|
|
2001-06-02 10:47:40 +02:00
|
|
|
// --- cMutex ----------------------------------------------------------------
|
|
|
|
|
|
|
|
cMutex::cMutex(void)
|
|
|
|
{
|
|
|
|
lockingPid = 0;
|
|
|
|
locked = 0;
|
|
|
|
pthread_mutex_init(&mutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
cMutex::~cMutex()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cMutex::Lock(void)
|
|
|
|
{
|
2001-09-23 14:19:39 +02:00
|
|
|
if (getpid() != lockingPid || !locked) {
|
2001-06-02 10:47:40 +02:00
|
|
|
pthread_mutex_lock(&mutex);
|
2001-09-23 14:19:39 +02:00
|
|
|
lockingPid = getpid();
|
|
|
|
}
|
2001-06-02 10:47:40 +02:00
|
|
|
locked++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cMutex::Unlock(void)
|
|
|
|
{
|
2001-09-23 14:19:39 +02:00
|
|
|
if (!--locked) {
|
|
|
|
lockingPid = 0;
|
2001-06-02 10:47:40 +02:00
|
|
|
pthread_mutex_unlock(&mutex);
|
2001-09-23 14:19:39 +02:00
|
|
|
}
|
2001-06-02 10:47:40 +02:00
|
|
|
}
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
// --- cThread ---------------------------------------------------------------
|
|
|
|
|
|
|
|
// The signal handler is necessary to be able to use SIGIO to wake up any
|
|
|
|
// pending 'select()' call.
|
|
|
|
|
2001-07-12 10:27:18 +02:00
|
|
|
time_t cThread::lastPanic = 0;
|
|
|
|
int cThread::panicLevel = 0;
|
2000-10-08 09:25:20 +02:00
|
|
|
bool cThread::signalHandlerInstalled = false;
|
2001-06-02 10:47:40 +02:00
|
|
|
bool cThread::emergencyExitRequested = false;
|
2000-10-08 09:25:20 +02:00
|
|
|
|
|
|
|
cThread::cThread(void)
|
|
|
|
{
|
|
|
|
if (!signalHandlerInstalled) {
|
|
|
|
signal(SIGIO, SignalHandler);
|
|
|
|
signalHandlerInstalled = true;
|
|
|
|
}
|
|
|
|
running = false;
|
2001-10-27 13:23:06 +02:00
|
|
|
parentPid = threadPid = 0;
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cThread::~cThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::SignalHandler(int signum)
|
|
|
|
{
|
|
|
|
signal(signum, SignalHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *cThread::StartThread(cThread *Thread)
|
|
|
|
{
|
2000-12-08 16:23:32 +01:00
|
|
|
Thread->threadPid = getpid();
|
2000-10-08 09:25:20 +02:00
|
|
|
Thread->Action();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cThread::Start(void)
|
|
|
|
{
|
|
|
|
if (!running) {
|
|
|
|
running = true;
|
|
|
|
parentPid = getpid();
|
2000-11-22 17:11:04 +01:00
|
|
|
pthread_create(&thread, NULL, (void *(*) (void *))&StartThread, (void *)this);
|
2001-08-03 14:18:08 +02:00
|
|
|
pthread_setschedparam(thread, SCHED_RR, 0);
|
2000-12-28 12:57:16 +01:00
|
|
|
usleep(10000); // otherwise calling Active() immediately after Start() causes a "pure virtual method called" error
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
return true; //XXX return value of pthread_create()???
|
|
|
|
}
|
|
|
|
|
2000-12-08 16:23:32 +01:00
|
|
|
bool cThread::Active(void)
|
2000-10-08 09:25:20 +02:00
|
|
|
{
|
2000-12-08 16:23:32 +01:00
|
|
|
if (threadPid) {
|
|
|
|
if (kill(threadPid, SIGIO) < 0) { // couldn't find another way of checking whether the thread is still running - any ideas?
|
|
|
|
if (errno == ESRCH)
|
|
|
|
threadPid = 0;
|
|
|
|
else
|
|
|
|
LOG_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::Cancel(int WaitSeconds)
|
|
|
|
{
|
2002-06-16 12:57:31 +02:00
|
|
|
running = false;
|
2000-12-08 16:23:32 +01:00
|
|
|
if (WaitSeconds > 0) {
|
|
|
|
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
|
|
|
|
if (!Active())
|
|
|
|
return;
|
|
|
|
usleep(10000);
|
|
|
|
}
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: thread %d won't end (waited %d seconds) - cancelling it...", threadPid, WaitSeconds);
|
2000-12-08 16:23:32 +01:00
|
|
|
}
|
2000-10-08 16:49:41 +02:00
|
|
|
pthread_cancel(thread);
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::WakeUp(void)
|
|
|
|
{
|
|
|
|
kill(parentPid, SIGIO); // makes any waiting 'select()' call return immediately
|
|
|
|
}
|
|
|
|
|
2001-07-12 10:27:18 +02:00
|
|
|
#define MAXPANICLEVEL 10
|
|
|
|
|
|
|
|
void cThread::RaisePanic(void)
|
|
|
|
{
|
|
|
|
if (lastPanic > 0) {
|
|
|
|
if (time(NULL) - lastPanic < 5)
|
|
|
|
panicLevel++;
|
|
|
|
else if (panicLevel > 0)
|
|
|
|
panicLevel--;
|
|
|
|
}
|
|
|
|
lastPanic = time(NULL);
|
|
|
|
if (panicLevel > MAXPANICLEVEL) {
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: max. panic level exceeded");
|
2001-07-12 10:27:18 +02:00
|
|
|
EmergencyExit(true);
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("panic level: %d", panicLevel);
|
2001-07-12 10:27:18 +02:00
|
|
|
}
|
|
|
|
|
2001-06-02 10:47:40 +02:00
|
|
|
bool cThread::EmergencyExit(bool Request)
|
|
|
|
{
|
|
|
|
if (!Request)
|
|
|
|
return emergencyExitRequested;
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("initiating emergency exit");
|
2001-06-02 10:47:40 +02:00
|
|
|
return emergencyExitRequested = true; // yes, it's an assignment, not a comparison!
|
|
|
|
}
|
|
|
|
|
2002-02-23 13:55:57 +01:00
|
|
|
// --- cMutexLock ------------------------------------------------------------
|
|
|
|
|
|
|
|
cMutexLock::cMutexLock(cMutex *Mutex)
|
|
|
|
{
|
|
|
|
mutex = NULL;
|
|
|
|
locked = false;
|
|
|
|
Lock(Mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
cMutexLock::~cMutexLock()
|
|
|
|
{
|
|
|
|
if (mutex && locked)
|
|
|
|
mutex->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cMutexLock::Lock(cMutex *Mutex)
|
|
|
|
{
|
|
|
|
if (Mutex && !mutex) {
|
|
|
|
mutex = Mutex;
|
|
|
|
Mutex->Lock();
|
|
|
|
locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
// --- cThreadLock -----------------------------------------------------------
|
|
|
|
|
|
|
|
cThreadLock::cThreadLock(cThread *Thread)
|
|
|
|
{
|
2000-10-29 13:17:22 +01:00
|
|
|
thread = NULL;
|
|
|
|
locked = false;
|
|
|
|
Lock(Thread);
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cThreadLock::~cThreadLock()
|
|
|
|
{
|
2000-10-29 13:17:22 +01:00
|
|
|
if (thread && locked)
|
2000-10-08 09:25:20 +02:00
|
|
|
thread->Unlock();
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
bool cThreadLock::Lock(cThread *Thread)
|
|
|
|
{
|
|
|
|
if (Thread && !thread) {
|
|
|
|
thread = Thread;
|
2001-10-27 13:23:06 +02:00
|
|
|
Thread->Lock();
|
|
|
|
locked = true;
|
|
|
|
return true;
|
2000-10-29 13:17:22 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-09-15 13:00:58 +02:00
|
|
|
// --- cPipe -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// cPipe::Open() and cPipe::Close() are based on code originally received from
|
|
|
|
// Andreas Vitting <Andreas@huji.de>
|
|
|
|
|
|
|
|
cPipe::cPipe(void)
|
|
|
|
{
|
|
|
|
pid = -1;
|
|
|
|
f = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cPipe::~cPipe()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPipe::Open(const char *Command, const char *Mode)
|
|
|
|
{
|
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
if (pipe(fd) < 0) {
|
|
|
|
LOG_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((pid = fork()) < 0) { // fork failed
|
|
|
|
LOG_ERROR;
|
|
|
|
close(fd[0]);
|
|
|
|
close(fd[1]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *mode = "w";
|
|
|
|
int iopipe = 0;
|
|
|
|
|
|
|
|
if (pid > 0) { // parent process
|
|
|
|
if (strcmp(Mode, "r") == 0) {
|
|
|
|
mode = "r";
|
|
|
|
iopipe = 1;
|
|
|
|
}
|
|
|
|
close(fd[iopipe]);
|
|
|
|
f = fdopen(fd[1 - iopipe], mode);
|
|
|
|
if ((f = fdopen(fd[1 - iopipe], mode)) == NULL) {
|
|
|
|
LOG_ERROR;
|
|
|
|
close(fd[1 - iopipe]);
|
|
|
|
}
|
|
|
|
return f != NULL;
|
|
|
|
}
|
|
|
|
else { // child process
|
|
|
|
int iofd = STDOUT_FILENO;
|
|
|
|
if (strcmp(Mode, "w") == 0) {
|
|
|
|
mode = "r";
|
|
|
|
iopipe = 1;
|
|
|
|
iofd = STDIN_FILENO;
|
|
|
|
}
|
|
|
|
close(fd[iopipe]);
|
|
|
|
if (dup2(fd[1 - iopipe], iofd) == -1) { // now redirect
|
|
|
|
LOG_ERROR;
|
|
|
|
close(fd[1 - iopipe]);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
else {
|
2002-02-17 14:51:29 +01:00
|
|
|
for (int i = 0; i <= fd[1]; i++) {
|
|
|
|
if (i == STDIN_FILENO || i == STDOUT_FILENO || i == STDERR_FILENO)
|
|
|
|
continue;
|
|
|
|
close(i); // close all dup'ed filedescriptors
|
|
|
|
}
|
2001-09-15 13:00:58 +02:00
|
|
|
if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) {
|
|
|
|
LOG_ERROR_STR(Command);
|
|
|
|
close(fd[1 - iopipe]);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cPipe::Close(void)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
|
|
|
f = NULL;
|
|
|
|
}
|
|
|
|
|
2002-03-09 11:51:56 +01:00
|
|
|
if (pid > 0) {
|
2001-09-15 13:00:58 +02:00
|
|
|
int status = 0;
|
|
|
|
int i = 5;
|
2002-03-09 11:51:56 +01:00
|
|
|
while (i > 0) {
|
|
|
|
ret = waitpid(pid, &status, WNOHANG);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != EINTR && errno != ECHILD) {
|
|
|
|
LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ret == pid)
|
|
|
|
break;
|
2001-09-15 13:00:58 +02:00
|
|
|
i--;
|
2002-03-09 11:51:56 +01:00
|
|
|
usleep(100000);
|
2001-09-15 13:00:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!i) {
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (ret == -1 || !WIFEXITED(status))
|
|
|
|
ret = -1;
|
|
|
|
pid = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2001-10-20 10:39:27 +02:00
|
|
|
|
|
|
|
// --- SystemExec ------------------------------------------------------------
|
|
|
|
|
|
|
|
int SystemExec(const char *Command)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if ((pid = fork()) < 0) { // fork failed
|
|
|
|
LOG_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid > 0) { // parent process
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) < 0) {
|
|
|
|
LOG_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
else { // child process
|
|
|
|
int MaxPossibleFileDescriptors = getdtablesize();
|
|
|
|
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
|
|
|
|
close(i); //close all dup'ed filedescriptors
|
|
|
|
if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) {
|
|
|
|
LOG_ERROR_STR(Command);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|