+ if (args.log_file || args.detach) {
+ FILE *log_file;
+ /* In the detach case, we force redirection to a log file. */
+ if (args.log_file == NULL)
+ args.log_file = "/var/log/ttt-server.log";
+ log_file = fopen (args.log_file, "a");
+ if (log_file == NULL) {
+ printf ("Warning: Failed to open log file %s: %s.\n",
+ args.log_file, strerror (errno));
+ printf ("Logging will be disabled.\n");
+ xdup2 (1, 2);
+ } else {
+ xdup2 (fileno (log_file), 2);
+ }
+ }
+
+ if (args.detach)
+ _detach_and_write_child_pid_to (args.pid_file);
+
+ /* Now that we've setup logging and the pid file, drop any special
+ * permissions we might have if we were asked to do that. */
+ if (args.user) {
+ int ret;
+ struct passwd *pwd;
+ errno = 0;
+ pwd = getpwnam (args.user);
+ if (pwd == NULL) {
+ fprintf (stderr, "Error: Failed to lookup uid for %s: %s. Aborting.\n",
+ args.user,
+ errno == 0 ? "User not found" : strerror (errno));
+ exit (1);
+ }
+ ret = setuid (pwd->pw_uid);
+ if (ret == -1) {
+ fprintf (stderr, "Error: Failed to setuid to %d (%s): %s. Aborting.\n",
+ pwd->pw_uid, args.user, strerror (errno));
+ exit (1);
+ }
+ }