Exemplo n.º 1
0
/**
 * Returns the process-ID of the multicast LDM sender corresponding to a
 * multicast group.
 *
 * @param[in] info  Information on the multicast group.
 * @retval    -1    Error. `log_add()` called.
 * @retval     0    No such process exists.
 * @return          The process-ID of the multicast LDM sender.
 */
static pid_t
getPidFromInfo(
    const McastInfo* const info)
{
    pid_t       pid;
    char* const pathname = getPidPathname(info);

    if (pathname == NULL) {
        pid = -1;
    }
    else {
        pid = getPidFromFile(pathname);
        free(pathname);
    }

    return pid;
}
Exemplo n.º 2
0
int CommandLine::run(int argc, char *argv[])
{
    int32_t ret = 0;
    int32_t err = 0;
    sigset_t oldMask;
    bool needWait = false;
    char pidFile[PATH_MAX + 1] = {0};
    char confName[PATH_MAX + 1] = {0};
    pid_t curPid = 0;
    Args args(argc, argv);
    char *pconf = get_realpath(args.getConfPath(), confName);
    if (_server) {
        fprintf(stderr, "%s is running.\n", _name);
        return -1;
    }
    if (args.isForHelp()) {
        help();
        return 0;
    }
    else if (args.isForLongVersion()) {
        showLongVersion();
        return 0;
    }
    else if (args.isForShortVersion()) {
        showShortVersion();
        return 0;
    }
    sigemptyset(&_mask);
    sigaddset(&_mask, SIGINT);
    sigaddset(&_mask, SIGTERM);
    sigaddset(&_mask, SIGUSR1);
    sigaddset(&_mask, SIGUSR2);
    sigaddset(&_mask, SIGPIPE);
    if ((err=pthread_sigmask(SIG_BLOCK, &_mask, &oldMask))!=0) {
        fprintf(stderr, "Set sigmask error.\n", _name);
        return -1;
    }
    //    register_sig();
    for (char *pTmp=pconf; *pTmp; pTmp++) {
        if (*pTmp == '/') {
            *pTmp='_';
        }
    }
    snprintf(pidFile, strlen(_name)+strlen(pidFileTMPL)+strlen(pconf)+1,
             pidFileTMPL, _name, pconf);
    curPid = getPidFromFile(pidFile);
    if (args.getCommandType() == cmd_unknown
            || strlen(args.getConfPath()) == 0)
    {
        help(-1);
        return -1;
    }
    else if (args.getCommandType() == cmd_stop) {
        if (curPid > 0) {
            if (kill(curPid, SIGTERM)!=0) {
                fprintf(stderr, "kill process error, pid = %ld\n", curPid);
            }
        }
        else {
            fprintf(stderr, "process not running.\n");
        }
        return -1;
    }
    else if (args.getCommandType() == cmd_restart) {
        if (curPid > 0) {
            if (kill(curPid, SIGTERM)!=0) {
                fprintf(stderr, "kill process error, pid = %ld\n", curPid);
                return -1;
            }
            needWait = true;
        }
        else {
            fprintf(stderr, "process not running.\n");
        }
    }
    else if (args.getCommandType() == cmd_start) {
        if (curPid > 0) {
            fprintf(stderr, "process already running."
                    "stop it first. pid=%ld, pidfile=%s\n", curPid, pidFile);
            return -1;
        }
    }
    _server = makeServer();
    if (unlikely(!_server)) {
        alog::Logger::shutdown();
        fprintf(stderr, "create server failed.\n");
    }
    if (args.asDaemon()) {
        if (daemon(1, 1) == -1) {
            fprintf(stderr, "set self process as daemon failed.\n");
        }
    }
    if ((args.getCommandType()==cmd_restart)&&(needWait)) {
        int errnum = 0;
        while (getPidFromFile(pidFile) >= 0) {
            if (errnum > 3000) {
                fprintf(stderr, "shutdown previous server failed: too slow\n");
                if (_server) {
                    delete _server;
                }
                alog::Logger::shutdown();
                return -1;
            }
            usleep(20);
            errnum++;
        }
    }
    if (updatePidFile(pidFile) < 0) {
        fprintf(stderr, "write pid to file %s error\n", pidFile);
        delete _server;
        _server = NULL;
        alog::Logger::shutdown();
        return -1;
    }
    if (strlen(args.getLogConfPath()) > 0) {
        try {
            alog::Configurator::configureLogger(args.getLogConfPath());
        } catch(std::exception &e) {
            fprintf(stderr, "config from '%s' failed, using default root.\n",
                    args.getLogConfPath());
            alog::Configurator::configureRootLogger();
        }
    } else {
        alog::Configurator::configureRootLogger();
    }
    ret = _server->start(args.getConfPath());
    if (ret != KS_SUCCESS) {
        fprintf(stderr, "startting server by `%s' failed(%d).\n",
                args.getConfPath(), ret);
        delete _server;
        _server = NULL;
        alog::Logger::shutdown();
        unlink(pidFile);
        return -2;
    }
    MUTEX_LOCK(G_CMD_LOCK);
    _next = G_CMD_LIST;
    G_CMD_LIST = this;
    MUTEX_UNLOCK(G_CMD_LOCK);
    waitSig();
    _server->wait();
    delete _server;
    _server = NULL;
    alog::Logger::shutdown();
    unlink(pidFile);
    MUTEX_LOCK(G_CMD_LOCK);
    if (G_CMD_LIST == this) {
        G_CMD_LIST = _next;
    }
    else {
        for (CommandLine *prev = G_CMD_LIST; prev; prev = prev->_next) {
            if (prev->_next == this) {
                prev->_next = _next;
                break;
            }
        }
    }
    MUTEX_UNLOCK(G_CMD_LOCK);
    return 0;
}