void a() { inotify_init1(IN_NONBLOCK); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: 'inotify_init1' should use IN_CLOEXEC where possible [android-cloexec-inotify-init1] // CHECK-FIXES: inotify_init1(IN_NONBLOCK | IN_CLOEXEC); TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK)); // CHECK-MESSAGES: :[[@LINE-1]]:47: warning: 'inotify_init1' // CHECK-FIXES: TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); }
void f() { inotify_init1(0); // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'inotify_init1' // CHECK-FIXES: inotify_init1(IN_CLOEXEC); TEMP_FAILURE_RETRY(inotify_init1(0)); // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: 'inotify_init1' // CHECK-FIXES: TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); int flag = 1; inotify_init1(flag); TEMP_FAILURE_RETRY(inotify_init1(flag)); }
pa_inotify *pa_inotify_start(const char *filename, void *userdata, pa_inotify_cb cb, pa_core *core) { pa_inotify *i = pa_xnew0(pa_inotify, 1); pa_assert(i); i->core = core; pa_core_ref(core); i->filename = pa_xstrdup(filename); i->callback_data = userdata; i->callback = cb; i->fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); if (i->fd < 0) { pa_log("inotify_init1() failed: %s", pa_cstrerror(errno)); pa_inotify_stop(i); return NULL; } if (inotify_add_watch(i->fd, filename, IN_DELETE_SELF|IN_MOVE_SELF) < 0) { pa_log("inotify_add_watch() failed: %s", pa_cstrerror(errno)); pa_inotify_stop(i); return NULL; } pa_assert_se(i->io_event = core->mainloop->io_new(core->mainloop, i->fd, PA_IO_EVENT_INPUT, inotify_cb, i)); return i; }
/* Thread: main & scan */ static int inofd_event_set(void) { #if defined(__linux__) inofd = inotify_init1(IN_CLOEXEC); if (inofd < 0) { DPRINTF(E_FATAL, L_SCAN, "Could not create inotify fd: %s\n", strerror(errno)); return -1; } event_set(&inoev, inofd, EV_READ, inotify_cb, NULL); #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) inofd = kqueue(); if (inofd < 0) { DPRINTF(E_FATAL, L_SCAN, "Could not create kqueue: %s\n", strerror(errno)); return -1; } event_set(&inoev, inofd, EV_READ, kqueue_cb, NULL); #endif event_base_set(evbase_scan, &inoev); return 0; }
/* Thread: main & scan */ static int inofd_event_set(void) { inofd = inotify_init1(IN_CLOEXEC); if (inofd < 0) { DPRINTF(E_FATAL, L_SCAN, "Could not create inotify fd: %s\n", strerror(errno)); return -1; } inoev = event_new(evbase_scan, inofd, EV_READ, inotify_cb, NULL); #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) deferred_inoev = evtimer_new(evbase_scan, inotify_deferred_cb, NULL); if (!deferred_inoev) { DPRINTF(E_LOG, L_SCAN, "Could not create deferred inotify event\n"); return -1; } #endif return 0; }
/* inotify descriptor, will be shared with rules directory; * set to cloexec since we need our children to be able to add * watches for us */ int udev_watch_init(struct udev *udev) { inotify_fd = inotify_init1(IN_CLOEXEC); if (inotify_fd < 0) log_error("inotify_init failed: %m"); return inotify_fd; }
bool inot_root_init(watchman_global_watcher_t watcher, w_root_t *root, char **errmsg) { struct inot_root_state *state; unused_parameter(watcher); state = calloc(1, sizeof(*state)); if (!state) { *errmsg = strdup("out of memory"); return false; } root->watch = state; pthread_mutex_init(&state->lock, NULL); #ifdef HAVE_INOTIFY_INIT1 state->infd = inotify_init1(IN_CLOEXEC); #else state->infd = inotify_init(); #endif if (state->infd == -1) { ignore_result(asprintf(errmsg, "watch(%.*s): inotify_init error: %s", root->root_path->len, root->root_path->buf, inot_strerror(errno))); w_log(W_LOG_ERR, "%s\n", *errmsg); return false; } w_set_cloexec(state->infd); state->wd_to_name = w_ht_new(HINT_NUM_DIRS, &w_ht_string_val_funcs); state->move_map = w_ht_new(2, &move_hash_funcs); return true; }
/* register (r)queue-specific inotify watches, returning 1 if successful */ static int reg_watches(const char *qpath, const char *rqpath) { /* don't block on read(), since select() is used for polling */ if (inotfd == -1 && (inotfd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) == -1) { warning("failed to initialize inotify instance"); return 0; } #ifdef TESTING if (getenv("CABLE_NOWATCH")) return 1; #endif /* existing watch is ok */ if ((inotqwd = inotify_add_watch(inotfd, qpath, INOTIFY_MASK)) == -1) { warning("could not add inotify watch"); return 0; } /* existing watch is ok */ if ((inotrqwd = inotify_add_watch(inotfd, rqpath, INOTIFY_MASK)) == -1) { warning("could not add inotify watch"); return 0; } return 1; }
static int check_fdinfo_inotify(void) { int ifd, wd, proc_wd = -1, ret; ifd = inotify_init1(0); if (ifd < 0) { pr_perror("Can't make inotify fd"); return -1; } wd = inotify_add_watch(ifd, ".", IN_ALL_EVENTS); if (wd < 0) { pr_perror("Can't add watch"); close(ifd); return -1; } ret = parse_fdinfo(ifd, FD_TYPES__INOTIFY, check_one_inotify, &proc_wd); close(ifd); if (ret < 0) { pr_err("Error parsing proc fdinfo\n"); return -1; } if (wd != proc_wd) { pr_err("WD mismatch (or not met) %d want %d\n", proc_wd, wd); return -1; } pr_info("Inotify fdinfo works OK (%d vs %d)\n", wd, proc_wd); return 0; }
_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) { int fd, k; bool good = false; assert_return(m, -EINVAL); fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); if (fd < 0) return -errno; if (!category || streq(category, "links")) { k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE); if (k < 0) { safe_close(fd); return -errno; } good = true; } if (!good) { close_nointr(fd); return -EINVAL; } *m = FD_TO_MONITOR(fd); return 0; }
int main(void) { struct inotify_event* event; int desc; int file_fd; int fd = inotify_init(); test_assert(fd >= 0); test_assert(0 == close(fd)); fd = inotify_init1(IN_CLOEXEC); test_assert(fd >= 0); file_fd = open("foo", O_WRONLY | O_CREAT, 0777); test_assert(file_fd >= 0); test_assert(0 == close(file_fd)); desc = inotify_add_watch(fd, "foo", IN_ALL_EVENTS); test_assert(desc >= 0); test_assert(0 <= open("foo", O_WRONLY | O_CREAT, 0777)); ALLOCATE_GUARD(event, 'x'); test_assert(sizeof(*event) == read(fd, event, sizeof(*event))); VERIFY_GUARD(event); test_assert(event->wd == desc); test_assert(event->mask == IN_OPEN); test_assert(0 == inotify_rm_watch(fd, desc)); test_assert(0 == unlink("foo")); atomic_puts("EXIT-SUCCESS"); return 0; }
static struct watch_dir_struct *watch_dir_init(void) { struct watch_dir_struct *w; w = malloc(sizeof(*w)); if (!w) return NULL; w->wds_curr_idx = 0; w->wds_size = 1024; w->fd = inotify_init1(IN_NONBLOCK); if (w->fd == -1) { perror("inotify_init1"); free(w); return NULL; } /* Allocate memory for watch descriptors */ w->wds = calloc(w->wds_size, sizeof(int)); if (!w->wds) { perror("calloc"); close(w->fd); free(w); return NULL; } return w; }
_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) { _cleanup_close_ int fd = -1; int k; bool good = false; assert_return(m, -EINVAL); fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); if (fd < 0) return -errno; if (!category || streq(category, "links")) { k = monitor_add_inotify_watch(fd); if (k < 0) return k; good = true; } if (!good) return -EINVAL; *m = FD_TO_MONITOR(fd); fd = -1; return 0; }
virtual void init() { if (mInotifyFd == -1) { mInotifyFd = inotify_init1(IN_CLOEXEC); if (mInotifyFd == -1) { rtLogWarn("hotplug disabled: %s", getSystemError(errno).c_str()); } else { rtLogDebug("initializing inotify, adding watch: %s", kDevInput); mWatchFd = inotify_add_watch(mInotifyFd, kDevInput, (IN_DELETE | IN_CREATE)); if (mWatchFd == -1) { rtLogWarn("hotplug disabled: %s", getSystemError(errno).c_str()); } else { rtLogDebug("adding change notify descriptor: %d to poll list", mInotifyFd); pollfd p; p.fd = mInotifyFd; p.events = (POLLIN | POLLERR); p.revents = 0; mFds.push_back(p); } } } registerDevices(getKeyboardDevices()); registerDevices(getMouseDevices()); }
QT_END_NAMESPACE // --------- inotify.h end ---------- #else /* QT_NO_INOTIFY */ #include <sys/inotify.h> #endif QT_BEGIN_NAMESPACE QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent) { int fd = -1; #ifdef IN_CLOEXEC fd = inotify_init1(IN_CLOEXEC); #endif if (fd == -1) { fd = inotify_init(); if (fd == -1) return 0; } return new QInotifyFileSystemWatcherEngine(fd, parent); }
QT_END_NAMESPACE // --------- inotify.h end ---------- #else /* QT_NO_INOTIFY */ #include <sys/inotify.h> #endif QT_BEGIN_NAMESPACE QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create() { register int fd = -1; #ifdef IN_CLOEXEC fd = inotify_init1(IN_CLOEXEC); #endif if (fd == -1) { fd = inotify_init(); if (fd == -1) return 0; ::fcntl(fd, F_SETFD, FD_CLOEXEC); } return new QInotifyFileSystemWatcherEngine(fd); }
/* inotify descriptor, will be shared with rules directory; * set to cloexec since we need our children to be able to add * watches for us */ int udev_watch_init(struct udev *udev) { inotify_fd = inotify_init1(IN_CLOEXEC); if (inotify_fd < 0) err(udev, "inotify_init failed: %m\n"); return inotify_fd; }
void install_monitor() { if (_inotify_fd == -1) { _inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); _monitor_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)g_object_unref); g_timeout_add(50, (GSourceFunc)_inotify_poll, NULL); _desktop_file = g_file_new_for_commandline_arg(DESKTOP_DIR()); _trash_can = g_file_new_for_uri("trash:///"); GFileMonitor* m = g_file_monitor(_trash_can, G_FILE_MONITOR_NONE, NULL, NULL); g_signal_connect(m, "changed", G_CALLBACK(trash_changed), NULL); _add_monitor_directory(_desktop_file); GDir *dir = g_dir_open(DESKTOP_DIR(), 0, NULL); if (dir != NULL) { const char* filename = NULL; while ((filename = g_dir_read_name(dir)) != NULL) { GFile* f = g_file_get_child(_desktop_file, filename); _add_monitor_directory(f); g_object_unref(f); } g_dir_close(dir); } } }
static void * monitor_file(void *arg) { char *path = arg; int fd = inotify_init1(IN_NONBLOCK); if(fd == -1) err(EXIT_FAILURE, "inotify_init1"); if(inotify_add_watch(fd, path, IN_CLOSE_WRITE) == -1) err(EXIT_FAILURE, "inotify_add_watch"); for(;;) { fd_set set; FD_SET(fd, &set); if(select(fd + 1, &set, NULL, NULL, NULL) == -1) err(EXIT_FAILURE, "select"); if(!(FD_ISSET(fd, &set))) continue; int numbytes; if(ioctl(fd, FIONREAD, &numbytes) == -1) err(EXIT_FAILURE, "ioctl"); char *buf[65536]; if(read(fd, buf, sizeof(buf)) == -1) err(EXIT_FAILURE, "read"); update_image(path); } return NULL; }
CMultiFileWatch() { m_fd = inotify_init1(IN_NONBLOCK); log_d(MODULE, "CMultiFileWatch(): m_fd=%i", m_fd); if(m_fd == -1){ throw Exception(ss_()+"inotify_init() failed: "+strerror(errno)); } }
void testValues() { f = 2; inotify_init1(anyint()); //@ assert f == 2; //@ assert vacuous: \false; }
void File::setTrackEvents(Event events) { #ifdef HAVE_INOTIFY if (events == d->m_events) { return; } Application::PrivateImpl *app_d = static_cast<Application::PrivateImpl*>(application()->d); if (!app_d->m_inotifyStarted) { if ((app_d->m_inotify = inotify_init1(IN_NONBLOCK)) > -1) { app_d->m_inotifyStarted = true; } } if (app_d->m_inotifyStarted) { if (events != NoEvent) { uint32_t inotifyMask = 0; if (events & AccessEvent) { inotifyMask |= IN_ACCESS; } if (events & AttributeChangeEvent) { inotifyMask |= IN_ATTRIB; } if (events & CloseEvent) { inotifyMask |= IN_CLOSE; } if (events & CreateEvent) { inotifyMask |= IN_CREATE; } if (events & DeleteEvent) { inotifyMask |= IN_DELETE; } if (events & ModifyEvent) { inotifyMask |= IN_MODIFY; } if (events & MoveEvent) { inotifyMask |= IN_MOVE; } if (events & OpenEvent) { inotifyMask |= IN_OPEN; } D_I->m_inotifyWatch = inotify_add_watch(app_d->m_inotify, uri().path().data(), inotifyMask); app_d->m_inotifyMap[D_I->m_inotifyWatch] = this; } else { inotify_rm_watch(app_d->m_inotify, D_I->m_inotifyWatch); app_d->m_inotifyMap.erase(D_I->m_inotifyWatch); if (!app_d->m_inotifyMap.size()) { app_d->m_inotifyStarted = false; close(app_d->m_inotify); } } } else { IDEAL_DEBUG_WARNING("it was not possible to track events for file " << uri().uri()); } #else if (events != NoEvent) { IDEAL_DEBUG_WARNING("it was not possible to track events for file " << uri().uri()); } #endif }
int do_inotify_init (int max_events) { #ifdef HAVE_SYS_INOTIFY_H FILE *fp; NEED_ROOT (, return -1); if (max_events < 0) { reply_with_error ("max_events < 0"); return -1; } if (max_events > 0) { fp = fopen (MQE_PATH, "w"); if (fp == NULL) { reply_with_perror (MQE_PATH); return -1; } fprintf (fp, "%d\n", max_events); fclose (fp); } if (inotify_fd >= 0) if (do_inotify_close () == -1) return -1; #ifdef HAVE_INOTIFY_INIT1 inotify_fd = inotify_init1 (IN_NONBLOCK | IN_CLOEXEC); if (inotify_fd == -1) { reply_with_perror ("inotify_init1"); return -1; } #else inotify_fd = inotify_init (); if (inotify_fd == -1) { reply_with_perror ("inotify_init"); return -1; } if (fcntl (inotify_fd, F_SETFL, O_NONBLOCK) == -1) { reply_with_perror ("fcntl: O_NONBLOCK"); close (inotify_fd); inotify_fd = -1; return -1; } if (fcntl (inotify_fd, F_SETFD, FD_CLOEXEC) == -1) { reply_with_perror ("fcntl: FD_CLOEXEC"); close (inotify_fd); inotify_fd = -1; return -1; } #endif return 0; #else NOT_AVAILABLE (-1); #endif }
void imv_init_reload(struct imv_reload *rld) { rld->fd = inotify_init1(IN_NONBLOCK); if(rld->fd == -1) { perror("imv_init_reload"); } rld->wd = 0; }
static void setup(void) { if (plugin.io.fd > 0) close(plugin.io.fd); plugin.io.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); if (-1 == plugin.io.fd || inotify_add_watch(plugin.io.fd, "/dev", IN_CREATE | IN_DELETE) < 0) _pe("Failed starting TTY watcher"); }
bool FDirectoryWatchRequestLinux::Init(const FString& InDirectory) { if (InDirectory.Len() == 0) { // Verify input return false; } Directory = InDirectory; if (bRunning) { Shutdown(); } bEndWatchRequestInvoked = false; // Make sure the path is absolute const FString FullPath = FPaths::ConvertRelativePathToFull(InDirectory); FileDescriptor = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); if (FileDescriptor == -1) { // Failed to init inotify UE_LOG(LogDirectoryWatcher, Error, TEXT("Failed to init inotify")); return false; } IFileManager::Get().FindFilesRecursive(AllFiles, *FullPath, TEXT("*"), false, true); // Allocate memory for watch descriptors SIZE_T AllocSize = AllFiles.Num()+1 * sizeof(int); WatchDescriptor = reinterpret_cast<int*>(FMemory::Malloc(AllocSize)); if (WatchDescriptor == nullptr) { UE_LOG(LogDirectoryWatcher, Error, TEXT("Failed to allocate memory for WatchDescriptor")); return false; } FMemory::Memzero(WatchDescriptor, AllocSize); for (int32 FileIdx = 0; FileIdx < AllFiles.Num(); ++FileIdx) { const FString& FolderName = AllFiles[FileIdx]; WatchDescriptor[FileIdx] = inotify_add_watch(FileDescriptor, TCHAR_TO_UTF8(*FolderName), NotifyFilter); //FileIdx+1 if (WatchDescriptor[FileIdx] == -1) { UE_LOG(LogDirectoryWatcher, Error, TEXT("inotify_add_watch cannot watch folder %s"), *FolderName); return false; } } bRunning = true; return true; }
Inotify::Inotify() : mError(0) , mEventTimeout(0) , mLastEventTime(std::chrono::steady_clock::now()) , mEventMask(IN_ALL_EVENTS) , mThreadSleep(250) , mIgnoredDirectories(std::vector<std::string>()) , mInotifyFd(0) , mOnEventTimeout([](FileSystemEvent) {}) , mEventBuffer(MAX_EVENTS * (EVENT_SIZE + 16), 0) , mPipeReadIdx(0) , mPipeWriteIdx(1) { mStopped = false; if (pipe2(mStopPipeFd, O_NONBLOCK) == -1) { mError = errno; std::stringstream errorStream; errorStream << "Can't initialize stop pipe ! " << strerror(mError) << "."; throw std::runtime_error(errorStream.str()); } mInotifyFd = inotify_init1(IN_NONBLOCK); if (mInotifyFd == -1) { mError = errno; std::stringstream errorStream; errorStream << "Can't initialize inotify ! " << strerror(mError) << "."; throw std::runtime_error(errorStream.str()); } mEpollFd = epoll_create1(0); if (mEpollFd == -1) { mError = errno; std::stringstream errorStream; errorStream << "Can't initialize epoll ! " << strerror(mError) << "."; throw std::runtime_error(errorStream.str()); } mInotifyEpollEvent.events = EPOLLIN | EPOLLET; mInotifyEpollEvent.data.fd = mInotifyFd; if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mInotifyFd, &mInotifyEpollEvent) == -1) { mError = errno; std::stringstream errorStream; errorStream << "Can't add inotify filedescriptor to epoll ! " << strerror(mError) << "."; throw std::runtime_error(errorStream.str()); } mStopPipeEpollEvent.events = EPOLLIN | EPOLLET; mStopPipeEpollEvent.data.fd = mStopPipeFd[mPipeReadIdx]; if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mStopPipeFd[mPipeReadIdx], &mStopPipeEpollEvent) == -1) { mError = errno; std::stringstream errorStream; errorStream << "Can't add pipe filedescriptor to epoll ! " << strerror(mError) << "."; throw std::runtime_error(errorStream.str()); } }
int linux_sync_init(fuser_t *u, sync_t *sync, void *unused) { int fd; fd = inotify_init1(O_NONBLOCK); if (fd == -1) return (-errno); sync->sync_fd = fd; return (linux_sync_scan(u, sync)); }
void add_notify(const char *dirname, uint32_t mask) { if (notify_fd < 0) { notify_fd = inotify_init1(IN_NONBLOCK); pfd[1].fd = notify_fd; pfd[1].events = POLLIN; } inotify_add_watch(notify_fd, dirname, mask); tmplog("Watching '%s'\n", dirname); }
int32_t directory_monitor_startup(Allocator& allocator) { assert(_monitor_state == nullptr); _monitor_state = MEMORY2_NEW(allocator, DirectoryMonitorState)(allocator); _monitor_state->inotify_instance = inotify_init1(IN_NONBLOCK); if (_monitor_state->inotify_instance == -1) { LOGE("inotify_init failed, errno = %i\n", errno); return errno; } return 0; } // directory_monitor_startup