Logo Search packages:      
Sourcecode: yate version File versions  Download package

int Engine::main ( int  argc,
const char **  argv,
const char **  environ 
) [static]

Main entry point to be called directly from a wrapper program

Parameters:
argc Argument count
argv Argument array
environ Environment variables
Returns:
Program exit code

Definition at line 613 of file Engine.cpp.

References TelEngine::abortOnBug(), TelEngine::ObjList::append(), TelEngine::debugLevel(), TelEngine::Debugger::enableOutput(), TelEngine::Output(), run(), and TelEngine::setDebugTimestamp().

{
    bool daemonic = false;
    bool supervised = false;
    bool tstamp = false;
    int debug_level = debugLevel();
    const char *logfile = 0;
    const char *pidfile = 0;

    s_cfgfile = ::strrchr(argv[0],'/');
    if (s_cfgfile)
      s_cfgfile++;
    else
      s_cfgfile = "yate";

    int i;
    bool inopt = true;
    for (i=1;i<argc;i++) {
      const char *pc = argv[i];
      if (inopt && (pc[0] == '-') && pc[1]) {
          while (pc && *++pc) {
            switch (*pc) {
                case '-':
                  if (!*++pc) {
                      inopt=false;
                      pc=0;
                      continue;
                  }
                  if (!::strcmp(pc,"help")) {
                      usage(stdout);
                      return 0;
                  }
                  badopt(0,argv[i]);
                  return EINVAL;
                  break;
                case 'h':
                  usage(stdout);
                  return 0;
                case 'v':
                  debug_level++;
                  break;
                case 'q':
                  debug_level--;
                  break;
                case 'd':
                  daemonic = true;
                  break;
                case 's':
                  supervised = true;
                  break;
                case 'l':
                  if (i+1 >= argc) {
                      noarg(argv[i]);
                      return ENOENT;
                  }
                  pc = 0;
                  logfile=argv[++i];
                  break;
                case 'p':
                  if (i+1 >= argc) {
                      noarg(argv[i]);
                      return ENOENT;
                  }
                  pc = 0;
                  pidfile=argv[++i];
                  break;
                case 'n':
                  if (i+1 >= argc) {
                      noarg(argv[i]);
                      return ENOENT;
                  }
                  pc = 0;
                  s_cfgfile=argv[++i];
                  break;
                case 'c':
                  if (i+1 >= argc) {
                      noarg(argv[i]);
                      return ENOENT;
                  }
                  pc = 0;
                  s_cfgpath=argv[++i];
                  break;
                case 'm':
                  if (i+1 >= argc) {
                      noarg(argv[i]);
                      return ENOENT;
                  }
                  pc = 0;
                  s_modpath=argv[++i];
                  break;
#ifndef NDEBUG
                case 'D':
                  while (*++pc) {
                      switch (*pc) {
                        case 'a':
                            s_sigabrt = true;
                            break;
                        case 'c':
                            s_keepclosing = true;
                            break;
                        case 'i':
                            s_init = true;
                            break;
                        case 'x':
                            s_haltcode++;
                            break;
                        case 'w':
                            s_makeworker = false;
                            break;
                        case 't':
                            tstamp = true;
                            break;
                        default:
                            badopt(*pc,argv[i]);
                            return EINVAL;
                      }
                  }
                  pc = 0;
                  break;
#endif
                default:
                  badopt(*pc,argv[i]);
                  return EINVAL;
            }
          }
      }
      else {
          if (!s_cmds)
            s_cmds = new ObjList;
          s_cmds->append(new String(argv[i]));
      }
    }

    if (daemonic) {
      Debugger::enableOutput(false);
      // Make sure X client modules fail initialization in daemon mode
      ::unsetenv("DISPLAY");
      if (::daemon(1,0) == -1) {
          int err = errno;
          ::fprintf(stderr,"Daemonification failed: %s (%d)\n",::strerror(err),err);
          return err;
      }
    }

    if (pidfile) {
      int fd = ::open(pidfile,O_WRONLY|O_CREAT,0644);
      if (fd >= 0) {
          char pid[32];
          ::snprintf(pid,sizeof(pid),"%u\n",::getpid());
          ::write(fd,pid,::strlen(pid));
          ::close(fd);
      }
    }

    if (logfile) {
      int fd = ::open(logfile,O_WRONLY|O_CREAT|O_APPEND,0640);
      if (fd >= 0) {
          // Redirect stdout and stderr to the new file
          ::fflush(stdout);
          ::dup2(fd,1);
          ::fflush(stderr);
          ::dup2(fd,2);
          ::close(fd);
          Debugger::enableOutput(true);
      }
    }

    debugLevel(debug_level);
    abortOnBug(s_sigabrt);

    int retcode = supervised ? supervise() : -1;
    if (retcode >= 0)
      return retcode;

    if (tstamp)
      setDebugTimestamp();
    time_t t = ::time(0);
    Output("Yate (%u) is starting %s",::getpid(),::ctime(&t));
    retcode = self()->run();
    t = ::time(0);
    Output("Yate (%u) is stopping %s",::getpid(),::ctime(&t));
    return retcode;
}


Generated by  Doxygen 1.6.0   Back to index