Пример #1
0
 ResolverService::~ResolverService()
 {
     shutdown_service();
 }
Пример #2
0
static int init_service(void)
{
    int r = 0;
    const char *sockdir = get_socket_dir();
    const char *sockpath = get_socket_path();
    struct sockaddr_un sun;
    if (strlen(sockpath) >= sizeof sun.sun_path) {
        fprintf(stderr, "port name too long\n");
        return -1;
    }
    struct stat s;
    if (lstat(sockdir, &s) < 0 || !S_ISDIR(s.st_mode)) {
        (void)unlink(sockdir);
        (void)rmdir(sockdir);
        if (mkdir(sockdir, 0700) < 0 && errno != EEXIST) {
            syslog(LOG_ERR, "mkdir %s failed: %m", sockdir);
            return -1;
        }
        if (lstat(sockdir, &s) < 0) {
            syslog(LOG_ERR, "lstat %s failed: %m", sockdir);
            return -1;
        }
        if (s.st_mode != 040700 || s.st_uid != geteuid()) {
            syslog(LOG_ERR,
                   "%s has wrong user/mode.  Expected %d/%0o, got %d/%0o.",
                   sockdir, geteuid(), 040700, s.st_uid, s.st_mode);
            return -1;
        }
    }
    (void)unlink(sockpath);
    listen_socket = socket(AF_UNIX, SOCK_STREAM, 0);
    if (listen_socket < 0) {
        syslog(LOG_ERR, "listener socket failed: %m");
        (void)rmdir(sockdir);
        return -1;
    }
    sun.sun_family = AF_UNIX;
    strncpy(sun.sun_path, sockpath, sizeof sun.sun_path);
    if (bind(listen_socket, (struct sockaddr *)&sun, sizeof sun) < 0) {
        syslog(LOG_ERR, "bind to %s failed: %m", sun.sun_path);
        (void)rmdir(sockdir);
        return -1;
    }
    if (atexit(shutdown_service) != 0) {
        syslog(LOG_ERR, "atexit failed: %m");
        shutdown_service();
        return -1;
    }
    if (listen(listen_socket, 1)) {
        syslog(LOG_ERR, "listen failed: %m");
        return -1;
    }

    // Start acceptor thread.
    r = pthread_create(&acceptor_thread, NULL, acceptor_thread_main, NULL);
    if (r) {
        syslog(LOG_ERR, "can't create acceptor thread: %s", strerror(r));
        return -1;
    }    

    r = pthread_detach(acceptor_thread);
    if (r) {
        syslog(LOG_ERR, "can't detach acceptor thread: %s", strerror(r));
        return -1;
    }

    return 0;
}
Пример #3
0
int service_engine(char *addr, int port)
{
    int i;
    if(MAX_TASK_NUM > 0)
    {
        for (i = 0; i < MAX_TASK_NUM; i++)
        {
            sem_init(&event[i], 0, 0);
            int temp = i;
           if (pthread_create(&thread_id[i], NULL, (void *)handle_requests, (void *)temp) != 0)
            {
                printf("pthread_create error, %s:%d\n",__FILE__, __LINE__);
                exit(-1);
            }

        }
    }
    /* 
     * command line console
     */

    init_mdb();

    ServiceHandler h = -1;
    initialize_service(addr,port);
    while(1)
    {
        h = service_start();
        //        handle_request(h);  
        task_node_t *tnode = (task_node_t *)malloc( sizeof(task_node_t));
        tnode->buf_size = MAX_BUF_LEN;
        if (receive_data (h, tnode->buf, &(tnode->buf_size)) == 0)
        {
            service_stop(h);
            continue;
        }

        if (MAX_TASK_NUM > 0)
        {
            tnode->req = h;
            int i = random_int(MAX_TASK_NUM);
            /* put tnode into task_list */
            if (task_list[i] == NULL)
            {
                task_list[i] = tnode;
                tnode->next = NULL;
                tnode->last = tnode;
            }
            else
            {
                task_node_t *p = task_list[i];
                p->last->next = tnode;
                p->last = tnode;
                tnode->next = NULL;
            }
            sem_post(&event[i]);

            //        service_stop(h); 
        }
        else 
        {
            handle_one_request(h, tnode->buf, tnode->buf_size);
            free(tnode);
        }
    }
    shutdown_service();
    close_mdb();
    if (MAX_TASK_NUM > 0 )
    {
        for (i = 0; i< MAX_TASK_NUM; i++)
        {
            task_node_t *p = task_list[i];
            task_node_t *q;
            while (p)
            {
                q = p->next;
                free(p);
                p = q;
            }
        }
    }
    return 0;
}