Pete's Log: i hate select
Entry #575, (Coding, Hacking, & CS stuff)(posted when I was 22 years old.)
select(2) loves to make my life miserable. The first mistake I used to make with select: the first argument to select (int nfds) is not the number of file descriptors you're interested in, but the maximum file descriptor you're interested in. Alright, I know this now, even though I spent forever figuring that one out back whenever I first learned select. Second mistake I still occasionaly make: nfds is actually one plus the maximum file descriptor you're interested in, so after figuring out the max I just need to remember to increment it before using it. Both these two things have caused me headaches and wasted time, but once you figure them out they're easy enough to deal with. But now select has made a true enemy of me.
So for my hot potato program (OS-p4) I've decided to add signal handlers so that if one of the players is interrupted or killed or whatever, it can inform the rest so that they all shut down gracefully. Good enough, right? Should be easy, I hope. Well, select says no. Damnit! How the hell am I getting a segfault while doing a signal handler for SIGINT? Turns out that select will, if you leave the default signal handlers in place, allow a program to terminate upon SIGINT. But if you install your own signal handler, then if you catch SIGINT while inside select, your signal handler don't get called. Instead select returns -1 and sets errno to EINTR (Interrupted system call). Grrr. Just call my freaking signal handler, you bastard system call! Oh well, we can work around this, I suppose. Onwards goes the struggle. Someday I think I'll give in, tho, and learn poll. Assuming, of course, that it lets my signal handler get called.