static void do_connection(void) { FAMConnection fc; FAMRequest fr; int data; int loop; int ret; ret = FAMOpen(&fc); if (ret < 0) { fprintf(stderr, "Failed to connect to the FAM server\n"); exit(1); } for (loop = 0; loop < 1; loop++) { ret = FAMMonitorDirectory(&fc, "/u/veillard/test", &fr, &data); if (ret != 0) { fprintf(stderr, "Failed register monitor for /tmp\n"); exit(1); } sleep(1); check_event(&fc); } ret = FAMClose(&fc); if (ret < 0) { fprintf(stderr, "Failed to close connection to the FAM server\n"); exit(1); } }
static void viewer_fam_close(viewer_t *viewer) { viewer_fam_cancel(viewer); if ( viewer->fc_tag != -1 ) { gdk_input_remove(viewer->fc_tag); viewer->fc_tag = -1; FAMClose(&(viewer->fc)); } }
static void* fhs_event_process(void *data) { struct fe_handler *feh = (struct fe_handler*)data; FAMEvent fe; FAMRequest fr; FAMConnection fc; char* root_path; root_path = g_hash_table_lookup(feh->ohh->config, "root_path"); FAMOpen(&fc); FAMMonitorDirectory(&fc, root_path, &fr, REQ_RES); while(!feh->closing) { /* The old code is: FAMNextEvent(&fc, &fe); The new code is if (1 == FAMPending(&fc)) { FAMNextEvent(&fc, &fe); }else continue; In the old code, if this thread is processing FAMNextEvent and then feh->closing is set by another thread, this thread is still hung up. so the synchronized call is changed as asychronized call (polling call) though I don't like this mode. */ if (1 == FAMPending(&fc)) { FAMNextEvent(&fc, &fe); }else continue; if ((fe.userdata == REQ_RES) && ((fe.code == FAMCreated) || (fe.code == FAMExists))) { if (!IS_DIR(fe.filename)) fhs_event_add_resource(feh, fe.filename, &fe); }else if ((fe.userdata == REQ_RDR) && (fe.code == FAMDeleted)) { if (!IS_DIR(fe.filename)) fhs_event_remove_resource(feh, &fe); else printf("faint! why delete root path\n"); }else if (fe.userdata == REQ_RDR) { if (((fe.code == FAMChanged) || (fe.code == FAMExists))&& ((!strcmp(fe.filename, "reading"))|| (!strcmp(fe.filename, "thres")))) { fhs_event_sensor_update(feh, &fe); } } } FAMClose(&fc); return 0; }
static void disconnect_from_fam() { if ( fam_io_channel ) { g_io_channel_unref( fam_io_channel ); fam_io_channel = NULL; g_source_remove( fam_io_watch ); #ifdef USE_INOTIFY close( inotify_fd ); inotify_fd = -1; #else FAMClose( &fam ); #endif } }
static void* fhs_event_process(void *data) { struct fe_handler *feh = (struct fe_handler*)data; FAMEvent fe; FAMRequest fr; FAMConnection fc; char* root_path; #ifndef UNIT_TEST root_path = g_hash_table_lookup(feh->ohh->config, "root_path"); #else root_path = "/home/guorj/HPI/openhpi/src/plugins/simulator/test/resources"; #endif FAMOpen(&fc); FAMMonitorDirectory(&fc, root_path, &fr, (void*)req_res); while(!feh->done) { FAMNextEvent(&fc, &fe); if ((fe.userdata == (void *)req_res) && ((fe.code == FAMCreated) || (fe.code == FAMExists))) { if (!IS_DIR(fe.filename)) fhs_event_add_resource(feh, fe.filename, &fe); }else if ((fe.userdata == (void*) req_res) && (fe.code == FAMDeleted)) { if (!IS_DIR(fe.filename)) fhs_event_remove_resource(feh, &fe); else printf("faint! why delete root path\n"); }else if (fe.userdata == (void *)req_rdr) { if (((fe.code == FAMChanged) || (fe.code == FAMExists))&& ((!strcmp(fe.filename, "reading"))|| (!strcmp(fe.filename, "thres")))) { fhs_event_sensor_update(feh, &fe); } } } FAMClose(&fc); #if 0 free all resource id and rdr id; #endif return 0; }
void stat_cache_free(stat_cache *sc) { while (sc->files) { int osize; splay_tree *node = sc->files; osize = sc->files->size; stat_cache_entry_free(node->data); sc->files = splaytree_delete(sc->files, node->key); force_assert(osize - 1 == splaytree_size(sc->files)); } buffer_free(sc->dir_name); buffer_free(sc->hash_key); #ifdef HAVE_FAM_H while (sc->dirs) { int osize; splay_tree *node = sc->dirs; osize = sc->dirs->size; fam_dir_entry_free(&sc->fam, node->data); sc->dirs = splaytree_delete(sc->dirs, node->key); if (osize == 1) { force_assert(NULL == sc->dirs); } else { force_assert(osize == (sc->dirs->size + 1)); } } if (-1 != sc->fam_fcce_ndx) { /* fd events already gone */ sc->fam_fcce_ndx = -1; FAMClose(&sc->fam); } #endif free(sc); }
void stat_cache_free(stat_cache *sc) { while (sc->files) { int osize; splay_tree *node = sc->files; osize = sc->files->size; stat_cache_entry_free(node->data); sc->files = splaytree_delete(sc->files, node->key); assert(osize - 1 == splaytree_size(sc->files)); } buffer_free(sc->dir_name); buffer_free(sc->hash_key); #ifdef HAVE_FAM_H while (sc->dirs) { int osize; splay_tree *node = sc->dirs; osize = sc->dirs->size; fam_dir_entry_free(node->data); sc->dirs = splaytree_delete(sc->dirs, node->key); if (osize == 1) { assert(NULL == sc->dirs); } else { assert(osize == (sc->dirs->size + 1)); } } if (sc->fam) { FAMClose(sc->fam); free(sc->fam); } #endif free(sc); }
handler_t stat_cache_handle_fdevent(server *srv, void *_fce, int revent) { size_t i; stat_cache *sc = srv->stat_cache; size_t events; UNUSED(_fce); /* */ if (revent & FDEVENT_IN) { events = FAMPending(&sc->fam); for (i = 0; i < events; i++) { FAMEvent fe; fam_dir_entry *fam_dir; splay_tree *node; int ndx, j; FAMNextEvent(&sc->fam, &fe); /* handle event */ switch(fe.code) { case FAMChanged: case FAMDeleted: case FAMMoved: /* if the filename is a directory remove the entry */ fam_dir = fe.userdata; fam_dir->version++; /* file/dir is still here */ if (fe.code == FAMChanged) break; /* we have 2 versions, follow and no-follow-symlink */ for (j = 0; j < 2; j++) { buffer_copy_string(sc->hash_key, fe.filename); buffer_append_int(sc->hash_key, j); ndx = hashme(sc->hash_key); sc->dirs = splaytree_splay(sc->dirs, ndx); node = sc->dirs; if (node && (node->key == ndx)) { int osize = splaytree_size(sc->dirs); fam_dir_entry_free(&sc->fam, node->data); sc->dirs = splaytree_delete(sc->dirs, ndx); force_assert(osize - 1 == splaytree_size(sc->dirs)); } } break; default: break; } } } if (revent & FDEVENT_HUP) { /* fam closed the connection */ fdevent_event_del(srv->ev, &(sc->fam_fcce_ndx), FAMCONNECTION_GETFD(&sc->fam)); fdevent_unregister(srv->ev, FAMCONNECTION_GETFD(&sc->fam)); FAMClose(&sc->fam); } return HANDLER_GO_ON; }
static int processCommand(char *line, int no) { int ret, args; char *command = NULL; char *arg = NULL; char *arg2 = NULL; if (line == NULL) return (-1); if (line[0] == '#') return (0); args = scanCommand(line, &command, &arg, &arg2); if (args < 0) return (-1); if (args == 0) return (0); if (!strcmp(command, "connect")) { if (testState.connected) { fprintf(stderr, "connect line %d: already connected\n", no); return (-1); } if (arg != NULL) { #ifdef HAVE_SETENV setenv("GAM_CLIENT_ID", arg, 1); #elif HAVE_PUTENV char *client_id = malloc (strlen (arg) + sizeof "GAM_CLIENT_ID="); if (client_id) { strcpy (client_id, "GAM_CLIENT_ID="); strcat (client_id, arg); putenv (client_id); } #endif /* HAVE_SETENV */ } ret = FAMOpen(&(testState.fc)); if (ret < 0) { fprintf(stderr, "connect line %d: failed to connect\n", no); return (-1); } testState.connected = 1; if (arg != NULL) printf("connected to %s\n", arg); else printf("connected\n"); } else if (!strcmp(command, "kill")) { /* * okay, it's heavy but that's the simplest way since we do not have * the pid(s) of the servers running. */ ret = system("killall gam_server"); if (ret < 0) { fprintf(stderr, "kill line %d: failed to killall gam_server\n", no); return (-1); } printf("killall gam_server\n"); } else if (!strcmp(command, "disconnect")) { if (testState.connected == 0) { fprintf(stderr, "disconnect line %d: not connected\n", no); return (-1); } ret = FAMClose(&(testState.fc)); if (ret < 0) { fprintf(stderr, "connect line %d: failed to disconnect\n", no); return (-1); } testState.connected = 0; printf("disconnected\n"); } else if (!strcmp(command, "mondir")) { if (args >= 2) { if (arg[0] != '/') snprintf(filename, sizeof(filename), "%s/%s", pwd, arg); else snprintf(filename, sizeof(filename), "%s", arg); } if (args == 2) { ret = FAMMonitorDirectory(&(testState.fc), filename, &(testState. fr[testState.nb_requests]), NULL); } else if (args == 3) { int index; if (sscanf(arg2, "%d", &index) <= 0) { fprintf(stderr, "mondir line %d: invalid index value %s\n", no, arg2); return (-1); } testState.fr[testState.nb_requests].reqnum = index; ret = FAMMonitorDirectory2(&(testState.fc), filename, &(testState. fr[testState.nb_requests])); } else { fprintf(stderr, "mondir line %d: invalid format\n", no); return (-1); } if (ret < 0) { fprintf(stderr, "mondir line %d: failed to monitor %s\n", no, arg); return (-1); } printf("mondir %s %d\n", arg, testState.nb_requests); testState.nb_requests++; } else if (!strcmp(command, "monfile")) { if (args != 2) { fprintf(stderr, "monfile line %d: lacks name\n", no); return (-1); } if (arg[0] != '/') snprintf(filename, sizeof(filename), "%s/%s", pwd, arg); else snprintf(filename, sizeof(filename), "%s", arg); ret = FAMMonitorFile(&(testState.fc), filename, &(testState.fr[testState.nb_requests]), NULL); if (ret < 0) { fprintf(stderr, "monfile line %d: failed to monitor %s\n", no, arg); return (-1); } printf("monfile %s %d\n", arg, testState.nb_requests); testState.nb_requests++; } else if (!strcmp(command, "pending")) { if (args != 1) { fprintf(stderr, "pending line %d: extra argument %s\n", no, arg); return (-1); } ret = FAMPending(&(testState.fc)); if (ret < 0) { fprintf(stderr, "pending line %d: failed\n", no); return (-1); } printf("pending %d\n", ret); } else if (!strcmp(command, "mkdir")) { if (args != 2) { fprintf(stderr, "mkdir line %d: lacks name\n", no); return (-1); } ret = mkdir(arg, 0755); if (ret < 0) { fprintf(stderr, "mkdir line %d: failed to create %s\n", no, arg); return (-1); } printf("mkdir %s\n", arg); } else if (!strcmp(command, "chmod")) { if (args != 3) { fprintf(stderr, "chmod line %d: lacks path and mode\n", no); return (-1); } ret = chmod(arg, strtol (arg2, NULL, 8)); if (ret < 0) { fprintf(stderr, "chmod line %d: failed to chmod %s to %s\n", no, arg, arg2); return (-1); } printf("chmod %s to %s\n", arg, arg2); } else if (!strcmp(command, "chown")) { if (args != 3) { fprintf(stderr, "chown line %d: lacks path and owner\n", no); return (-1); } struct stat sb; if (!lstat (arg, &sb)) { ret = (S_ISLNK (sb.st_mode)) ? lchown(arg, strtol(arg2, NULL, 10), -1) : chown(arg, strtol(arg2, NULL, 10), -1); } else ret=-1; if (ret < 0) { fprintf(stderr, "chown line %d: failed to chown %s to %s\n", no, arg, arg2); return (-1); } printf("chown %s to %s\n", arg, arg2); } else if (!strcmp(command, "mkfile")) { if (args != 2) { fprintf(stderr, "mkfile line %d: lacks name\n", no); return (-1); } ret = open(arg, O_CREAT | O_WRONLY, 0666); if (ret < 0) { fprintf(stderr, "mkfile line %d: failed to open %s\n", no, arg); return (-1); } close(ret); printf("mkfile %s\n", arg); } else if (!strcmp(command, "append")) { if (args != 2) { fprintf(stderr, "mkfile line %d: lacks name\n", no); return (-1); } ret = open(arg, O_RDWR | O_APPEND); if (ret < 0) { fprintf(stderr, "append line %d: failed to open %s\n", no, arg); return (-1); } write(ret, "a", 1); close(ret); printf("append %s\n", arg); } else if (!strcmp(command, "rmdir")) { if (args != 2) { fprintf(stderr, "rmdir line %d: lacks name\n", no); return (-1); } ret = rmdir(arg); if (ret < 0) { fprintf(stderr, "rmdir line %d: failed to remove %s\n", no, arg); return (-1); } printf("rmdir %s\n", arg); } else if (!strcmp(command, "rmfile")) { if (args != 2) { fprintf(stderr, "rmfile line %d: lacks name\n", no); return (-1); } ret = unlink(arg); if (ret < 0) { fprintf(stderr, "rmfile line %d: failed to unlink %s\n", no, arg); return (-1); } printf("rmfile %s\n", arg); } else if (!strcmp(command, "move")) { if (args != 3) { fprintf(stderr, "move line %d: lacks something\n", no); return (-1); } ret = rename(arg, arg2); if (ret < 0) { fprintf(stderr, "move line %d: failed to move %s\n", no, arg); return (-1); } printf("move %s %s\n", arg, arg2); } else if (!strcmp(command, "link")) { if (args != 3) { fprintf(stderr, "link line %d: lacks target and name\n", no); return (-1); } ret = symlink(arg, arg2); if (ret < 0) { fprintf(stderr, "link line %d: failed to link to %s\n", no, arg); return (-1); } printf("link %s to %s\n", arg2, arg); } else if (!strcmp(command, "event")) { printEvent(no); } else if (!strcmp(command, "events")) { printEvents(no); } else if (!strcmp(command, "expect")) { int count; int delay = 0; int nb_events = testState.nb_events; if (args != 2) { fprintf(stderr, "expect line %d: lacks number\n", no); return (-1); } if (sscanf(arg, "%d", &count) <= 0) { fprintf(stderr, "expect line %d: invalid number value %s\n", no, arg); return (-1); } /* * wait at most 3 secs before declaring failure */ while ((delay < 30) && (testState.nb_events < nb_events + count)) { debugLoop(100); /* printf("+"); fflush(stdout); */ delay++; } if (testState.nb_events < nb_events + count) { printf("expect line %d: got %d of %d expected events\n", no, testState.nb_events - nb_events, count); return (-1); } } else if (!strcmp(command, "sleep")) { int i; for (i = 0; (i < 30) && (FAMPending(&(testState.fc)) == 0); i++) usleep(50000); } else if (!strcmp(command, "wait")) { sleep(1); } else if (!strcmp(command, "cancel")) { if (args == 2) { int req_index = 0; if (sscanf(arg, "%d", &req_index) <= 0 || req_index >= testState.nb_requests) { fprintf(stderr, "cancel line %d: invalid req_index value %s\n", no, arg); return (-1); } ret = FAMCancelMonitor(&(testState.fc), &(testState.fr[req_index])); } else { fprintf(stderr, "cancel line %d: invalid format\n", no); return (-1); } if (ret < 0) { fprintf(stderr, "cancel line %d: failed to cancel req_index %s\n", no, arg); return (-1); } printf("cancel %s %d\n", arg, testState.nb_requests); } else { fprintf(stderr, "Unable to parse line %d: %s\n", no, line); return (-1); } return (0); }
/** * Main worker thread. Register FAM events and process. */ static void * worker(void * arg) { DIC * cls = arg; int i; int more; int wasMore; int ret; void * unused; char * fn; PTHREAD_T helperThread; cls->log(cls->logContext, DOODLE_LOG_VERY_VERBOSE, _("Main worker thread created.\n")); cls->eventCount = 0; cls->continueRunning = 1; cls->events = NULL; cls->signal = SEMAPHORE_NEW(0); if (0 != PTHREAD_CREATE(&helperThread, &processEvents, cls, 64 * 1024)) { cls->log(cls->logContext, DOODLE_LOG_CRITICAL, _("Failed to spawn event processing thread.\n")); run_shutdown(0); return NULL; } cls->log(cls->logContext, DOODLE_LOG_VERBOSE, _("Registering with FAM for file system events.\n")); for (i=0;i<cls->argc;i++) { char * exp; cls->log(cls->logContext, DOODLE_LOG_VERY_VERBOSE, _("Indexing '%s'\n"), cls->argv[i]); exp = expandFileName(cls->argv[i]); if (-1 == do_index(exp, cls)) { ret = -1; free(exp); break; } free(exp); } DOODLE_tree_destroy(cls->tree); cls->treePresent = 0; cls->tree = NULL; cls->log(cls->logContext, DOODLE_LOG_VERBOSE, _("doodled startup complete. Now waiting for FAM events.\n")); wasMore = 0; while ( (cls->continueRunning) && (0 == testShutdown()) ) { SEMAPHORE_DOWN(cls->signal); cls->log(cls->logContext, DOODLE_LOG_INSANELY_VERBOSE, "Received signal to process fam event.\n"); MUTEX_LOCK(&cls->lock); if (cls->eventCount > 0) { fn = cls->events[cls->eventCount-1]; GROW(cls->events, cls->eventCount, cls->eventCount-1); more = cls->eventCount > 0; cls->log(cls->logContext, DOODLE_LOG_INSANELY_VERBOSE, "Processing fam event '%s'.\n", fn); } else { fn = NULL; more = 0; } if (! wasMore) { cls->treePresent++; if (cls->treePresent == 1) cls->tree = DOODLE_tree_create((DOODLE_Logger) cls->log, cls->logContext, cls->ename); } MUTEX_UNLOCK(&cls->lock); if (fn != NULL) { do_index(fn, cls); free(fn); } MUTEX_LOCK(&cls->lock); if (! more) { cls->treePresent--; if (cls->treePresent == 0) DOODLE_tree_destroy(cls->tree); } MUTEX_UNLOCK(&cls->lock); wasMore = more; } /* forever (until signal) */ cls->continueRunning = 0; if (0 != FAMClose(&cls->fc)) { cls->log(cls->logContext, DOODLE_LOG_CRITICAL, _("Error disconnecting from fam.\n")); } PTHREAD_KILL(&helperThread, SIGTERM); PTHREAD_JOIN(&helperThread, &unused); SEMAPHORE_FREE(cls->signal); if (cls->treePresent > 0) DOODLE_tree_destroy(cls->tree); return NULL; }