void *writeT(){ char x; while(1){ if(pipe_size==1){ if(pipe_wait==1){ printf("Pipe buf is full\n"); while(pipe_wait==1){} } scanf(" %c", &x); pipe_write(x); pipe_wait=1; }else{ while(i_read == ((i_write+1)%pipe_size)){ printf("Pipe buf is full\n"); pipe_wait = 1; while(pipe_wait==1){} } scanf(" %c", &x); pipe_write(x); pipe_wait=1; i_write = (i_write+1)%pipe_size; } if(x==c_term){ pipe_close(); break; } } return NULL; }
/* * close method of the file structure */ void wav_close(struct file *file) { struct wav *f = (struct wav *)file, **pf; if (f->mode & MODE_RECMASK) { pipe_trunc(&f->pipe.file, f->endpos); if (f->hdr == HDR_WAV) { wav_writehdr(f->pipe.fd, &f->hpar, &f->startpos, f->endpos - f->startpos); } } pipe_close(file); if (f->pstate != WAV_CFG) dev_unref(f->dev); for (pf = &wav_list; *pf != f; pf = &(*pf)->next) { #ifdef DEBUG if (*pf == NULL) { dbg_puts("wav_close: not on list\n"); dbg_panic(); } #endif } *pf = f->next; }
int close(int fd) { task_t* curr = task_with_pid(getpid()); fd_entry ent = curr->fd_table[fd]; if (fd_empty(ent)) { return -1; } int ret = -1; switch (ent.type) { case STD_TYPE: ret = -1; break; case FILE_TYPE: fclose((FILE*)ent.payload); ret = 0; break; case PIPE_TYPE: default: ret = pipe_close(fd); break; } fd_remove(curr, fd); return ret; }
/** * Free an Ecore_Pipe object created with ecore_pipe_add(). * * @param p The Ecore_Pipe object to be freed. * @return The pointer to the private data * @ingroup Ecore_Pipe_Group */ EAPI void *ecore_pipe_del(Ecore_Pipe * p) { void *data; if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_del"); return NULL; } if (p->fd_handler) ecore_main_fd_handler_del(p->fd_handler); if (p->fd_read != PIPE_FD_INVALID) pipe_close(p->fd_read); if (p->fd_write != PIPE_FD_INVALID) pipe_close(p->fd_write); data = (void *) p->data; free(p); return data; }
void * _ecore_pipe_del(Ecore_Pipe *p) { void *data = NULL; if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_del"); return NULL; } p->delete_me = EINA_TRUE; if (p->handling > 0) return (void *)p->data; if (p->fd_handler) _ecore_main_fd_handler_del(p->fd_handler); if (p->fd_read != PIPE_FD_INVALID) pipe_close(p->fd_read); if (p->fd_write != PIPE_FD_INVALID) pipe_close(p->fd_write); data = (void *)p->data; ecore_pipe_mp_free(p); return data; }
/** * Close the write end of an Ecore_Pipe object created with ecore_pipe_add(). * * @param p The Ecore_Pipe object. * @ingroup Ecore_Pipe_Group */ EAPI void ecore_pipe_write_close(Ecore_Pipe * p) { if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write_close"); return; } pipe_close(p->fd_write); p->fd_write = PIPE_FD_INVALID; }
int main() { int fd; int pid; char temp[1000]; char str[1000]; int n ; //Pipe output through a GZIP program fd = pipe_output_command ( "gzip", "pipe_out.txt.gz", &pid ) ; write ( fd, text, strlen(text)) ; pipe_close(fd, pid); //Read input through a GUNZIP program fd = pipe_input_command ( "gunzip", "pipe_out.txt.gz", &pid ); //Read entire input from the pipe. //Note: // 'read' doesn't have to return the entire input in one read - so // we loop until the end of the file and concatnate the input. strcat(str,""); while (1) { n = read ( fd, temp, sizeof(temp)-1) ; if (n==-1) err(1,"Read from GZIP input pipe failed"); if (n==0) break; //End-Of-File //Ensure proper NULL termination temp[n] = 0 ; strncpy ( str, temp, sizeof(str)-1); } pipe_close(fd,pid); //Validate the input ASSERT ( strcmp(str, text)==0 ) ; return 0 ; }
/** * Close the read end of an Ecore_Pipe object created with ecore_pipe_add(). * * @param p The Ecore_Pipe object. * @ingroup Ecore_Pipe_Group */ EAPI void ecore_pipe_read_close(Ecore_Pipe * p) { if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_close"); return; } ecore_main_fd_handler_del(p->fd_handler); p->fd_handler = NULL; pipe_close(p->fd_read); p->fd_read = PIPE_FD_INVALID; }
int disp_pipe_init(void) { int err = 0; g_DISP_CmdIn = pipe_init(DISP_FIFO1); if (!g_DISP_CmdIn) goto init_fail; g_DISP_CmdOut = pipe_init(DISP_FIFO2); if (!g_DISP_CmdOut) goto init_fail; return 0; init_fail: pipe_close(g_DISP_CmdIn); pipe_close(g_DISP_CmdOut); g_DISP_CmdIn = g_DISP_CmdOut = 0; return -1; }
int dvr_pipe_init(void) { int err = 0; g_CmdIn = pipe_init(FIFO1); if (!g_CmdIn) goto init_fail; g_CmdOut = pipe_init(FIFO2); if (!g_CmdOut) goto init_fail; pthread_mutex_init(&mutex_pipewrite, NULL); return 0; init_fail: pipe_close(g_CmdIn); pipe_close(g_CmdOut); return -1; }
void serial_port_destroy (struct serial_port* port) { if (port->erts) { pipe_close(port->erts); } if (port->chan) { serial_close(port->chan); } free(port->outgoing); free(port->incoming); free(port->incomingHeader); free(port); }
static void test_fgetln_single(void) { FILE *fp; size_t len; int i; fp = pipe_feed("%s", (const void **)data_ascii, DATA_LINES); for (i = 0; i < DATA_LINES; i++) { char *str = fgetln(fp, &len); assert(str); assert(memcmp(str, data_ascii[i], len) == 0); } assert(fgetln(fp, &len) == NULL); pipe_close(fp); }
/** * Close the write end of an Ecore_Pipe object created with ecore_pipe_add(). * * @param p The Ecore_Pipe object. */ EAPI void ecore_pipe_write_close(Ecore_Pipe *p) { _ecore_lock(); if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write_close"); goto out; } if (p->fd_write != PIPE_FD_INVALID) { pipe_close(p->fd_write); p->fd_write = PIPE_FD_INVALID; } out: _ecore_unlock(); }
static void test_fgetwln_single(void) { FILE *fp; size_t len; int i; fp = pipe_feed("%ls", (const void **)data_wide, DATA_LINES); for (i = 0; i < DATA_LINES; i++) { wchar_t *wstr; wstr = fgetwln(fp, &len); assert(wstr); assert(wmemcmp(data_wide[i], wstr, len) == 0); } assert(fgetwln(fp, &len) == NULL); pipe_close(fp); }
int notice_init(event_base_t *base, notice_t *n, wake_up_hander handler, void *data) { conn_t *c = NULL; event_t *rev = NULL; if (pipe_open(&n->channel) != FAST_OK) { return FAST_ERROR; } c = conn_get_from_mem(n->channel.pfd[0]); if (!c) { goto error; } conn_nonblocking(n->channel.pfd[0]); conn_nonblocking(n->channel.pfd[1]); n->call_back = handler; n->data = data; n->wake_up = notice_wake_up; c->ev_base = base; c->conn_data = n; c->log = base->log; rev = c->read; rev->handler = noice_read_event_handler; if (event_add(base,rev, EVENT_READ_EVENT, 0) == FAST_ERROR) { fast_log_error(base->log, FAST_LOG_FATAL, 0,"event adde failed"); goto error; } n->log = base->log; return FAST_OK; error: pipe_close(&n->channel); if (c) { conn_free_mem(c); } return FAST_ERROR; }
/** * Close the read end of an Ecore_Pipe object created with ecore_pipe_add(). * * @param p The Ecore_Pipe object. */ EAPI void ecore_pipe_read_close(Ecore_Pipe *p) { EINA_MAIN_LOOP_CHECK_RETURN; if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_close"); return; } if (p->fd_handler) { _ecore_main_fd_handler_del(p->fd_handler); p->fd_handler = NULL; } if (p->fd_read != PIPE_FD_INVALID) { pipe_close(p->fd_read); p->fd_read = PIPE_FD_INVALID; } }
static void test_fgetln_multi(void) { struct file files[FILE_COUNT]; int i, l; for (i = 0; i < FILE_COUNT; i++) { char *str; str = strdup("A\n"); str[0] += i; files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *)); files[i].lines[0] = str; files[i].lines[1] = str; files[i].fp = pipe_feed("%s", files[i].lines, LINE_COUNT); } for (l = 0; l < LINE_COUNT; l++) { for (i = 0; i < FILE_COUNT; i++) { size_t len; char *str; str = fgetln(files[i].fp, &len); assert(str); assert(len == LINE_LEN); files[i].got_len = len; files[i].got_buf = str; } for (i = 0; i < FILE_COUNT; i++) { assert(memcmp(files[i].lines[l], files[i].got_buf, files[i].got_len) == 0); } } for (i = 0; i < LINE_COUNT; i++) pipe_close(files[i].fp); }
/* * Formats data LV for a use as a VDO pool LV. * * Calls tool 'vdoformat' on the already active volume. */ static int _format_vdo_pool_data_lv(struct logical_volume *data_lv, const struct dm_vdo_target_params *vtp, uint64_t *logical_size) { char *dpath; const struct dm_config_node *cn; const struct dm_config_value *cv; struct pipe_data pdata; FILE *f; uint64_t lb; unsigned slabbits; int args = 1; char buf_args[5][128]; char buf[256]; /* buffer for short disk header (64B) */ const char *argv[19] = { /* Max supported args */ find_config_tree_str_allow_empty(data_lv->vg->cmd, global_vdo_format_executable_CFG, NULL) }; if (!(dpath = lv_path_dup(data_lv->vg->cmd->mem, data_lv))) { log_error("Failed to build device path for VDO formating of data volume %s.", display_lvname(data_lv)); return 0; } if (*logical_size) { if (dm_snprintf(buf_args[args], sizeof(buf_args[0]), "--logical-size=" FMTu64 "K", (*logical_size / 2)) < 0) return_0; argv[args] = buf_args[args]; args++; } slabbits = 31 - clz(vtp->slab_size_mb / DM_VDO_BLOCK_SIZE * 512); log_debug("Slab size %s converted to %u bits.", display_size(data_lv->vg->cmd, vtp->slab_size_mb * UINT64_C(2 * 1024)), slabbits); if (dm_snprintf(buf_args[args], sizeof(buf_args[0]), "--slab-bits=%u", slabbits) < 0) return_0; argv[args] = buf_args[args]; args++; if (vtp->check_point_frequency) { if (dm_snprintf(buf_args[args], sizeof(buf_args[0]), "--uds-checkpoint-frequency=%u", vtp->check_point_frequency) < 0) return_0; argv[args] = buf_args[args]; args++; } /* Convert size to GiB units or one of these strings: 0.25, 0.50, 0.75 */ if (vtp->index_memory_size_mb >= 1024) { if (dm_snprintf(buf_args[args], sizeof(buf_args[0]), "--uds-memory-size=%u", vtp->index_memory_size_mb / 1024) < 0) return_0; } else if (dm_snprintf(buf_args[args], sizeof(buf_args[0]), "--uds-memory-size=0.%u", (vtp->index_memory_size_mb < 512) ? 25 : (vtp->index_memory_size_mb < 768) ? 50 : 75) < 0) return_0; argv[args] = buf_args[args]; args++; if (vtp->use_sparse_index) { if (dm_snprintf(buf_args[args], sizeof(buf_args[0]), "--uds-sparse") < 0) return_0; argv[args] = buf_args[args]; args++; } /* Any other user opts add here */ if (!(cn = find_config_tree_array(data_lv->vg->cmd, global_vdo_format_options_CFG, NULL))) { log_error(INTERNAL_ERROR "Unable to find configuration for vdoformat command options."); return 0; } for (cv = cn->v; cv && args < 16; cv = cv->next) { if (cv->type != DM_CFG_STRING) { log_error("Invalid string in config file: " "global/vdoformat_options."); return 0; } if (cv->v.str[0]) argv[++args] = cv->v.str; } /* Only unused VDO data LV could be activated and wiped */ if (!dm_list_empty(&data_lv->segs_using_this_lv)) { log_error(INTERNAL_ERROR "Failed to wipe logical VDO data for volume %s.", display_lvname(data_lv)); return 0; } argv[args] = dpath; if (!(f = pipe_open(data_lv->vg->cmd, argv, 0, &pdata))) { log_error("WARNING: Cannot read output from %s.", argv[0]); return 0; } if (!*logical_size) while (fgets(buf, sizeof(buf), f)) { /* TODO: Watch out for locales */ if (sscanf(buf, "Logical blocks defaulted to " FMTu64 " blocks", &lb) == 1) { *logical_size = lb * DM_VDO_BLOCK_SIZE; log_verbose("Available VDO logical blocks " FMTu64 " (%s).", lb, display_size(data_lv->vg->cmd, *logical_size)); break; } else log_warn("WARNING: Cannot parse output '%s' from %s.", buf, argv[0]); } if (!pipe_close(&pdata)) { log_error("Command %s failed.", argv[0]); return 0; } return 1; }
/** * Write on the file descriptor the data passed as parameter. * * @param p The Ecore_Pipe object. * @param buffer The data to write into the pipe. * @param nbytes The size of the @p buffer in bytes * @return @c EINA_TRUE on a successful write, @c EINA_FALSE on error. */ EAPI Eina_Bool ecore_pipe_write(Ecore_Pipe *p, const void *buffer, unsigned int nbytes) { ssize_t ret; size_t already_written = 0; int retry = ECORE_PIPE_WRITE_RETRY; if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE)) { ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write"); return EINA_FALSE; } if (p->delete_me) return EINA_FALSE; if (p->fd_write == PIPE_FD_INVALID) return EINA_FALSE; /* First write the len into the pipe */ do { ret = pipe_write(p->fd_write, &nbytes, sizeof(nbytes)); if (ret == sizeof(nbytes)) { retry = ECORE_PIPE_WRITE_RETRY; break; } else if (ret > 0) { /* XXX What should we do here? */ ERR("The length of the data was not written complete" " to the pipe"); return EINA_FALSE; } else if (ret == PIPE_FD_ERROR && errno == EPIPE) { pipe_close(p->fd_write); p->fd_write = PIPE_FD_INVALID; return EINA_FALSE; } else if (ret == PIPE_FD_ERROR && errno == EINTR) /* try it again */ ; else { ERR("An unhandled error (ret: %zd errno: %d)" "occurred while writing to the pipe the length", ret, errno); } } while (retry--); if (retry != ECORE_PIPE_WRITE_RETRY) return EINA_FALSE; /* and now pass the data to the pipe */ do { ret = pipe_write(p->fd_write, ((unsigned char *)buffer) + already_written, nbytes - already_written); if (ret == (ssize_t)(nbytes - already_written)) return EINA_TRUE; else if (ret >= 0) { already_written -= ret; continue; } else if (ret == PIPE_FD_ERROR && errno == EPIPE) { pipe_close(p->fd_write); p->fd_write = PIPE_FD_INVALID; return EINA_FALSE; } else if (ret == PIPE_FD_ERROR && errno == EINTR) /* try it again */ ; else { ERR("An unhandled error (ret: %zd errno: %d)" "occurred while writing to the pipe the length", ret, errno); } } while (retry--); return EINA_FALSE; }
void pipe_free(struct pipe *p) { if (p) pipe_close(p); free(p); }