예제 #1
0
int			network_listen(struct timeval *timeout)
{
  fd_set		set;
  int			ret;

  flush_list();
  setfd(&set);
  if (timeout)
    logger_debug("[NETWORK] waiting for next action in %lu:%lu",
	   timeout->tv_sec,
	   timeout->tv_usec);
  else
    logger_debug("[NETWORK] waiting for extern action. nothing planified");
  if ((ret = (select(g_network->nfds, &set, NULL, NULL, timeout))) > 0)
    {
      find_speaker(&set,
		   g_network->read,
		   &extract_from_socket,
		   &execute_from_socket);
      find_speaker(&set,
		   g_network->listened,
		   &extract_from_listener,
		   &execute_from_listener);
    }
  g_network->nfds = 0;
  return (ret);
}
예제 #2
0
int HttpListener::setSockAttr( int fd, GSockAddr &addr )
{
    setfd( fd );
    ::fcntl( fd, F_SETFD, FD_CLOEXEC );
    ::fcntl( fd, F_SETFL, HttpGlobals::getMultiplexer()->getFLTag());
    int nodelay = 1;
    //::setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof( int ) );
#ifdef TCP_DEFER_ACCEPT
    ::setsockopt( fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &nodelay, sizeof( int ) );
#endif
    
    //int tos = IPTOS_THROUGHPUT;
    //setsockopt( fd, IPPROTO_IP, IP_TOS, &tos, sizeof( tos ));
    m_pMapVHost->setPort( addr.getPort() );
    
#ifdef SO_ACCEPTFILTER
    /*
     * FreeBSD accf_http filter
     */
    struct accept_filter_arg arg;
    memset( &arg, 0, sizeof(arg) );
    strcpy( arg.af_name, "httpready" );
    if ( setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &arg, sizeof(arg)) < 0)
    {
        if (errno != ENOENT)
        {
            LOG_NOTICE(( "Failed to set accept-filter 'httpready': %s", strerror(errno) ));
        }
    }
#endif
    
    return HttpGlobals::getMultiplexer()->add( this, POLLIN|POLLHUP|POLLERR );
}
int AioOutputStream::open(const char *pathname, int flags, mode_t mode)
{
    if (m_closeRequested)
        m_closeRequested = 0;
    if (getfd() == -1)
        setfd(ls_fio_open(pathname, flags, mode));
    return getfd();
}
예제 #4
0
int pipe2(int fildes[2], int flags)
{
	int rc = pipe(fildes);
	if (rc == 0) {
		if (flags & O_NONBLOCK) {
			rc |= setfl(fildes[0], O_NONBLOCK);
			rc |= setfl(fildes[1], O_NONBLOCK);
		}
		if (flags & O_CLOEXEC) {
			rc |= setfd(fildes[0], FD_CLOEXEC);
			rc |= setfd(fildes[1], FD_CLOEXEC);
		}
		if (rc != 0) {
			int e = errno;
			close(fildes[0]);
			close(fildes[1]);
			errno = e;
		}
	}
	return rc;
}
예제 #5
0
int EdStream::close()
{
    if (getfd() != -1)
    {
        if (m_pMplex)
            m_pMplex->remove(this);
        //::shutdown( getfd(), SHUT_RDWR );
        ::close(getfd());
        setfd(-1);
    }
    return 0;
}
예제 #6
0
int HttpListener::stop()
{
    if ( getfd() != -1 )
    {
        LOG_INFO(( "Stop listener %s.", getAddrStr() ));
        HttpGlobals::getMultiplexer()->remove( this );
        close( getfd() );
        setfd( -1 );
        return 0;
    }
    return EBADF;
}
예제 #7
0
/*
 * creat() and open() replacement.
 * BUGS: An unusually long file name could be dangerous.
 */
int
creat_(tchar *name_, int mode)
{
	int fd;
	char chbuf[MAXPATHLEN * MB_LEN_MAX]; /* General use buffer. */

	tstostr(chbuf, name_);
	fd = creat((char *)chbuf, mode);
	if (fd != -1) {
		setfd(fd);
	}
	return (fd);
}
예제 #8
0
int bsd_socketpair(int domain, int type, int protocol, int socket_vector[2])
{
	int flags = type & ~0xf;
	type &= 0xf;
	int rc = socketpair(domain, type, protocol, socket_vector);
	if (rc == 0) {
		if (flags & SOCK_NONBLOCK) {
			rc |= setfl(socket_vector[0], O_NONBLOCK);
			rc |= setfl(socket_vector[1], O_NONBLOCK);
		}
		if (flags & SOCK_CLOEXEC) {
			rc |= setfd(socket_vector[0], FD_CLOEXEC);
			rc |= setfd(socket_vector[1], FD_CLOEXEC);
		}
		if (rc != 0) {
			int e = errno;
			close(socket_vector[0]);
			close(socket_vector[1]);
			errno = e;
		}
	}
	return rc;
}
예제 #9
0
int CoreSocket::close()
{
    int iRet;
    for (int i = 0; i < 3; i++)
    {
        iRet = ::close(getfd());
        if (iRet != EINTR)   // not interupted
        {
            setfd(INVALID_FD);
            return iRet;
        }
    }
    return iRet;
}
int AioOutputStream::close()
{
    if (getfd() == -1)
        return 0;
    if (m_pRecv)
        flush();
    if (m_pSend)
        m_closeRequested = 1;
    else
    {
        ls_fio_close(getfd());
        setfd(-1);
        m_closeRequested = 0;
        m_flushRequested = 0;
    }
    return 0;
}
int FileCacheDataEx::readyData(const char * pPath)
{
    int fd;
    int ret = openFile( pPath, fd );
    if ( ret )
        return ret;
    if ( (size_t)m_lSize < s_iMaxInMemCacheSize )
    {
        ret = allocateCache( m_lSize );
        if ( ret == 0 )
        {
            ret = nio_read( fd, m_pCache, m_lSize );
            if ( ret == m_lSize )
            {
                close( fd );
                return 0;
            }
            else
            {
                release();
            }
        }
    }
    else if (( (size_t)m_lSize < s_iMaxMMapCacheSize )
            &&((size_t)m_lSize + s_iCurTotalMMAPCache < s_iMaxTotalMMAPCache ))
    {
        m_pCache = (char *)mmap( 0, m_lSize, PROT_READ,
                MAP_PRIVATE, fd, 0 );
        s_iCurTotalMMAPCache += m_lSize;
        if ( D_ENABLED( DL_MORE ))
            LOG_D(( "[MMAP] Map %p to file:%s", m_pCache, pPath ));
        if ( m_pCache == MAP_FAILED )
        {
            m_pCache = 0;
        }
        else
        {
            setStatus( MMAPED );
            close( fd );
            return 0;
        }
    }
    setfd( fd );
    fcntl( fd, F_SETFD, FD_CLOEXEC );
    return 0;
}
int EventNotifier::initNotifier(Multiplexer *pMultiplexer)
{
#ifdef LSEFD_AVAIL
    setfd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
#else
    int fds[2];

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
        return LS_FAIL;

    ::fcntl(fds[0], F_SETFD, FD_CLOEXEC);
    ::fcntl(fds[1], F_SETFD, FD_CLOEXEC);
    int fl = 0;
    ::fcntl(fds[1], F_GETFL, &fl);
    ::fcntl(fds[1], F_SETFL, fl | pMultiplexer->getFLTag());
    EventReactor::setfd(fds[1]);
    m_fdIn = fds[0];
#endif
    if (getfd() == -1)
        return LS_FAIL;
    pMultiplexer->add(this, POLLIN | POLLHUP | POLLERR);

    return 0;
}
예제 #13
0
void BVCan::initializeCan(    ){

#ifdef USE_CAN
  
  WORD awBTR0BTR1=0x0014;
 int aCANMsgType=0;


   h = NULL;

  errno = 0;


  device = string("/dev/pcan32");
  //  open the device
  //    //  O_RDWR which request opening the file read/write
  h = LINUX_CAN_Open((char*)(device.c_str()), O_RDWR);
  if (h==0)
  {
      device = string("/dev/pcan0");
      h = LINUX_CAN_Open((char*)(device.c_str()), O_RDWR);
  }
  if(h==0){
      cerr<< "PCan_Device nicht gefunden !!!"<<endl;
      exit(37);
  }
  
  
  
  if(h){
    PCAN_DESCRIPTOR *desc= (PCAN_DESCRIPTOR *)h;
    BV_ASSERT( desc->nFileNo>0 );
    BV_DEBUGINFO4NNL("BVCan::ctor device=[" << desc->szDevicePath << "]");
    BV_DEBUGINFO4C(" fd=[" << desc->nFileNo << "]" << endl);
    setfd(desc->nFileNo);
    CAN_Status(h);
    // -----------------------------------------------------------
    // returns a text string with driver version info
    errno = CAN_VersionInfo(h, info);
    if (!errno){
      string infostr( string(info) );
      BV_DEBUGINFO4("BVCan::ctor driver version=[" << infostr << "]");
    }
    else
      perror("BVCan::ctor CAN_VersionInfo()");

    wBTR0BTR1 = 0x401c; 
    if(LINUX_CAN_BTR0BTR1(h,wBTR0BTR1)==0)
      BV_WARNING("BVCan::ctor 0x" << hex << wBTR0BTR1 << dec << " is not possible" );	 
    else
      BV_DEBUGINFO4("BVCan::ctor baudrate changes to 0x" << hex << wBTR0BTR1 << dec );	      
  } 
  else
    BV_DEBUGINFO4("BVCan::ctor cannot open the can device");	  

  BV_WARNING("BVCan::initializeCan .....................");
  if(h){
    // init to a user defined bit rate
    //nExtended = CAN_INIT_TYPE_ST;
    //wBTR0BTR1 = CAN_BAUD_500K;
    canMsgType = aCANMsgType;
    wBTR0BTR1 = awBTR0BTR1;


    errno = CAN_Init(h, wBTR0BTR1, canMsgType);
    if (errno)
      perror("BVCan::initializeCan CAN_Init failed");
    else
      BV_WARNING("BVCan::initializeCan baudrate=0x" << hex << wBTR0BTR1 << dec << " msgType=" << canMsgType );
  }
  else
    BV_WARNING("BVCan::initializeCan can HANDLE is NULL");

  
  #endif

  
  
}
예제 #14
0
int LocalWorker::startWorker( )
{
    int fd = getfd();
    LocalWorkerConfig& config = getConfig();
    struct stat st;
//    if (( stat( config.getCommand(), &st ) == -1 )||
//        ( access(config.getCommand(), X_OK) == -1 ))
//    {
//        LOG_ERR(("Start FCGI [%s]: invalid path to executable - %s,"
//                 " not exist or not executable ",
//                config.getName(),config.getCommand() ));
//        return -1;
//    }
//    if ( st.st_mode & S_ISUID )
//    {
//        if ( D_ENABLED( DL_LESS ))
//            LOG_D(( "Fast CGI [%s]: Setuid bit is not allowed : %s\n",
//                config.getName(), config.getCommand() ));
//        return -1;
//    }
    if ( fd < 0 )
    {
        fd = ExtWorker::startServerSock( &config, config.getBackLog() );
        if ( fd != -1 )
        {
            setfd( fd );
            if ( config.getServerAddr().family() == PF_UNIX )
            {
                nio_stat( config.getServerAddr().getUnix(), &st );
                HttpGlobals::getServerInfo()->addUnixSocket(
                     config.getServerAddr().getUnix(), &st );
            }
        }
        else
            return -1;
    }
    int instances = config.getInstances();
    int cur_instances = getCurInstances();
    int new_instances = getConnPool().getTotalConns() + 2 - cur_instances;
    if ( new_instances <= 0 )
        new_instances = 1;
    if ( instances < new_instances + cur_instances )
    {
        new_instances = instances - cur_instances;
    }
    if ( new_instances <= 0 )
        return 0;
    int i;
    for( i = 0; i < new_instances; ++i )
    {
        int pid;
        pid = workerExec( config, fd );
        if ( pid > 0 )
        {
            if ( D_ENABLED( DL_LESS ) )
                LOG_D(( "[%s] add child process pid: %d", getName(), pid ));
           PidRegistry::add( pid, this, 0 );
        }
        else
        {
            LOG_ERR(("Start FCGI [%s]: failed to start the # %d of %d instances.",
                config.getName(), i+1, instances ));
            break;
        }
    }
    return (i==0)?-1:0;
}