void set_pipe_is_written_into(char* pipe_name) { PipeRec* p; p = find_pipe(pipe_name); // this also uses the lock. if(p) p->is_written_into = 1; }
void set_pipe_is_read_from(char* pipe_name) { PipeRec* p; p = find_pipe(pipe_name); // this also uses the lock. if(p) p->is_read_from = 1; }
static void remove_pipe(text *pipe_name, bool purge) { pipe *p; bool created; if (NULL != (p = find_pipe(pipe_name, &created, true))) { queue_item *q = p->items; while (q != NULL) { queue_item *aux_q; aux_q = q->next_item; if (q->ptr) ora_sfree(q->ptr); ora_sfree(q); q = aux_q; } p->items = NULL; p->size = 0; p->count = 0; if (!(purge && p->registered)) { ora_sfree(p->pipe_name); p->is_valid = false; } } }
static message_buffer* get_from_pipe(text *pipe_name, bool *found) { pipe *p; bool created; message_buffer *shm_msg; message_buffer *result = NULL; if (!ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS, false)) return NULL; if (NULL != (p = find_pipe(pipe_name, &created,false))) { if (!created) { if (NULL != (shm_msg = remove_first(p, found))) { p->size -= shm_msg->size; result = (message_buffer*) MemoryContextAlloc(TopMemoryContext, shm_msg->size); memcpy(result, shm_msg, shm_msg->size); ora_sfree(shm_msg); } } } LWLockRelease(shmem_lockid); return result; }
static bool add_to_pipe(text *pipe_name, message_buffer *ptr, int limit, bool limit_is_valid) { pipe *p; bool created; bool result = false; message_buffer *sh_ptr; if (!ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES, MAX_EVENTS, MAX_LOCKS,false)) return false; for (;;) { if (NULL != (p = find_pipe(pipe_name, &created, false))) { if (created) p->registered = ptr == NULL; if (limit_is_valid && (created || (p->limit < limit))) p->limit = limit; if (ptr != NULL) { if (NULL != (sh_ptr = ora_salloc(ptr->size))) { memcpy(sh_ptr,ptr,ptr->size); if (new_last(p, sh_ptr)) { p->size += ptr->size; result = true; break; } ora_sfree(sh_ptr); } if (created) { /* I created new pipe, but haven't memory for new value */ ora_sfree(p->pipe_name); p->is_valid = false; result = false; } } else result = true; } break; } LWLockRelease(shmem_lockid); return result; }
// return 0 on success, 1 on error. uint32_t register_pipe(char* pipe_name, int pipe_depth, int pipe_width, int pipe_mode) { PipeRec* p; if(pipe_depth <= 0) { fprintf(stderr,"\nWarning: pipeHandler: pipe %s with declared depth %d set to depth=1.\n", pipe_name, pipe_depth); pipe_depth = 1; } p = find_pipe(pipe_name); // this also uses the lock. if(p != NULL) { if(p->pipe_width != pipe_width) { fprintf(stderr,"\nError: pipeHandler: redefinition of pipe %s with conflicting widths (%d or %d?)\n", pipe_name, p->pipe_width, pipe_width); return(1); } if(p->pipe_depth != pipe_depth) { fprintf(stderr,"\nError: pipeHandler: redefinition of pipe %s with conflicting depths (%d or %d?)\n", pipe_name, p->pipe_depth, pipe_depth); return(1); } if(p->pipe_mode != pipe_mode) { fprintf(stderr,"\nError: pipeHandler: redefinition of pipe %s with conflicting modes (FIFO or LIFO?)\n", pipe_name); return(1); } return(0); } PipeRec* new_p = (PipeRec*) calloc(1,sizeof(PipeRec)); new_p->pipe_name = strdup(pipe_name); new_p->pipe_width = pipe_width; new_p->pipe_depth = pipe_depth; new_p->number_of_entries = 0; new_p->write_pointer = 0; new_p->read_pointer = 0; new_p->buffer.ptr8 = (uint8_t*) calloc(1, ((pipe_depth*pipe_width)/8)*sizeof(uint8_t)); new_p->pipe_mode = pipe_mode; new_p->next = NULL; #ifdef USE_GNUPTH pth_mutex_init(&(new_p->pm)); #else pthread_mutex_init (&(new_p->pm), NULL); #endif ___LOCK___ new_p->next = pipes; pipes = new_p; ___UNLOCK___ if(log_file != NULL) { __LOCKLOG__ fprintf(log_file,"\nAdded: %s depth %d width %d pipe_mode %d.", pipe_name,pipe_depth,pipe_width, pipe_mode); fflush(log_file); __UNLOCKLOG__ }
/*---------------------------------------------------------------------------*/ static struct shell_command * start_command(char *commandline, struct shell_command *child) { char *next, *args; int command_len; struct shell_command *c; /* Shave off any leading spaces. */ while(*commandline == ' ') { commandline++; } /* Find the next command in a pipeline and start it. */ next = find_pipe(commandline); if(next != NULL) { *next = 0; child = start_command(next + 1, child); } /* Separate the command arguments, and remove braces. */ replace_braces(commandline); args = strchr(commandline, ' '); if(args != NULL) { args++; } /* Shave off any trailing spaces. */ command_len = (int)strlen(commandline); while(command_len > 0 && commandline[command_len - 1] == ' ') { commandline[command_len - 1] = 0; command_len--; } if(args == NULL) { command_len = (int)strlen(commandline); args = &commandline[command_len]; } else { command_len = (int)(args - commandline - 1); } /* Go through list of commands to find a match for the first word in the command line. */ for(c = list_head(commands); c != NULL && !(strncmp(c->command, commandline, command_len) == 0 && c->command[command_len] == 0); c = c->next); if(c == NULL) { shell_output_str(NULL, commandline, ": command not found (try 'help')"); command_kill(child); c = NULL; } else if(process_is_running(c->process) || child == c) { shell_output_str(NULL, commandline, ": command already running"); c->child = NULL; c = NULL; } else { c->child = child; /* printf("shell: start_command starting '%s'\n", c->process->name);*/ /* Start a new process for the command. */ process_start(c->process, args); } return c; }
Datum dbms_pipe_create_pipe (PG_FUNCTION_ARGS) { text *pipe_name = NULL; int limit = 0; bool is_private; bool limit_is_valid = false; bool created; float8 endtime; int cycle = 0; int timeout = 10; if (PG_ARGISNULL(0)) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("pipe name is NULL"), errdetail("Pipename may not be NULL."))); else pipe_name = PG_GETARG_TEXT_P(0); if (!PG_ARGISNULL(1)) { limit = PG_GETARG_INT32(1); limit_is_valid = true; } is_private = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2); WATCH_PRE(timeout, endtime, cycle); if (ora_lock_shmem(SHMEMMSGSZ, MAX_PIPES,MAX_EVENTS,MAX_LOCKS,false)) { pipe *p; if (NULL != (p = find_pipe(pipe_name, &created, false))) { if (!created) { LWLockRelease(shmem_lockid); ereport(ERROR, (errcode(ERRCODE_DUPLICATE_OBJECT), errmsg("pipe creation error"), errdetail("Pipe is registered."))); } if (is_private) { char *user; p->uid = GetUserId(); #if PG_VERSION_NUM >= 90500 user = (char*)DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(p->uid, false))); #else user = (char*)DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(p->uid))); #endif p->creator = ora_sstrcpy(user); pfree(user); } p->limit = limit_is_valid ? limit : -1; p->registered = true; LWLockRelease(shmem_lockid); PG_RETURN_VOID(); } } WATCH_POST(timeout, endtime, cycle); LOCK_ERROR(); PG_RETURN_VOID(); }