}
}
- printf ("Client %s has joined.\r\n", username);
+ fprintf (stderr, "Client %s has joined.\r\n", username);
server->num_clients++;
assert (i < server->num_clients);
- printf ("Client %s has left.\r\n", ttt_client_get_username (client));
+ fprintf (stderr, "Client %s has left.\r\n", ttt_client_get_username (client));
memmove (&server->clients[i], &server->clients[i+1],
(server->num_clients - i - 1) * sizeof (ttt_client_t *));
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);
+ }
+ }
+
socket = ttt_socket_create_server (args.host, args.port);
if (args.detach) {
- xdup2 (2, 1);
printf ("Server started listening on %s:%s\n", args.host, args.port);
+ fprintf (stderr, "Server started listening on %s:%s\n", args.host, args.port);
} else {
printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
}