void IpcServer::testCaseShutdownOnRequest() { LOG4CPLUS_TRACE_METHOD(mLogger, __PRETTY_FUNCTION__); mIpc->beginWaitForConnection(); for (int i = 0; i < mClientsNumber; i++) { mConnectionSema.wait(); } mIpc->endWaitForConnection(); assert (mIpc->isConnected()); assert (mClients.size() == mClientsNumber); LOG4CPLUS_INFO(mLogger, "Established connection with all expected clients: " + convertIntegerToString(mClientsNumber)); if (mClientsNumber == 1) { sendPing(); sendKill(); } else { for (int i = 0; i < mClientsNumber; i++) { sendPing(&mClients.at(i)); } for (int i = 0; i < mClientsNumber; i++) { sendKill(&mClients.at(i)); } } LOG4CPLUS_INFO(mLogger, "Test passed successfully!"); }
void do_server(server_state_t * s) { fd_set readset; int ret, handle, i; char buf[1024]; for(;;) { print_vec(s, "vm_sockets"); FD_ZERO(&readset); /* conn socket is in Live_vms at posn 0 */ FD_SET(s->conn_socket, &readset); for (i = 0; i < s->total_count; i++) { if (s->live_vms[i].alive != 0) { FD_SET(s->live_vms[i].sockfd, &readset); } } if (s->sigfd >= 0) FD_SET(s->sigfd, &readset); printf("\nPID %d waiting (maxfd = %d)\n", getpid(), s->maxfd); ret = select(s->maxfd + 1, &readset, NULL, NULL, NULL); if (ret == -1) { perror("select()"); } handle = find_set(&readset, s->maxfd + 1); if (handle == -1) continue; if (handle == s->sigfd) { // a read will clear the signal struct signalfd_siginfo fdsi; if (read(s->sigfd, &fdsi, sizeof(fdsi)) != sizeof(fdsi)) { perror("read sigfd"); exit(4); } switch(fdsi.ssi_signo) { case SIGHUP: printf("HUP\n"); break; case SIGINT: case SIGQUIT: printf("INT or QUIT\n"); // Like Ctrl-C close(s->sigfd); close(s->shm_fd); exit(0); break; default: printf("WTF\n"); // shouldn't happen :-) break; } continue; } if (handle == s->conn_socket) { printf("[NC] new connection\n"); FD_CLR(s->conn_socket, &readset); /* The Total_count is equal to the new guests VM ID */ add_new_guest(s); /* update our the maximum file descriptor number */ s->maxfd = s->live_vms[s->total_count - 1].sockfd > s->maxfd ? s->live_vms[s->total_count - 1].sockfd : s->maxfd; s->live_count++; printf("Live_count is %ld\n", s->live_count); } else { /* then we have received a disconnection */ int recv_ret; long i, j; long deadposn = -1; recv_ret = recv(handle, buf, 1, 0); printf("[DC] recv returned %d\n", recv_ret); /* find the dead VM in our list and move it to the dead list. */ for (i = 0; i < s->total_count; i++) { if (s->live_vms[i].sockfd == handle) { deadposn = i; s->live_vms[i].alive = 0; close(s->live_vms[i].sockfd); for (j = 0; j < s->msi_vectors; j++) { close(s->live_vms[i].efd[j]); } free(s->live_vms[i].efd); s->live_vms[i].sockfd = -1; break; } } for (j = 0; j < s->total_count; j++) { /* update remaining clients that one client has left/died */ if (s->live_vms[j].alive) { printf("[UD] sending kill of fd[%ld] to %ld\n", deadposn, j); sendKill(s->live_vms[j].sockfd, deadposn, sizeof(deadposn)); } } s->live_count--; /* close the socket for the departed VM */ close(handle); } } }
int main(int argc, char ** argv) { fd_set readset; server_state_t * s; s = (server_state_t *)calloc(1, sizeof(server_state_t)); s->live_count = 0; s->total_count = 0; parse_args(argc, argv, s); /* open shared memory file */ if ((s->shm_fd = shm_open(s->shmobj, O_CREAT|O_RDWR, S_IRWXU)) < 0) { fprintf(stderr, "ivshmem server: could not open shared file\n"); exit(-1); } if (ftruncate(s->shm_fd, s->shm_size) != 0) { fprintf(stderr, "ivshmem server: could not truncate memory region\n"); exit(-1); } s->conn_socket = create_listening_socket(s->path); s->maxfd = s->conn_socket; for(;;) { int ret, handle, i; char buf[1024]; print_vec(s, "vm_sockets"); FD_ZERO(&readset); /* conn socket is in Live_vms at posn 0 */ FD_SET(s->conn_socket, &readset); for (i = 0; i < s->total_count; i++) { if (s->live_vms[i].alive != 0) { FD_SET(s->live_vms[i].sockfd, &readset); } } printf("\nWaiting (maxfd = %d)\n", s->maxfd); ret = select(s->maxfd + 1, &readset, NULL, NULL, NULL); if (ret == -1) { perror("select()"); } handle = find_set(&readset, s->maxfd + 1); if (handle == -1) continue; if (handle == s->conn_socket) { printf("[NC] new connection\n"); FD_CLR(s->conn_socket, &readset); /* The Total_count is equal to the new guests VM ID */ add_new_guest(s); /* update our the maximum file descriptor number */ s->maxfd = s->live_vms[s->total_count - 1].sockfd > s->maxfd ? s->live_vms[s->total_count - 1].sockfd : s->maxfd; s->live_count++; printf("Live_count is %ld\n", s->live_count); } else { /* then we have received a disconnection */ int recv_ret; long i, j; long deadposn = -1; recv_ret = recv(handle, buf, 1, 0); printf("[DC] recv returned %d\n", recv_ret); /* find the dead VM in our list and move it do the dead list. */ for (i = 0; i < s->total_count; i++) { if (s->live_vms[i].sockfd == handle) { deadposn = i; s->live_vms[i].alive = 0; close(s->live_vms[i].sockfd); for (j = 0; j < s->msi_vectors; j++) { close(s->live_vms[i].efd[j]); } free(s->live_vms[i].efd); s->live_vms[i].sockfd = -1; break; } } for (j = 0; j < s->total_count; j++) { /* update remaining clients that one client has left/died */ if (s->live_vms[j].alive) { printf("[UD] sending kill of fd[%ld] to %ld\n", deadposn, j); sendKill(s->live_vms[j].sockfd, deadposn, sizeof(deadposn)); } } s->live_count--; /* close the socket for the departed VM */ close(handle); } } return 0; }