Esempio n. 1
0
int readData(aeEventLoop *el,  int fd, char *data, size_t size){
	if(master_fd == -1){
		connectToMaster(el);
	}
	if(fd == -1){
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "readData fd ==-1 at line %d in file %s", __LINE__, __FILE__);
		return -1;
	}
	int nread = read(fd, data, size);
	if(nread == 0){
		if(errno != EAGAIN  && errno != EINPROGRESS){
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "connection closed,  errno = %d at line %d in file %s",errno,  __LINE__, __FILE__);
			closeFd(fd, el);
			return -1;
		}
		return 0;
	}
	else if(nread < 0){
		if(errno != EAGAIN  && errno != EINPROGRESS ){
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "readData  errno = %d at line %d in file %s",errno,  __LINE__, __FILE__);
			closeFd(fd, el);
			return -1;
		}
		return 0;
	}
	else
		return nread;

}
Esempio n. 2
0
int main(int argc, char *argv[]) {
  int fd;
  fd = open("contract.txt", O_RDONLY);
  if(fd == -1) {
    printf("error opening file\n");
    exit(EXIT_FAILURE);
  }

  closeFd(fd);

  fd = open("contract1.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if(fd == -1) {
    printf("error opening file\n");
    exit(EXIT_FAILURE);
  }

  closeFd(fd);

  fd = open("w.log", O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, S_IRUSR | S_IWUSR);

  if(fd ==-1) {
    printf("error opening file\n");
    exit(EXIT_FAILURE);
  }
  closeFd(fd);

  exit(EXIT_SUCCESS);
}
Esempio n. 3
0
  void dupChild() {
#ifndef _WIN32
    if ((mode & ~DESC_PARENT_MODE_WRITE) == DESC_PIPE) {
      closeFd(parentend);
      parentend = defaultFd;
    }
    if (dup2(childend, index) < 0) {
      perror("dup2");
    }
    if (childend != index) {
      closeFd(childend);
      childend = defaultFd;
    }
#endif
  }
Esempio n. 4
0
int writeData( aeEventLoop* el,int fd, char *data, int len){
	int wr;
	int all_write = 0;
	if(master_fd == -1){
		connectToMaster(el);
	}
	if(fd == -1){
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s , at line %d in file %s", "write fd == -1 error ",  __LINE__, __FILE__);
		return -1;
	}
	while(all_write < len){
		wr = write(fd, data + all_write, len - all_write);
		if(wr <= 0){
			if(errno == 11 || errno == EINPROGRESS){
				continue;
			}
			else{
				log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "write fd %d all write %d, len %d, errno %d at line %d in file %s", fd, all_write, len, errno, __LINE__, __FILE__);
				closeFd(fd, el);
				return -1;
			}
		}
		else{
			all_write += wr;
			if(all_write < wr){
				continue;
			}
		}

	}
	return all_write;
}
Esempio n. 5
0
void VirtualTerminal::setup(int vtNr)
{
    if (m_vt != -1) {
        return;
    }
    if (vtNr == -1) {
        // error condition
        return;
    }
    QString ttyName = QStringLiteral("/dev/tty%1").arg(vtNr);

    m_vt = open(ttyName.toUtf8().constData(), O_RDWR|O_CLOEXEC|O_NONBLOCK);
    if (m_vt < 0) {
        qCWarning(KWIN_CORE) << "Failed to open tty" << vtNr;
        return;
    }
    if (!isTty(m_vt)) {
        qCWarning(KWIN_CORE) << vtNr << " is no tty";
        closeFd();
        return;
    }
    if (ioctl(m_vt, KDSETMODE, KD_GRAPHICS) < 0) {
        qCWarning(KWIN_CORE()) << "Failed to set tty " << vtNr << " in graphics mode";
        closeFd();
        return;
    }
    if (!createSignalHandler()) {
        qCWarning(KWIN_CORE) << "Failed to create signalfd";
        closeFd();
        return;
    }
    vt_mode mode = {
        VT_PROCESS,
        0,
        RELEASE_SIGNAL,
        ACQUISITION_SIGNAL,
        0
    };
    if (ioctl(m_vt, VT_SETMODE, &mode) < 0) {
        qCWarning(KWIN_CORE) << "Failed to take over virtual terminal";
        closeFd();
        return;
    }
    m_vtNumber = vtNr;
    setActive(true);
    emit kwinApp()->virtualTerminalCreated();
}
Esempio n. 6
0
int EventLoopImpl::unregisterFd(SOCKET_FD fd, bool close_fd)
{
    if(isInEventLoopThread()) {
        int ret = poll_->unregisterFd(fd);
        if(close_fd) {
            closeFd(fd);
        }
        return ret;
    } else {
        int ret = runInEventLoopSync([=] {
            poll_->unregisterFd(fd);
            if(close_fd) {
                closeFd(fd);
            }
        });
        if(KUMA_ERROR_NOERR != ret) {
            return ret;
        }
        return KUMA_ERROR_NOERR;
    }
}
Esempio n. 7
0
KMError EventLoop::Impl::unregisterFd(SOCKET_FD fd, bool close_fd)
{
    if(inSameThread()) {
        auto ret = poll_->unregisterFd(fd);
        if(close_fd) {
            closeFd(fd);
        }
        return ret;
    } else {
        auto ret = sync([=] {
            poll_->unregisterFd(fd);
            if(close_fd) {
                closeFd(fd);
            }
        });
        if(KMError::NOERR != ret) {
            return ret;
        }
        return KMError::NOERR;
    }
}
Esempio n. 8
0
bool TcpSerial::makeAvailable()
{
	closeFd();
	setFd(socket(PF_INET, SOCK_STREAM, IPPROTO_TCP));
	addrinfo hints;
	addrinfo *res;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	getaddrinfo(m_host, m_service, &hints, &res);
	bool ret = ::connect(fd(), res->ai_addr, res->ai_addrlen);
#ifdef WIN32
	std::cout << "makeAvail ret = " << ret << " " << WSAGetLastError() << std::endl;
#endif
	freeaddrinfo(res);
	return ret == 0;
}
Esempio n. 9
0
  /* clean up all the child ends and then open streams on the parent
   * ends, where appropriate */
  Resource dupParent() {
    closeFd(childend);
    childend = defaultFd;

    if ((mode & ~DESC_PARENT_MODE_WRITE) == DESC_PIPE) {
#ifdef _WIN32
      return Resource(
        req::make<PlainFile>(_open_osfhandle((intptr_t)parentend, mode_flags),
          true));
#else
      /* mark the descriptor close-on-exec, so that it won't be inherited
         by potential other children */
      fcntl(parentend, F_SETFD, FD_CLOEXEC);
      return Resource(req::make<PlainFile>(parentend, true));
#endif
    }

    return Resource();
  }
Esempio n. 10
0
void TcpSocketImpl::cleanup()
{
#ifdef KUMA_HAS_OPENSSL
    if(ssl_handler_) {
        ssl_handler_->close();
        delete ssl_handler_;
        ssl_handler_ = nullptr;
    }
#endif
    if(INVALID_FD != fd_) {
        SOCKET_FD fd = fd_;
        fd_ = INVALID_FD;
        shutdown(fd, 0); // only stop receive
        if(registered_) {
            registered_ = false;
            loop_->unregisterFd(fd, true);
        } else {
            closeFd(fd);
        }
    }
}
Esempio n. 11
0
VirtualTerminal::~VirtualTerminal()
{
    s_self = nullptr;
    closeFd();
}
Esempio n. 12
0
 void cleanup() {
   closeFd(childend);
   closeFd(parentend);
 }
Esempio n. 13
0
void TcpSerial::endSession()
{
	closeFd();
}