int Epoll_Watcher::open(void) { pending_io_map_.resize(max_fd(), (Event_Handler *)0); Heart_Map heart_map0(max_fd()), heart_map1(max_fd()); io_heart_map_[0].swap(heart_map0); io_heart_map_[1].swap(heart_map1); if ((this->epfd_ = ::epoll_create(1)) == -1) { LOG_SYS("epoll_create"); return -1; } timer_open(); io_open(); return 0; }
void Reception::readPipes(void) { fd_set rfds; struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 100000; setReadFD(&rfds); if (select(max_fd(), &rfds, NULL, NULL, &timeout) != -1) checkReadFD(&rfds); }
/* Handle incoming connections */ void server_loop () { struct sockaddr_un client_name; socklen_t name_len = sizeof (client_name); logit ("MOC server started, pid: %d", getpid()); assert (server_sock != -1); log_circular_start (); do { int res; fd_set fds_write, fds_read; FD_ZERO (&fds_read); FD_ZERO (&fds_write); FD_SET (server_sock, &fds_read); FD_SET (wake_up_pipe[0], &fds_read); add_clients_fds (&fds_read, &fds_write); res = 0; if (!server_quit) res = select (max_fd(server_sock)+1, &fds_read, &fds_write, NULL, NULL); if (res == -1 && errno != EINTR && !server_quit) fatal ("select() failed: %s", xstrerror (errno)); if (!server_quit && res >= 0) { if (FD_ISSET(server_sock, &fds_read)) { int client_sock; debug ("accept()ing connection..."); client_sock = accept (server_sock, (struct sockaddr *)&client_name, &name_len); if (client_sock == -1) fatal ("accept() failed: %s", xstrerror (errno)); logit ("Incoming connection"); if (!add_client(client_sock)) busy (client_sock); } if (FD_ISSET(wake_up_pipe[0], &fds_read)) { int w; logit ("Got 'wake up'"); if (read(wake_up_pipe[0], &w, sizeof(w)) < 0) fatal ("Can't read wake up signal: %s", xstrerror (errno)); } send_events (&fds_write); handle_clients (&fds_read); } if (server_quit) logit ("Exiting..."); } while (!server_quit); log_circular_log (); log_circular_stop (); close_clients (); clients_cleanup (); close (server_sock); server_sock = -1; server_shutdown (); }
int main ( int argc, char *argv[] ) { if ( argc != 4 ) { printf ( "ERROR: you forgot to enter the number\n" ); return -1; } fd_set rd_set, wr_set; int r_fd, w_fd; int buff_size; int pipefd1[2]; int pipefd2[2]; int status; int length; int n = atoi ( argv[1] ); int i = 0; int fd_max = 0; struct w_buff *buf = ( struct w_buff* ) malloc ( sizeof ( struct w_buff ) * n ); if ( buf <= 0 ) { printf ( "ERROR: can not allocate memory\n" ); return -1; } for ( i = 0; i < n; ++i ) //preparation { buf[i].size = (n-i) * 3000; buf[i].buff = ( char* ) malloc ( buf[i].size ); buf[i].l_tran = 0; pipe ( pipefd1 ); pipe ( pipefd2 ); buff_size = buf[i].size; status = fork(); if ( status ) //parent { close ( pipefd1[0] ); close ( pipefd2[1] ); buf[i].w_fd = pipefd1[1]; buf[i + 1].r_fd = pipefd2[0]; if ( fd_max < max_fd ( pipefd1[1], pipefd2[0] ) ) fd_max = max_fd ( pipefd1[1], pipefd2[0] ); fcntl ( buf[i+1].r_fd, F_SETFL , O_NONBLOCK ); fcntl ( buf[i].w_fd, F_SETFL , O_NONBLOCK ); } else //child { close( pipefd1[1] ); close( pipefd2[0] ); if ( i == 0 ) { close ( pipefd1[0] ); printf ( "open %s for read\n", argv[2] ); r_fd = open ( argv[2], O_RDONLY ); } else r_fd = pipefd1[0]; if ( i == (n-1) ) { close ( pipefd2[1] ); printf ( "open %s for write\n", argv[3] ); w_fd = open ( argv[3], O_WRONLY | O_CREAT , 0644 ); } else w_fd = pipefd2[1]; int j; for( j = 0; j < i; j++) { close ( buf[j].w_fd ); close ( buf[j + 1].r_fd ); } break; } } //transmission if ( status ) //parent { while ( status ) { FD_ZERO ( &rd_set ); FD_ZERO ( &wr_set ); status = 0; for( i = 0 ; i <= n ; i ++) { if ( buf[i].r_fd && ( buf[i].size != buf[i].l_tran ) ) { FD_SET( buf[i].r_fd, &rd_set ); ++status; } if ( buf[i].w_fd && buf[i].l_tran ) { FD_SET( buf[i].w_fd, &wr_set ); ++status; } } if (status == 0) break; status = select( fd_max + 1, &rd_set, &wr_set, NULL, NULL ); if ( status < 1) break; for( i = 0 ; i <= n ; i ++) if ( FD_ISSET ( buf[i].r_fd, &rd_set ) ) {// reading length = read ( buf[i].r_fd, buf[i].buff + buf[i].l_tran, buf[i].size - buf[i].l_tran ); if ( length > 0 ) buf[i].l_tran += length; if ( length < 1 ) { close ( buf[i].r_fd ); buf[i].r_fd = 0; } } for( i = 0 ; i <= n ; i ++) if ( FD_ISSET ( buf[i].w_fd, &wr_set ) ) {//writing length = write ( buf[i].w_fd, buf[i].buff, buf[i].l_tran ); if ( length > 0 ) { shft_left ( buf[i].buff, length, buf[i].l_tran ); buf[i].l_tran -= length; } if ( ( length < 1 ) && ( buf[i].r_fd == 0 ) && ( buf[i].l_tran == 0 ) ) { close ( buf[i].w_fd ); buf[i].w_fd = 0; } } for( i = 0 ; i <= n ; i ++) if ( buf[i].w_fd && ( buf[i].r_fd == 0) && ( buf[i].l_tran == 0 ) ) { close ( buf[i].w_fd ); buf[i].w_fd = 0; } } } else //child { char buff[buff_size]; length = 1; while( length > 0 ) { length = read ( r_fd, buff, buff_size ); write ( w_fd, buff, length ); } close( r_fd ); close( w_fd ); } return 0; }
int pr_open (const char *command, int flags, int *infd, int *outfd, int *errfd) { int pid; int fdin[2]; int fdout[2]; int fderr[2]; int null; _pr_init (); if (flags & ~(PR_USE_STDIN | PR_USE_STDOUT | PR_USE_STDERR | PR_CREATE_STDIN | PR_CREATE_STDOUT | PR_CREATE_STDERR | PR_STDERR_TO_STDOUT)) err_internal (__func__, "Illegal flags passed to pr_open"); if ((flags & PR_USE_STDIN) && (flags & PR_CREATE_STDIN)) err_internal (__func__, "Cannot both use and create stdin"); if ((flags & PR_USE_STDOUT) && (flags & PR_CREATE_STDOUT)) err_internal (__func__, "Cannot both use and create stdout"); if ((flags & PR_USE_STDERR) && (flags & PR_CREATE_STDERR)) err_internal (__func__, "Cannot both use and create stderr"); if ((flags & PR_STDERR_TO_STDOUT) && ((flags & PR_USE_STDERR) || (flags & PR_CREATE_STDERR))) err_internal (__func__, "Cannot use/create stderr when duping to stdout"); if ((flags & PR_CREATE_STDIN) && pipe (fdin) < 0) err_fatal_errno ("paexec: Cannot create pipe for stdin" ); if ((flags & PR_CREATE_STDOUT) && pipe (fdout) < 0) err_fatal_errno ("paexec: Cannot create pipe for stdout" ); if ((flags & PR_CREATE_STDERR) && pipe (fderr) < 0) err_fatal_errno ("paexec: Cannot create pipe for stderr" ); if ((pid = fork ()) < 0) err_fatal_errno ("paexec: Cannot fork" ); if (pid == 0) { /* child */ int i; #define CHILD(CREATE,USE,fds,writefd,readfd,fd,FILENO,flag) \ if (flags & CREATE){ \ close (fds [writefd]); \ dup2 (fds [readfd], FILENO); \ close (fds [readfd]); \ }else if (flags & USE) { \ if (fd && *fd){ \ dup2 (*fd, FILENO); \ close (*fd); \ }else{ \ if ((null = open ("/dev/null", flag)) >= 0) { \ dup2 (null, FILENO); \ close (null); \ } \ } \ } CHILD (PR_CREATE_STDIN, PR_USE_STDIN, fdin, 1, 0, infd, STDIN_FILENO, O_RDONLY); CHILD (PR_CREATE_STDOUT, PR_USE_STDOUT, fdout, 0, 1, outfd, STDOUT_FILENO, O_WRONLY); CHILD (PR_CREATE_STDERR, PR_USE_STDERR, fderr, 0, 1, errfd, STDERR_FILENO, O_WRONLY); #undef CHILD if (flags & PR_STDERR_TO_STDOUT) dup2 (STDOUT_FILENO, STDERR_FILENO); for (i = 0; i < max_fd(); i++) if (_pr_objects [i].pid > 0) close (i); /* (void ) NULL is for Solaris where NULL is 0L */ execl ("/bin/sh", "/bin/sh", "-c", command, (void *)NULL); _exit (127); } /* parent */ #define PARENT(CREATE,USE,fds,readfd,writefd,fd,flag,name) \ if (flags & CREATE) { \ close (fds [readfd]); \ *fd = fds [writefd]; \ _pr_objects [*fd].pid = pid; \ }else if (flags & USE) { \ if (fd && *fd) { \ _pr_objects [*fd].pid =0; \ close (*fd); \ } \ } PARENT (PR_CREATE_STDIN, PR_USE_STDIN, fdin, 0, 1, infd, "w", "stdin"); PARENT (PR_CREATE_STDOUT, PR_USE_STDOUT, fdout, 1, 0, outfd, "r", "stdout"); PARENT (PR_CREATE_STDERR, PR_USE_STDERR, fderr, 1, 0, errfd, "r", "stderr"); #undef PARENT return pid; }
static void _pr_init (void) { if (!_pr_objects) _pr_objects = xcalloc (max_fd (), sizeof (struct _pr_Obj)); }