/*
* daemon.c by Kern Sibbald
*
+ * Version $Id$
+ *
* this code is inspired by the Prentice Hall book
* "Unix Network Programming" by W. Richard Stevens
* and later updated from his book "Advanced Programming
{
#ifndef HAVE_CYGWIN
int i;
- int cpid;
+ pid_t cpid;
+ mode_t oldmask;
/*
* Become a daemon.
*/
setsid();
/* In the PRODUCTION system, we close ALL
- * file descriptors. It is useful
- * for debugging to leave the standard ones open.
+ * file descriptors except stdin, stdout, and stderr.
*/
- for (i=sysconf(_SC_OPEN_MAX)-1; i >=0; i--) {
-#ifdef DEBUG
- if (i != STDIN_FILENO && i != STDOUT_FILENO && i != STDERR_FILENO) {
- close(i);
- }
-#else
+ for (i=sysconf(_SC_OPEN_MAX)-1; i > 2; i--) {
close(i);
-#endif
}
/* Move to root directory. For debug we stay
chdir("/");
#endif
- /* clear any inherited umask */
- umask(0);
+ /*
+ * Avoid creating files 666 but don't override any
+ * more restrictive mask set by the user.
+ */
+ oldmask = umask(026);
+ oldmask |= 026;
+ umask(oldmask);
Dmsg0(200, "Exit daemon_start\n");
#endif /* HAVE_CYGWIN */