1. Updated wolfSSHd with the common practices for starting a daemon.
2. Close std I/O and reopen as /dev/null.
pull/505/head
John Safranek 2023-03-28 13:59:16 -07:00
parent 07f901d207
commit 9c9cb5adbd
No known key found for this signature in database
GPG Key ID: 8CE817DE0D3CCB4A
1 changed files with 37 additions and 8 deletions

View File

@ -1217,31 +1217,60 @@ int main(int argc, char** argv)
/* run as a daemon */
if (ret == WS_SUCCESS && isDaemon) {
pid_t p;
#ifdef __unix__
/* Daemonizing in POSIX, so set a syslog based log */
wolfSSH_SetLoggingCb(SyslogCb);
#endif
pid_t p;
p = fork();
if (p < 0) {
fprintf(stderr, "Failed to fork process\n");
ret = WS_FATAL_ERROR;
exit(EXIT_FAILURE);
}
if (p > 0) {
exit(EXIT_SUCCESS); /* stop parent process */
}
if (ret == WS_SUCCESS) {
if (setsid() < 0) {
fprintf(stderr, "Failed to set a new session");
ret = WS_FATAL_ERROR;
}
else {
signal(SIGCHLD, ConnClose);
p = fork();
if (p < 0) {
fprintf(stderr, "Failed to fork process\n");
exit(EXIT_FAILURE);
}
if (p > 0) {
exit(0); /* stop parent process */
exit(EXIT_SUCCESS);
}
if (setsid() < 0) {
fprintf(stderr, "Failed to set a new session");
umask(0);
if (chdir("/") < 0) {
ret = WS_FATAL_ERROR;
}
if (ret == WS_SUCCESS) {
int fd;
fd = open("/dev/null", O_RDWR);
if (fd < 0) {
ret = WS_FATAL_ERROR;
}
else {
if (dup2(fd, STDIN_FILENO) < 0 ||
dup2(fd, STDOUT_FILENO) < 0 ||
dup2(fd, STDERR_FILENO) < 0) {
ret = WS_FATAL_ERROR;
}
close(fd);
}
}
}
}
signal(SIGCHLD, ConnClose);
if (ret == WS_SUCCESS) {
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Starting to listen on port %d", port);
tcp_listen(&listenFd, &port, 1);