The touch(1) command will open the file in question and then call utimes("/proc/self/fd/0", NULL) to set the time. This means that if /proc is not mounted or if SE Linux denies access to /proc (which will be the case for some shell scripts on a strict or MLS system and may also occur with the targeted policy) then the touch will return 1 and the time stamp will not be changed. Also if the file in question does exist then touch(1) will create it (thus giving it the desired time stamp as part of the process) but still return an error! I can't work out why touch calls open(2) before utimes(2). I suggest that a better algorithm would be: fd = open(filename, O_WRONLY|O_NONBLOCK|O_CREATE|O_EXCL, 0666); if(fd == -1) utimes(filename, NULL); else close(fd); This will save one system call in the case where the file is created, make it work when /proc is not mounted or not available, and has no down-side that I can think of.
Thanks for the report. That has been fixed upstream. Now, it falls back on using utimes if futimes or futimesat (the libc calls that might use /proc/self/fd/0) fails. Also, with newer kernels (2.6.16), futimesat is a syscall, so doesn't rely on /proc. If we know beforehand that the file doesn't exist, then you're right that we could save syscalls. However, it's not possible to detect the condition in a useful/reliable manner -- there'd be a race: someone could create the file between when lstat says it doesn't exist and the open above. Then touch would fail unnecessarily once again.