void tr_runInEventThread( struct tr_handle * handle, void func( void* ), void * user_data ) { assert( handle ); assert( handle->events ); if( tr_amInThread( handle->events->thread ) ) { (func)( user_data ); } else { const char ch = 'r'; int fd = handle->events->fds[1]; tr_lock * lock = handle->events->lock; struct tr_run_data data; tr_lockLock( lock ); pipewrite( fd, &ch, 1 ); data.func = func; data.user_data = user_data; pipewrite( fd, &data, sizeof( data ) ); tr_lockUnlock( lock ); } }
void tr_runInEventThread( tr_session * session, void func( void* ), void * user_data ) { assert( tr_isSession( session ) ); assert( session->events != NULL ); if( tr_amInThread( session->events->thread ) ) { (func)( user_data ); } else { const char ch = 'r'; int fd = session->events->fds[1]; tr_lock * lock = session->events->lock; struct tr_run_data data; tr_lockLock( lock ); pipewrite( fd, &ch, 1 ); data.func = func; data.user_data = user_data; pipewrite( fd, &data, sizeof( data ) ); tr_lockUnlock( lock ); } }
void yuv_write_frame (int fd, unsigned char *yuv[], int width, int height) { pipewrite (fd, "FRAME\n", 6); /*FIXME! this is subject to be extended to work with lav2yuv: */ pipewrite (fd, yuv[0], width*height); pipewrite (fd, yuv[1], width*height/4); pipewrite (fd, yuv[2], width*height/4); }
static void * block_tx_client_thread (void *vdata) { BlockTxClient *client = vdata; BlockTxInfo *info = client->info; BlockTxClientDoneCB cb = client->cb; evutil_socket_t data_fd; gboolean restart; retry: data_fd = connect_chunk_server (info->cs); if (data_fd < 0) { info->result = BLOCK_CLIENT_NET_ERROR; if (!info->transfer_once) { pipewrite (info->done_pipe[1], &info->result, sizeof(info->result)); /* Transfer manager always expects an ENDED response. */ int rsp = BLOCK_CLIENT_ENDED; pipewrite (info->done_pipe[1], &rsp, sizeof(rsp)); } return vdata; } client->data_fd = data_fd; if (send_handshake (data_fd, info) < 0) { if (!info->transfer_once) { pipewrite (info->done_pipe[1], &info->result, sizeof(info->result)); int rsp = BLOCK_CLIENT_ENDED; pipewrite (info->done_pipe[1], &rsp, sizeof(rsp)); } evutil_closesocket (client->data_fd); return vdata; } client->recv_buf = evbuffer_new (); restart = client_thread_loop (client); if (restart) { seaf_message ("Restarting block tx client.\n"); memset (client, 0, sizeof(BlockTxClient)); client->info = info; client->cb = cb; client->info->result = BLOCK_CLIENT_UNKNOWN; goto retry; } return vdata; }
size_t url_execute_fwrite(void *ptr, size_t size, URL_FILE *file, CopyState pstate) { URL_EXECUTE_FILE *efile = (URL_EXECUTE_FILE *) file; int fd = efile->handle->pipes[EXEC_DATA_P]; size_t offset = 0; const char* p = (const char* ) ptr; size_t n; /* ensure all data in buffer is send out to pipe*/ while(size > offset) { n = pipewrite(fd,p,size - offset); if(n == -1) return -1; if(n == 0) break; offset += n; p = (const char*)ptr + offset; } if(offset < size) elog(WARNING,"partial write, expected %lu, written %lu", size, offset); return offset; }
void yuv_write_header (int fd, int width, int height, int frame_rate_code) { char str[256]; snprintf (str, sizeof (str), "YUV4MPEG %d %d %d\n", width, height, frame_rate_code); pipewrite (fd, str, strlen (str)); }
//PAGEBREAK! // Write to file f. int filewrite(struct file *f, char *addr, int n) { int r; if (f->writable == 0) { return -1; } if (f->type == FD_PIPE) { return pipewrite(f->pipe, addr, n); } if (f->type == FD_INODE) { // write a few blocks at a time to avoid exceeding // the maximum log transaction size, including // i-node, indirect block, allocation blocks, // and 2 blocks of slop for non-aligned writes. // this really belongs lower down, since writei() // might be writing a device like the console. int max = ((LOGSIZE - 1 - 1 - 2) / 2) * 512; int i = 0; while (i < n) { int n1 = n - i; if (n1 > max) { n1 = max; } begin_trans(); ilock(f->ip); if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) { f->off += r; } iunlock(f->ip); commit_trans(); if (r < 0) { break; } if (r != n1) { panic("short filewrite"); } i += r; } return i == n ? n : -1; } panic("filewrite"); }
tr_timer* tr_timerNew( struct tr_handle * handle, timer_func func, void * user_data, uint64_t interval_milliseconds ) { tr_timer * timer; assert( handle ); assert( handle->events ); timer = tr_new0( tr_timer, 1 ); tr_timevalMsec( interval_milliseconds, &timer->tv ); timer->func = func; timer->user_data = user_data; timer->eh = handle->events; evtimer_set( &timer->event, timerCallback, timer ); if( tr_amInThread( handle->events->thread ) ) { evtimer_add( &timer->event, &timer->tv ); } else { const char ch = 't'; int fd = handle->events->fds[1]; tr_lock * lock = handle->events->lock; tr_lockLock( lock ); pipewrite( fd, &ch, 1 ); pipewrite( fd, &timer, sizeof( timer ) ); tr_lockUnlock( lock ); } return timer; }
void tr_runInEventThread (tr_session * session, void func (void*), void * user_data) { assert (tr_isSession (session)); assert (session->events != NULL); if (tr_amInThread (session->events->thread)) { (func)(user_data); } else { int fd; char ch; ssize_t res_1; ssize_t res_2; tr_event_handle * e = session->events; struct tr_run_data data; tr_lockLock (e->lock); fd = e->fds[1]; ch = 'r'; res_1 = pipewrite (fd, &ch, 1); data.func = func; data.user_data = user_data; res_2 = pipewrite (fd, &data, sizeof (data)); tr_lockUnlock (e->lock); if ((res_1 == -1) || (res_2 == -1)) tr_logAddError ("Unable to write to libtransmisison event queue: %s", tr_strerror(errno)); } }
/* * Return cluster report including all node status. The response should be * handled asynchronously. */ int RB_LIBYARN_getClusterReport(const char *quename, List **machines, double *maxcapacity) { int res = FUNC_RETURN_OK; uint32_t MessageID = RM2RB_GET_CLUSTERREPORT; int piperes = 0; if ( PipeReceivePending ) { elog(DEBUG5, "YARN resource broker skip sending cluster report due to " "not getting response of last request."); return FUNC_RETURN_OK; } /* Write request to pipe */ SelfMaintainBufferData sendBuffer; initializeSelfMaintainBuffer(&sendBuffer, PCONTEXT); appendSMBVar(&sendBuffer, MessageID); appendSelfMaintainBufferTill64bitAligned(&sendBuffer); RPCRequestRBGetClusterReportHeadData request; request.QueueNameLen = __SIZE_ALIGN64(strlen(quename) + 1); request.Reserved = 0; appendSMBVar(&sendBuffer, request); appendSMBStr(&sendBuffer, quename); appendSelfMaintainBufferTill64bitAligned(&sendBuffer); piperes = pipewrite(ResBrokerRequestPipe[1], sendBuffer.Buffer, sendBuffer.Cursor + 1); if ( piperes != sendBuffer.Cursor + 1 ) { elog(WARNING, "YARN mode resource broker failed to generate cluster " "report request to resource broker process through pipe. " "Wrote length %d, expected length %d, errno %d", piperes, sendBuffer.Cursor + 1, errno); res = RESBROK_PIPE_ERROR; } destroySelfMaintainBuffer(&sendBuffer); elog(LOG, "YARN mode resource broker generated cluster report request to " "resource broker process."); PipeReceivePending = res == FUNC_RETURN_OK; return res; }
static gboolean do_break_loop (BlockTxClient *client) { if (client->info->transfer_once) { shutdown_client (client); return TRUE; } else { int rsp = client->info->result; pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp)); if (client->info->result != BLOCK_CLIENT_SUCCESS) shutdown_client (client); client->break_loop = FALSE; return FALSE; } }
// Write to file f. Addr is kernel address. int filewrite(struct file *f, char *addr, int n) { int r; if(f->writable == 0) return -1; if(f->type == FD_PIPE) return pipewrite(f->pipe, addr, n); if(f->type == FD_INODE){ ilock(f->ip); if((r = writei(f->ip, addr, f->off, n)) > 0) f->off += r; iunlock(f->ip); return r; } panic("filewrite"); }
static int handle_auth_rsp_content_cb (char *content, int clen, void *cbarg) { BlockTxClient *client = cbarg; AuthResponse *rsp; if (clen != sizeof(AuthResponse)) { seaf_warning ("Invalid auth respnose length %d.\n", clen); client->info->result = BLOCK_CLIENT_FAILED; return -1; } rsp = (AuthResponse *)content; rsp->status = ntohl (rsp->status); if (rsp->status == STATUS_OK) { seaf_debug ("Auth OK.\n"); if (!client->info->transfer_once) { int rsp = BLOCK_CLIENT_READY; pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp)); } /* If in interactive mode, wait for TRANSFER command to start transfer. */ if (client->info->transfer_once) return transfer_next_block (client); } else if (rsp->status == STATUS_ACCESS_DENIED) { seaf_warning ("Authentication failed.\n"); /* this is a hard error. */ client->info->result = BLOCK_CLIENT_FAILED; return -1; } else if (rsp->status == STATUS_INTERNAL_SERVER_ERROR) { seaf_warning ("Server error when handling auth.\n"); client->info->result = BLOCK_CLIENT_SERVER_ERROR; return -1; } else { seaf_warning ("Bad status code %d in handshake.\n", rsp->status); client->info->result = BLOCK_CLIENT_FAILED; return -1; } return 0; }
// Write to file f. Addr is kernel address. int filewrite(struct file *f, char *addr, int n) { // cprintf(" -- filewrite -- f-type %d\n", f->type); int r; if(f->writable == 0) return -1; if(f->type == FD_PIPE) return pipewrite(f->pipe, addr, n); if(f->type == FD_INODE){ ilock(f->ip); if((r = writei(f->ip, addr, f->off, n)) > 0) f->off += r; iunlock(f->ip); // cprintf(" -- filewrite -- after iunlock \n"); return r; } panic("filewrite"); }
// Write to file f. Addr is kernel address. int filewrite(struct file *f, char *addr, int n) { //cprintf("filewrite...\n"); int r; //verified if(f->writable == 0) return -1; if(f->type == FD_PIPE) return pipewrite(f->pipe, addr, n); if(f->type == FD_INODE){ ilock(f->ip); if((r = writei(f->ip, addr, f->off, n)) > 0) f->off += r; iunlock(f->ip); //cprintf("filwrite: r(%d)\n"); return r; } panic("filewrite"); }
int wait_until_received(const int mtype) { if (mtype > 0 && mtype <= MSG_TYPES_NUMBER) { if (pipewrite(Pdesc2[1], Pdesc[0], &mtype, sizeof(int)) == FAIL) return FAIL; if (mtype == RESPONSE) { if (piperead(Pdesc2[1], Pdesc[0], &response_data.response_type, sizeof(int)) == FAIL || piperead(Pdesc2[1], Pdesc[0], response_data.content, RESPONSE_LENGTH) == FAIL) { return FAIL; } } else if (mtype == USERS || mtype == ROOMS || mtype == ROOM_USERS_LIST) { int j = 0; while (piperead(Pdesc2[1], Pdesc[0], request_response_data->content[j], USER_NAME_MAX_LENGTH) > FAIL) ++j; if (j < MAX_SERVERS_NUMBER * MAX_USERS_NUMBER) request_response_data->content[j][0] = '\0'; } return 0; } return FAIL; }
int writePipe(int fd, void *buff, int buffsize) { int s = 0; int res = 0; retry: res = pipewrite(fd, (char *)buff + s, buffsize - s); if ( res >= 0 ) { s += res; if ( buffsize - s > 0 ) { goto retry; } return s; } else if ( res == -1 && (errno == EAGAIN || errno == EINTR)) { goto retry; } write_log("writePipe got write() error , fd %d, (errno %d)", fd, errno); return -1; }
static int pty_write(struct file *filp, char *buffer, int nbyte, int blocking) { struct pty *pty; struct pipe *pipeb; int master,total = 0, final = 0; demand(filp, bogus filp); DPRINTF(CLU_LEVEL, ("pty_write: filp: %08x offset: %qd nbyte: %d\n", (int)filp, filp->f_pos, nbyte)); pty = GETPTYP(filp); master = filp->f_pos; demand (master == 1 || master == 0,master flag set improperly); pipeb = &(pty->pipe[1 - master]); if (pipeb->length == 0 && CHECKNB(filp)) { errno = EWOULDBLOCK; return -1; } while(nbyte > 0) { if (!(PTY_BUSY(1 - master,pty))) { /* signal(getpid(),SIGPIPE); */ return -1; } if (pipeb->length != 0) { int len; len = MIN(nbyte,pipeb->length); lock_pipepty(pipeb); total = pipewrite(pipeb,buffer,len); final += total; buffer += total; unlock_pipepty(pipeb); nbyte -= total; } else {
static void BaseBackup(void) { PGresult *res; char *sysidentifier; uint32 timeline; char current_path[MAXPGPATH]; char escaped_label[MAXPGPATH]; int i; char xlogstart[64]; char xlogend[64]; /* * Connect in replication mode to the server */ conn = GetConnection(); /* * Run IDENTIFY_SYSTEM so we can get the timeline */ res = PQexec(conn, "IDENTIFY_SYSTEM"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, _("%s: could not identify system: %s\n"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } if (PQntuples(res) != 1) { fprintf(stderr, _("%s: could not identify system, got %i rows\n"), progname, PQntuples(res)); disconnect_and_exit(1); } sysidentifier = strdup(PQgetvalue(res, 0, 0)); timeline = atoi(PQgetvalue(res, 0, 1)); PQclear(res); /* * Start the actual backup */ PQescapeStringConn(conn, escaped_label, label, sizeof(escaped_label), &i); snprintf(current_path, sizeof(current_path), "BASE_BACKUP LABEL '%s' %s %s %s %s", escaped_label, showprogress ? "PROGRESS" : "", includewal && !streamwal ? "WAL" : "", fastcheckpoint ? "FAST" : "", includewal ? "NOWAIT" : ""); if (PQsendQuery(conn, current_path) == 0) { fprintf(stderr, _("%s: could not send base backup command: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } /* * Get the starting xlog position */ res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, _("%s: could not initiate base backup: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } if (PQntuples(res) != 1) { fprintf(stderr, _("%s: no start point returned from server\n"), progname); disconnect_and_exit(1); } strcpy(xlogstart, PQgetvalue(res, 0, 0)); if (verbose && includewal) fprintf(stderr, "xlog start point: %s\n", xlogstart); PQclear(res); MemSet(xlogend, 0, sizeof(xlogend)); /* * Get the header */ res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, _("%s: could not get backup header: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } if (PQntuples(res) < 1) { fprintf(stderr, _("%s: no data returned from server\n"), progname); disconnect_and_exit(1); } /* * Sum up the total size, for progress reporting */ totalsize = totaldone = 0; tablespacecount = PQntuples(res); for (i = 0; i < PQntuples(res); i++) { if (showprogress) totalsize += atol(PQgetvalue(res, i, 2)); /* * Verify tablespace directories are empty. Don't bother with the * first once since it can be relocated, and it will be checked before * we do anything anyway. */ if (format == 'p' && !PQgetisnull(res, i, 1)) verify_dir_is_empty_or_create(PQgetvalue(res, i, 1)); } /* * When writing to stdout, require a single tablespace */ if (format == 't' && strcmp(basedir, "-") == 0 && PQntuples(res) > 1) { fprintf(stderr, _("%s: can only write single tablespace to stdout, database has %d\n"), progname, PQntuples(res)); disconnect_and_exit(1); } /* * If we're streaming WAL, start the streaming session before we start * receiving the actual data chunks. */ if (streamwal) { if (verbose) fprintf(stderr, _("%s: starting background WAL receiver\n"), progname); StartLogStreamer(xlogstart, timeline, sysidentifier); } /* * Start receiving chunks */ for (i = 0; i < PQntuples(res); i++) { if (format == 't') ReceiveTarFile(conn, res, i); else ReceiveAndUnpackTarFile(conn, res, i); } /* Loop over all tablespaces */ if (showprogress) { progress_report(PQntuples(res), NULL); fprintf(stderr, "\n"); /* Need to move to next line */ } PQclear(res); /* * Get the stop position */ res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, _("%s: could not get WAL end position from server\n"), progname); disconnect_and_exit(1); } if (PQntuples(res) != 1) { fprintf(stderr, _("%s: no WAL end position returned from server\n"), progname); disconnect_and_exit(1); } strcpy(xlogend, PQgetvalue(res, 0, 0)); if (verbose && includewal) fprintf(stderr, "xlog end point: %s\n", xlogend); PQclear(res); res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, _("%s: final receive failed: %s"), progname, PQerrorMessage(conn)); disconnect_and_exit(1); } if (bgchild > 0) { int status; #ifndef WIN32 int r; #endif if (verbose) fprintf(stderr, _("%s: waiting for background process to finish streaming...\n"), progname); #ifndef WIN32 if (pipewrite(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend)) { fprintf(stderr, _("%s: could not send command to background pipe: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } /* Just wait for the background process to exit */ r = waitpid(bgchild, &status, 0); if (r == -1) { fprintf(stderr, _("%s: could not wait for child process: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } if (r != bgchild) { fprintf(stderr, _("%s: child %i died, expected %i\n"), progname, r, bgchild); disconnect_and_exit(1); } if (!WIFEXITED(status)) { fprintf(stderr, _("%s: child process did not exit normally\n"), progname); disconnect_and_exit(1); } if (WEXITSTATUS(status) != 0) { fprintf(stderr, _("%s: child process exited with error %i\n"), progname, WEXITSTATUS(status)); disconnect_and_exit(1); } /* Exited normally, we're happy! */ #else /* WIN32 */ /* * On Windows, since we are in the same process, we can just store the * value directly in the variable, and then set the flag that says * it's there. */ if (sscanf(xlogend, "%X/%X", &xlogendptr.xlogid, &xlogendptr.xrecoff) != 2) { fprintf(stderr, _("%s: could not parse xlog end position \"%s\"\n"), progname, xlogend); exit(1); } InterlockedIncrement(&has_xlogendptr); /* First wait for the thread to exit */ if (WaitForSingleObjectEx((HANDLE) bgchild, INFINITE, FALSE) != WAIT_OBJECT_0) { _dosmaperr(GetLastError()); fprintf(stderr, _("%s: could not wait for child thread: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } if (GetExitCodeThread((HANDLE) bgchild, &status) == 0) { _dosmaperr(GetLastError()); fprintf(stderr, _("%s: could not get child thread exit status: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } if (status != 0) { fprintf(stderr, _("%s: child thread exited with error %u\n"), progname, status); disconnect_and_exit(1); } /* Exited normally, we're happy */ #endif } /* * End of copy data. Final result is already checked inside the loop. */ PQfinish(conn); if (verbose) fprintf(stderr, "%s: base backup completed\n", progname); }
void block_tx_client_run_command (BlockTxInfo *info, int command) { pipewrite (info->cmd_pipe[1], &command, sizeof(int)); }
char *rackname = GET_SEGINFO_GRMRACKNAME(&(segres->Stat->Info)); uint32_t preferredLen = sizeof(preferredLen) + sizeof(num_containers) + sizeof(flag) + __SIZE_ALIGN64((strlen(hostname) + 1 + strlen(rackname) + 1)); appendSMBVar(&sendBuffer, preferredLen); appendSMBVar(&sendBuffer, num_containers); appendSMBVar(&sendBuffer, flag); appendSMBStr(&sendBuffer, hostname); appendSMBStr(&sendBuffer, rackname); appendSelfMaintainBufferTill64bitAligned(&sendBuffer); elog(LOG, "YARN mode resource broker build a preferred request." "host:%s, rack:%s, container number:%d", hostname, rackname, num_containers); } int piperes = pipewrite(ResBrokerRequestPipe[1], sendBuffer.Buffer, sendBuffer.Cursor + 1); if ( piperes != sendBuffer.Cursor + 1 ) { elog(WARNING, "YARN mode resource broker failed to write resource " "allocation request to resource broker process. errno %d.", errno); res = RESBROK_PIPE_ERROR; } elog(DEBUG3, "LIBYARN mode resource broker wrote %d bytes out.", sendBuffer.Cursor+1); destroySelfMaintainBuffer(&sendBuffer); elog(LOG, "YARN mode resource broker wrote resource allocation request to " "resource broker process."); PipeReceivePending = res == FUNC_RETURN_OK;
static gboolean handle_command (BlockTxClient *client, int command) { gboolean ret = FALSE; int rsp; switch (command) { case BLOCK_CLIENT_CMD_TRANSFER: /* Ignore TRANSFER command if client has been shutdown. */ if (client->recv_state == RECV_STATE_DONE) { seaf_debug ("Client was shutdown, ignore transfer command.\n"); break; } if (transfer_next_block (client) < 0) { rsp = client->info->result; pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp)); shutdown_client (client); client->break_loop = FALSE; } break; case BLOCK_CLIENT_CMD_CANCEL: if (client->recv_state == RECV_STATE_DONE) { seaf_debug ("Client was shutdown, ignore cancel command.\n"); break; } seaf_debug ("Canceled command received.\n"); client->info->result = BLOCK_CLIENT_CANCELED; if (client->info->transfer_once) { shutdown_client (client); ret = TRUE; } else { rsp = client->info->result; pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp)); shutdown_client (client); client->break_loop = FALSE; ret = FALSE; } break; case BLOCK_CLIENT_CMD_END: client->info->result = BLOCK_CLIENT_ENDED; rsp = client->info->result; pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp)); /* Don't need to shutdown_client() if it's already called. */ if (client->recv_state != RECV_STATE_DONE) shutdown_client (client); client->break_loop = FALSE; ret = TRUE; break; } return ret; }