Author: viethen Date: Fri Jan 14 17:07:44 2011 New Revision: 2410 Log: Make handling of return value of open() conform to POSIX (great thing is, this actually fixes a few bugs as well). Make "point stdin, stdout, stderr to /dev/null" magic in util.c a little more generic & (hopefully) more obvious. Modified: trunk/libpisa/sysdep_tun.c trunk/libpisa/util.c Modified: trunk/libpisa/sysdep_tun.c ============================================================================== --- trunk/libpisa/sysdep_tun.c Mon Jan 10 15:35:48 2011 (r2409) +++ trunk/libpisa/sysdep_tun.c Fri Jan 14 17:07:44 2011 (r2410) @@ -40,7 +40,7 @@ #ifdef IFF_TUN struct ifreq ifr; - if ((fd_tun = open("/dev/net/tun", O_RDWR)) < 0) { + if ((fd_tun = open("/dev/net/tun", O_RDWR)) == -1) { pisa_die("Couldn't open tunnel device. Are you root?", EXIT_FAILURE); } @@ -59,7 +59,7 @@ for (i = 0; i < 255; i++) { snprintf(name, sizeof(name), "/dev/tun%d", i); - if ((fd_tun = open(name, O_RDWR)) > 0) { + if ((fd_tun = open(name, O_RDWR)) >= 0) { snprintf(devicename, len, "tun%d", i); return fd_tun; } Modified: trunk/libpisa/util.c ============================================================================== --- trunk/libpisa/util.c Mon Jan 10 15:35:48 2011 (r2409) +++ trunk/libpisa/util.c Fri Jan 14 17:07:44 2011 (r2410) @@ -258,8 +258,8 @@ char pid_str[8]; int fd, pid, len; - if (!(fd = open(filename, O_RDWR | O_CREAT, 0644))) { - PISA_ERROR("Can't open lock file."); + if ((fd = open(filename, O_RDWR | O_CREAT, 0644)) == -1) { + PISA_ERROR("Can't open lock file.\n"); exit(EXIT_FAILURE); } @@ -297,29 +297,29 @@ /* become a process group leader */ setsid(); - /* point stdin, stdout and stderr to /dev/null so we can't - * accidentially mess up a console */ + /* stdin, stdout and stderr should be the only file descriptors at all + * used by our process at this point, so the following operations are + * expected to result in all of them pointing to /dev/null + * (this should keep us from accidentally messing up a console) + */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); - /* no file descriptors should be used at this point, so we expect - * the first open call to return 0 - */ - if ((result = open("/dev/null", O_RDWR))) { + if ((result = open("/dev/null", O_RDWR)) != STDIN_FILENO) { PISA_ERROR("pisa_daemonize: open STDIN failed.\n"); } - if (dup(result) != 1) { + if (dup(result) != STDOUT_FILENO) { PISA_ERROR("pisa_daemonize: dup STDOUT failed.\n"); } - if (dup(result) != 2) { + if (dup(result) != STDERR_FILENO) { PISA_ERROR("pisa_daemonize: dup STDERR failed.\n"); } /* change the current working directory to root in order * not to prevent any filesystems from being unmounted */ - if (chdir("/") < 0) { + if (chdir("/") == -1) { PISA_ERROR("Failed to change current working directory to root.\n"); exit(EXIT_FAILURE); } -- This is the pisa developer mailing list. Please also subscribe to the main pisa list at: //www.freelists.org/list/pisa