int ReliSock::get_bytes_nobuffer(char *buffer, int max_length, int receive_size) { int result; int length; unsigned char * buf = NULL; ASSERT(buffer != NULL); ASSERT(max_length > 0); // Find out how big the file is going to be, if requested. // No receive_size means read max_length bytes. this->decode(); if ( receive_size ) { ASSERT( this->code(length) != FALSE ); ASSERT( this->end_of_message() != FALSE ); } else { length = max_length; } // First drain incoming buffers if ( !prepare_for_nobuffering(stream_decode) ) { // error draining buffers; error message already printed goto error; } if( length > max_length ) { dprintf(D_ALWAYS, "ReliSock::get_bytes_nobuffer: data too large for buffer.\n"); goto error; } result = condor_read(peer_description(), _sock, buffer, length, _timeout); if( result < 0 ) { dprintf(D_ALWAYS, "ReliSock::get_bytes_nobuffer: Failed to receive file.\n"); goto error; } else { // See if it needs to be decrypted if (get_encryption()) { unwrap((unsigned char *) buffer, result, buf, length); // I am reusing length memcpy(buffer, buf, result); free(buf); } _bytes_recvd += result; return result; } error: return -1; }
int DockerAPI::stats(const std::string &container, uint64_t &memUsage, uint64_t &netIn, uint64_t &netOut, uint64_t &userCpu, uint64_t &sysCpu) { #if defined(WIN32) return -1; #else /* * Create a Unix domain socket * * This is what the docker server listens on */ int uds = socket(AF_UNIX, SOCK_STREAM, 0); if (uds < 0) { dprintf(D_ALWAYS, "Can't create unix domain socket, no docker statistics will be available\n"); return -1; } struct sockaddr_un sa; memset(&sa, 0, sizeof(sa)); sa.sun_family = AF_UNIX; strncpy(sa.sun_path, "/var/run/docker.sock",sizeof(sa.sun_path) - 1); { TemporaryPrivSentry sentry(PRIV_ROOT); int cr = connect(uds, (struct sockaddr *) &sa, sizeof(sa)); if (cr != 0) { dprintf(D_ALWAYS, "Can't connect to /var/run/docker.sock %s, no statistics will be available\n", strerror(errno)); close(uds); return -1; } } char request[256]; sprintf(request, "GET /containers/%s/stats?stream=0 HTTP/0.9\r\n\r\n", container.c_str()); int ret = write(uds, request, strlen(request)); if (ret < 0) { dprintf(D_ALWAYS, "Can't send request to docker server, no statistics will be available\n"); close(uds); return -1; } std::string response; char buf[1024]; int written; // read with 200 second timeout, no flags, nonblocking while ((written = condor_read("Docker Socket", uds, buf, 1, 5, 0, false)) > 0) { response.append(buf, written); } dprintf(D_FULLDEBUG, "docker stats: %s\n", response.c_str()); close(uds); // Response now contains an enormous JSON formatted response // Hackily extract the fields we are interested in size_t pos; memUsage = netIn = netOut = userCpu = sysCpu = 0; // Would really like a real JSON parser here... pos = response.find("\"max_usage\""); if (pos != std::string::npos) { sscanf(response.c_str()+pos, "\"max_usage\":%ld", &memUsage); } pos = response.find("\"tx_bytes\""); if (pos != std::string::npos) { sscanf(response.c_str()+pos, "\"tx_bytes\":%ld", &netOut); } pos = response.find("\"rx_bytes\""); if (pos != std::string::npos) { sscanf(response.c_str()+pos, "\"rx_bytes\":%ld", &netIn); } pos = response.find("\"usage_in_usermode\""); if (pos != std::string::npos) { sscanf(response.c_str()+pos, "\"usage_in_usermode\":%ld", &userCpu); } pos = response.find("\"usage_in_kernelmode\""); if (pos != std::string::npos) { sscanf(response.c_str()+pos, "\"usage_in_kernelmode\":%ld", &sysCpu); } dprintf(D_FULLDEBUG, "docker stats reports max_usage is %ld rx_bytes is %ld tx_bytes is %ld usage_in_usermode is %ld usage_in-sysmode is %ld\n", memUsage, netIn, netOut, userCpu, sysCpu); return 0; #endif } /*
int ReliSock::RcvMsg::rcv_packet( char const *peer_description, SOCKET _sock, int _timeout) { Buf *tmp; char hdr[MAX_HEADER_SIZE]; int end; int len, len_t, header_size; int tmp_len; header_size = (mode_ != MD_OFF) ? MAX_HEADER_SIZE : NORMAL_HEADER_SIZE; int retval = condor_read(peer_description,_sock,hdr,header_size,_timeout); if ( retval < 0 && retval != -2 ) // -2 means peer just closed the socket { dprintf(D_ALWAYS,"IO: Failed to read packet header\n"); return FALSE; } if ( retval == -2 ) { // -2 means peer just closed the socket dprintf(D_FULLDEBUG,"IO: EOF reading packet header\n"); return FALSE; } end = (int) ((char *)hdr)[0]; memcpy(&len_t, &hdr[1], 4); len = (int) ntohl(len_t); if (end < 0 || end > 10) { dprintf(D_ALWAYS,"IO: Incoming packet header unrecognized\n"); return FALSE; } if (!(tmp = new Buf)){ dprintf(D_ALWAYS, "IO: Out of memory\n"); return FALSE; } if (len > tmp->max_size()){ delete tmp; dprintf(D_ALWAYS, "IO: Incoming packet is too big\n"); return FALSE; } if (len <= 0) { delete tmp; dprintf(D_ALWAYS, "IO: Incoming packet improperly sized (len=%d,end=%d)\n", len,end); return FALSE; } if ((tmp_len = tmp->read(peer_description, _sock, len, _timeout)) != len){ delete tmp; dprintf(D_ALWAYS, "IO: Packet read failed: read %d of %d\n", tmp_len, len); return FALSE; } // Now, check MD if (mode_ != MD_OFF) { if (!tmp->verifyMD(&hdr[5], mdChecker_)) { delete tmp; dprintf(D_ALWAYS, "IO: Message Digest/MAC verification failed!\n"); return FALSE; // or something other than this } } if (!buf.put(tmp)) { delete tmp; dprintf(D_ALWAYS, "IO: Packet storing failed\n"); return FALSE; } if (end) { ready = TRUE; } return TRUE; }
int ReliSock::get_bytes_raw( char *buffer, int length ) { return condor_read(peer_description(),_sock,buffer,length,_timeout); }