SASC-NG
DVBSoftCAM

thread.h File Reference

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>

Go to the source code of this file.

Classes

class  cCondWait
class  cCondVar
class  cRwLock
class  cMutex
class  cThread
class  cMutexLock
class  cThreadLock
class  cPipe

Defines

#define LOCK_THREAD   cThreadLock ThreadLock(this)

Typedefs

typedef pid_t tThreadId

Functions

int SystemExec (const char *Command, bool Detached=false)

Define Documentation

#define LOCK_THREAD   cThreadLock ThreadLock(this)

Definition at line 157 of file thread.h.


Typedef Documentation

typedef pid_t tThreadId

Definition at line 75 of file thread.h.


Function Documentation

int SystemExec ( const char *  Command,
bool  Detached = false 
)

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