summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2014-03-15 17:20:46 +0100
committerGravatar Christian Hesse <mail@eworm.de>2014-03-15 17:20:46 +0100
commit8257e4f7d9c5123ecc227a828a460e756e897567 (patch)
treed2e9993be341faa464fae33eb8206f54b5dbfb68
parentec1704a2d218c263c60900ef0c04db66ccabdcd6 (diff)
downloadmkinitcpio-ykfde-8257e4f7d9c5123ecc227a828a460e756e897567.tar.gz
mkinitcpio-ykfde-8257e4f7d9c5123ecc227a828a460e756e897567.tar.zst
set up inotify watch before looking at the dir, prevent race
-rw-r--r--udev/ykfde.c30
1 files changed, 15 insertions, 15 deletions
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));