Exemple #1
0
void * sel_thread_fun(void * arg){
	
	struct sockaddr_in client_address;
	socklen_t client_addrlength = sizeof(client_address);
	char addr_p[16];
	
	int i = 0;
	printf("sel_thread_fun begin\n");
  while(1){
  	max = get_max_fd();
	  printf("max=%d\n", max);
	  
	  int ret = select(max+1, &read_fds, NULL, &exception_fds, NULL);
	  if (ret < 0){
	    printf("selection failure \n");
	    continue;
	  }  	
	  

	  
	  for (i = 0; i < max+1; i++){
	  	memset(buf, 0x00, sizeof(buf) );
		  if (FD_ISSET(i, &read_fds)){
		  	if (i == listenfd){
				  int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
				  printf("connection from %s \n", inet_ntop(AF_INET, &client_address.sin_addr, addr_p, sizeof(addr_p)));
				  if ( connfd < 0){
				    printf("errno is %d\n", errno);
				    close(listenfd);
				  }
				    
				  sel_fd_arr[connfd] = 0;
		    }
		    else{
			    ret = recv(i, buf, sizeof(buf)-1, 0);
			    if (ret <= 0){
			    	printf("recv ret=%d\n", ret);
			    	sel_fd_arr[i] = -1;
			      break;
			    }
			    printf("get %d bytes of normal data:%s\n", ret, buf);
		    }  
		  }
		  else if( FD_ISSET(i, &exception_fds) ){
		    ret = recv(i, buf, sizeof(buf)-1, MSG_OOB);
		    if (ret <= 0){
		    	sel_fd_arr[i] = -1;
		      break;
		    }
		    printf("get %d bytes of oob data:%s\n", ret, buf);
		  }	  	
	  }
	  
	  set_sel_events();
  	
  }	
	
	return (int*)(1) ;
}
Exemple #2
0
void terminate_client (int ci)
{
    close (cd[ci]);
    isopen[ci] = 0;
    FD_CLR (cd[ci], &fdset);
    nopen--;
    printf ("connection closed   (cd[%2d] = %2d), nopen = %2d\n",
            ci, cd[ci], nopen);
    if (cd[ci] == fdmax)        /* has to be reset */
        fdmax = get_max_fd (fdmax, isopen, cd, ncons);
}
Exemple #3
0
void terminate_client (int ci)
{
    struct pollfd *pfd = &ufds[ci];
    close (cd[ci]);
    isopen[ci] = 0;
    pfd->fd = -1;
    pfd->events = 0;
    nopen--;
    printf ("connection closed   (cd[%2d] = %2d), nopen = %2d\n",
            ci, cd[ci], nopen);
    if (cd[ci] == fdmax)        /* has to be reset */
        fdmax = get_max_fd (fdmax, isopen, cd, ncons);
}
Exemple #4
0
int sd_listen_fds(int unset_environment) {
        int r, fd;
        const char *e;
        char *p = NULL;
        unsigned long l;

        e = getenv("LISTEN_PID");
        if (!e) {
                r = 0;
                goto finish;
        }

        errno = 0;
        l = strtoul(e, &p, 10);

        if (errno > 0) {
                r = -errno;
                goto finish;
        }

        if (!p || p == e || *p || l == 0) {
                r = -EINVAL;
                goto finish;
        }

        /* Is this for us? */
        if (getpid() != (pid_t) l) {
                r = 0;
                goto finish;
        }

        e = getenv("LISTEN_FDS");
        if (!e) {
                r = 0;
                goto finish;
        }

        errno = 0;
        l = strtoul(e, &p, 10);

        if (errno > 0) {
                r = -errno;
                goto finish;
        }

        if (!p || p == e || *p) {
                r = -EINVAL;
                goto finish;
        }

        if (l > get_max_fd() - SD_LISTEN_FDS_START) {
                r = -EINVAL;
                goto finish;
        }

        for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
                int flags;

                flags = fcntl(fd, F_GETFD);
                if (flags < 0) {
                        r = -errno;
                        goto finish;
                }

                if (flags & FD_CLOEXEC)
                        continue;

                if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
                        r = -errno;
                        goto finish;
                }
        }

        r = (int) l;

finish:
        if (unset_environment) {
                unsetenv("LISTEN_PID");
                unsetenv("LISTEN_FDS");
        }

        return r;
}