static status_t get_socket_descriptor(int fd, bool kernel, file_descriptor*& descriptor) { if (fd < 0) return EBADF; descriptor = get_fd(get_current_io_context(kernel), fd); if (descriptor == NULL) return EBADF; if (descriptor->type != FDTYPE_SOCKET) { put_fd(descriptor); return ENOTSOCK; } return B_OK; }
static SLFile_FD_Type *find_chained_fd (int fd) { SLFile_FD_Type *f; f = FD_Type_List; while (f != NULL) { int fd1; if ((0 == get_fd (f, &fd1)) && (fd1 == fd)) return f; f = f->next; } return NULL; }
static status_t fd_ioctl(bool kernelFD, int fd, ulong op, void* buffer, size_t length) { struct file_descriptor* descriptor; int status; descriptor = get_fd(get_current_io_context(kernelFD), fd); if (descriptor == NULL) return B_FILE_ERROR; if (descriptor->ops->fd_ioctl) status = descriptor->ops->fd_ioctl(descriptor, op, buffer, length); else status = EOPNOTSUPP; put_fd(descriptor); return status; }
int parse(const char *input, cmd *cmds[], int size) { elementType type; redirectMode redir; int cmd_count = -1, fd; int valid_pipe = 0; char *str; YY_BUFFER_STATE buf = yy_scan_string(input); while ((type = yylex()) != 0) { if (cmd_count == -1) cmd_count = 0; fd = -1; str = NULL; switch (type) { case SEMICOLON: cmd_count++; valid_pipe = 0; break; case PIPE: if (!valid_pipe) { fprintf(stderr, "shell: invalid pipe\n"); yy_delete_buffer(buf); return -1; } cmd_add_pcmd(&cmds[cmd_count]); break; case CMD: valid_pipe = 1; case STRING: str = handle_escaped_chs(yytext); //fprintf(stderr, "%s %s\n", yytext, str); cmd_addarg(cmds[cmd_count], str); break; case FD_IN_RED: case FD_OUT_RED: case FD_APPEND: fd = get_fd(yytext, type); case IN_RED: case OUT_RED: case OUT_APPEND: case OUT_ERR_RED: case OUT_ERR_APPEND: yylex(); str = handle_escaped_chs(yytext); cmd_redirect(cmds[cmd_count], fd, type, str); break; case BACKGROUND: cmd_set_bg(cmds[cmd_count]); break; default: fprintf(stderr, "Unknown type: %d, %s\n", type, yytext); } free(str); } yy_delete_buffer(buf); return cmd_count + 1; }
void tracer_writer::write(const std::string &trc) { if (tls_fd < 0) { tls_fd = get_fd(); if (tls_fd < 0) { // Something is wrong with /dev/null, // so all writes are going to drop return; } } ASSERT(tls_fd >= 0); // Writes to /dev/null should always succeed. // Still, error check because if m_fd changes // for some reason, tls_fd needs to get // cleared so we pick up the new m_fd next time. auto ret = ::write(tls_fd, trc.c_str(), trc.length()); if (ret < 0 && errno == EINTR) { // Try once more before giving up ret = ::write(tls_fd, trc.c_str(), trc.length()); } if (ret < 0 && errno != EINTR) { g_logger.format(sinsp_logger::SEV_ERROR, "Unable to write tracer (%s) to %s: %s", trc.c_str(), m_file, strerror(errno)); close_fd(); } // We know ret >= 0 so size_t cast is safe else if ((size_t)ret != trc.length()) { ASSERT(false); g_logger.format(sinsp_logger::SEV_ERROR, "Incomplete write of tracer (%s) to %s", trc.c_str(), m_file); close_fd(); } return; }
fssh_ssize_t _kern_writev(int fd, fssh_off_t pos, const fssh_iovec *vecs, fssh_size_t count) { struct file_descriptor *descriptor; fssh_ssize_t bytesWritten = 0; fssh_status_t status; uint32_t i; descriptor = get_fd(get_current_io_context(true), fd); if (!descriptor) return FSSH_B_FILE_ERROR; if ((descriptor->open_mode & FSSH_O_RWMASK) == FSSH_O_RDONLY) { put_fd(descriptor); return FSSH_B_FILE_ERROR; } if (pos == -1) pos = descriptor->pos; if (descriptor->ops->fd_write) { for (i = 0; i < count; i++) { fssh_size_t length = vecs[i].iov_len; status = descriptor->ops->fd_write(descriptor, pos, vecs[i].iov_base, &length); if (status < FSSH_B_OK) { bytesWritten = status; break; } if ((uint32_t)bytesWritten + length > FSSH_SSIZE_MAX) bytesWritten = FSSH_SSIZE_MAX; else bytesWritten += (fssh_ssize_t)length; pos += vecs[i].iov_len; } } else bytesWritten = FSSH_B_BAD_VALUE; descriptor->pos = pos; put_fd(descriptor); return bytesWritten; }
int sys_open(userptr_t filename, int flags, mode_t mode, int* retval){ int copyerr; int openerr; size_t actual; char k_filename[MAX_FILENAME_SIZE]; int rw_flag = (3 & flags); // should be 0, 1, or 2 if(rw_flag > 2){ return EINVAL; } if(flags > 127){ return EINVAL; } copyerr = copyinstr(filename, k_filename, MAX_FILENAME_SIZE, &actual); if(copyerr != 0){ // Something went wrong return copyerr; } *(retval) = get_fd(curthread->t_fh_table); // Dereference retval to change its pointed-to value if(*(retval) == -1){ return EMFILE; } struct vnode* vn = kmalloc(sizeof(struct vnode)); openerr = vfs_open(k_filename, flags, mode, &vn); if(openerr != 0){ // Something else went wrong return openerr; } struct file_handle* fh; fh = fh_create(rw_flag, vn); curthread->t_fh_table[*(retval)] = fh; return 0; }
fssh_status_t _kern_ioctl(int fd, uint32_t op, void *buffer, fssh_size_t length) { struct file_descriptor *descriptor; int status; TRACE(("sys_ioctl: fd %d\n", fd)); descriptor = get_fd(get_current_io_context(true), fd); if (descriptor == NULL) return FSSH_B_FILE_ERROR; if (descriptor->ops->fd_ioctl) status = descriptor->ops->fd_ioctl(descriptor, op, buffer, length); else status = FSSH_EOPNOTSUPP; put_fd(descriptor); return status; }
FileFork* FileServer::new_filefork() { lock->lock("FileServer::open_file"); FileFork *dummy_fork = new FileFork(this); // Create real file fork on the server send_command(FileServer::NEW_FILEFORK, 0, 0); int parent_fd = get_fd(); read_result(); // Transfer fd to dummy file fork dummy_fork->start_dummy(parent_fd, *(int*)(result_data + sizeof(FileFork*))); dummy_fork->real_fork = *(FileFork**)result_data; // printf("FileServer::new_filefork %d parent_fd=%d real_fork=%p\n", // __LINE__, // parent_fd, // dummy_fork->real_fork); lock->unlock(); return dummy_fork; }
status_t _user_rewind_dir(int fd) { struct file_descriptor* descriptor; status_t status; TRACE(("user_rewind_dir(fd = %d)\n", fd)); descriptor = get_fd(get_current_io_context(false), fd); if (descriptor == NULL) return B_FILE_ERROR; if (descriptor->ops->fd_rewind_dir) status = descriptor->ops->fd_rewind_dir(descriptor); else status = EOPNOTSUPP; put_fd(descriptor); return status; }
json_t * rpi_i2c_byte_post(duda_request_t *dr, json_t *data, int parameter) { int fd = get_fd(dr); if (fd == -1) { return json->create_string("Invalid address."); } if (data->type != cJSON_Number) { return NULL; } int value = data->valueint; if (value < 0 || value > 255) { return NULL; } if (wiringPiI2CWrite(fd, value) != 0) { return json->create_string("Write failed."); } return json->create_string("Successful!"); }
fssh_status_t _kern_rewind_dir(int fd) { struct file_descriptor *descriptor; fssh_status_t status; TRACE(("sys_rewind_dir(fd = %d)\n",fd)); descriptor = get_fd(get_current_io_context(true), fd); if (descriptor == NULL) return FSSH_B_FILE_ERROR; if (descriptor->ops->fd_rewind_dir) status = descriptor->ops->fd_rewind_dir(descriptor); else status = FSSH_EOPNOTSUPP; put_fd(descriptor); return status; }
status_t _user_rewind_dir(int fd) { struct file_descriptor* descriptor; status_t status; TRACE(("user_rewind_dir(fd = %d)\n", fd)); descriptor = get_fd(get_current_io_context(false), fd); if (descriptor == NULL || (descriptor->open_mode & O_DISCONNECTED) != 0) return B_FILE_ERROR; if (descriptor->ops->fd_rewind_dir != NULL) status = descriptor->ops->fd_rewind_dir(descriptor); else status = B_UNSUPPORTED; put_fd(descriptor); return status; }
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)); }
int check_file(t_list **lst, int fd) { char c; t_maillon *tetris; if (!(tetris = (t_maillon*)malloc(sizeof(t_maillon)))) return (0); while (1) { if (!(get_fd(tetris, fd))) return (0); if (!(read_one_c(&c, fd))) return (0); if (!(check_tetris(tetris->tab))) return (0); put_tetris(lst, *tetris); if (c == 0) break ; } return (1); }
off_t _user_seek(int fd, off_t pos, int seekType) { syscall_64_bit_return_value(); struct file_descriptor* descriptor; descriptor = get_fd(get_current_io_context(false), fd); if (descriptor == NULL || (descriptor->open_mode & O_DISCONNECTED) != 0) return B_FILE_ERROR; TRACE(("user_seek(descriptor = %p)\n", descriptor)); if (descriptor->ops->fd_seek != NULL) pos = descriptor->ops->fd_seek(descriptor, pos, seekType); else pos = ESPIPE; put_fd(descriptor); return pos; }
int kopen(const char *path) { struct vnode *vn = NULL; int dev = is_dev(path); int offset = dev ? NONSEEK : 0; int next = get_fd(); if (next == -1) return -2; /* fd_table is full, can't open anything */ const char *name = (dev) ? path + 4 : path; int err = (dev) ? dev_open(&vn, name) : file_open(&vn, name); if (err) return -2; /* no such device or file */ fd_table[next] = fd_create(vn, offset); if (fd_table[next] == NULL) return -2; /* ENOMEM */ /* logically this should be done in file_open, dev_open, dir_open, but * complicates error handling */ vn->ref_count++; return next; }
static status_t fd_ioctl(bool kernelFD, int fd, uint32 op, void* buffer, size_t length) { struct file_descriptor* descriptor; int status; descriptor = get_fd(get_current_io_context(kernelFD), fd); if (descriptor == NULL || (descriptor->open_mode & O_DISCONNECTED) != 0) return B_FILE_ERROR; if (descriptor->ops->fd_ioctl != NULL) status = descriptor->ops->fd_ioctl(descriptor, op, buffer, length); else status = B_DEV_INVALID_IOCTL; if (status == B_DEV_INVALID_IOCTL) status = ENOTTY; put_fd(descriptor); return status; }
static int print_one_arg(t_param *buf) { char *standout; char buf2[30]; char *ap; ap = buf2; if ((buf->select % 2) == 1 && (standout = tgetstr("us", &ap)) != NULL) tputs(standout, 1, fputchar); if ((buf->select / 2) == 1 && (standout = tgetstr("so", &ap)) != NULL) tputs(standout, 1, fputchar); get_colors(buf->var); ft_dprintf(get_fd(0), "%s\x1b[0;m", buf->var); if ((buf->select % 2) == 1 && (standout = tgetstr("ue", &ap)) != NULL) tputs(standout, 1, fputchar); if ((buf->select / 2) == 1 && (standout = tgetstr("se", &ap)) != NULL) tputs(standout, 1, fputchar); if ((int)ft_strlen(buf->var) > get_width()) return (-1); return (0); }
int srslte_band_get_fd_band( uint32_t band, srslte_earfcn_t* earfcn, int start_earfcn, int end_earfcn, uint32_t max_elems) { uint32_t i, j; uint32_t nof_earfcn; i=0; while(i < SRSLTE_NOF_LTE_BANDS && lte_bands[i].band != band) { i++; } if (i >= SRSLTE_NOF_LTE_BANDS - 1) { ERROR("Error: Invalid band %d\n", band); return SRSLTE_ERROR; } if (end_earfcn == -1) { end_earfcn = lte_bands[i+1].dl_earfcn_offset-1; } else { if (end_earfcn > lte_bands[i+1].dl_earfcn_offset-1) { ERROR("Error: Invalid end earfcn %d. Max is %d\n", end_earfcn, lte_bands[i + 1].dl_earfcn_offset - 1); return SRSLTE_ERROR; } } if (start_earfcn == -1) { start_earfcn = lte_bands[i].dl_earfcn_offset; } else { if (start_earfcn < lte_bands[i].dl_earfcn_offset) { ERROR("Error: Invalid start earfcn %d. Min is %d\n", start_earfcn, lte_bands[i].dl_earfcn_offset); return SRSLTE_ERROR; } } nof_earfcn = end_earfcn - start_earfcn; if (nof_earfcn > max_elems) { nof_earfcn = max_elems; } for (j=0;j<nof_earfcn;j++) { earfcn[j].id = j + start_earfcn; earfcn[j].fd = get_fd(<e_bands[i], earfcn[j].id); } return (int)j; }
static int pop_fd (int *fdp, SLFile_FD_Type **fp, SLang_MMT_Type **mmtp) { int fd; *fp = NULL; *mmtp = NULL; switch (SLang_peek_at_stack ()) { case SLANG_FILE_PTR_TYPE: { SLang_MMT_Type *mmt; FILE *p; if (-1 == SLang_pop_fileptr (&mmt, &p)) return -1; fd = fileno (p); *mmtp = mmt; } break; case SLANG_FILE_FD_TYPE: { SLFile_FD_Type *f; if (-1 == SLfile_pop_fd (&f)) return -1; if (-1 == get_fd (f, &fd)) { SLfile_free_fd (f); return -1; } } break; default: if (-1 == SLang_pop_int (&fd)) return -1; } *fdp = fd; return 0; }
/* Not yet a public function */ static int SLfile_dup2_fd (SLFile_FD_Type *f0, int newfd) { int fd0, fd; if ((f0 == NULL) || (-1 == get_fd (f0, &fd0))) { #ifdef EBADF SLerrno_set_errno (EBADF); #endif return -1; } while (-1 == (fd = dup2 (fd0, newfd))) { if (is_interrupt (errno, 1)) continue; return -1; } return fd; }
int lte_band_get_fd_band(int band, lte_earfcn_t *earfcn, int start_earfcn, int end_earfcn, int max_elems) { int i, j; int nof_earfcn; i=0; while(i < NOF_LTE_BANDS && lte_bands[i].band != band) { i++; } if (i == NOF_LTE_BANDS) { fprintf(stderr, "Error: Invalid band %d\n", band); return -1; } if (end_earfcn == -1) { end_earfcn = lte_bands[i].earfcn_max; } else { if (end_earfcn > lte_bands[i].earfcn_max) { fprintf(stderr, "Error: Invalid end earfcn %d. Max is %d\n", end_earfcn, lte_bands[i].earfcn_max); return -1; } } if (start_earfcn == -1) { start_earfcn = lte_bands[i].earfcn_offset; } else { if (start_earfcn < lte_bands[i].earfcn_offset) { fprintf(stderr, "Error: Invalid start earfcn %d. Min is %d\n", start_earfcn, lte_bands[i].earfcn_offset); return -1; } } nof_earfcn = end_earfcn - start_earfcn; if (nof_earfcn > max_elems) { nof_earfcn = max_elems; } for (j=0;j<nof_earfcn;j++) { earfcn[j].id = j + start_earfcn; earfcn[j].fd = get_fd(<e_bands[i], earfcn[j].id); } return j; }
void X11Window::setup_joysticks() { for (auto & elem : joysticks) elem.get_provider()->dispose(); joysticks.clear(); #ifdef HAVE_LINUX_JOYSTICK_H std::string joydev; if (access("/dev/input/", R_OK | X_OK) == 0) joydev = "/dev/input/js%1"; else joydev = "/dev/js%1"; const int max_joysticks = 16; for(int i = 0; i < max_joysticks; ++i) { std::string pathname = string_format(joydev, i); if (access(pathname.c_str(), R_OK) == 0) { try { auto joystick_provider = new InputDeviceProvider_LinuxJoystick(this, pathname); InputDevice device(joystick_provider); joysticks.push_back(device); current_window_events.push_back(joystick_provider->get_fd()); } catch (Exception error) { log_event("debug", "Joystick Error: %1", error.message); } } } #endif }
int lseek(int fd, user_ptr<off_t> _poffset, int whence) { off_t* const poffset = _poffset.get(); if (unlikely(!poffset)) return -EFAULT; int ret; shared_ptr<file> f; ret = get_fd(fd, f); if (unlikely(ret != 0)) return ret; off_t pos = f->position; switch (whence) { case SEEK_SET: pos = *poffset; break; case SEEK_CUR: pos += *poffset; break; case SEEK_END: pos = f->nd->ind->size + *poffset; break; default: return -EINVAL; } ret = f->seek(pos); // update new position if (likely(!ret)) *poffset = pos; return ret; }
bool TcpServerBase::poll() const { UniqueFile client(::accept(get_fd(), NULLPTR, NULLPTR)); if(!client){ if(errno != EAGAIN){ DEBUG_THROW(SystemException); } return false; } AUTO(session, on_client_connect(STD_MOVE(client))); if(!session){ LOG_POSEIDON_WARNING("on_client_connect() returns a null pointer."); DEBUG_THROW(Exception, sslit("Null client pointer")); } if(m_ssl_factory){ AUTO(ssl, m_ssl_factory->create_ssl()); boost::scoped_ptr<SslFilterBase> filter(new SslFilter(STD_MOVE(ssl), session->get_fd())); session->init_ssl(STD_MOVE(filter)); } session->set_timeout(EpollDaemon::get_tcp_request_timeout()); EpollDaemon::add_session(session); LOG_POSEIDON_INFO("Accepted TCP connection from ", session->get_remote_info()); return true; }
int tcp_create_socket(int reuse_addr) { int retval; int yes = 1; if ((retval = socket(AF_INET, SOCK_STREAM, 0)) < 0) { debug(DEBUG_ERROR, "tcp: can't create socket"); } if (reuse_addr) { setsockopt( retval, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(int)); } debug(DEBUG_TCP, "tcp: socket created"); #ifdef WIN32 return get_fd(retval, WIN32_SOCKET); #else return retval; #endif }
fssh_ssize_t _kern_read_dir(int fd, struct fssh_dirent *buffer, fssh_size_t bufferSize, uint32_t maxCount) { struct file_descriptor *descriptor; fssh_ssize_t retval; TRACE(("sys_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = %lu)\n",fd, buffer, bufferSize, maxCount)); descriptor = get_fd(get_current_io_context(true), fd); if (descriptor == NULL) return FSSH_B_FILE_ERROR; if (descriptor->ops->fd_read_dir) { uint32_t count = maxCount; retval = descriptor->ops->fd_read_dir(descriptor, buffer, bufferSize, &count); if (retval >= 0) retval = count; } else retval = FSSH_EOPNOTSUPP; put_fd(descriptor); return retval; }
/// \method register(obj[, eventmask]) STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) { mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); bool is_fd = mp_obj_is_int(args[1]); int fd = get_fd(args[1]); mp_uint_t flags; if (n_args == 3) { flags = mp_obj_get_int(args[2]); } else { flags = POLLIN | POLLOUT; } struct pollfd *free_slot = NULL; struct pollfd *entry = self->entries; for (int i = 0; i < self->len; i++, entry++) { int entry_fd = entry->fd; if (entry_fd == fd) { entry->events = flags; return mp_const_false; } if (entry_fd == -1) { free_slot = entry; } } if (free_slot == NULL) { if (self->len >= self->alloc) { self->entries = m_renew(struct pollfd, self->entries, self->alloc, self->alloc + 4); if (self->obj_map) { self->obj_map = m_renew(mp_obj_t, self->obj_map, self->alloc, self->alloc + 4); } self->alloc += 4; } free_slot = &self->entries[self->len++]; }
inline FDGetter(io_context* context, int fd, bool contextLocked = false) : AutoLocker<file_descriptor, FDGetterLocking>( contextLocked ? get_fd_locked(context, fd) : get_fd(context, fd)) { }