void *readT(){ int readRes =1; char x='\0'; while(readRes){ if(pipe_size==1){ if(pipe_wait==0){ printf("Pipe buf is empty\n"); while(pipe_wait==0){} } readRes=pipe_read(&x); pipe_wait=0; printf("Pipe read char: %c\n", x); }else{ while(i_read==i_write){ printf("Pipe buf is empty\n"); pipe_wait=0; while(pipe_wait==0){} } readRes=pipe_read(&x); if(readRes==0)break; pipe_wait=0; printf("Pipe read char: %c\n", x); i_read=(i_read+1)%pipe_size; } } pipe_wait = 2; return NULL; }
static void *log_commit_thread (void *arg) { INFO0 ("started"); while (1) { int ret = util_timed_wait_for_fd (logger_fd[0], 5000); if (ret == 0) continue; if (ret > 0) { char cm[80]; ret = pipe_read (logger_fd[0], cm, sizeof cm); if (ret > 0) { // fprintf (stderr, "logger woken with %d\n", ret); log_commit_entries (); continue; } } if (ret < 0 && sock_recoverable (sock_error())) continue; int err = sock_error(); sock_close (logger_fd[0]); sock_close (logger_fd[1]); if (worker_count) { worker_control_create (logger_fd); ERROR1 ("logger received code %d", err); continue; } // fprintf (stderr, "logger closed with zero workers\n"); break; } return NULL; }
uint32_t read(int fd, void* buf, uint32_t count) { if (!tasking_installed()) { return -1; } if (!count) { return 0; } unsigned char* chbuf = buf; memset(chbuf, 0, count); //find fd_entry corresponding to this fd task_t* current = task_with_pid(getpid()); fd_entry ent = current->fd_table[fd]; if (fd_empty(ent)) { //errno = EBADF; return -1; } //what type of file descriptor is this? //dispatch appropriately switch (ent.type) { case STD_TYPE: return std_read(current, fd, buf, count); case FILE_TYPE: return fread(buf, sizeof(char), count, (FILE*)ent.payload); case PIPE_TYPE: default: return pipe_read(fd, buf, count); } return -1; }
int get_work(int fp) { int read_size; char buf[128]; while ((read_size = pipe_read(fp, buf)) < 1); buf[read_size] = 0; printf("Buff Read %d bytes: %s\n", read_size, buf); if (!strncmp(buf, "CMD_LISTEN", read_size)) return CMD_LISTEN; if (!strncmp(buf, "CMD_CONNECT", read_size)) return CMD_CONNECT; if (!strncmp(buf, "CMD_SEND_PKT", read_size)) return CMD_SEND_PKT; if (strstr(buf, "CMD_CHNG_WINDOW_SIZE") != NULL) { /* return the first char after the '=' token */ char *new_window_sz_str = (strstr(buf, "=")); window_size = atoi(++new_window_sz_str); return CMD_CHNG_WINDOW_SIZE; } return -1; }
/* * Wrapper around pread() that checks for errors and keeps retrying until all * requested bytes have been read or until end-of file has occurred. This also * transparently handle reading from pipe files, but the caller needs to be sure * the requested offset is greater than or equal to the current offset, or else * WIMLIB_ERR_RESOURCE_ORDER will be returned. * * Return values: * WIMLIB_ERR_SUCCESS (0) * WIMLIB_ERR_READ (errno set) * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to EINVAL) * WIMLIB_ERR_RESOURCE_ORDER (errno set to ESPIPE) */ int full_pread(struct filedes *fd, void *buf, size_t count, off_t offset) { if (fd->is_pipe) goto is_pipe; while (count) { ssize_t ret = pread(fd->fd, buf, count, offset); if (unlikely(ret <= 0)) { if (ret == 0) { errno = EINVAL; return WIMLIB_ERR_UNEXPECTED_END_OF_FILE; } if (errno == EINTR) continue; if (errno == ESPIPE) { fd->is_pipe = 1; goto is_pipe; } return WIMLIB_ERR_READ; } buf += ret; count -= ret; offset += ret; } return 0; is_pipe: return pipe_read(fd, buf, count, offset); }
// Create anonymous pipe, preventing inheritance of the read pipe and setting // security of the write pipe to sa. static bool _create_anonymous_pipe(unique_handle* pipe_read_out, unique_handle* pipe_write_out, SECURITY_ATTRIBUTES* sa) { HANDLE pipe_read_raw = NULL; HANDLE pipe_write_raw = NULL; if (!CreatePipe(&pipe_read_raw, &pipe_write_raw, sa, 0)) { fprintf(stderr, "Cannot create pipe: %s\n", android::base::SystemErrorCodeToString(GetLastError()).c_str()); return false; } unique_handle pipe_read(pipe_read_raw); pipe_read_raw = NULL; unique_handle pipe_write(pipe_write_raw); pipe_write_raw = NULL; if (!_make_handle_noninheritable(pipe_read.get())) { return false; } *pipe_read_out = std::move(pipe_read); *pipe_write_out = std::move(pipe_write); return true; }
static void event_handler(int flags, void *arg) { struct mqueue *mq = arg; struct msg msg; ssize_t n; if (!(flags & FD_READ)) return; n = pipe_read(mq->pfd[0], &msg, sizeof(msg)); if (n < 0) return; if (n != sizeof(msg)) { (void)re_fprintf(stderr, "mqueue: short read of %d bytes\n", n); return; } if (msg.magic != MAGIC) { (void)re_fprintf(stderr, "mqueue: bad magic on read (%08x)\n", msg.magic); return; } mq->h(msg.id, msg.data, mq->arg); }
ssize_t lisp_read(HANDLE hfile, void *buf, unsigned int count) { switch(GetFileType(hfile)) { case FILE_TYPE_CHAR: return console_read(hfile, buf, count); break; case FILE_TYPE_PIPE: /* pipe or one of these newfangled socket things */ { int socktype, optlen = sizeof(int); if ((getsockopt((SOCKET)hfile, SOL_SOCKET, SO_TYPE, (char *)&socktype, &optlen) != 0) && (GetLastError() == WSAENOTSOCK)) { return pipe_read(hfile, buf, count); } } /* It's a socket, fall through */ case FILE_TYPE_DISK: return lisp_standard_read(hfile, buf, count); break; default: errno = EBADF; return -1; } }
int open(const char *filename, int flags, ...) { static int (*open_orig)(const char *, int, mode_t); int ret; va_list ap; mode_t mode; if (!open_orig) { open_orig = dlsym(RTLD_NEXT, "open"); } va_start(ap, flags); mode = va_arg(ap, mode_t); va_end(ap); if(strcmp(filename,"/tmp/a") != 0) { ret = open_orig(filename, flags, mode); //ret = syscall(SYS_open,redirect_name(filename),flags,mode); } else { //ret = fileno(popen("echo aaaa", "rb")); if((flags & O_WRONLY) || (flags & O_RDWR)) { ret = pipe_write(); } else { ret = pipe_read(); } //ssize_t write(int fd, const void *buf, size_t count); } //printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); return ret; }
/* * read method of the file structure */ unsigned int wav_read(struct file *file, unsigned char *data, unsigned int count) { struct wav *f = (struct wav *)file; unsigned int n; if (f->map) count /= sizeof(adata_t); if (f->rbytes >= 0 && count > f->rbytes) { count = f->rbytes; /* file->rbytes fits in count */ if (count == 0) { #ifdef DEBUG if (debug_level >= 3) { wav_dbg(f); dbg_puts(": read complete\n"); } #endif if (!f->mmc) file_eof(&f->pipe.file); return 0; } } n = pipe_read(file, data, count); if (n == 0) return 0; if (f->rbytes >= 0) f->rbytes -= n; if (f->map) { wav_conv(data, n, f->map); n *= sizeof(adata_t); } return n; }
int main(int argc, char **argv) { int res; int i; struct voxind_t *v; struct sigaction act; #ifdef DEBUG { struct stat buf; while (!stat(VOXIND_DBG, &buf)) { sleep(1); } } #endif ENTER(); BUILD_ASSERT(PIPE_MAX_BLOCK > MIN_MSG_SIZE); memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = sighandler; for (i = 1; i < NSIG; i++) { sigaction(i, &act, NULL); } my_voxind = calloc(1, sizeof(struct voxind_t)); if (!my_voxind) { res = errno; goto exit0; } my_voxind->msg = calloc(1, PIPE_MAX_BLOCK); if (!my_voxind->msg) { res = errno; goto exit0; } my_voxind->msg_length = PIPE_MAX_BLOCK; res = pipe_restore(&my_voxind->pipe_command, PIPE_COMMAND_FILENO); if (res) goto exit0; atexit(my_exit); do { size_t msg_length = my_voxind->msg_length; if(pipe_read(my_voxind->pipe_command, my_voxind->msg, &msg_length)) goto exit0; if (unserialize(my_voxind->msg, &msg_length)) goto exit0; pipe_write(my_voxind->pipe_command, my_voxind->msg, &msg_length); } while (1); exit0: err("LEAVE, (err=%d)",res); return res; }
void c_pipe(run_params* par) { int c = 0; while (1) { c = pipe_read(par->in); if (c == -1) break; pipe_write(par->out, c); } }
void c_cmd_run(run_params* params) { int c, ac_in, ac_out, ac_err; int i = 0; int out_path = 1; char* line = (char*)malloc(sizeof(char) * MAX_LINE_LEN); pipe_out* in = params->in; pipe_in* out = params->out; pipe_in* err = params->err; if (out_path && pipe_out_is_keyboard(in)) write_path(params); ac_in = params->in->autoclose; ac_out = params->out->autoclose; ac_err = params->err->autoclose; params->in->autoclose = 0; params->out->autoclose = 0; params->err->autoclose = 0; int pos = 0; while (true) { c = pipe_read(in); if (pos >= MAX_CMD_LEN) { pipe_write_s(err, "Maximal length of a line reached.\n"); break; } if ((c == CMD_END_CHAR_LINE) || (c == -1)) { line[pos] = '\0'; int ret = parse_line(line, params); if (ret == 1) break; pos = 0; if (c == -1) { if (params->secret_params == 1) { ac_in = 1; ac_out = 1; ac_err = 1; } break; } if (out_path && pipe_out_is_keyboard(in)) write_path(params); } else { line[pos] = c; pos++; } } free(line); params->in->autoclose = ac_in; params->out->autoclose = ac_out; params->err->autoclose = ac_err; }
void *mill_piperecv(struct mill_pipe_s *mp, int *done) { void *ptr = mill_valbuf(mill->running, mp->sz); mill_assert(done); int rc = pipe_read(mp, ptr); if (mill_slow(rc == -1)) mill_panic(strerror(errno)); /* Hmm! */ *done = (rc == 0); return ptr; }
/* * The 'connect_xxx()' functions are needed for named pipes when * the open() code hasn't guaranteed a connection (O_NONBLOCK), * and we need to act differently until we do get a writer.. */ static ssize_t connect_read(struct file * filp, char * buf, size_t count, loff_t *ppos) { struct inode * inode = filp->f_dentry->d_inode; if (PIPE_EMPTY(*inode) && !PIPE_WRITERS(*inode)) return 0; filp->f_op = &read_fifo_fops; return pipe_read(filp,buf,count,ppos); }
int Y_PipeRead(int pipe_id, void *buf, int len, UserContext *user_context) { log_info("PID(%d) going to read from pipe %d", running_proc->pid, pipe_id); pipe_t *pipe = (pipe_t*)util_get(pipe_id); log_info("Got pipe pointer %p", pipe); if(pipe == NULL) { log_err("Cannot get pipe %d from hashmap", pipe->id); } return pipe_read(pipe, buf, len, user_context); }
/** * handle_client: Main application layer thread for each client * @author ndemarinis (Basic implementation) */ void *handle_client(void *data) { struct client_handler_data *clnt = (struct client_handler_data *)data; int pipes[2]; // Make a pipe to connect to the layer stack int to_read, bytes_written; char read_buffer[PIPE_BUFFER_SIZE]; struct packet* pkt_in; pid_t clnt_pid; // PID we receive from the cilent before startup struct layer_stack *stack; // Work data for layer stack implementation memset(read_buffer, 0, PIPE_BUFFER_SIZE); // Receive the client's PID for use as an identifier. if((recv(clnt->sock, &clnt_pid, sizeof(pid_t), 0) != sizeof(pid_t))) die_with_error("Error receiving PID from client!"); stack = create_layer_stack(clnt->sock, clnt_pid, pipes); // Initialize all of our layer threads sleep(1); // Wait for the layer stack creation to settle for(;;) { // Just try and echo a message for now. printf("%d: APP: Starting a test read.\n\n", clnt_pid); // Grab a string if((to_read = read(pipe_read(pipes), read_buffer, PIPE_BUFFER_SIZE)) <= 0) { printf("%d: APP: Read 0 bytes from socket. Terminating!\n", clnt_pid); break; } pkt_in = (struct packet *)read_buffer; printf("%d: APP: Read packet of %d bytes with payload of %d bytes\n", clnt_pid, to_read, pkt_in->length); // Send it straight back printf("%d: APP: Sending packet of %d bytes back to client\n", clnt_pid, to_read); if((bytes_written = write(pipe_write(pipes), read_buffer, to_read)) <= 0) { printf("%d: APP: Wrote %d bytes, socket must have closed. Terminating!\n", clnt_pid, bytes_written); break; } } printf("%d: Client successfully terminated!\n", clnt_pid); pthread_exit(NULL); }
static void iokit_forward_notifications(void *opaque) { uint8_t byte; (void)opaque; if (pipe_read(&_notification_pipe, &byte, sizeof(byte)) < 0) { log_error("Could not read from notification pipe: %s (%d)", get_errno_name(errno), errno); return; } usb_reopen(); }
static ssize_t IMFS_fifo_read( rtems_libio_t *iop, void *buffer, size_t count ) { IMFS_jnode_t *jnode = iop->pathinfo.node_access; int err = pipe_read(JNODE2PIPE(jnode), buffer, count, iop); if (err > 0) IMFS_update_atime(jnode); IMFS_FIFO_RETURN(err); }
ssize_t IMFS_fifo_read( epos_libio_t *iop, void *buffer, size_t count ) { IMFS_jnode_t *jnode = iop->file_info; int err = pipe_read(JNODE2PIPE(jnode), buffer, count, iop); if (err > 0) IMFS_update_atime(jnode); IMFS_FIFO_RETURN(err); }
void scan(run_params* par) { while (true) { int c = pipe_read(par->in); if (c == -1) break; char str[10]; int i; for (i = 0; i < 10; i++) { str[i] = '\0'; } sprintf_s(str, "%d\n", c); pipe_write_s(par->out, str); } }
void wc(run_params* par) { if (par->argc >= 1) { char* filename = par->args[0]; node* n = node_get(filename, par->root_node, par->start_node); if (n == NULL) { pipe_write_s(par->err, "File does not exist.\n"); return; } if (n->directory) { pipe_write_s(par->err, "File is a directory.\n"); return; } if (!node_try_lock(n)) { pipe_write_s(par->err, "Can't open the file.\n"); return; } wc_count(par, n->content, n->name); node_unlock(n); } else { int pos, c = -1, buf_size = 300; char* buf = (char*)malloc(sizeof(char) * buf_size); pos = 0; while (true) { c = pipe_read(par->in); if ((c == -1)) break; if (pos == buf_size - 1) { buf_size *= 2; buf = (char*)realloc(buf, sizeof(char*) * buf_size); } buf[pos] = c; pos++; } buf[pos] = '\0'; wc_count(par, buf, ""); free(buf); buf = NULL; } }
/** * Reads at most length bytes from the file identified by filehandle into the * specified buffer, starting at the current position, and advancing the file * position. * * @return The number of bytes actually read. It is invalid to read from * FILEHANDLE_STDOUT or FILEHANDLE_STDERR. In this case, VFS_INVALID_PARAMS is * returned. */ int io_read(openfile_t file, void* buffer, int length) { int res; // Don't pipe if writing to std if (file > 2) { // Get the pipe for the file pipe_t *pipe = pipe_get_pipe(file); // Don't read to empty pipes if (pipe == NULL) { KERNEL_PANIC("Cannot read from empty pipe\n"); } // Get the read end and update the file file = pipe->read_end; // Pipe read to the buffer pipe_read(buffer, length, pipe); } switch(file) { case FILEHANDLE_STDIN: res = tty_read(buffer, length); break; case FILEHANDLE_STDOUT: case FILEHANDLE_STDERR: res = VFS_INVALID_PARAMS; break; default: file -= 3; if (!process_has_open_file(file)) { res = VFS_NOT_OPEN_IN_PROCESS; } else { res = vfs_read(file, buffer, length); } } return res; }
void sort(run_params* par) { int i,pos, c = -1, arr_pos = 0, arr_size = 200; char* buf = (char*)malloc(sizeof(char) * SORT_SIZE); char** arr = (char**)malloc(sizeof(char *) * arr_size); while (true) { pos = 0; while (true) { c = pipe_read(par->in); if ((c == 10) || (c == -1)) break; buf[pos] = c; if (pos == SORT_SIZE) pos = 0; // do not accept lines longer than 500 pos++; } buf[pos] = '\0'; arr[arr_pos++] = buf; buf = (char*)malloc(sizeof(char) * SORT_SIZE); if (buf == NULL) goto end; if (arr_pos == arr_size) { arr_size += 200; arr = (char**)realloc(arr, sizeof(char*) * arr_size); } if (c == -1) break; } qsort(arr, arr_pos, sizeof(char*), cmp_func); for (i = 0; i < arr_pos; i++) { printf("%s\n", arr[i]); } end: for (i = 0; i < arr_pos; i++) { free(arr[i]); } free(arr); return; }
void serial_port_ertsr (struct serial_port* port) { int r; DBG_LOG(fprintf(debug_log_file, "event: ertsr\n")); /* Try to read data from erts. This event was called because * we believe there may be data available now which needs to * be copied into the outgoing buffer. */ r = pipe_read( port->erts, port->outgoing + port->outgoingLen, port->outgoingBufSz - port->outgoingLen); DBG_LOG(fprintf(debug_log_file, "ertsr: pipe_read = %i\n", r)); /* If new data was received during this call, update the buffer * state. If the serial port is writable, the packets will be * processed, but not until that event is fired. Its bad form * to try and write to the serial port if its not ready (at least * on the Windows implementations). */ if (r > 0) { port->outgoingLen += r; return; } port->isDead = 1; /* EOF or read error */ if (pipe_last_error(port->erts)) { int ecode; char msg[256]; ecode = pipe_last_error(port->erts); pipe_format_error(port->erts, ecode, msg, sizeof(msg)); DBG_LOG(fprintf(debug_log_file, "pipe_read error: %i %s\n", ecode, msg)); } }
void freq(run_params* par) { int arr[FREQ_SIZE], i; char buf[50]; for (i = 0; i < FREQ_SIZE; i++) arr[i] = 0; while (true) { int c = pipe_read(par->in); if (c == -1) break; if ((c < 0) || (c >= FREQ_SIZE)) continue; arr[c]++; } for (i = 0; i < FREQ_SIZE; i++) { if (arr[i] > 0) { sprintf_s(buf, "0x%hhx : %d\n", i, arr[i]); pipe_write_s(par->out, buf); } } }
void* worker_thread(void* myid){ stats_message work_stats; request recived_request; pthread_mutex_lock(&mutex); recived_request=pipe_read(0); /* Search file with html page and send to client*/ send_page(recived_request.socket_id); work_stats.mtype=1; strcpy(work_stats.request_type,recived_request.request_type); strcpy(work_stats.file,recived_request.request_file); work_stats.thread_number=(int)myid; work_stats.entry_time[0]=1; work_stats.entry_time[1]=2; work_stats.end_time[0]=3; work_stats.end_time[1]=4; if(msgsnd(queue, &work_stats, sizeof(stats_message)-sizeof(long), 0)==-1){/*está a dar erro aqui*/ perror("Message Queue Sending"); } printf("[Worker]Data to write on file\n"); printf("--------------------------------\n"); printf("\tMyType:%lu\n",work_stats.mtype); printf("\tRequest Type:%s\n",work_stats.request_type); printf("\tFile:%s\n",work_stats.file); printf("\tThread Number:%d\n",work_stats.thread_number); printf("\tEntry Time:%d:%d\n",work_stats.entry_time[0],work_stats.entry_time[1]); printf("\tEnd Time:%d:%d\n",work_stats.end_time[0],work_stats.end_time[1]); printf("-------------------------------\n"); pthread_mutex_unlock(&mutex); /*depois de efectuar os pedidos vai para meter na message queue*/ }
static void process_handle_state_change(void *opaque) { Process *process = opaque; ProcessStateChange change; if (pipe_read(&process->state_change_pipe, &change, sizeof(change)) < 0) { log_error("Could not read from state change pipe for child process (executable: %s, pid: %u): %s (%d)", process->executable->buffer, process->pid, get_errno_name(errno), errno); return; } process->state = change.state; process->timestamp = change.timestamp; process->exit_code = change.exit_code; if (!process_is_alive(process)) { process->pid = 0; } if (process->state_changed != NULL) { process->state_changed(process->opaque); } // only send a process-state-changed callback if there is at least one // external reference to the process object. otherwise there is no one that // could be interested in this callback anyway. also this logic avoids // sending process-state-changed callbacks for scheduled program executions if (process->base.external_reference_count > 0) { api_send_process_state_changed_callback(process->base.id, change.state, change.timestamp, change.exit_code); } if (process->release_on_death && !process_is_alive(process)) { process->release_on_death = false; // only release-on-death once object_remove_internal_reference(&process->base); } }
void parent_rdwr (struct pipes *pipes, fd_set *rfds, fd_set *wfds, int nFds, int nForks) { int i = 0; int nChecked_fds = 0; i = 0; while (nChecked_fds < nFds) { if (FD_ISSET (pipes[i].rfd, rfds)) { if (!pipe_read (&pipes[i])) { if(close (pipes[i].rfd) == -1) ERROR("CLOSE ERROR5"); } nChecked_fds++; } if (FD_ISSET (pipes[i].wfd, wfds)) { pipe_write (&pipes[i]); if (pipes[i].can_write == 2 && pipes[i].wfd != STDOUT_FILENO) { if (close (pipes[i].wfd) == -1) ERROR("CLOSE ERROR6"); } nChecked_fds++; } i++; } }
/* return Wert: -1,0 : Fehler 1 : Pipe ist ready 2 : Socket lesbar */ int ueberwacheFd(int pipe_fd[],char *getText){ //Observe the pi(p)e with the magic of Gandalf the Grey int socket_fd = getSocketFd(); fd_set set; FD_ZERO(&set); FD_SET(pipe_fd[READ],&set); FD_SET(socket_fd,&set); int max = pipe_fd[READ]; if (max < socket_fd) { max = socket_fd; } int ret = select(max+1,&set,NULL,NULL,NULL); if(ret < 1){ log_printf(LOG_ERROR,"Fehler bei Select\n"); perror("error"); return 0; } else if (FD_ISSET(pipe_fd[READ],&set)) { log_printf(LOG_DEBUG,"Gandalf hat gedacht.\n"); return pipe_read(pipe_fd, getText); } log_printf(LOG_DEBUG,"Gandalf denkt zu langsam.\n"); return 2; }