Beispiel #1
0
t_module *	get_module(void)
{
  t_module *	module;

  if (!(module = malloc(sizeof(*module) + sizeof(t_mod_func) * 40)))
    return (NULL);
  bzero(module, sizeof(*module) + sizeof(t_mod_func) * 40);
  module->name = strdup("Graphic Protocol");
  module->delim = strdup("\n");
  module->port = -1;
  module->antiflood = 10;
  module->clients = NULL;
  _commands(module);
  _events();
  return (module);
}
Beispiel #2
0
int main( int argc, char* argv[] ){
    int inotifyFD = -1;

    struct inotify_event* in_event = nullptr;
    std::vector<Watched> watched;

    inotifyFD = inotify_init();
    if( inotifyFD < 0 ){
        std::cout << "error initializing inotify" << std::endl;
        exit(1);
    }

    watched.reserve(argc - 1);
    for ( int j = 1; j < argc; ++j ){
        std::string watchFile = argv[j];
        int wd = inotify_add_watch(inotifyFD, watchFile.c_str(), IN_ALL_EVENTS);
        watched.emplace_back( Watched{ watchFile, wd, new std::ifstream{ watchFile } } );
        
        if( wd == -1 ) {
            std::cout << "error adding watch" << std::endl;
            exit(1);
        }
    }

    std::vector<epoll_event> _events( 10 );

    int epollFD = epoll_create( _events.size() );
    if( epollFD < 0 ) {
        std::cout << "error creating epoll" << std::endl;
        exit(1);
    }

    epoll_event ep_event;
    ep_event.data.ptr = 0;
    ep_event.events = EPOLLIN | EPOLLET;

    int epollCtl = epoll_ctl( epollFD, EPOLL_CTL_ADD, inotifyFD, &ep_event );

    if( epollCtl < 0 ){
        char buf[128];
        
        std::cout << "error adding notify fd to epoll group: " << ::strerror_r( errno, buf, sizeof(buf) ) << std::endl;
        exit(1);
    }

    for(;;){
        ::memset( _events.data(), 0, sizeof( epoll_event ) * _events.size() );
        int n = epoll_wait( epollFD, _events.data(), _events.size(), 10000 );

        for( int i = 0; i < n; ++i ){
            epoll_event& event = _events[i];
            if( event.events & EPOLLIN ){
                char buf[1024];
                int bytesRead = ::read( inotifyFD, buf, sizeof(buf) );

                if( bytesRead < 0 ){ 
                    std::cout << "read error: bytesRead:" << bytesRead << std::endl;
                    exit(1);
                }

                for( char* p = buf; p < buf + bytesRead; ){
                    in_event = (struct inotify_event*)p;
                    for( Watched& watchedFile : watched ){
                        if( watchedFile.fd == in_event->wd ){
                            if (in_event->mask & IN_MODIFY) {
                                char fbuf[1024];
                                int fbytesRead = 0;
                                do{
                                    fbytesRead = watchedFile.file->readsome( fbuf, sizeof(fbuf) );
                                    if( fbytesRead ) { std::cout.write( fbuf, fbytesRead ); }
                                }while( fbytesRead == sizeof(fbuf) );
                            }
                        }
                    }

                    p += sizeof(struct inotify_event) + in_event->len;
                }
            }
        }
    }

    exit(0);
}