static void handle_sigpoll(int signal_num) { fprintf(stderr, "*** Received SIGPOL event\n"); gDevPollArray[0].events = POLLIN; poll(gDevPollArray, 1, 1000); if (gDevPollArray[0].revents | POLLERR) { fprintf(stderr, " POLLERR occurred\n"); SKU_close(gDevPollArray[0].fd); open_net(Ip_address, Port); } if (gDevPollArray[0].revents | POLLHUP) { fprintf(stderr, " POLLHUP occurred\n"); SKU_close(gDevPollArray[0].fd); open_net(Ip_address, Port); } if (gDevPollArray[0].revents | POLLNVAL) { fprintf(stderr, " POLLNVAL occurred\n"); SKU_close(gDevPollArray[0].fd); open_net(Ip_address, Port); } PORTsignal(signal_num, handle_sigpoll); }
/* * function do_bind(): open block devicename, bind network, do handshake, * set block device configuration (size, blocksize), * start session, fork and go to background * returns: 1 when session finished, otherwise POSIX error code */ int do_bind(client_t * client) { struct dnbd_file cfile; /* open block device */ if ((client->dnbd = open_dnbd(client)) < 0) return -EINVAL; printf("DNBD device successfully set.\n"); /* bind network */ if ((client->sock = open_net(client)) < 0) return -EINVAL; fprintf(stdout, "Socket successfully opened.\n"); /* configure block device */ if (ioctl(client->dnbd, DNBD_SET_SOCK, client->sock) < 0) { close(client->sock); fprintf(stderr, "ERROR: ioctl DNBD_SET_SOCKET failed!\n"); return -EINVAL; } if (ioctl(client->dnbd, DNBD_SET_GROUPNET, &client->mca_adr) < 0) { fprintf(stderr, "ERROR: ioctl DNBD_SET_GROUPNET failed!\n"); return -EINVAL; } fprintf(stdout, "Multicast address successfully set to %s.\n", inet_ntoa(client->mca_adr.sin_addr)); /* start handshake */ if (do_handshake(client) < 0) return -EINVAL; /* set block size and capacity of device */ if (ioctl(client->dnbd, DNBD_SET_BLKSIZE, client->blksize) < 0) { fprintf(stderr, "ERROR: ioctl DNBD_SET_BLKSIZE failed!\n"); return -EINVAL; } if (ioctl(client->dnbd, DNBD_SET_CAPACITY, &client->capacity) < 0) { fprintf(stderr, "ERROR: ioctl DNBD_SET_SIZE failed!\n"); return -EINVAL; } /* activate cache, if necessary */ if (client->cachefile) { cfile.name = client->cachefile; cfile.len = strlen(client->cachefile); if (ioctl(client->dnbd, DNBD_SET_CACHE, &cfile) < 0) { fprintf(stderr, "ERROR: ioctl DNBD_SET_CACHE failed!\n"); return -EINVAL; } printf("Cachefile successfully set.\n"); } /* go to background */ if (!daemonize()) return -ECHILD; if (ioctl(client->dnbd, DNBD_DO_IT) < 0) { fprintf(stderr, "ERROR: ioctl DNBD_DO_IT terminated unexpected!\n"); } else { fprintf(stdout, "dnbd terminated.\n"); } return 1; }
int NetIORead(ui08 *pBuf, int count) { int chCount, lenmsg, nread, nbyts; PMU_auto_register("Getting network data"); chCount = count; if (count > byteCount) { if (byteCount) { memcpy( pBuf, pExtract, byteCount); pBuf += byteCount; chCount -= byteCount; } pExtract = buffer; for (byteCount = 0; lenmsg = kNetRdSz, byteCount < kNetRdSz; /* NULL increment */) { nread = kNetRdSz; if (nread > (lenmsg - byteCount)) nread = lenmsg - byteCount; do { /* if (Params.debug_level >= DEBUG_EXTRA) * fprintf(stderr, * "Trying to read %d bytes from socket\n", * nread); */ nbyts = SKU_read_timed_hb(gDevPollArray[0].fd, (void *)&buffer[byteCount], nread, READ_RETRIES, WAIT_MSECS, 1, PMU_auto_register); /* if (Params.debug_level >= DEBUG_EXTRA) * fprintf(stderr, * " %d bytes read\n", nbyts); */ } while (nbyts == -1); if (nread > 0 && nbyts <= 0) { /* * Try to reconnect to the socket in case it died */ fprintf(stderr, "*** Error %d returned from SKU_read_timed_hb(), reconnecting...\n", nbyts); SKU_close(gDevPollArray[0].fd); open_net(Ip_address, Port); perror("receive"); return(nbyts); } byteCount += nbyts; } if (chCount > byteCount) { fprintf(stderr, "\nNetIORead Size Exceeded!\n"); return( -1); } } memcpy(pBuf, pExtract, chCount); pExtract += chCount; byteCount -= chCount; return(count); }