void AfterAlignmentInit(ArgStruct *p) { int bytesRead; /* Exchange buffer pointers and remote infiniband keys if doing rdma. Do * the exchange in this function because this will happen after any * memory alignment is done, which is important for getting the * correct remote address. */ if( p->prot.commtype == NP_COMM_RDMAWRITE || p->prot.commtype == NP_COMM_RDMAWRITE_WITH_IMM ) { /* Send my receive buffer address */ if(write(p->commfd, (void *)&p->r_buff, sizeof(void*)) < 0) { perror("NetPIPE: write of buffer address failed in AfterAlignmentInit"); exit(-1); } LOGPRINTF("Sent buffer address: %p\n", p->r_buff); /* Send my remote key for accessing * my remote buffer via IB RDMA */ if(write(p->commfd, (void *)&r_mr_out.r_key, sizeof(VAPI_rkey_t)) < 0) { perror("NetPIPE: write of remote key failed in AfterAlignmentInit"); exit(-1); } LOGPRINTF("Sent remote key: %d\n", r_mr_out.r_key); /* Read the sent data */ bytesRead = readFully(p->commfd, (void *)&remote_address, sizeof(void*)); if (bytesRead < 0) { perror("NetPIPE: read of buffer address failed in AfterAlignmentInit"); exit(-1); } else if (bytesRead != sizeof(void*)) { perror("NetPIPE: partial read of buffer address in AfterAlignmentInit"); exit(-1); } LOGPRINTF("Received remote address from other node: %p\n", remote_address); bytesRead = readFully(p->commfd, (void *)&remote_key, sizeof(VAPI_rkey_t)); if (bytesRead < 0) { perror("NetPIPE: read of remote key failed in AfterAlignmentInit"); exit(-1); } else if (bytesRead != sizeof(VAPI_rkey_t)) { perror("NetPIPE: partial read of remote key in AfterAlignmentInit"); exit(-1); } LOGPRINTF("Received remote key from other node: %d\n", remote_key); } }
int main() { while (1) { puts("shell> "); char* in = gets(); char **args = 0; if (in == 0) goto done; args = split(in); if (args == 0) goto done; char *cmd = args[0]; if (cmd == 0) goto done; int fd = open(cmd); if (fd < 0) { notFound(cmd); goto done; } int magic = 0; readFully(fd,&magic,4); if (magic == 0x464c457f) { /* executable file */ close(fd); long id = fork(); if (id == 0) { /* child */ long rc = execv(cmd,args); notFound(cmd); exit(rc); } else { join(id); } } else { /* write it */ seek(fd,0); char buf[100]; while (1) { int n = read(fd,buf,100); if (n == 0) break; if (n < 0) { notFound(cmd); break; } for (int j=0; j<n; j++) { putchar(buf[j]); } } close(fd); } done: if (in) free(in); if (args) free(args); } return 0; }
void RecvData(ArgStruct *p) { gm_provide_receive_buffer(gm_p, p->r_ptr, gm_min_size_for_length(p->bufflen), GM_LOW_PRIORITY); readFully(p->r_ptr, p->bufflen); }
UINT16 DataInputStream::readUInt16() { UINT16 x = 0; UINT8 buf[2]; readFully(&buf[0], 2); x += SETBYTE(buf[0], 1); x += SETBYTE(buf[1], 0); return x; }
/* * Reads len bytes of data from the specified offset into buf. * Returns 0 if all bytes could be read, otherwise returns -1. */ static int readFullyAt(ZFILE zfd, void *buf, jlong len, jlong offset) { if (IO_Lseek(zfd, offset, SEEK_SET) == -1) { return -1; /* lseek failure. */ } return readFully(zfd, buf, len); }
void RecvTime(ArgStruct *p, double *t) { gm_provide_receive_buffer(gm_p, ltime, gm_min_size_for_length(sizeof(unsigned long)), GM_LOW_PRIORITY); readFully(ltime, sizeof(unsigned long)); /* ltime = ntohl(p->buff1); */ /* Result is ltime (in microseconds) divided by 1.0e6 to get seconds */ *t = (double)(*ltime) / 1.0e6; }
void InputSocketStream::readValue(serialization::pimpl::Data &data) { size_t size = (size_t)readInt(); if (size > 0) { std::vector<byte> &buffer = data.toByteArray(); buffer.resize(size); readFully(buffer); } }
UINT32 DataInputStream::readUInt32() { UINT32 x = 0; UINT8 buf[4]; readFully(&buf[0], 4); x += SETBYTE(buf[0], 3); x += SETBYTE(buf[1], 2); x += SETBYTE(buf[2], 1); x += SETBYTE(buf[3], 0); return x; }
void DataInputStream::readUTF8(StringStorage *storage) { UINT32 sizeInBytes = readUInt32(); if (sizeInBytes > 0) { std::vector<char> buffer(sizeInBytes); readFully(&buffer.front(), sizeInBytes); Utf8StringStorage utf8String(&buffer); utf8String.toStringStorage(storage); } else { storage->setString(_T("")); } }
void Sync(ArgStruct *p) { int len; len=strlen(sync); gm_send_to_peer_with_callback(gm_p, sync, gm_min_size_for_length(len), (unsigned long) (len+1), GM_LOW_PRIORITY, p->prot.host_id, my_send_callback, NULL); gm_provide_receive_buffer(gm_p, sync1, gm_min_size_for_length(len), GM_LOW_PRIORITY); readFully(sync1,len+1); }
UINT64 DataInputStream::readUInt64() { UINT64 x = 0; UINT8 buf[8]; readFully(&buf[0], 8); x += (UINT64)buf[0] << (7 * 8); x += (UINT64)buf[1] << (6 * 8); x += (UINT64)buf[2] << (5 * 8); x += (UINT64)buf[3] << (4 * 8); x += (UINT64)buf[4] << (3 * 8); x += (UINT64)buf[5] << (2 * 8); x += (UINT64)buf[6] << (1 * 8); x += (UINT64)buf[7] << (0 * 8); return x; }
void Sync(ArgStruct *p) { char s[] = "SyncMe", response[] = " "; if (write(p->commfd, s, strlen(s)) < 0 || /* Write to nbor */ readFully(p->commfd, response, strlen(s)) < 0) /* Read from nbor */ { perror("NetPIPE: error writing or reading synchronization string"); exit(3); } if (strncmp(s, response, strlen(s))) { fprintf(stderr, "NetPIPE: Synchronization string incorrect! |%s|\n", response); exit(3); } }
int main() { long fd = open("."); if (fd < 0) { exit(fd); } long n = getlen(fd) / 16; for (long i = 0; i<n; i++) { char buf[13]; seek(fd,i*16); readFully(fd,buf,12); buf[12] = 0; puts(buf); puts("\n"); } return 0; }
void Sync(ArgStruct * p) { char s[] = "SyncMe"; char response[7]; if (write(p->commfd, s, strlen(s)) < 0 || readFully(p->commfd, response, strlen(s)) < 0) { perror ("NetPIPE: error writing or reading synchronization string"); exit(3); } if (strncmp(s, response, strlen(s))) { fprintf(stderr, "NetPIPE: Synchronization string incorrect!\n"); exit(3); } }
int main(int argc, char** argv) { if(argc <= 1)return 0; for(int i=1; i<argc; i++){ long id = open(argv[i]); if(id < 0){ puts(argv[0]); puts(": "); puts(argv[i]); puts(": No such file or directory\n"); } long len = getlen(id); char* buf = (char*)malloc(len + 1); readFully(id, (void*)buf, len); buf[len] = 0; puts(buf); } return 0; }
void RecvRepeat(ArgStruct * p, int *rpt) { unsigned int lrpt, nrpt; int bytesRead; bytesRead = readFully(p->commfd, (void *)&nrpt, sizeof(unsigned int)); if (bytesRead < 0) { printf("NetPIPE: read failed in RecvRepeat: errno=%d\n", errno); exit(305); } else if (bytesRead != sizeof(unsigned int)) { fprintf(stderr, "NetPIPE: partial read in RecvRepeat of %d bytes\n", bytesRead); exit(306); } lrpt = ntohl(nrpt); *rpt = lrpt; }
void RecvTime(ArgStruct * p, double *t) { unsigned int ltime, ntime; int bytesRead; bytesRead = readFully(p->commfd, (void *)&ntime, sizeof(unsigned int)); if (bytesRead < 0) { printf("NetPIPE: read failed in RecvTime: errno=%d\n", errno); exit(302); } else if (bytesRead != sizeof(unsigned int)) { fprintf(stderr, "NetPIPE: partial read in RecvTime of %d bytes\n", bytesRead); exit(303); } ltime = ntohl(ntime); /* Result is ltime (in microseconds) divided by 1.0e6 to get seconds */ *t = (double)ltime / 1.0e6; }
static char * readCENHeader(jzfile *zip, jlong cenpos, jint bufsize) { jint censize; ZFILE zfd = zip->zfd; char *cen; if (bufsize > zip->len - cenpos) bufsize = (jint)(zip->len - cenpos); if ((cen = malloc(bufsize)) == NULL) goto Catch; if (readFullyAt(zfd, cen, bufsize, cenpos) == -1) goto Catch; censize = CENSIZE(cen); if (censize <= bufsize) return cen; if ((cen = realloc(cen, censize)) == NULL) goto Catch; if (readFully(zfd, cen+bufsize, censize-bufsize) == -1) goto Catch; return cen; Catch: free(cen); return NULL; }
int main(int argc, char **argv) { struct addrinfo *addrList = NULL; char *host = NULL, *port = NULL, *inputFile = NULL; char *mode = NULL; char *status = NULL; char *seg_addr = NULL; char *seg_pm_port = NULL; char *seg_rep_port = NULL; char *peer_addr = NULL; char *peer_pm_port = NULL; char *peer_rep_port = NULL; char *num_retries_str = NULL; char *transition_timeout_str = NULL; int num_retries = 20; int transition_timeout = 3600; /* 1 hour */ char opt; char msgBuffer[SEGMENT_MSG_BUF_SIZE]; char *msg = NULL; int msgLen = 0; while ((opt = getopt(argc, argv, "m:s:H:P:R:h:p:r:i:n:t:")) != -1) { switch (opt) { case 'i': inputFile = optarg; break; case 'm': mode = optarg; break; case 's': status = optarg; break; case 'H': seg_addr = optarg; break; case 'P': seg_pm_port = optarg; break; case 'R': seg_rep_port = optarg; break; case 'h': host = peer_addr = optarg; break; case 'p': port = peer_pm_port = optarg; break; case 'r': peer_rep_port = optarg; break; case 'n': num_retries_str = optarg; break; case 't': transition_timeout_str = optarg; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); } } if (num_retries_str != NULL) { num_retries = (int) strtol(num_retries_str, NULL, 10); if (num_retries == 0 || errno == ERANGE) { fprintf(stderr, "Invalid num_retries (-n) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } } if (transition_timeout_str != NULL) { transition_timeout = (int) strtol (transition_timeout_str, NULL, 10); if (transition_timeout == 0 || errno == ERANGE) { fprintf(stderr, "Invalid transition_timeout (-t) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } } /* check if input file parameter is passed */ if (seg_addr == NULL) { if ( host == NULL) { fprintf(stderr, "Missing host (-h) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if ( port == NULL ) { fprintf(stderr, "Missing port (-p) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } /* find the target machine */ if ( ! determineTargetHost(&addrList, host, port)) { return TRANS_ERRCODE_ERROR_HOST_LOOKUP_FAILED; } /* load the input message into memory */ if ( inputFile == NULL) { msg = readFully(stdin, &msgLen); } else { FILE *f = fopen(inputFile, "r"); if ( f == NULL) { fprintf(stderr, "Unable to open file %s\n", inputFile); return TRANS_ERRCODE_ERROR_READING_INPUT; } msg = readFully(f, &msgLen); fclose(f); } } else { /* build message from passed parameters */ if (mode == NULL) { fprintf(stderr, "Missing mode (-m) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (status == NULL) { fprintf(stderr, "Missing status (-s) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (seg_addr == NULL) { fprintf(stderr, "Missing segment host (-H) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (seg_pm_port == NULL) { fprintf(stderr, "Missing segment postmaster port (-P) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (seg_rep_port == NULL) { fprintf(stderr, "Missing segment replication port (-R) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (peer_addr == NULL) { fprintf(stderr, "Missing peer host (-h) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (peer_pm_port == NULL) { fprintf(stderr, "Missing peer postmaster port (-p) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } if (peer_rep_port == NULL) { fprintf(stderr, "Missing peer replication port (-r) argument\n"); return TRANS_ERRCODE_ERROR_INVALID_ARGUMENT; } /* build message */ msgLen = snprintf( msgBuffer, sizeof(msgBuffer), "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", mode, status, seg_addr, seg_rep_port, peer_addr, peer_rep_port, peer_pm_port ); msg = msgBuffer; /* find the target machine */ if (!determineTargetHost(&addrList, seg_addr, seg_pm_port)) { return TRANS_ERRCODE_ERROR_HOST_LOOKUP_FAILED; } } /* check for errors while building the message */ if ( msg == NULL ) { return TRANS_ERRCODE_ERROR_READING_INPUT; } /* send the message */ PrimaryMirrorTransitionClientInfo client; client.receivedDataCallbackFn = gpMirrorReceivedDataCallbackFunction; client.errorLogFn = gpMirrorErrorLogFunction; client.checkForNeedToExitFn = gpCheckForNeedToExitFn; return sendTransitionMessage(&client, addrList, msg, msgLen, num_retries, transition_timeout); }
void RecvRepeat(ArgStruct *p, int *rpt) { gm_provide_receive_buffer(gm_p, lrpt, gm_min_size_for_length(sizeof(unsigned long)), GM_LOW_PRIORITY); readFully(lrpt,sizeof(unsigned long)); *rpt =(int)*lrpt; }
UINT8 DataInputStream::readUInt8() { UINT8 x; readFully(&x, 1); return x; }
/* * getIncomingMessage() - block until we receive a message from the client, * populate the fields of a RaptorSessionMessage struct based on the data * we receive, and return that struct */ AbstractPacket* RaptorRemoteSession::getIncomingMessage() { bool messageReceived = false; int bytesRead; AbstractPacket* returnMessage = (AbstractPacket*)malloc(MAX_PACKET_BYTES); VideoInitPacket* _videoInitMessage; pthread_mutex_lock(&receiveMutex); bzero(this->receiveBuffer, RECEIVE_BUFFER_SIZE); // read the message type bytesRead = readFully(this->socketFD, this->receiveBuffer, 0, sizeof(AbstractPacket)); if (bytesRead < 0) { printf("Error reading message type from new message\n"); pthread_mutex_unlock(&receiveMutex); returnMessage->messageType = SOCKET_READ_ERROR; return returnMessage; } else if (bytesRead == 0) { printf("Connection closed by remote client"); pthread_mutex_unlock(&receiveMutex); returnMessage->messageType = SOCKET_CLOSED; return returnMessage; } // switch on the message type received switch (this->receiveBuffer[0]) { case PING_MSG: printf("ping message received\n"); bytesRead = readFully(this->socketFD, this->receiveBuffer, bytesRead, sizeof(PingPacket)); returnMessage->messageType = PING_MSG; messageReceived = true; break; case INIT_MSG: printf("init message received\n"); bytesRead = recv(this->socketFD, (this->receiveBuffer), bytesRead, sizeof(InitPacket)); returnMessage->messageType = INIT_MSG; messageReceived = true; break; case QUIT_MSG: printf("quit message received\n"); bytesRead = readFully(this->socketFD, this->receiveBuffer, bytesRead, sizeof(QuitPacket)); returnMessage->messageType = QUIT_MSG; break; case VID_START: printf("vid start message received\n"); returnMessage->messageType = VID_START; bytesRead = readFully(this->socketFD, this->receiveBuffer, bytesRead, sizeof(VideoInitPacket)); if (bytesRead < sizeof(VideoInitPacket)) { pthread_mutex_unlock(&receiveMutex); returnMessage->messageType = SOCKET_READ_ERROR; return returnMessage; } memcpy(returnMessage, this->receiveBuffer, sizeof(VideoInitPacket)); _videoInitMessage = (VideoInitPacket*) returnMessage; _videoInitMessage->remoteDataPort = ntohl(_videoInitMessage->remoteDataPort); _videoInitMessage->remoteServerPort = ntohl(_videoInitMessage->remoteServerPort); break; /* case VID_END: printf("vid end message received\n"); returnMessage->messageType = VID_END; messageReceived = true; break;*/ default: printf("unknown message type received\n"); returnMessage->messageType = UNKNOWN_MESSAGE_TYPE; messageReceived = true; break; } pthread_mutex_unlock(&receiveMutex); return returnMessage; }
jzfile * ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap) { char errbuf[256]; jlong len; jzfile *zip; if ((zip = allocZip(name)) == NULL) { return NULL; } #ifdef USE_MMAP zip->usemmap = usemmap; #endif zip->refs = 1; zip->lastModified = lastModified; if (zfd == -1) { if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0) *pmsg = strdup(errbuf); freeZip(zip); return NULL; } // Assumption, zfd refers to start of file. Trivially, reuse errbuf. if (readFully(zfd, errbuf, 4) != -1) { // errors will be handled later zip->locsig = LOCSIG_AT(errbuf) ? JNI_TRUE : JNI_FALSE; } len = zip->len = IO_Lseek(zfd, 0, SEEK_END); if (len <= 0) { if (len == 0) { /* zip file is empty */ if (pmsg) { *pmsg = strdup("zip file is empty"); } } else { /* error */ if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0) *pmsg = strdup(errbuf); } ZFILE_Close(zfd); freeZip(zip); return NULL; } zip->zfd = zfd; if (readCEN(zip, -1) < 0) { /* An error occurred while trying to read the zip file */ if (pmsg != 0) { /* Set the zip error message */ if (zip->msg != NULL) *pmsg = strdup(zip->msg); } freeZip(zip); return NULL; } MLOCK(zfiles_lock); zip->next = zfiles; zfiles = zip; MUNLOCK(zfiles_lock); return zip; }