bool init_inotify() { inotify_fd = inotify_init(); if (inotify_fd < 0) { int e = errno; userlog(LOG_ERR, "inotify_init: %s", strerror(e)); if (e == EMFILE) { message(MSG_INSTANCE_LIMIT); } return false; } userlog(LOG_DEBUG, "inotify fd: %d", get_inotify_fd()); read_watch_descriptors_count(); if (watch_count <= 0) { close(inotify_fd); inotify_fd = -1; return false; } userlog(LOG_INFO, "inotify watch descriptors: %d", watch_count); watches = table_create(watch_count); if (watches == NULL) { userlog(LOG_ERR, "out of memory"); close(inotify_fd); inotify_fd = -1; return false; } return true; }
static void main_loop() { int input_fd = fileno(stdin), inotify_fd = get_inotify_fd(); int nfds = (inotify_fd > input_fd ? inotify_fd : input_fd) + 1; fd_set rfds; bool go_on = true; while (go_on) { FD_ZERO(&rfds); FD_SET(input_fd, &rfds); FD_SET(inotify_fd, &rfds); if (select(nfds, &rfds, NULL, NULL, NULL) < 0) { userlog(LOG_ERR, "select: %s", strerror(errno)); go_on = false; } else if (FD_ISSET(input_fd, &rfds)) { go_on = read_input(); } else if (FD_ISSET(inotify_fd, &rfds)) { go_on = process_inotify_input(); } } }