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.
|
|
|
|
*
|
2006-06-02 13:54:04 +02:00
|
|
|
* $Id: thread.c 1.55 2006/06/02 13:51:39 kls Exp $
|
2000-10-08 09:25:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "thread.h"
|
2000-12-08 16:23:32 +01:00
|
|
|
#include <errno.h>
|
2006-01-08 16:05:23 +01:00
|
|
|
#include <linux/unistd.h>
|
2003-10-18 12:29:08 +02:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdarg.h>
|
2001-09-15 13:00:58 +02:00
|
|
|
#include <sys/resource.h>
|
2005-12-11 12:10:28 +01:00
|
|
|
#include <sys/syscall.h>
|
2002-08-15 11:46:22 +02:00
|
|
|
#include <sys/time.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
|
|
|
|
2005-05-06 14:43:17 +02:00
|
|
|
static bool GetAbsTime(struct timespec *Abstime, int MillisecondsFromNow)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
if (gettimeofday(&now, NULL) == 0) { // get current time
|
|
|
|
now.tv_usec += MillisecondsFromNow * 1000; // add the timeout
|
|
|
|
while (now.tv_usec >= 1000000) { // take care of an overflow
|
|
|
|
now.tv_sec++;
|
|
|
|
now.tv_usec -= 1000000;
|
|
|
|
}
|
|
|
|
Abstime->tv_sec = now.tv_sec; // seconds
|
|
|
|
Abstime->tv_nsec = now.tv_usec * 1000; // nano seconds
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
// --- cCondWait -------------------------------------------------------------
|
|
|
|
|
|
|
|
cCondWait::cCondWait(void)
|
|
|
|
{
|
|
|
|
signaled = false;
|
|
|
|
pthread_mutex_init(&mutex, NULL);
|
|
|
|
pthread_cond_init(&cond, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
cCondWait::~cCondWait()
|
|
|
|
{
|
2004-10-31 09:54:50 +01:00
|
|
|
pthread_cond_broadcast(&cond); // wake up any sleepers
|
2004-10-16 09:36:28 +02:00
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
}
|
|
|
|
|
2004-10-24 11:12:05 +02:00
|
|
|
void cCondWait::SleepMs(int TimeoutMs)
|
|
|
|
{
|
|
|
|
cCondWait w;
|
2005-01-14 14:08:47 +01:00
|
|
|
w.Wait(max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait
|
2004-10-24 11:12:05 +02:00
|
|
|
}
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
bool cCondWait::Wait(int TimeoutMs)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
if (!signaled) {
|
|
|
|
if (TimeoutMs) {
|
2005-05-06 14:43:17 +02:00
|
|
|
struct timespec abstime;
|
|
|
|
if (GetAbsTime(&abstime, TimeoutMs)) {
|
2004-10-16 09:36:28 +02:00
|
|
|
while (!signaled) {
|
|
|
|
if (pthread_cond_timedwait(&cond, &mutex, &abstime) == ETIMEDOUT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pthread_cond_wait(&cond, &mutex);
|
|
|
|
}
|
|
|
|
bool r = signaled;
|
|
|
|
signaled = false;
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCondWait::Signal(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
signaled = true;
|
2004-10-31 09:54:50 +01:00
|
|
|
pthread_cond_broadcast(&cond);
|
2004-10-16 09:36:28 +02:00
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
|
2001-08-03 14:18:08 +02:00
|
|
|
// --- cCondVar --------------------------------------------------------------
|
|
|
|
|
|
|
|
cCondVar::cCondVar(void)
|
|
|
|
{
|
|
|
|
pthread_cond_init(&cond, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
cCondVar::~cCondVar()
|
|
|
|
{
|
2004-10-31 09:54:50 +01:00
|
|
|
pthread_cond_broadcast(&cond); // wake up any sleepers
|
2001-08-03 14:18:08 +02:00
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
}
|
|
|
|
|
2002-08-15 11:46:22 +02:00
|
|
|
void cCondVar::Wait(cMutex &Mutex)
|
2001-08-03 14:18:08 +02:00
|
|
|
{
|
2003-10-18 13:20:01 +02:00
|
|
|
if (Mutex.locked) {
|
2002-08-15 11:46:22 +02:00
|
|
|
int locked = Mutex.locked;
|
|
|
|
Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_wait
|
2006-01-28 11:34:35 +01:00
|
|
|
// does an implicit unlock of the mutex
|
2002-08-15 11:46:22 +02:00
|
|
|
pthread_cond_wait(&cond, &Mutex.mutex);
|
|
|
|
Mutex.locked = locked;
|
|
|
|
}
|
2001-08-03 14:18:08 +02:00
|
|
|
}
|
|
|
|
|
2002-08-15 11:46:22 +02:00
|
|
|
bool cCondVar::TimedWait(cMutex &Mutex, int TimeoutMs)
|
2001-08-03 14:18:08 +02:00
|
|
|
{
|
2006-01-28 11:34:35 +01:00
|
|
|
bool r = true; // true = condition signaled, false = timeout
|
2002-08-15 11:46:22 +02:00
|
|
|
|
2003-10-18 13:20:01 +02:00
|
|
|
if (Mutex.locked) {
|
2005-05-06 14:43:17 +02:00
|
|
|
struct timespec abstime;
|
|
|
|
if (GetAbsTime(&abstime, TimeoutMs)) {
|
2002-08-15 11:46:22 +02:00
|
|
|
int locked = Mutex.locked;
|
|
|
|
Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_timedwait
|
2006-01-28 11:34:35 +01:00
|
|
|
// does an implicit unlock of the mutex.
|
2002-08-15 11:46:22 +02:00
|
|
|
if (pthread_cond_timedwait(&cond, &Mutex.mutex, &abstime) == ETIMEDOUT)
|
|
|
|
r = false;
|
|
|
|
Mutex.locked = locked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
2001-08-03 14:18:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCondVar::Broadcast(void)
|
|
|
|
{
|
|
|
|
pthread_cond_broadcast(&cond);
|
|
|
|
}
|
|
|
|
|
2004-01-04 12:30:00 +01:00
|
|
|
// --- cRwLock ---------------------------------------------------------------
|
2003-12-22 13:29:24 +01:00
|
|
|
|
2004-01-04 12:30:00 +01:00
|
|
|
cRwLock::cRwLock(bool PreferWriter)
|
2003-12-22 13:29:24 +01:00
|
|
|
{
|
2006-01-01 14:53:03 +01:00
|
|
|
pthread_rwlockattr_t attr;
|
|
|
|
pthread_rwlockattr_init(&attr);
|
|
|
|
pthread_rwlockattr_setkind_np(&attr, PreferWriter ? PTHREAD_RWLOCK_PREFER_WRITER_NP : PTHREAD_RWLOCK_PREFER_READER_NP);
|
2003-12-22 13:29:24 +01:00
|
|
|
pthread_rwlock_init(&rwlock, &attr);
|
|
|
|
}
|
|
|
|
|
2004-01-04 12:30:00 +01:00
|
|
|
cRwLock::~cRwLock()
|
2003-12-22 13:29:24 +01:00
|
|
|
{
|
|
|
|
pthread_rwlock_destroy(&rwlock);
|
|
|
|
}
|
|
|
|
|
2004-01-04 12:30:00 +01:00
|
|
|
bool cRwLock::Lock(bool Write, int TimeoutMs)
|
2003-12-22 13:29:24 +01:00
|
|
|
{
|
|
|
|
int Result = 0;
|
|
|
|
struct timespec abstime;
|
|
|
|
if (TimeoutMs) {
|
2005-05-06 14:43:17 +02:00
|
|
|
if (!GetAbsTime(&abstime, TimeoutMs))
|
|
|
|
TimeoutMs = 0;
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
if (Write)
|
|
|
|
Result = TimeoutMs ? pthread_rwlock_timedwrlock(&rwlock, &abstime) : pthread_rwlock_wrlock(&rwlock);
|
|
|
|
else
|
|
|
|
Result = TimeoutMs ? pthread_rwlock_timedrdlock(&rwlock, &abstime) : pthread_rwlock_rdlock(&rwlock);
|
|
|
|
return Result == 0;
|
|
|
|
}
|
|
|
|
|
2004-01-04 12:30:00 +01:00
|
|
|
void cRwLock::Unlock(void)
|
2003-12-22 13:29:24 +01:00
|
|
|
{
|
|
|
|
pthread_rwlock_unlock(&rwlock);
|
|
|
|
}
|
|
|
|
|
2001-06-02 10:47:40 +02:00
|
|
|
// --- cMutex ----------------------------------------------------------------
|
|
|
|
|
|
|
|
cMutex::cMutex(void)
|
|
|
|
{
|
|
|
|
locked = 0;
|
2006-01-01 14:53:03 +01:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
|
2003-10-18 13:20:01 +02:00
|
|
|
pthread_mutex_init(&mutex, &attr);
|
2001-06-02 10:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cMutex::~cMutex()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cMutex::Lock(void)
|
|
|
|
{
|
2003-10-18 13:20:01 +02:00
|
|
|
pthread_mutex_lock(&mutex);
|
2001-06-02 10:47:40 +02:00
|
|
|
locked++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cMutex::Unlock(void)
|
|
|
|
{
|
2003-10-18 13:20:01 +02:00
|
|
|
if (!--locked)
|
2001-06-02 10:47:40 +02:00
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
// --- cThread ---------------------------------------------------------------
|
|
|
|
|
2006-01-03 10:20:41 +01:00
|
|
|
tThreadId cThread::mainThreadId = 0;
|
2001-06-02 10:47:40 +02:00
|
|
|
bool cThread::emergencyExitRequested = false;
|
2000-10-08 09:25:20 +02:00
|
|
|
|
2003-10-18 12:29:08 +02:00
|
|
|
cThread::cThread(const char *Description)
|
2000-10-08 09:25:20 +02:00
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
active = running = false;
|
2004-12-19 10:58:20 +01:00
|
|
|
childTid = 0;
|
2006-01-04 15:01:22 +01:00
|
|
|
childThreadId = 0;
|
2003-10-18 12:29:08 +02:00
|
|
|
description = NULL;
|
2006-03-26 09:27:30 +02:00
|
|
|
if (Description)
|
|
|
|
SetDescription("%s", Description);
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cThread::~cThread()
|
|
|
|
{
|
2005-08-13 13:17:24 +02:00
|
|
|
Cancel(); // just in case the derived class didn't call it
|
2003-10-18 12:29:08 +02:00
|
|
|
free(description);
|
|
|
|
}
|
|
|
|
|
2005-05-29 11:44:52 +02:00
|
|
|
void cThread::SetPriority(int Priority)
|
|
|
|
{
|
|
|
|
if (setpriority(PRIO_PROCESS, 0, Priority) < 0)
|
|
|
|
LOG_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-10-18 12:29:08 +02:00
|
|
|
void cThread::SetDescription(const char *Description, ...)
|
|
|
|
{
|
|
|
|
free(description);
|
|
|
|
description = NULL;
|
|
|
|
if (Description) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Description);
|
|
|
|
vasprintf(&description, Description, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *cThread::StartThread(cThread *Thread)
|
|
|
|
{
|
2006-01-04 15:01:22 +01:00
|
|
|
Thread->childThreadId = ThreadId();
|
2003-10-18 12:29:08 +02:00
|
|
|
if (Thread->description)
|
2006-01-04 15:01:22 +01:00
|
|
|
dsyslog("%s thread started (pid=%d, tid=%d)", Thread->description, getpid(), Thread->childThreadId);
|
2000-10-08 09:25:20 +02:00
|
|
|
Thread->Action();
|
2003-10-18 12:29:08 +02:00
|
|
|
if (Thread->description)
|
2006-01-04 15:01:22 +01:00
|
|
|
dsyslog("%s thread ended (pid=%d, tid=%d)", Thread->description, getpid(), Thread->childThreadId);
|
2004-12-19 10:58:20 +01:00
|
|
|
Thread->running = false;
|
2005-08-14 11:24:57 +02:00
|
|
|
Thread->active = false;
|
2000-10-08 09:25:20 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cThread::Start(void)
|
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
if (!active) {
|
|
|
|
active = running = true;
|
2004-12-19 10:58:20 +01:00
|
|
|
if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) {
|
|
|
|
pthread_detach(childTid); // auto-reap
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_ERROR;
|
2005-08-14 11:24:57 +02:00
|
|
|
active = running = false;
|
2004-12-19 10:58:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
2004-12-19 10:58:20 +01:00
|
|
|
return true;
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
2005-08-14 11:24:57 +02:00
|
|
|
bool cThread::Active(void)
|
2000-10-08 09:25:20 +02:00
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
if (active) {
|
2003-10-18 11:14:33 +02:00
|
|
|
//
|
|
|
|
// Single UNIX Spec v2 says:
|
|
|
|
//
|
|
|
|
// The pthread_kill() function is used to request
|
|
|
|
// that a signal be delivered to the specified thread.
|
|
|
|
//
|
|
|
|
// As in kill(), if sig is zero, error checking is
|
|
|
|
// performed but no signal is actually sent.
|
|
|
|
//
|
|
|
|
int err;
|
|
|
|
if ((err = pthread_kill(childTid, 0)) != 0) {
|
|
|
|
if (err != ESRCH)
|
2000-12-08 16:23:32 +01:00
|
|
|
LOG_ERROR;
|
2003-10-18 11:14:33 +02:00
|
|
|
childTid = 0;
|
2005-08-14 11:24:57 +02:00
|
|
|
active = running = false;
|
2000-12-08 16:23:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::Cancel(int WaitSeconds)
|
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
running = false;
|
|
|
|
if (active) {
|
2004-10-24 10:34:20 +02:00
|
|
|
if (WaitSeconds > 0) {
|
|
|
|
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
|
2005-08-14 11:24:57 +02:00
|
|
|
if (!Active())
|
2004-10-24 10:34:20 +02:00
|
|
|
return;
|
2004-10-24 11:12:05 +02:00
|
|
|
cCondWait::SleepMs(10);
|
2004-10-24 10:34:20 +02:00
|
|
|
}
|
2006-02-12 12:26:06 +01:00
|
|
|
esyslog("ERROR: %s thread %d won't end (waited %d seconds) - canceling it...", description ? description : "", childThreadId, WaitSeconds);
|
2004-10-24 10:34:20 +02:00
|
|
|
}
|
2004-12-19 10:58:20 +01:00
|
|
|
pthread_cancel(childTid);
|
|
|
|
childTid = 0;
|
2005-08-14 11:24:57 +02:00
|
|
|
active = false;
|
2000-12-08 16:23:32 +01:00
|
|
|
}
|
2000-10-08 09:25:20 +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!
|
|
|
|
}
|
|
|
|
|
2005-12-11 12:10:28 +01:00
|
|
|
_syscall0(pid_t, gettid)
|
|
|
|
|
|
|
|
tThreadId cThread::ThreadId(void)
|
|
|
|
{
|
|
|
|
return gettid();
|
|
|
|
}
|
|
|
|
|
2006-01-03 10:20:41 +01:00
|
|
|
void cThread::SetMainThreadId(void)
|
|
|
|
{
|
|
|
|
if (mainThreadId == 0)
|
|
|
|
mainThreadId = ThreadId();
|
|
|
|
else
|
|
|
|
esyslog("ERROR: attempt to set main thread id to %d while it already is %d", ThreadId(), mainThreadId);
|
|
|
|
}
|
|
|
|
|
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-11-02 14:00:25 +01:00
|
|
|
int MaxPossibleFileDescriptors = getdtablesize();
|
|
|
|
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
|
|
|
|
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--;
|
2004-10-24 11:12:05 +02:00
|
|
|
cCondWait::SleepMs(100);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|