SASC-NG
DVBSoftCAM

thread.cpp File Reference

#include "include/vdr/thread.h"
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "include/vdr/tools.h"

Go to the source code of this file.

Defines

#define THREAD_STOP_TIMEOUT   3000
#define THREAD_STOP_SLEEP   30

Functions

static bool GetAbsTime (struct timespec *Abstime, int MillisecondsFromNow)
int SystemExec (const char *Command, bool Detached)

Define Documentation

#define THREAD_STOP_SLEEP   30

Definition at line 246 of file thread.cpp.

Referenced by cThread::Start().

#define THREAD_STOP_TIMEOUT   3000

Definition at line 245 of file thread.cpp.

Referenced by cThread::Start().


Function Documentation

static bool GetAbsTime ( struct timespec *  Abstime,
int  MillisecondsFromNow 
) [static]

Definition at line 21 of file thread.cpp.

References NULL.

Referenced by cRwLock::Lock(), cCondVar::TimedWait(), and cCondWait::Wait().

{
  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;
}
int SystemExec ( const char *  Command,
bool  Detached 
)

Definition at line 487 of file thread.cpp.

References i, LOG_ERROR, LOG_ERROR_STR, NULL, and open().

{
  pid_t pid;

  if ((pid = fork()) < 0) { // fork failed
     LOG_ERROR;
     return -1;
     }

  if (pid > 0) { // parent process
     int status = 0;
     if (waitpid(pid, &status, 0) < 0) {
        LOG_ERROR;
        return -1;
        }
     return status;
     }
  else { // child process
     if (Detached) {
        // Fork again and let first child die - grandchild stays alive without parent
        if (fork() > 0)
           exit(0);
        // Start a new session
        pid_t sid = setsid();
        if (sid < 0)
           LOG_ERROR;
        // close STDIN and re-open as /dev/null
        int devnull = open("/dev/null", O_RDONLY);
        if (devnull < 0 || dup2(devnull, 0) < 0)
           LOG_ERROR;
        }
     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);
     }
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines