static int init(char *server_addr, char *server_port, char *event_sub_server_port, int *listen_fd, int *event_sub_fd) { int ret; *listen_fd = create_listening_fd(server_addr, server_port); if(*listen_fd == -1) { fprintf(stderr, "I cannot create the listening socket\n"); return -1; } if(set_non_blocking(*listen_fd) == 0) return -1; *event_sub_fd = create_listening_fd(server_addr, event_sub_server_port); if(*event_sub_fd == -1) { fprintf(stderr, "I cannot create the event subscribe listening socket\n"); return -1; } if(set_non_blocking(*event_sub_fd) == 0) return -1; /* I get my architecture */ if( (my_arch = aconv_get_host_arch()) == ACONV_ARCH_ERROR ) { fprintf(stderr, "I cannot get my architecture\n"); return -1; } /* I init the librsc module */ ret = rscs_init(my_arch); if(ret == -1) { fprintf(stderr, "I cannot initialize the RSC module\n"); return -1; } GDEBUG(1, "My architecture is %s\n", aconv_arch2str(my_arch)); /* I set the list of ioctl request I support AFTER the initialization * of the rsc_module (otherwise segfault)*/ init_ioctl_register_request(); /* I Init the pollfd structure */ if((pfdinfo = pollfd_init()) == NULL) { fprintf(stderr, "I cannot init the pollfd structure\n"); return -1; } return 0; }
void loop2(void) { int i, listenfd, clifd, nread; char buf[MAXLENLINE]; uid_t uid; struct pollfd *pollfd = NULL; int numfd = 1; pollfd_wrap *pfd_wrap; struct pollfd *default_pfd; default_pfd->fd = -1; default_pfd->events = POLLIN; default_pfd->events = 0; pfd_wrap->pfd = default_pfd; pfd_wrap->maxfd = NALLOC; pollfd_init(pfd_wrap, default_pfd); pollfd = pfd_wrap->pfd; int maxfd = pfd_wrap->maxfd; if ((listenfd = serv_listen(CS_OPEN)) < 0) { //log_sys("serv_listen error"); } client_add(listenfd, 0); pollfd[0].fd = listenfd; for (;;) { if (poll(pollfd, numfd, -1) < 0) { //log_sys("poll error"); } if (pollfd[0].revents & POLLIN) { if ((clifd = serv_accept(listenfd, &uid)) < 0) { //log_sys("serv_accept error: %d", clifd); } client_add(clifd,uid); if (numfd == pfd_wrap->maxfd) { default_pfd->fd = -1; pollfd_alloc(pfd_wrap, default_pfd); } else { default_pfd->fd = clifd; pollfd_add(pfd_wrap, default_pfd); } pollfd = pfd_wrap->pfd; pollfd[numfd].fd = clifd; pollfd[numfd].events = POLLIN; pollfd[numfd].revents = 0; numfd++; //log_msg("new connection: uid %d, fd %d, uid, clifd"); } for (i = 1; i < numfd; i++) { if (pollfd[i].revents & POLLHUP) { goto hungup; } else if(pollfd[i].revents & POLLIN) { if ((nread = read(pollfd[i].fd, buf, MAXLENLINE)) < 0) { //log_sys("read error on fd %d",pollfd[i].fd); } else if(nread == 0) { hungup: //log_msg("closed: fd %d", pollfd[i].fd); client_del(pollfd[i].fd); close(pollfd[i].fd); //pack the pollfd //TODO there is a drawback, if you allocate //many pollfds, it cannot be released if you //needn't them; if (i < (numfd-1)) { pollfd[i].fd = pollfd[numfd-1].fd; pollfd[i].events = pollfd[numfd-1].events; pollfd[i].revents = pollfd[numfd-1].revents; i--; } numfd--; } else { handle_request(buf, nread, pollfd[i].fd,client[i].uid); } } } } }