Live data from Hacker News

fork() can fail

rachelbythebay.com

11–20 of 320 posts

Re: fork() can fail

#11

This is an important point, along similar lines to something we discussed at work today in a code review. The programmer in question assumed that all and every request for the resources consumed would always work. Literally, no testing or consideration for what if one of those request fails. Will be emailing them this article tomorrow!

Your username is offensive. I'd like to take what you say seriously, and perhaps even engage in further conversation about the topic at hand, but ... you know ... you look stupid. Grow up. You should be embarrassed.

Shouldn't take long

  user:	emo_tards_on_hn
  created:	48 minutes ago
  karma:	-20

Re: fork() can fail

#12
post #8

Easy to test out to. In C: #include int main(void) { while(1) { fork(); } }

Haha too cruel. I remember in one of my early college CS classes the professor told us about fork bombs and wasn't sure if they still worked on the Sun Fire servers we were using. About 5 minutes later someone piped up and confirmed that yep they still worked at bringing the server down. :)

Re: fork() can fail

#14
post #7

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…

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

#15

This is an important point, along similar lines to something we discussed at work today in a code review. The programmer in question assumed that all and every request for the resources consumed would always work. Literally, no testing or consideration for what if one of those request fails. Will be emailing them this article tomorrow!

Your username is offensive. I'd like to take what you say seriously, and perhaps even engage in further conversation about the topic at hand, but ... you know ... you look stupid. Grow up. You should be embarrassed.

So. You mean you actually DON'T like to take what he said seriously.

His username is offensive, but you just also gave it credibility.

Re: fork() can fail

#16
post #6

Quietly goes to check the last piece of C I wrote containing a fork(): if (daemon && !test_mode) { int pid = fork(); if (pid == -1) { fatal_error("Failed to fork"); } if (pid != 0) { write_pid(pid_file, pid, !test_mode); exit(0); } } else { write_pid(pid_file, getpid(), !test_mode); } Phew!

I usually use switch with fork:

    if (daemon && !test_mode) {
        int pid;
        switch (pid = fork()) {
        case -1: /* Error */
            fatal_error("Failed to fork");
        case 0: /* In child */
            break;
        default: /* In parent */
            write_pid(pid_file, pid, !test_mode);
            exit(0);
        }
    } else {
        write_pid(pid_file, getpid(), !test_mode);
    }

Re: fork() can fail

#17
post #6

Quietly goes to check the last piece of C I wrote containing a fork(): if (daemon && !test_mode) { int pid = fork(); if (pid == -1) { fatal_error("Failed to fork"); } if (pid != 0) { write_pid(pid_file, pid, !test_mode); exit(0); } } else { write_pid(pid_file, getpid(), !test_mode); } Phew!

I usually use switch with fork: if (daemon && !test_mode) { int pid; switch (pid = fork()) { case -1: /* Error */ fatal_error("Failed to fork"); case 0: /* In child */ break; default: /* In parent */ write_pid(pid_file, pid, !test_mode); exit(0); } } else { write_pid(pid_file, getpid(), !test_mode); }

Yes. That's rather nice. Although I'm not a fan of the (pid = fork()) inline assignment and condition. But that's a matter of taste, not technology.

Re: fork() can fail

#19
post #14
post #7

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…

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.

In this particular case, I didn't see the chdir until after the fact. This wasn't my code...

Re: fork() can fail

#20
If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest "it cannot happen to me", the gods shall surely punish thee for thy arrogance. [0]

[0]: http://www.lysator.liu.se/c/ten-commandments.html

Post reply on HN