News for hackers. Stuff that matters.
Re:fork() can fail by cperciva on Wednesday August 20, 2014 @03:53PM (#8204197)
This reminds me of one of the most epic bugs I've ever run into:

    mkdir("/foo", 0700);
    chdir("/foo");
    recursively_delete_everything_in_current_directory();
Running as root, this usually worked fine: It would create a directory, move into it, and clean out any garbage left behind by a previous run before doing anything new.

Running as non-root, the mkdir failed, the chdir failed, and it started eating my home directory.

Re:fork() can fail by swah on Wednesday August 20, 2014 @04:12PM (#8204298)
In those times I wish I could use the emacs lisp way:

    (let (dir "/foo")
      (create-directory dir)
      (with-current-directory dir
        (delete-all-files-recursively)))
Factor recognized the value of dynamically scoped variables: http://concatenative.org/wiki/view/Factor/FAQ/What's%20Facto...

A lot of code became much simpler because of that decision.

Re:fork() can fail by staunch on Wednesday August 20, 2014 @04:53PM (#8204555)

  $ mkdir /tmp/foo && cd /tmp/foo && touch bar.txt
Re:fork() can fail by davvid on Wednesday August 20, 2014 @10:51PM (#8205738)
Yes. Always chain sequential commands with &&. Always.

This style is pretty prevalent when writing test cases in shell (or just shell scripts in general), e.g. when using something like sharness[1].

[1] https://github.com/mlafeldt/sharness

Re:fork() can fail by pimlottc on Thursday August 21, 2014 @01:07AM (#8205992)
"bash -e" has much the same effect: it basically exits on the first failed command, with considerations for pipelines, conditionals and the like.

http://www.gnu.org/software/bash/manual/bashref.html#index-s...

Re:fork() can fail by pflanze on Thursday August 21, 2014 @01:32AM (#8206036)
Note this recent discussion about bash -e, including my post that "the disappointment with set -e is that it does not work everywhere".

https://news.ycombinator.com/item?id=8054440

Re:fork() can fail by blueskin_ on Thursday August 21, 2014 @04:32AM (#8206433)
It's nice to have, but I wouldn't rely on it to save me - it's no replacement for checking return codes properly.
Re:fork() can fail by pimlottc on Thursday August 21, 2014 @07:52PM (#8210308)
Certainly, but it's a good habit to get into, like ``use strict`` in Perl.
Re:fork() can fail by blueskin_ on Friday August 22, 2014 @06:20AM (#8211519)
"Perl and line noise are distinguishable. Properly written perl starts with 'use strict'; line noise rarely does."
Re:fork() can fail by stingraycharles on Thursday August 21, 2014 @12:38AM (#8205953)
When writing test cases in shell, you generally just implement a die() function and execute all commands `foo || die "bar"`
Re:fork() can fail by nitrogen on Thursday August 21, 2014 @12:41AM (#8205957)
If you don't need to specify a custom message, you could also use "set -e" to die automatically on command failure and use "trap ... EXIT" to display some kind of failure message.
Re:fork() can fail by blueskin_ on Friday August 22, 2014 @06:19AM (#8211517)

    $ mkdir -m 0700
Re:fork() can fail by hollerith on Thursday August 21, 2014 @01:23AM (#8206017)
I fail to see how dynamic scope pertains to the code you wrote or helps avoid the bug described in grandparent.

Specifically, the code you wrote would behave the same if `dir` had lexical scope.

Re:fork() can fail by mst on Thursday August 21, 2014 @08:01AM (#8206891)
The with-current-directory part is the dynamic part - i.e. within its dynamic scope, the current directory is now dir.

The scoping of the dir variable itself is irrelevant.

Re:fork() can fail by hollerith on Thursday August 21, 2014 @08:31AM (#8206994)
`with-current-directory` has to be a macro because if it is not, then the evaluation of (delete-all-files-recursively) happens before the definition of with-current-directory can change the current directory.

What the person meant when he wrote, "In those times I wish I could use the emacs lisp way," is, "In those times I wish I could use a lisp _macro_" -- particularly, one of those macros that makes a change, runs some code ("the body") then undoes the change.

Since all lisps have macros, the code above would work in any lisp -- not just Emacs Lisp. Among lisps, Emacs Lisp is famous for its dynamically scoped variables. Consequently, the specific reference to Emacs Lisp perpetuates the confusion that how variables are scoped has anything to do with what we have been talking about.

Re:fork() can fail by swah on Thursday August 21, 2014 @11:17AM (#8207737)
Yes, but emacs lisp is the only lisp I used, and lives in an environment where those macros are really useful.

    with-auto-compression-mode  with-case-table
    with-category-table         with-coding-priority
    with-current-buffer         with-decoded-time-value
    with-demoted-errors         with-electric-help
    with-help-window            with-local-quit
    with-no-warnings            with-output-to-string
    with-output-to-temp-buffer  with-selected-frame
    with-selected-window        with-silent-modifications
    with-syntax-table           with-temp-buffer
    with-temp-buffer-window     with-temp-file
    with-temp-message           with-timeout
    with-timeout-suspend        with-timeout-unsuspend
    with-wrapper-hook
Re:fork() can fail by Alupis on Wednesday August 20, 2014 @04:43PM (#8204493)
This isn't really fork() failing per se -- but rather a failed program/script that did not understand the well defined and clearly documented behavior of fork().
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @04:46PM (#8204517)
The man page says, "On failure, -1 is returned in the parent, no process is created, and errno is set appropriately."

So if fork is behaving as documented, returning -1 is because of "fork failing".

Re:fork() can fail by Alupis on Wednesday August 20, 2014 @04:52PM (#8204545)
True -- I guess fork() has failed at that point. I was more getting at the article's authors scenarios of careless scripts treating -1 as a valid pid (which should always be > 0), which would be a failure of the script instead.
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @04:57PM (#8204566)
Well, it's a failure of the script and the POSIX API - it is unfortunate that a pid_t inhabited by -1 means "failure" in one case and "everything" in another. It is certainly not fork, narrowly, to blame.
Re:fork() can fail by M00n_Sl47r on Wednesday August 20, 2014 @06:05PM (#8204931)
I think the point that the author was making, outside of any criticism to the API itself, is that the hapless programmer may not know that fork() could even return -1. The article is pointing out that possibility as documented by the API, not criticizing a failure of the API. Whether or not the API is faulty in this--and I tend to agree with you--is outside of the scope of the article, though only just.
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @07:49PM (#8205290)
I wasn't making any particular comment on the content of the article. It was certainly relevant to the discussion tangent the thread had wandered down.
Re:fork() can fail by yosefk on Wednesday August 20, 2014 @06:10PM (#8204953)
Taking the square root of a negative number removing all the files in your home directory could be "well defined and clearly documented behavior". Would you blame the API author at that point or would it still be strictly your fault?

At what point do API authors share the blame for a needlessly harsh punishment delivered upon a predictably common error?

I certainly prefer to work with systems produced by people tending to think it'd be their fault more often than not.

Re:fork() can fail by Alupis on Wednesday August 20, 2014 @06:23PM (#8205009)
I could argue, however, that in this particular case, it's the user's fault for failing to understand the full and defined behavior of fork() in addition to failing to understand the full and defined behavior of other functions, ...say, kill().

It's just as wrong to feed kill() -1 as it would be to feed it -48585 or "babdkd" (unless that is explicitly your intention). A simple sanity check of if [ "${pid} > "0" ]; is all that's necessary to protect against this behavior.

So, I would argue, the fault lays on the users, not the creator, for not understanding the API when all materials necessary to understand said API are freely available.

(with that said, I think it's safe to say, we've all been bitten by not fully understanding some function before)

Re:fork() can fail by ajkjk on Wednesday August 20, 2014 @09:42PM (#8205586)
There'd be far fewer bugs if everyone knew exactly how everything else works.

This kind of mistake is godawful and should not be defended. (but it's correctly fixed through stronger typing, not through choosing -48585 as the code for killing everything).

Re:fork() can fail by Alupis on Thursday August 21, 2014 @10:45AM (#8207576)
> not through choosing -48585 as the code for killing everything)

Actually, only -1 is the code that "kills everything"

> There'd be far fewer bugs if everyone knew exactly how everything else works.

Perhaps I misinterpreted your meaning, because you seem to be advocating using programming and scripting languages without actually bothering to learn them. Of course this can, will and does lead to very bad effects.

The bottom line is, if you are going to use a function in your program/script -- please, read the docs and understand what is will return at the very least.

Re:fork() can fail by mercurial on Thursday August 21, 2014 @11:22AM (#8207764)
> Perhaps I misinterpreted your meaning, because you seem to be advocating using programming and scripting languages without actually bothering to learn them. Of course this can, will and does lead to very bad effects.

You're arguing that people should read the API before doing anything with it. Parent's point is that this class of error can be avoided by strong typing (eg, via algebraic datatypes), negating the chance that it would happen in the first place. Which, I think, is the right way to look at the problem. But certainly, if you do have to use a weakly-typed, unsafe language which does not provide this kind of guarantee, be sure to read the documentation twice.

Which doesn't mean you won't get bitten when it turns out that the person writing a library you rely didn't RTFM.

Re:fork() can fail by rwallace on Thursday August 21, 2014 @01:48AM (#8206066)
Fault is not a rivalrous good. It's the user's fault, and it's the API creator's fault.

Is there a reason fork can't be changed to just crash the program on failure? Are situations where a program usefully does something other than crash on fork failure, more or less common than situations where a program fails in the way described in the article?

Re:fork() can fail by paulfurtado on Thursday August 21, 2014 @04:19AM (#8206394)
I can't think of any good cases for crashing a program when fork fails. If you're doing work in a pool of processes and the parent process tries to fork another process, but fails, there are many ways to handle this: wait a few seconds before trying again, wait for another process to finish, kill a few processes, etc.

If you were forking in a high-level language such as Python, failing to fork would raise an exception which would possibly crash the program if left unhandled.

C does not have exceptions so return codes are used to indicate success or failure. This is true for nearly every function, not just fork. If you're not checking for errors in a C program, it's going to break in unexpected ways, and will possibly be vulnerable to exploitation.

fork has 3 possible return values: - 0 for the child process - a positive number for the parent process - a negative number if it failed.

If you look at the man page's "Return Value" section, it is extremely clear, see: http://linux.die.net/man/2/fork

"On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately."

Re:fork() can fail by dllthomas on Thursday August 21, 2014 @11:16AM (#8207724)
"Is there a reason fork can't be changed to just crash the program on failure?"

YES. Situation: fork bomb, can't create new processes. How do you notice? Typically because you can't spawn new processes from your shell because fork is failing. With bash, there is a kill builtin - so you have a chance of cleaning things up (depending) if you have a shell open. If the failed fork kills the shell, then oops, you don't have a shell open.

Re:fork() can fail by Sanddancer on Thursday August 21, 2014 @06:10AM (#8206629)
That should be up to the programmer to decide. If fork() fails, it's almost always a transient situation that can be recovered from by spinning until fork() succeeds. Or one could have jobs doing useful things that can finish up, state saved, etc, for when the process is re-run. Just dropping everything onto the floor is usually the worst option.
Re:fork() can fail by brazzy on Wednesday August 20, 2014 @04:56PM (#8204564)
Psst - it's "per se"...
Re:fork() can fail by bostonpete on Wednesday August 20, 2014 @05:14PM (#8204681)
¿Por qué?
Re:fork() can fail by Alupis on Wednesday August 20, 2014 @05:47PM (#8204851)
I originally had "per-say" instead of the correct "per se"
Re:fork() can fail by Alupis on Wednesday August 20, 2014 @05:05PM (#8204626)
;-P
Re:fork() can fail by tedunangst on Wednesday August 20, 2014 @06:27PM (#8205023)
What's wrong with the C version?

    char *dir = "/foo";
    mkdir(dir, 0700);
    if (chdir(dir) == 0)
        delete_all_files();
Re:fork() can fail by swah on Wednesday August 20, 2014 @06:36PM (#8205051)
Nothing, of course. I just find the macros that give you a "modified environment" to run some code short and sweet.

with-temp-buffer is another example: a macro that bridges the functions for "string manipulation" and the ones for "buffer manipulation", since you start writing stuff this way:

  (defun replace-in-string (str from to) 
    (with-temp-buffer
      (insert str)
      (beginning-of-file)
      ;;; Here you can use all your normal text editing commands
      (replace-regexp from to nil t)         
      (buffer-string)))
Lots of dirty manipulation, but from outside its a pure function, and doesn't change the editor state in any way after it runs.
Re:fork() can fail by qewrffewqwfqew on Wednesday August 20, 2014 @10:30PM (#8205694)
That's incomplete in that it doesn't automatically chdir back. A proper block-scope "with-" macro will wrap the body in something like:

    char *olddir = getcwd();
    chdir(newdir);
    try {
        do_stuff();
    } finally {
        chdir(olddir);
    }
Re:fork() can fail by jacquesm on Thursday August 21, 2014 @02:51AM (#8206168)
'try' and 'finally' are in C now? Someone should warn the GCC guys they're behind the times.

Also, getcwd has a size parameter these days, and of course you want to check if the getcwd actually worked.

Re:fork() can fail by xxs on Thursday August 21, 2014 @03:52AM (#8206301)
The OP said: wrap in a macro having similar properties (ala pseudo code).
Re:fork() can fail by jacquesm on Thursday August 21, 2014 @06:47AM (#8206711)
Good luck writing a macro in C to express language functionality for which you don't have the primitives. It's not exactly lisp. Think of C macros as a way to save you some typing and lisp macros as a way to extend the language.
Re:fork() can fail by tedunangst on Thursday August 21, 2014 @10:08AM (#8207381)
setjmp and longjmp are sufficient building blocks for this.
Re:fork() can fail by xxs on Thursday August 21, 2014 @08:19AM (#8206955)
you can definitely save the chdir and restore it macro.
Re:fork() can fail by xxs on Thursday August 21, 2014 @03:51AM (#8206300)
Any multithreaded code will be destroyed if relying on chdir. chdir should be deprecated, as resolving relative to is just trivial in an application.
Re:fork() can fail by stinos on Wednesday August 20, 2014 @03:57PM (#8204225)
When you see chdir, or any notion of the current working directory being used for anything: run as fast as you can. (or refactor if it's not too late). Things I've seen because of software relying on it.. Sometimes it's just directories/files it creates popping up all over the place, sometimes it's 'just' crashing, but yes sometimes it starts to erase and all hell really breaks loose.
Re:fork() can fail by pachydermic on Wednesday August 20, 2014 @04:46PM (#8204507)
If you can't rely on current working directories then you have to specify any file locations absolutely? That doesn't seem like a good idea because then your code quickly turns into a hot mess if you ever have to change where stuff lives.

This is such a stupid problem I run into a lot. Both alternatives (doing things with absolute paths vs doing things entirely with relative paths) seem to have a lot of downsides. Overall relative paths seems to be way better, but then you leave yourself open to problems which "rhyme" with the one OP was talking about.

Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @04:49PM (#8204533)
As agwa and I mentioned in sibling comments, there are the ...at() functions, which let you specify actions on paths relative to a specific directory you have a file descriptor for. This not only avoids issues like the above (failing to open the directory and then failing to check for failure will mean you're passing -1 into unlinkat, which would simply fail) but will also keep you talking about the same place if links are moved around somewhere up the tree from where you are working.
Re:fork() can fail by tedunangst on Wednesday August 20, 2014 @05:42PM (#8204823)
Rearranging the tree above CWD isn't a problem, CWD will follow along just fine (as in, be the same directory). Also, you're assuming AT_FDCWD won't be -1, which could be reasonable but isn't guaranteed afaik.
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @07:22PM (#8205202)
"Rearranging the tree above CWD isn't a problem, CWD will follow along just fine (as in, be the same directory)."

I was citing rearranging the tree is a potential issue with absolute directories, not with relying on CWD - that certainly could have been clearer.

"Also, you're assuming AT_FDCWD won't be -1, which could be reasonable but isn't guaranteed afaik."

Interesting point regarding guarantees. It's not -1 on any existing OS that I can find (it seems to be -100 on Linux and FreeBSD, -3041965 on Solaris, -2 on AIX), and shouldn't be for precisely this reason, but something to bear in mind if you are working on something more obscure that nonetheless has these functions.

Of course, you shouldn't be relying on reasonable behavior from functions passed a bad FD in general. It's just nice to have the additional defense when that does get missed.

Re:fork() can fail by stinos on Thursday August 21, 2014 @03:02AM (#8206183)
That doesn't seem like a good idea because then your code quickly turns into a hot mess if you ever have to change where stuff lives.

It doesn't turn into a mess if you handle it correctly from the start. The way we ususally handle this in large applications is to have one single class like 'ApplicationPaths' which internally figures out all paths needed. No other code uses paths directly, instead always uses paths relative to ApplicationPaths.AppConfigDir/ApplicationPaths.UserConfigDir/ApplicationPaths.ExecutableDir and so on.

Re:fork() can fail by nuxi on Wednesday August 20, 2014 @05:44PM (#8204836)
There's a good reason you can't rely on CWD other than root - the directory can be on a different mountpoint and possibly even on a remote mountpoint. If the remote server goes down or the mountpoint is forcefully unmounted, what would your CWD point to then? Root is the only directory guaranteed to always exist, that's why you'd see a chdir('/') as one of the first steps in properly written unix daemons.
Re:fork() can fail by jjnoakes on Wednesday August 20, 2014 @05:50PM (#8204862)
Almost - the main reason for the chdir('/') is so that if you do happen to be in a mounted file system (locally or remotely), your daemon doesn't prevent the system administrator from gracefully unmounting that file system for whatever reason.
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @04:04PM (#8204262)
Right, better to use the *at() functions (e.g. http://linux.die.net/man/2/unlinkat) where applicable.
Re:fork() can fail by agwa on Wednesday August 20, 2014 @04:09PM (#8204282)
Sometimes there are legitimate reasons for relying on the CWD, such as avoiding time-of-check/time-of-use race conditions. The *at syscalls provide a better alternative, but (at least as of a few years ago) they weren't widely implemented outside of Linux and Solaris.
Re:fork() can fail by cperciva on Wednesday August 20, 2014 @04:00PM (#8204239)
In this particular case, I didn't see the chdir until after the fact. This wasn't my code...
Re:fork() can fail by jrockway on Thursday August 21, 2014 @12:56PM (#8208315)
I chdir("/") before daemonizing so that the filesystem the process was using as its cwd can be unmounted :)
Re:fork() can fail by peterwwillis on Thursday August 21, 2014 @12:59PM (#8208332)
The exception is chdir("/"). This will always give you the desired behavior, only error on EACCES, and prevent many nasty filesystem problems. This also removes the idea of relative paths, so the user is forced to use full paths (or, every relative path is a full path). Not very practical but it does work.
Re:fork() can fail by bodyfour on Wednesday August 20, 2014 @04:48PM (#8204529)
The real problem with chdir is when that code ends up refactored into a library and ends up used in a multithreaded program. Then you've got an ugly bug.
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @04:51PM (#8204541)
That's yet another real problem with chdir, on top of the 10 other real problems with chdir, of which we've mentioned a couple here.
Re:fork() can fail by ygra on Wednesday August 20, 2014 @05:05PM (#8204624)
Which probably can be summarized as »global state is a really bad idea«.
Re:fork() can fail by dllthomas on Wednesday August 20, 2014 @05:10PM (#8204652)
I wonder if it would be worthwhile to forego the notion of a CWD for processes entirely.
Re:fork() can fail by mnw21cam on Thursday August 21, 2014 @07:39AM (#8206826)
CWD is a useful concept. If I run gimp in a particular directory, I'd like it to show that directory in the file dialogue when I try to load or save an image.

What is evil is a program changing its working directory. That's when it becomes an evil global variable, rather than a non-evil global constant.

Re:fork() can fail by hderms on Thursday August 21, 2014 @10:17AM (#8207438)
I think that's probably the best way to look at it. You are given the parent node with CWD and you can attempt to modify child nodes by relatively addressing them.

I was wondering before if it would be interesting to have a filesystem with transactional locking of paths, though I'm sure the performance would take a hit. Would be kind of cool to be able to do filesystem operations without constantly opening yourself up to race conditions and requiring extremely defensive programming.

Re:fork() can fail by dllthomas on Thursday August 21, 2014 @11:12AM (#8207706)
Why limit that to one directory? As I expanded on a bit in my response to ygra, I don't mean eliminating any notion of carrying a directory, just that I don't know that there is actually good reason to privilege one particular path universally.

I agree that treating cwd as a global constant solves most (at least) of the issues, I'm just poking assumptions to see what ideas arise.

Re:fork() can fail by ygra on Wednesday August 20, 2014 @11:57PM (#8205875)
You'd have to use absolute paths everywhere, but that probably doesn't hurt that much in programs or scripts. The CWD seems most useful in interactive shells, I guess. A fun thing is PowerShell on Windows where you have two CWDs, one from the process and another one from the shell which had is own VFS handling (e.g. the registry is a place that has no representation in the normal file system). Cmdlets use one of them and external commands and .NET APIs use the other. So in the latter case you always need Resolve-Path foo.bar instead of just foo.bar for things to work properly.
Re:fork() can fail by dllthomas on Thursday August 21, 2014 @12:33AM (#8205950)
You wouldn't have to use absolute paths everywhere - you could use paths relative to any file descriptor that pointed at a directory. The shell itself wouldn't seem to have any problem - it could maintain a logical CWD without assistance. Utilities would probably need some other convention - maybe fd 4 points at the directory they are to operate in at start?

In a sense, this is "still a CWD" - but the differences would be 1) you can maintain multiple at the same time, and 2) you could close it.

Re:fork() can fail by dlitz on Friday August 22, 2014 @12:00PM (#8212644)
> You'd have to use absolute paths everywhere

Ever heard of PATH_MAX and ENAMETOOLONG? You will if you're using absolute paths everywhere. Sigh

Re:fork() can fail by blueskin_ on Thursday August 21, 2014 @04:28AM (#8206422)
Normal users can't make a directory at the root of the filesystem by design.

To write that safely, you'd test for the result of mkdir and cd to return success before deleting.

In a shell, you'd just go

    mkdir /foo -m 0700 && cd /foo && ${deleteallfiles}
Re:fork() can fail by Alupis on Wednesday August 20, 2014 @04:46PM (#8204510)
Could you have not just checked to see if you actually created the directory and/or check to make sure you moved into the directory before proceeding with your destructive function?

(Defensive Programming 101 really).

Re:fork() can fail by ColinDabritz on Wednesday August 20, 2014 @04:50PM (#8204536)
They could have, but then they wouldn't have a great 'epic bug' story to share! Missing the basics is part of what leads to mistakes like this, and I'd wager that after getting bit by that they are much more careful today.

Also, don't rely on implicit state (the "current directory") for a destructive command, pass the dependency in:

recursively_delete_directory("/foo")

Re:fork() can fail by Alupis on Wednesday August 20, 2014 @04:54PM (#8204556)
Very true.

Reminds me of a time when I was working on a package system for an in-house linux os build and carelessly had my fakeroot directory set wrong in my configurations, which ended up treating my local root / directory as the root of the fakeroot, which is as bad as it sounds. Running the script over-wrote my entire /etc directory among other important and un-recoverable things...

Gladly, it was a development system so nothing crucial was lost. Needless to say, I am way more careful today in-part due to this mishap (and hours of setting up a new dev system!)

Re:fork() can fail by cperciva on Wednesday August 20, 2014 @05:24PM (#8204738)
There's lots of things they could have done. As I said elsewhere, this wasn't my code,
Re:fork() can fail by jwr on Thursday August 21, 2014 @04:56AM (#8206486)
You do realize that this, in turn, creates a race condition, right?
Re:fork() can fail by Alupis on Thursday August 21, 2014 @10:39AM (#8207551)
? not at all -- you make the deletion of the directory dependent on whether or not you tested true for the directory being present. I don't see why this would cause any race condition...
Re:fork() can fail by roryokane on Thursday August 21, 2014 @11:56AM (#8207964)
The race condition is when another program is interacting with that directory too. So you check that the directory exists, and then the other program gets CPU time and uses it to delete that directory, and then your program tries to delete that directory but fails.

To avoid this, you have to either get a lock on that directory somehow (opening a transaction), or you have to try deleting the directory and then check the return code or catch any exceptions to tell whether the directory existed at the time of the attempted deletion.

Re:fork() can fail by jwr on Thursday August 21, 2014 @05:00PM (#8209572)
It does, because somebody might have done something to the filesystem between your test and the rest of your code.

This is why I found your "Defensive Programming 101 really" comment slightly arrogant (perhaps I misunderstood the intention). Writing correct programs is not easy and one should not mock others, because there is always something new to learn.

Re:fork() can fail by MaulingMonkey on Wednesday August 20, 2014 @11:34PM (#8205814)
The variant of this I ran into was significantly less destructive.

recursively_find_everything_in_current_directory() crashed due to a stack overflow when SetCurrentDirectory failed due to a single corrupt NTFS directory.