From 8257e4f7d9c5123ecc227a828a460e756e897567 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sat, 15 Mar 2014 17:20:46 +0100 Subject: set up inotify watch before looking at the dir, prevent race --- udev/ykfde.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'udev') diff --git a/udev/ykfde.c b/udev/ykfde.c index f8ccfd3..1c45833 100644 --- a/udev/ykfde.c +++ b/udev/ykfde.c @@ -189,28 +189,28 @@ int main(int argc, char **argv) { goto out50; } - /* is the request already there? */ + /* creating the INOTIFY instance and add ASK_PATH directory into watch list */ + if ((fd_inotify = inotify_init()) < 0) { + perror("inotify_init() failed"); + goto out50; + } + + watch = inotify_add_watch(fd_inotify, ASK_PATH, IN_MOVED_TO); + + /* Is the request already there? + * We do this AFTER setting up the inotify watch. This way we do not have race condition. */ if ((dir = opendir(ASK_PATH)) != NULL) { while ((ent = readdir(dir)) != NULL) { if (strncmp(ent->d_name, "ask.", 4) == 0) { if ((ret = try_answer(ent->d_name, response_askpass)) == EXIT_SUCCESS) - goto out60; + goto out70; } } } else { perror ("opendir() failed"); - goto out50; - } - - /* creating the INOTIFY instance */ - if ((fd_inotify = inotify_init()) < 0) { - perror("inotify_init() failed"); goto out60; } - /* adding ASK_PATH directory into watch list */ - watch = inotify_add_watch(fd_inotify, ASK_PATH, IN_MOVED_TO); - /* read to determine the event change happens. Actually this read blocks until the change event occurs */ if ((length = read(fd_inotify, buffer, EVENT_BUF_LEN)) < 0) { perror("read() failed"); @@ -228,14 +228,14 @@ int main(int argc, char **argv) { } out70: + /* close dir */ + closedir(dir); + +out60: /* remove inotify watch and remove file handle */ inotify_rm_watch(fd_inotify, watch); close(fd_inotify); -out60: - /* close dir */ - closedir(dir); - out50: /* wipe response (cleartext password!) from memory */ memset(response, 0, sizeof(response)); -- cgit v1.2.3-54-g00ecf