1272343Sngie/* $NetBSD: h_reconcli.c,v 1.2 2011/02/19 09:56:45 pooka Exp $ */ 2272343Sngie 3272343Sngie#include <sys/types.h> 4272343Sngie#include <sys/sysctl.h> 5272343Sngie 6272343Sngie#include <rump/rumpclient.h> 7272343Sngie#include <rump/rump_syscalls.h> 8272343Sngie 9272343Sngie#include <err.h> 10272343Sngie#include <pthread.h> 11272343Sngie#include <stdio.h> 12272343Sngie#include <stdlib.h> 13272343Sngie#include <string.h> 14272343Sngie#include <unistd.h> 15272343Sngie 16272343Sngiestatic volatile int quit, riseandwhine; 17272343Sngie 18272343Sngiestatic pthread_mutex_t closermtx; 19272343Sngiestatic pthread_cond_t closercv; 20272343Sngie 21272343Sngiestatic void * 22272343Sngiecloser(void *arg) 23272343Sngie{ 24272343Sngie 25272343Sngie pthread_mutex_lock(&closermtx); 26272343Sngie while (!quit) { 27272343Sngie while (!riseandwhine) 28272343Sngie pthread_cond_wait(&closercv, &closermtx); 29272343Sngie riseandwhine = 0; 30272343Sngie pthread_mutex_unlock(&closermtx); 31272343Sngie 32272343Sngie /* try to catch a random slot */ 33272343Sngie usleep(random() % 100000); 34272343Sngie 35272343Sngie /* 36272343Sngie * wide-angle disintegration beam, but takes care 37272343Sngie * of the client rumpkernel communication socket. 38272343Sngie */ 39272343Sngie closefrom(3); 40272343Sngie 41272343Sngie pthread_mutex_lock(&closermtx); 42272343Sngie } 43272343Sngie pthread_mutex_unlock(&closermtx); 44272343Sngie 45272343Sngie return NULL; 46272343Sngie} 47272343Sngie 48272343Sngiestatic const int hostnamemib[] = { CTL_KERN, KERN_HOSTNAME }; 49272343Sngiestatic char goodhostname[128]; 50272343Sngie 51272343Sngiestatic void * 52272343Sngieworker(void *arg) 53272343Sngie{ 54272343Sngie char hostnamebuf[128]; 55272343Sngie size_t blen; 56272343Sngie 57272343Sngie pthread_mutex_lock(&closermtx); 58272343Sngie while (!quit) { 59272343Sngie pthread_mutex_unlock(&closermtx); 60272343Sngie if (rump_sys_getpid() == -1) 61272343Sngie err(1, "getpid"); 62272343Sngie 63272343Sngie blen = sizeof(hostnamebuf); 64272343Sngie memset(hostnamebuf, 0, sizeof(hostnamebuf)); 65272343Sngie if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib), 66272343Sngie hostnamebuf, &blen, NULL, 0) == -1) 67272343Sngie err(1, "sysctl"); 68272343Sngie if (strcmp(hostnamebuf, goodhostname) != 0) 69272343Sngie exit(1); 70272343Sngie pthread_mutex_lock(&closermtx); 71272343Sngie riseandwhine = 1; 72272343Sngie pthread_cond_signal(&closercv); 73272343Sngie } 74272343Sngie riseandwhine = 1; 75272343Sngie pthread_cond_signal(&closercv); 76272343Sngie pthread_mutex_unlock(&closermtx); 77272343Sngie 78272343Sngie return NULL; 79272343Sngie} 80272343Sngie 81272343Sngieint 82272343Sngiemain(int argc, char *argv[]) 83272343Sngie{ 84272343Sngie pthread_t pt, w1, w2, w3, w4; 85272343Sngie size_t blen; 86272343Sngie int timecount; 87272343Sngie 88272343Sngie if (argc != 2) 89272343Sngie errx(1, "need timecount"); 90272343Sngie timecount = atoi(argv[1]); 91272343Sngie if (timecount <= 0) 92272343Sngie errx(1, "invalid timecount %d\n", timecount); 93272343Sngie 94272343Sngie srandom(time(NULL)); 95272343Sngie 96272343Sngie rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME); 97272343Sngie if (rumpclient_init() == -1) 98272343Sngie err(1, "init"); 99272343Sngie 100272343Sngie blen = sizeof(goodhostname); 101272343Sngie if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib), 102272343Sngie goodhostname, &blen, NULL, 0) == -1) 103272343Sngie err(1, "sysctl"); 104272343Sngie 105272343Sngie pthread_create(&pt, NULL, closer, NULL); 106272343Sngie pthread_create(&w1, NULL, worker, NULL); 107272343Sngie pthread_create(&w2, NULL, worker, NULL); 108272343Sngie pthread_create(&w3, NULL, worker, NULL); 109272343Sngie pthread_create(&w4, NULL, worker, NULL); 110272343Sngie 111272343Sngie sleep(timecount); 112272343Sngie quit = 1; 113272343Sngie 114272343Sngie pthread_join(pt, NULL); 115272343Sngie pthread_join(w1, NULL); 116272343Sngie pthread_join(w2, NULL); 117272343Sngie pthread_join(w3, NULL); 118272343Sngie pthread_join(w4, NULL); 119272343Sngie 120272343Sngie exit(0); 121272343Sngie} 122