summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeevan Ramakant Nagvekar <jeevan.nagvekar1@wipro.com>2018-07-27 16:01:51 +0530
committerChristoph Lipka <clipka@users.noreply.github.com>2018-12-21 10:16:46 +0100
commitd4ef0bbe46f7cb409792ab62ea1bdd3907fc4489 (patch)
tree595dbe87703eb6be227b83badb550095c7c71019
parent744b0d0c6b9231250dff00ed873e9cd5966da3ff (diff)
downloadDLT-daemon-d4ef0bbe46f7cb409792ab62ea1bdd3907fc4489.tar.gz
Improvement - use dup2 in place of dup in daemon fork
1> While daemonizing DLT, for connecting I/O file descriptors to /dev/NULL POSIX call dup() is used. dup2() POSIX call is now used where explicit standard I/O descriptor number is mentioned. 2>After fork() directory changed to root in place of FIFO Base directory - "/tmp" Signed-off-by: Jeevan Ramakant Nagvekar <jeevan.nagvekar1@wipro.com>
-rw-r--r--src/daemon/dlt-daemon.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c
index 660932c..6c1d070 100644
--- a/src/daemon/dlt-daemon.c
+++ b/src/daemon/dlt-daemon.c
@@ -1395,6 +1395,7 @@ void dlt_daemon_signal_handler(int sig)
void dlt_daemon_daemonize(int verbose)
{
int i;
+ int fd;
PRINT_FUNCTION_VERBOSE(verbose);
@@ -1423,30 +1424,32 @@ void dlt_daemon_daemonize(int verbose)
exit(-1); /* fork error */
}
- /* Close descriptors */
- for (i=getdtablesize();i>=0;--i)
- {
- close(i); /* close all descriptors */
- }
-
/* Open standard descriptors stdin, stdout, stderr */
- i=open("/dev/null",O_RDWR); /* open stdin */
- if (-1 < i)
+ fd = open("/dev/null", O_RDWR);
+ if (fd != -1)
{
- if(dup(i) < 0)
- dlt_log(LOG_WARNING, "Failed to direct stdout to /dev/null.\n");/* stdout */
- if(dup(i) < 0)
- dlt_log(LOG_WARNING, "Failed to direct stderr to /dev/null.\n"); /* stderr */
+ /* Redirect STDOUT to /dev/null */
+ if (dup2(fd, STDOUT_FILENO) < 0)
+ {
+ dlt_vlog(LOG_WARNING, "Failed to direct stdout to /dev/null. Error: %s\n", strerror(errno));
+ }
+
+ /* Redirect STDERR to /dev/null */
+ if (dup2(fd, STDERR_FILENO) < 0)
+ {
+ dlt_vlog(LOG_WARNING, "Failed to direct stderr to /dev/null. Error: %s\n", strerror(errno));
+ }
+
+ close(fd);
}
/* Set umask */
umask(DLT_DAEMON_UMASK);
- /* Change to known directory */
- if(chdir(dltFifoBaseDir) < 0)
+ /* Change to root directory */
+ if (chdir("/") < 0)
{
- snprintf(str, DLT_DAEMON_TEXTBUFSIZE, "Failed to chdir to fifo-dir %s\n", dltFifoBaseDir);
- dlt_log(LOG_WARNING, str);
+ dlt_log(LOG_WARNING, "Failed to chdir to root\n");
}
/* Catch signals */