void CSensor::create() { if (-1 == pipe(_pipe_fd)) throw sys::CSyscallException(errno, __FILE__, __LINE__, "pipe"); set_fd(_pipe_fd[0]); }
/*! \brief Re-initializes the BDirectory to the directory referred to by the supplied path name. \param path the directory's path name \return - \c B_OK: Everything went fine. - \c B_BAD_VALUE: \c NULL \a path. - \c B_ENTRY_NOT_FOUND: Directory not found. - \c B_PERMISSION_DENIED: Directory permissions didn't allow operation. - \c B_NO_MEMORY: Insufficient memory for operation. - \c B_NAME_TOO_LONG: The supplied path name (\a path) is too long. - \c B_LINK_LIMIT: Indicates a cyclic loop within the file system. - \c B_BUSY: A node was busy. - \c B_FILE_ERROR: A general file error. - \c B_NO_MORE_FDS: The application has run out of file descriptors. - \c B_NOT_A_DIRECTORY: \a path includes a non-directory. */ status_t BDirectory::SetTo(const char *path) { Unset(); status_t result = (path ? B_OK : B_BAD_VALUE); int newDirFd = -1; if (result == B_OK) result = BPrivate::Storage::open_dir(path, newDirFd); if (result == B_OK) { // We have to take care that BNode doesn't stick to a symbolic link. // open_dir() does always traverse those. Therefore we open the FD for // BNode (without the O_NOTRAVERSE flag). int fd = -1; result = BPrivate::Storage::open(path, O_RDWR, fd, true); if (result == B_OK) { result = set_fd(fd); if (result != B_OK) BPrivate::Storage::close(fd); } if (result == B_OK) fDirFd = newDirFd; else BPrivate::Storage::close_dir(newDirFd); } // finally set the BNode status set_status(result); return result; }
/*! \brief Re-initializes the BFile to the file referred to by the supplied entry_ref and according to the specified open mode. \param ref the entry_ref referring to the file \param openMode the mode in which the file should be opened \a openMode must be a bitwise or of exactly one of the flags - \c B_READ_ONLY: The file is opened read only. - \c B_WRITE_ONLY: The file is opened write only. - \c B_READ_WRITE: The file is opened for random read/write access. and any number of the flags - \c B_CREATE_FILE: A new file will be created, if it does not already exist. - \c B_FAIL_IF_EXISTS: If the file does already exist and B_CREATE_FILE is set, SetTo() fails. - \c B_ERASE_FILE: An already existing file is truncated to zero size. - \c B_OPEN_AT_END: Seek() to the end of the file after opening. \return - \c B_OK: Everything went fine. - \c B_BAD_VALUE: \c NULL \a ref or bad \a openMode. - \c B_ENTRY_NOT_FOUND: File not found or failed to create file. - \c B_FILE_EXISTS: File exists and \c B_FAIL_IF_EXISTS was passed. - \c B_PERMISSION_DENIED: File permissions didn't allow operation. - \c B_NO_MEMORY: Insufficient memory for operation. - \c B_LINK_LIMIT: Indicates a cyclic loop within the file system. - \c B_BUSY: A node was busy. - \c B_FILE_ERROR: A general file error. - \c B_NO_MORE_FDS: The application has run out of file descriptors. */ status_t BFile::SetTo(const entry_ref *ref, uint32 openMode) { Unset(); if (!ref) return (fCStatus = B_BAD_VALUE); // if ref->name is absolute, let the path-only SetTo() do the job if (BPrivate::Storage::is_absolute_path(ref->name)) return SetTo(ref->name, openMode); openMode |= O_CLOEXEC; int fd = _kern_open_entry_ref(ref->device, ref->directory, ref->name, openMode, DEFFILEMODE & ~__gUmask); if (fd >= 0) { set_fd(fd); fMode = openMode; fCStatus = B_OK; } else fCStatus = fd; return fCStatus; }
void Buffer_pool_node::reset(){ set_fd(INIT); set_pos(INIT); set_timestamp(INIT); mark_clean(); }
void CTcpWaiter::attach(int fd, const ip_address_t& peer_ip, port_t peer_port) { set_fd(fd); _peer_ip = peer_ip; _peer_port = peer_port; ((CDataChannel *)_data_channel)->attach(fd); }
TCPSocket(int fd, Messagizor *m, TCPProcessor *p) : messagizizor_(m), processor_(p) { ENTERING; set_fd(fd); LEAVING; };
inline void FileClient::DownloadFile(std::shared_ptr<FileRequest> request, int ai_family){ ++num_downloads_; struct addrinfo hints, *res, *p; // first, load up address structs with getaddrinfo(): memset(&hints, 0, sizeof hints); hints.ai_family = ai_family; // use IPv4 or IPv6, whichever hints.ai_socktype = SOCK_STREAM; int rv; if((rv = getaddrinfo(request->address()->c_str(), std::to_string(request->port()).c_str(), &hints, &res)) < 0){ LOG_ERR("Error from getaddrinfo: " + std::string(gai_strerror(rv))); } int one = 1; int fd = 0; // loop through until successful connection for(p = res;p != nullptr; p = p->ai_next) { fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); // make the socket reusable if((rv = evutil_make_listen_socket_reuseable(fd)) < 0){ LOG_ERR("Make reusable failed with code: " + std::to_string(rv)); continue; } // try to connect if ((rv = connect(fd, res->ai_addr, res->ai_addrlen)) < 0) { LOG_ERR("connect failed with code: " + std::to_string(rv)); continue; } // disable Nagle's algorithm if((rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof one)) < 0){ LOG_ERR("Set no delay failed with code: " + std::to_string(rv)); continue; } // set the socket to non blocking mode if((rv = evutil_make_socket_nonblocking(fd)) < 0){ LOG_ERR("make non blocking failed with code: " + std::to_string(rv)); continue; } break; // successfully connected } if(p == nullptr){ LOG_ERR("File client connect failed"); exit(EXIT_FAILURE); } // free the linked list freeaddrinfo(res); LOG_DEBUG("Downloading: " + request->file()->filename() + " from: " + std::to_string(request->port()) + " to: " + *request->output_path()); auto d_ptr = new FileDownload(this, request); d_ptr->set_fd(fd); d_ptr->set_ev_read(event_new(base_, fd, EV_READ|EV_PERSIST, FileClient::on_read, d_ptr)); d_ptr->set_ev_write(event_new(base_, fd, EV_WRITE, FileClient::on_write, d_ptr)); event_add(d_ptr->ev_write(), nullptr); }
void logInit(char *pOutFile) { char LogFile[MAX_PATH]="\0"; sprintf(LogFile, "%s_tbl.log", pOutFile); DebugLogSetLogType(DEBUGLOG_STDOUT_FILE); g_TableFileHandle= fopen(LogFile, "w+"); set_fd(g_TableFileHandle); }
void platform_init() { /* firstly, let's initialize and open first-three-canonical files (stdin, stdout, stderr -- shame on you if you didn't know!) */ linux_stdin.state = IO_STATE_OPENED; linux_stdin.internal = (void*)0; linux_stdin.handler = linux_handler; set_fd(0, linux_stdin); linux_stdout.state = IO_STATE_OPENED; linux_stdout.internal = (void*)1; linux_stdout.handler = linux_handler; set_fd(1, linux_stdout); linux_stderr.state = IO_STATE_OPENED; linux_stderr.internal = (void*)2; linux_stderr.handler = linux_handler; set_fd(2, linux_stderr); // randomize srand(time(NULL)); }
int init_monitor(t_init *data, struct timeval *tv, int *ret, t_player_lst **tmp) { tv->tv_sec = 0; tv->tv_usec = 500000; *ret = 0; set_fd(data); (*tmp) = data->head; return (0); }
void BaseServer::OnNewConnection(EventLoop::Status _status) { if (_status == EventLoop::READ_EVENT) { // The EventLoop indicated that the socket is writable. // Which means that a new client has connected to it. auto endPoint = new ConnectionEndPoint(options_.get_sin_family() != AF_INET); struct sockaddr* serv_addr = endPoint->addr(); socklen_t addrlen = endPoint->addrlen(); sp_int32 fd = accept(listen_fd_, serv_addr, &addrlen); endPoint->set_fd(fd); if (endPoint->get_fd() > 0) { // accept succeeded. // Set defaults if (SockUtils::setSocketDefaults(endPoint->get_fd()) < 0) { close(endPoint->get_fd()); delete endPoint; return; } // Create the connection object and register our callbacks on various events. BaseConnection* conn = CreateConnection(endPoint, &connection_options_, eventLoop_); auto ccb = [conn, this](NetworkErrorCode ec) { this->OnConnectionClose(conn, ec); }; conn->registerForClose(std::move(ccb)); if (conn->start() != 0) { // Connection didn't start properly. Cleanup. // We assume here that this particular connection went bad, so we simply return. LOG(ERROR) << "Could not start the connection for read write"; close(endPoint->get_fd()); delete conn; return; } active_connections_.insert(conn); HandleNewConnection_Base(conn); return; } else { // accept failed. if (errno == EAGAIN) { // This is really odd. We thought that we had a read event LOG(ERROR) << "accept failed with EAGAIN when it should have worked. Ignoring"; } else { LOG(ERROR) << "accept failed with errno " << errno; } close(endPoint->get_fd()); delete endPoint; return; } } else { // What the hell, we only registered ourselves to reading // Just print a warning message LOG(WARNING) << "WARNING while expecting a read event we got " << _status; return; } }
int vpipe_sockpair::open() { int fd[2] = {-1, -1}; if (::socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) { ERROR_LOG("socketpair failed, err: %s", ::strerror(errno)); return -1; } set_fd(fd); set_nonblocking(); return 0; }
void CSensor::close() { if (_pipe_fd[0] != -1) { before_close(); close_fd(_pipe_fd[0]); close_fd(_pipe_fd[1]); _pipe_fd[0] = -1; _pipe_fd[1] = -1; set_fd(-1); } }
int io_open(char *pathname, int flags, mode_t mode) { int fd = io_opened_files+8, l_fd; if((l_fd = open(pathname, flags, mode)) != -1) { struct X86_FILE f; f.state = IO_STATE_OPENED; f.internal = (void*)l_fd; f.handler = linux_handler; set_fd(fd, f); io_opened_files++; } else { fd = -1; } return fd; }
DTerr FileHandleCompressedFD::open_file (const FilePath &pathname, DTsize start, DTsize length, DTsize uncompressed_length) { DTint fd; fd = ::open(pathname.full_path().c_str(), O_RDONLY); if (fd < 0) { return DT3_ERR_FILE_OPEN_FAILED; } _eof = false; _file_g = 0; set_fd(fd,start,length,uncompressed_length); return DT3_ERR_NONE; }
void CUdpSocket::listen(uint16_t port) throw (sys::CSyscallException) { struct sockaddr_in listen_addr; int fd = ::socket(AF_INET, SOCK_DGRAM, 0); if (-1 == fd) THROW_SYSCALL_EXCEPTION(NULL, errno, "socket"); listen_addr.sin_family = AF_INET; listen_addr.sin_port = htons(port); listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(listen_addr.sin_zero, 0, sizeof(listen_addr.sin_zero)); if (-1 == bind(fd, (struct sockaddr*)&listen_addr, sizeof(listen_addr))) THROW_SYSCALL_EXCEPTION(NULL, errno, "bind"); set_fd(fd); }
void select_exec(t_select *t, int port) { add_server(&(t->list), port); while (42) { FD_ZERO(&(t->fd_read)); FD_ZERO(&(t->fd_write)); t->fd_max = 0; set_fd(t); if (select(t->fd_max + 1, &(t->fd_read), &(t->fd_write), NULL, NULL) < 0) { #ifdef DEBUG perror("select"); #endif /* !DEBUG */ exit(-1); } isset_fd(t); } }
int file::open(const std::string& path, file_flags_t flags) { file::path = path; int oflags = 0; if(flags & FILE_CREATE) oflags |= O_CREAT; if(flags & FILE_READWRITE) oflags |= O_RDWR; if(flags & FILE_READONLY) oflags |= O_RDONLY; if(flags & FILE_WRITEONLY) oflags |= O_WRONLY; int fd = ::open(path.c_str(), oflags); if(fd == -1) return -1; set_fd(fd); return 0; }
/*! \brief Re-initializes the BFile to the file referred to by the supplied path name relative to the specified BDirectory and according to the specified open mode. \param dir the BDirectory, relative to which the file's path name is given \param path the file's path name relative to \a dir \param openMode the mode in which the file should be opened - \c B_OK: Everything went fine. - \c B_BAD_VALUE: \c NULL \a dir or \a path or bad \a openMode. - \c B_ENTRY_NOT_FOUND: File not found or failed to create file. - \c B_FILE_EXISTS: File exists and \c B_FAIL_IF_EXISTS was passed. - \c B_PERMISSION_DENIED: File permissions didn't allow operation. - \c B_NO_MEMORY: Insufficient memory for operation. - \c B_LINK_LIMIT: Indicates a cyclic loop within the file system. - \c B_BUSY: A node was busy. - \c B_FILE_ERROR: A general file error. - \c B_NO_MORE_FDS: The application has run out of file descriptors. \todo Implemented using SetTo(BEntry*, uint32). Check, if necessary to reimplement! */ status_t BFile::SetTo(const BDirectory *dir, const char *path, uint32 openMode) { Unset(); if (!dir) return (fCStatus = B_BAD_VALUE); openMode |= O_CLOEXEC; int fd = _kern_open(dir->fDirFd, path, openMode, DEFFILEMODE & ~__gUmask); if (fd >= 0) { set_fd(fd); fMode = openMode; fCStatus = B_OK; } else fCStatus = fd; return fCStatus; }
int get_next_line(int fd, char **line) { t_fd *fd_list; char *buff; char *tmp; int size; size = 0; if (!(fd_list = get_fd(fd)) || !line || fd < 0) return (-1); if (!(buff = (char*)malloc(BUFF_SIZE))) return (-1); tmp = (fd_list->current ? ft_strchr(fd_list->current, '\n') : NULL); if (tmp) return (set_line(line, fd_list, tmp)); if ((size = (int)read(fd, buff, BUFF_SIZE)) <= 0) return (!*(fd_list->current) ? size : set_line(line, fd_list, tmp)); if (size > 0) set_fd(size, buff, fd_list); return (get_next_line(fd, line)); }
/*! \brief Re-initializes the BFile to the file referred to by the supplied BEntry and according to the specified open mode. \param entry the BEntry referring to the file \param openMode the mode in which the file should be opened \return - \c B_OK: Everything went fine. - \c B_BAD_VALUE: \c NULL \a entry or bad \a openMode. - \c B_ENTRY_NOT_FOUND: File not found or failed to create file. - \c B_FILE_EXISTS: File exists and \c B_FAIL_IF_EXISTS was passed. - \c B_PERMISSION_DENIED: File permissions didn't allow operation. - \c B_NO_MEMORY: Insufficient memory for operation. - \c B_LINK_LIMIT: Indicates a cyclic loop within the file system. - \c B_BUSY: A node was busy. - \c B_FILE_ERROR: A general file error. - \c B_NO_MORE_FDS: The application has run out of file descriptors. \todo Implemented using SetTo(entry_ref*, uint32). Check, if necessary to reimplement! */ status_t BFile::SetTo(const BEntry *entry, uint32 openMode) { Unset(); if (!entry) return (fCStatus = B_BAD_VALUE); if (entry->InitCheck() != B_OK) return (fCStatus = entry->InitCheck()); openMode |= O_CLOEXEC; int fd = _kern_open(entry->fDirFd, entry->fName, openMode | O_CLOEXEC, DEFFILEMODE & ~__gUmask); if (fd >= 0) { set_fd(fd); fMode = openMode; fCStatus = B_OK; } else fCStatus = fd; return fCStatus; }
int main() { t_client client; init_param(&client); to_serv(&client); assign_func(&client); while (client.s < 0) init_connection(&client); while (client.s) { set_fd(&client); if (select(client.fd_max, &(client.fd_read), &(client.fd_write), NULL, NULL) == -1) { my_printf(2, "select: %s\n", strerror(errno)); break; } check_fd(&client); } if (client.s) my_close(client.s); return (0); }
void do_taint(long ret, long call, long arg1, long arg2, long arg3, long arg4, long arg5, long arg6) { if (ret < 0) return; switch (call) { case __NR_read: taint_mem((char *)arg2, ret, taint_val(arg1)); return; case __NR_readv: taint_iov( (struct iovec *)arg2, arg3, ret, taint_val(arg1)); return; case __NR_open: case __NR_creat: case __NR_openat: set_fd(ret, FD_FILE); if (strcmp((char *)(call == __NR_openat ? arg2 : arg1), "/proc/self/stat") == 0) { taint_val(ret); fake_proc_self_stat(ret); } return; case __NR_dup: case __NR_dup2: set_fd( ret, get_thread_ctx()->files->fd_type[arg1]); return; case __NR_pipe: set_fd( ((long *)arg1)[0], FD_SOCKET); set_fd( ((long *)arg1)[1], FD_SOCKET); return; case __NR_socketcall: { long *sockargs = (long *)arg2; switch (arg1) { case SYS_GETPEERNAME: if ( (ret >= 0) && sockargs[1] && sockargs[2]) taint_mem((char *)sockargs[1], *(long *)sockargs[2], TAINT_SOCKADDR); return; case SYS_ACCEPT: if ( (ret >= 0) && sockargs[1] && sockargs[2]) taint_mem((char *)sockargs[1], *(long *)sockargs[2], TAINT_SOCKADDR); case SYS_SOCKET: set_fd(ret, FD_SOCKET); return; case SYS_RECV: case SYS_RECVFROM: taint_mem((char *)sockargs[1], ret, TAINT_SOCKET); return; case SYS_RECVMSG: { struct msghdr *msg = (struct msghdr *)sockargs[1]; taint_iov( msg->msg_iov, msg->msg_iovlen, ret, TAINT_SOCKET ); return; } default: return; } } default: return; } }
void BaseClient::Start_Base() { if (state_ != DISCONNECTED) { LOG(ERROR) << "Attempting to start a client which is already in state_ " << state_ << "\n"; HandleConnect_Base(DUPLICATE_START); return; } // open a socket int fd = -1; fd = socket(options_.get_socket_family(), SOCK_STREAM, 0); if (fd < 0) { LOG(ERROR) << "Opening of socket failed in Client " << errno << "\n"; HandleConnect_Base(CONNECT_ERROR); return; } // set default socket options if (SockUtils::setSocketDefaults(fd) < 0) { close(fd); HandleConnect_Base(CONNECT_ERROR); return; } // construct an endpoint to store the connection information auto endpoint = new ConnectionEndPoint(options_.get_socket_family() != PF_INET); endpoint->set_fd(fd); bzero(reinterpret_cast<char*>(endpoint->addr()), endpoint->addrlen()); // Set the address if (options_.get_socket_family() == PF_INET) { struct sockaddr_in* addr = (struct sockaddr_in*)endpoint->addr(); addr->sin_family = AF_INET; addr->sin_port = htons(options_.get_port()); struct sockaddr_in t; int error = IpUtils::getAddressInfo(t, options_.get_host().c_str(), PF_INET, SOCK_STREAM); if (error) { LOG(ERROR) << "getaddrinfo failed in Client " << errno << "\n"; close(fd); delete endpoint; HandleConnect_Base(CONNECT_ERROR); return; } memcpy(&(addr->sin_addr), &(t.sin_addr), sizeof(addr->sin_addr)); } else { struct sockaddr_un* addr = (struct sockaddr_un*)endpoint->addr(); addr->sun_family = options_.get_sin_family(); snprintf(addr->sun_path, sizeof(addr->sun_path), "%s", options_.get_sin_path().c_str()); } // connect to the address errno = 0; if (connect(endpoint->get_fd(), endpoint->addr(), endpoint->addrlen()) == 0 || errno == EINPROGRESS) { state_ = CONNECTING; // Either connect succeeded or it woud block auto cb = [endpoint, this](EventLoop::Status status) { this->OnConnect(endpoint, status); }; CHECK_EQ(eventLoop_->registerForWrite(endpoint->get_fd(), std::move(cb), false), 0); return; } else { // connect failed. Bail out saying that the start failed. LOG(ERROR) << "Connect failed " << errno << std::endl; close(endpoint->get_fd()); delete endpoint; HandleConnect_Base(CONNECT_ERROR); return; } }