Commit b11ed3b1 authored by Andrew Tridgell's avatar Andrew Tridgell

- close stdout and stderr and reopen then as /dev/null when running as

a daemon. This prevents library functions (such as getopt) stuffing up
our protocol stream when errors are detected.

- defer the error message from the options parsing until after the
socket is multiplexed. This allows clients sending new options which
the remote server doesn't understand to get a sensible error message.
parent 42245f1b
...@@ -566,7 +566,9 @@ int main(int argc,char *argv[]) ...@@ -566,7 +566,9 @@ int main(int argc,char *argv[])
carried across */ carried across */
orig_umask = (int)umask(0); orig_umask = (int)umask(0);
parse_arguments(argc, argv); if (!parse_arguments(argc, argv)) {
exit_cleanup(1);
}
argc -= optind; argc -= optind;
argv += optind; argv += optind;
......
...@@ -197,7 +197,8 @@ static struct option long_options[] = { ...@@ -197,7 +197,8 @@ static struct option long_options[] = {
{"port", 1, 0, OPT_PORT}, {"port", 1, 0, OPT_PORT},
{0,0,0,0}}; {0,0,0,0}};
void parse_arguments(int argc, char *argv[])
int parse_arguments(int argc, char *argv[])
{ {
int opt; int opt;
int option_index; int option_index;
...@@ -301,7 +302,7 @@ void parse_arguments(int argc, char *argv[]) ...@@ -301,7 +302,7 @@ void parse_arguments(int argc, char *argv[])
preserve_hard_links=1; preserve_hard_links=1;
#else #else
rprintf(FERROR,"ERROR: hard links not supported on this platform\n"); rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
exit_cleanup(1); return 0;
#endif #endif
break; break;
...@@ -412,10 +413,10 @@ void parse_arguments(int argc, char *argv[]) ...@@ -412,10 +413,10 @@ void parse_arguments(int argc, char *argv[])
break; break;
default: default:
/* rprintf(FERROR,"bad option -%c\n",opt); */ return 0;
exit_cleanup(1);
} }
} }
return 1;
} }
......
...@@ -281,6 +281,8 @@ become a daemon, discarding the controlling terminal ...@@ -281,6 +281,8 @@ become a daemon, discarding the controlling terminal
****************************************************************************/ ****************************************************************************/
void become_daemon(void) void become_daemon(void)
{ {
int i;
if (fork()) if (fork())
_exit(0); _exit(0);
...@@ -299,9 +301,12 @@ void become_daemon(void) ...@@ -299,9 +301,12 @@ void become_daemon(void)
} }
#endif /* TIOCNOTTY */ #endif /* TIOCNOTTY */
#endif #endif
close(0); /* make sure that stdin, stdout an stderr don't stuff things
close(1); up (library functions, for example) */
close(2); for (i=0;i<3;i++) {
close(i);
open("/dev/null", O_RDWR);
}
} }
/******************************************************************* /*******************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment