int sortOutPidFile(char *progName)
{
  
  int retVal=checkPidFile(OPENPORTD_PID_FILE);
  if(retVal) {
    fprintf(stderr,"%s already running (%d)\nRemove pidFile to over ride (%s)\n",progName,retVal,OPENPORTD_PID_FILE);
    syslog(LOG_ERR,"%s already running (%d)\n",progName,retVal);
    return -1;
  }
  writePidFile(OPENPORTD_PID_FILE);
  return 0;
}
Example #2
0
int initService(int argc, char *argv[])
{
    int i;
    char *optpattern = "hl:p:s:e";
    char *prog = argv[0];
    char *p = NULL;
    string logDir = "/tmp";
    int logLevel = Log::INFORMATION;
    int logMode = Log::DISABLE;
#if defined(_SCI_LINUX) || defined(__APPLE__)
    string pidDir = "/var/run/";
#else
    string pidDir = "/var/opt/";
#endif
    string logFile;

    extern char *optarg;
    extern int  optind;
    p = strrchr(prog, '/');
    if (p != NULL) 
        p++;
    else
        p = prog;
    
    while ((i = getopt(argc, argv, optpattern)) != EOF) {
        switch (i) {
            case 'l':
                logDir = optarg;
                break ;
            case 'p':
                pidDir = optarg;
                break;
            case 's':
                logLevel = atoi(optarg);
                break;
            case 'e':
                logMode = Log::ENABLE;
                break;
            case 'h':
                usage(p);
                exit(0);
                break;
        }
    }
    pidFile = pidDir + "/" + p + ".pid";
    if (checkPidFile(pidFile) < 0) {
        printf("%s is already running...\n", p);
        return -1;
    }
    if (getuid() != 0) {
        printf("Must running as root\n");
        return -1;
    }
    daemonInit();
    writePidFile(pidFile);

    logFile = string(p) + ".log"; 
    Log::getInstance()->init(logDir.c_str(), logFile.c_str(), logLevel, logMode);

    return 0;
}
Example #3
0
int main(int argc, char **argv)
{
    if(!QFile::exists(QString(CAPTURE_LOGGING_FILE)))
    {
        printf("Logging conf file is not exists, %s.", CAPTURE_LOGGING_FILE);
        exit(-1);
    }

    el::Configurations c(CAPTURE_LOGGING_FILE);
    el::Loggers::reconfigureLogger("default", c);
    el::Loggers::reconfigureAllLoggers(c);

    static struct option long_options[] =
    {
        {"help",                no_argument,        0,  'h'},
        {"version",             no_argument,        0,  'v'},
        {"pid-file",            required_argument,  0,  'p'},
        {"load-timeout",        required_argument,  0,  't'},
        {"max-memory",          required_argument,  0,  'm'},
        {0, 0, 0, 0}
    };

    char *pidFile = NULL;
    int loadTimeout = CAPTURE_LOAD_TIMEOUT;
    qint64 maxMemory = CAPTURE_MAX_MEMORY;
    FILE *pid = NULL;

    int option_index = 0;
    int opt = 0;

    while(1)
    {
        opt = getopt_long(argc, argv, "hvp:t:m:", long_options, &option_index);
        if (opt == -1)
            break;

        switch(opt)
        {
            case 'h':
                printHelp(argv);
                break;
            case 'v':
                printVersion();
                break;
            case 'p':
                pidFile = checkPidFile(optarg, pid);
                break;
            case 't':
                loadTimeout = atoi(optarg) * 1000;
                break;
            case 'm':
                maxMemory = (qint64)atoi(optarg) * 1024 * 1024;
                break;
            default:

                break;
        }
    }

    LOG(DEBUG) << "Conf : load timeout " << loadTimeout << " ms";
    LOG(DEBUG) << "Conf : max memory " << loadTimeout << " bytes";

    QApplication app(argc, argv);
    QApplication::setApplicationName("screenshot");
    QApplication::setApplicationVersion("0.1");

    LOG(INFO) << "Start application";

    Redis redis("192.168.3.5", 6379);
    WebViewRemoveQue removeQue(5);
    WebViewRemover remover(&removeQue, 1000);
    remover.start();
    if(!redis.connect())
    {
        LOG(FATAL) << "Cannot connect to redis.";
        exit(-1);
    }

    LOG(INFO) << "Redis connected successfully.";
    Capture capture(&app, &redis, &removeQue, maxMemory, loadTimeout);
    capture.start();

    return app.exec();
}