[hackers] [sbase] touch: prevent file creation race between utimensat() and open() || Michael Forney
commit 23160db9e0b545cead16ac48f23dc31eff2f1b1b
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Tue Mar 17 01:09:29 2026 -0700
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
CommitDate: Tue Mar 17 10:16:45 2026 +0100
touch: prevent file creation race between utimensat() and open()
If another process creates the file after we determined it was
missing, touch would fail on open() due to O_EXCL. This failure
doesn't serve any purpose; we want to update the mtime either way.
diff --git a/tests/0052-touch.sh b/tests/0052-touch.sh
new file mode 100755
index 0000000..47af79c
--- /dev/null
+++ b/tests/0052-touch.sh
_AT_@ -0,0 +1,13 @@
+#!/bin/sh
+
+tmp1=tmp1.$$
+tmp2=tmp2.$$
+
+trap 'rm -f $tmp1 $tmp2' EXIT
+trap 'exit $?' HUP INT TERM
+
+../touch -t 200711121015 $tmp1
+
+echo 'Modify: 2007-11-12 10:15:00.000000000' > $tmp2
+
+stat $tmp1 | awk '/^Modify:/ {$4 = ""; print}' | diff -wu - $tmp2
diff --git a/touch.c b/touch.c
index 6e63bf8..637a7d5 100644
--- a/touch.c
+++ b/touch.c
_AT_@ -26,7 +26,7 @@ touch(const char *file)
eprintf("utimensat %s:", file);
if (cflag)
return;
- if ((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0)
+ if ((fd = open(file, O_WRONLY | O_CREAT, 0666)) < 0)
eprintf("open %s:", file);
ret = futimens(fd, times);
close(fd);
Received on Tue Mar 17 2026 - 10:17:10 CET
This archive was generated by hypermail 2.3.0
: Tue Mar 17 2026 - 10:24:40 CET