예제 #1
0
파일: lechod.c 프로젝트: houweifeng/sbase
int main(int argc, char **argv)
{
    pid_t pid;
    char *conf = NULL;

    /* get configure file */
    if(getopt(argc, argv, "c:") != 'c')
    {
        fprintf(stderr, "Usage:%s -c config_file\n", argv[0]);
        _exit(-1);
    }       
    conf = optarg;
    // locale
    setlocale(LC_ALL, "C");
    // signal
    signal(SIGTERM, &lechod_stop);
    signal(SIGINT,  &lechod_stop);
    signal(SIGHUP,  &lechod_stop);
    signal(SIGPIPE, SIG_IGN);
    pid = fork();
    switch (pid) {
        case -1:
            perror("fork()");
            exit(EXIT_FAILURE);
            break;
        case 0: //child process
            if(setsid() == -1)
                exit(EXIT_FAILURE);
            break;
        default://parent
            _exit(EXIT_SUCCESS);
            break;
    }


    if((sbase = sbase_init()) == NULL)
    {
        exit(EXIT_FAILURE);
        return -1;
    }
    fprintf(stdout, "Initializing from configure file:%s\n", conf);
    /* Initialize sbase */
    if(sbase_initialize(sbase, conf) != 0 )
    {
        fprintf(stderr, "Initialize from configure file failed\n");
        return -1;
    }
    fprintf(stdout, "Initialized successed\n");
    //sbase->running(sbase, 3600);
    sbase->running(sbase, 0);
}
예제 #2
0
파일: lhttpd.c 프로젝트: 5bruce/sbase
int main(int argc, char **argv)
{
    /* locale */
    setlocale(LC_ALL, "C");
    /* signal */
    signal(SIGTERM, &lhttpd_stop);
    signal(SIGINT,  &lhttpd_stop);
    signal(SIGHUP,  SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, 10240);
    if((sbase = sbase_init()) == NULL)
    {
        exit(EXIT_FAILURE);
    }
    if((lhttpd = service_init()) == NULL)
    {
        fprintf(stderr, "Initialize service failed, %s", strerror(errno));
        _exit(-1);
    }
    lhttpd->family = AF_INET;
    lhttpd->sock_type = SOCK_STREAM;
    lhttpd->ip = "0.0.0.0";
    lhttpd->port = 2080;
    lhttpd->use_cond_wait = 1;
    lhttpd->flag = SB_USE_COND|SB_USE_OUTDAEMON;
    lhttpd->working_mode = WORKING_THREAD;
    lhttpd->service_type = S_SERVICE;
    lhttpd->service_name = "lhttpd";
    lhttpd->nprocthreads = 8;
    lhttpd->niodaemons = 2;
    lhttpd->session.packet_type= PACKET_DELIMITER;
    lhttpd->session.packet_delimiter = "\r\n\r\n";
    lhttpd->session.packet_delimiter_length = strlen(lhttpd->session.packet_delimiter);
    lhttpd->session.buffer_size = SB_BUF_SIZE;
    lhttpd->session.packet_handler = &lhttpd_packet_handler;
    lhttpd->session.timeout_handler = &lhttpd_timeout_handler;
    lhttpd->set_log(lhttpd, "/tmp/lhttpd.log");
    lhttpd->set_log_level(lhttpd, 2);
    httpd_home = "/data/www";
    if(sbase->add_service(sbase, lhttpd) != 0)
    {
        fprintf(stderr, "add service lhttpd failed, %s\r\n", strerror(errno));
        _exit(-1);
    }
    fprintf(stdout, "Initialized successed\n");
    sbase->running(sbase, 0);
    //sbase->running(sbase, 60000000); sbase->stop(sbase);
    sbase->clean(sbase);
    return 0;
}
예제 #3
0
파일: nowd.c 프로젝트: 5bruce/sbase
int main(int argc, char **argv)
{
    pid_t pid;
    char *conf = NULL, *p = NULL, ch = 0;
    int is_daemon = 0;

    /* get configure file */
    while((ch = getopt(argc, argv, "c:d")) != (char)-1)
    {
        if(ch == 'c') conf = optarg;
        else if(ch == 'd') is_daemon = 1;
    }
    if(conf == NULL)
    {
        fprintf(stderr, "Usage:%s -d -c config_file\n", argv[0]);
        _exit(-1);
    }
    /* locale */
    setlocale(LC_ALL, "C");
    /* signal */
    signal(SIGTERM, &nowd_stop);
    signal(SIGINT,  &nowd_stop);
    signal(SIGHUP,  SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    //daemon
    if(is_daemon)
    {
        pid = fork();
        switch (pid) {
            case -1:
                perror("fork()");
                exit(EXIT_FAILURE);
                break;
            case 0: //child
                if(setsid() == -1)
                    exit(EXIT_FAILURE);
                break;
            default://parent
                _exit(EXIT_SUCCESS);
                break;
        }
    }
    setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, 10240);
    if((sbase = sbase_init()) == NULL)
    {
        exit(EXIT_FAILURE);
        return -1;
    }
    fprintf(stdout, "Initializing from configure file:%s\n", conf);
    /* Initialize sbase */
    if(sbase_initialize(sbase, conf) != 0 )
    {
        fprintf(stderr, "Initialize from configure file failed\n");
        exit(EXIT_FAILURE);
        return -1;
    }
    fprintf(stdout, "Initialized successed\n");
    sbase->running(sbase, 0);
    //sbase->running(sbase, 60000000); sbase->stop(sbase);
    //sbase->running(sbase, 90000000);sbase->stop(sbase);
    sbase->clean(sbase);
    if(dict)iniparser_free(dict);
    return 0;
}
예제 #4
0
파일: wbenchmark.c 프로젝트: 5bruce/sbase
int main(int argc, char **argv)
{
    pid_t pid;
    char *url = NULL, *urllist = NULL, line[HTTP_BUF_SIZE], *s = NULL, *p = NULL, ch = 0;
    struct hostent *hent = NULL;
    int n = 0, log_level = 0, tcp_option = 0, socket_option = 0, niodaemons = 0, is_realtime = 0;

    /* get configure file */
    while((ch = getopt(argc, argv, "vqpkdr:i:s:x:w:l:c:t:n:e:")) != -1)
    {
        switch(ch)
        {
            case 'c': 
                concurrency = atoi(optarg);
                break;
            case 'n':
                ntasks = atoi(optarg);
                break;
            case 'l':
                urllist = optarg;
                break;
            case 'k':
                is_keepalive = 1;
                break;
            case 'w':
                if((n = atoi(optarg)) > 0) workers = n;
                break;
            case 'd':
                is_daemon = 1;
                break;
            case 'q':
                is_quiet = 1;
                break;
            case 'r':
                is_realtime = atoi(optarg);
                break;
            case 't':
                req_timeout = atoi(optarg);
                break;
            case 'p':
                is_post = 1;
                break;
            case 'x':
                tcp_option = atoi(optarg);
                break;
            case 's':
                socket_option = atoi(optarg);
                break;
            case 'i':
                niodaemons = atoi(optarg);
                break;
            case 'e':
                log_level = atoi(optarg);
                break;
            case 'v':
                is_verbosity = 1;
                break;
            case '?':
                url = argv[optind];
                break;
            default:
                break;
        }
    }
    if(url == NULL && optind < argc)
    {
        //fprintf(stdout, "opt:%c optind:%d arg:%s\n", ch, optind, argv[optind]);
        url = argv[optind];
    }
    //fprintf(stdout, "concurrency:%d nrequests:%d is_keepalive:%d is_daemon:%d\n",
    //       concurrency, ntasks, is_keepalive, is_daemon);
    if(url == NULL)
    {
        fprintf(stderr, "Usage:%s [options] http(s)://host:port/path\n"
                "Options:\n\t-c concurrency\n\t-n requests\n"
                "\t-w worker threads\n\t-e log level\n\t-x tcp_option 1:tcp_nodelay\n"
                "\t-s socket_option 1:socket_linger\n\t-i iodaemons\n"
                "\t-t timeout (microseconds, default 1000000)\n"
                "\t-r is_realtime_thread 1:SCHED_FIFO 2:SCHED_RR\n"
                "\t-p is_POST\n\t-v is_verbosity\n\t-l urllist file\n"
                "\t-k is_keepalive\n\t-d is_daemon\n ", argv[0]);
        _exit(-1);
    }
    p = url;
    s = line;
    while(*p != '\0')
    {
        if(*p >= 'A' && *p <= 'Z')
        {
            *s++ = *p++ + 'a' - 'A';
        }
        else if(*((unsigned char *)p) > 127 || *p == 0x20)
        {
            s += sprintf(s, "%%%02x", *((unsigned char *)p));
            ++p;
        }
        else *s++ = *p++;
    }
    *s = '\0';
    s = line;
    if(strncmp(s, "http://", 7) == 0)
    {
        s += 7;
        server_host = s;
    }
    else if(strncmp(s, "https://", 8) == 0)
    {
        s += 8;
        server_host = s;
        server_is_ssl = 1;
    }
    else goto invalid_url;
    while(*s != '\0' && *s != ':' && *s != '/')s++;
    if(*s == ':')
    {
        *s = '\0';
        ++s;
        server_port = atoi(s);          
        while(*s != '\0' && *s != '/')++s;
    }
    if(*s == '/')
    {
        *s = '\0';
        ++s;
        server_url = s;
    }
    while(*s != '\0' && *s != '?')++s;
    if(*s == '?')
    {
        *s = '\0';
        ++s;
        server_argv = s;
    }
invalid_url:
    if(server_host == NULL || server_port <= 0)
    {
        fprintf(stderr, "Invalid url:%s, url must be http://host:port/path?argv "
                " or https://host:port/path?argv\n", url);
        _exit(-1);
    }
    if(urllist) fp = fopen(urllist, "rd");
    if(is_post)
    {
        p = request;
        p += sprintf(p, "POST /%s HTTP/1.1\r\n", server_url);
        p += sprintf(p, "Host: %s:%d\r\n", server_host, server_port);
        if(is_keepalive) p += sprintf(p, "Connection: Keep-Alive\r\n");
        p += sprintf(p, "Content-Length: %d\r\n\r\n", (int)strlen(server_argv));
        p += sprintf(p, "\r\n");
        if(strlen(server_argv)) p += sprintf(p, "%s", server_argv);
        request_len = p - request;
    }
    else
    {
        p = request;
        if(strlen(server_argv) > 0)
            p += sprintf(p, "GET /%s?%s HTTP/1.1\r\n", server_url, server_argv);
        else 
            p += sprintf(p, "GET /%s HTTP/1.1\r\n", server_url);
        p += sprintf(p, "Host: %s:%d\r\n", server_host, server_port);
        if(is_keepalive) p += sprintf(p, "Connection: Keep-Alive\r\n");
        p += sprintf(p, "\r\n");
        request_len = p - request;
    }
    if((hent = gethostbyname(server_host)) == NULL)
    {
        fprintf(stderr, "resolve hostname:%s failed, %s\n", server_host, strerror(h_errno));
        _exit(-1);
    }
    else
    {
        //memcpy(&ip, &(hent->h_addr), sizeof(int));
        sprintf(server_ip, "%s", inet_ntoa(*((struct in_addr *)(hent->h_addr))));
        if(is_verbosity)
        {
            fprintf(stdout, "ip:%s request:%s\n", server_ip, request);
        }
    }
    //_exit(-1);
    /* locale */
    setlocale(LC_ALL, "C");
    /* signal */
    signal(SIGTERM, &benchmark_stop);
    signal(SIGINT,  &benchmark_stop);
    signal(SIGHUP,  &benchmark_stop);
    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);
    //daemon
    if(is_daemon)
    {
        pid = fork();
        switch (pid) {
            case -1:
                perror("fork()");
                exit(EXIT_FAILURE);
                break;
            case 0: //child
                if(setsid() == -1)
                    exit(EXIT_FAILURE);
                break;
            default://parent
                _exit(EXIT_SUCCESS);
                break;
        }
    }
    setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, 65536);
    if((sbase = sbase_init()) == NULL)
    {
        exit(EXIT_FAILURE);
        return -1;
    }
    sbase->nchilds = 0;
    sbase->usec_sleep = 1000;
    sbase->connections_limit = 65536;
    TIMER_INIT(timer);
    MUTEX_INIT(mutex);
    if(log_level > 1)sbase->set_evlog(sbase, "/tmp/benchmark_ev.log");
    if(log_level > 0) sbase->set_evlog_level(sbase, log_level);
    if((service = service_init()))
    {
        service->working_mode = 1;
        service->nprocthreads = workers;
        service->niodaemons = niodaemons;
        service->ndaemons = 0;
        service->use_cond_wait = 1;
        service->flag |= SB_USE_OUTDAEMON|SB_USE_COND;
        if(is_realtime) service->flag |= (SB_SCHED_FIFO|SB_SCHED_RR) & is_realtime;
        if(socket_option == 1) service->flag |= SB_SO_LINGER;
        if(tcp_option == 1) service->flag |= SB_TCP_NODELAY;
        service->service_type = C_SERVICE;
        service->family = AF_INET;
        service->sock_type = SOCK_STREAM;
        service->service_name = "benchmark";
        service->session.flags = SB_NONBLOCK;
        service->session.packet_type = PACKET_DELIMITER;
        service->session.packet_delimiter = "\r\n\r\n";
        service->session.packet_delimiter_length = 4;
        service->session.packet_handler = &benchmark_packet_handler;
        service->session.data_handler = &benchmark_data_handler;
        service->session.transaction_handler = &benchmark_trans_handler;
        service->session.error_handler = &benchmark_error_handler;
        service->session.timeout_handler = &benchmark_timeout_handler;
        service->session.ok_handler = &benchmark_ok_handler;
        service->session.buffer_size = 65536;
        service->set_heartbeat(service, 1000000, &benchmark_heartbeat_handler, NULL);
        //service->set_session(service, &session);
        service->set_log(service, "/tmp/benchmark.log");
        service->set_log_level(service, log_level);
        LOGGER_INIT(logger, "/tmp/benchmark_res.log");
        if(sbase->add_service(sbase, service) == 0)
        {
            sbase->running(sbase, 0);
            //sbase->running(sbase, 3600);
            //sbase->running(sbase, 90000000);sbase->stop(sbase);
        }
        else fprintf(stderr, "add service failed, %s", strerror(errno));
    }
    sbase->clean(sbase);
    MUTEX_DESTROY(mutex);
    return 0;
}