/* Simple demonstation of using xenstore watches by Michal Novotny Hard-coded to path of /tool/test in xenstore and token called "token". Please compile using: $ gcc -o xswatchtest xswatchtest.c -lxenstore -lpthread and run as root (otherwise it fails). It creates a new thread for watching, called "watcher" with xs_handle passed to it. Polling itself is done by select() on the xenstore file descriptor. This demo is running the countdown of 1 minute in the main thread and the secondary thread is polling on changes of /tool/test path in the xenstore. Please test by issuing: # xenstore-write /tool/test/some thing when running to see it's working fine. Hope this helps! Michal */ #include #include #include #include #define PATH "/tool/test" #define TOKEN "token" void *watcher(void *opaque); void *watcher(void *opaque) { int fd, num; fd_set fds; char **vec; struct xs_handle *xsh; xsh = opaque; fd = xs_fileno(xsh); FD_ZERO(&fds); FD_SET(fd, &fds); while (select(fd + 1, &fds, NULL, NULL, NULL) > 0) { vec = xs_read_watch(xsh, &num); if (vec != NULL) if (strcmp(vec[XS_WATCH_TOKEN], TOKEN) == 0) printf("Data on %s: %s\n", vec[XS_WATCH_PATH], xs_read(xsh, XBT_NULL, PATH, NULL)); } } int main() { pthread_t watch_thread; struct xs_handle *xsh; int i; xsh = xs_daemon_open(); if (xsh == NULL) { perror("Daemon open error"); return 1; } if (!xs_watch(xsh, PATH, TOKEN)) { fprintf(stderr, "Error when establishing a watch\n"); return 0; } pthread_create( &watch_thread, NULL, watcher, (void*) xsh); i = 0; while (i++ < 60) { printf("Sleeping. %d seconds remaining\n", 60 - i); sleep(1); } xs_unwatch(xsh, PATH, TOKEN); xs_daemon_close(xsh); return 0; }