summaryrefslogtreecommitdiff
path: root/daemon.c
blob: ad73061c14ec5916284f2ab67438306d6484827d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* SPDX-License-Identifier: GPL-2.0 */

/** \file daemon.c Some helpers for programs that detach from the console. */

#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grp.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include "gcc-compat.h"
#include "err.h"
#include "log.h"
#include "str.h"
#include "daemon.h"

/**
 * Do the usual stuff to become a daemon.
 *
 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
 *
 * \sa fork(2), setsid(2), dup(2).
 */
int daemon_init(void)
{
	pid_t pid;
	int null, fd[2];

	DSS_INFO_LOG(("daemonizing\n"));
	if (pipe(fd) < 0)
		goto err;
	pid = fork();
	if (pid < 0)
		goto err;
	if (pid) {
		/*
		 * The parent process exits once it has received one byte from
		 * the reading end of the pipe. If the child exits before it
		 * was able to complete its setup (acquire the lock on the
		 * semaphore), the read() below will return zero. In this case
		 * we let the parent die unsuccessfully.
		 */
		char c;
		int ret;
		close(fd[1]);
		ret = read(fd[0], &c, 1);
		if (ret <= 0) {
			DSS_EMERG_LOG(("child terminated unexpectedly\n"));
			exit(EXIT_FAILURE);
		}
		exit(EXIT_SUCCESS);
	}
	close(fd[0]);
	/* become session leader */
	if (setsid() < 0)
		goto err;
	null = open("/dev/null", O_RDWR);
	if (null < 0)
		goto err;
	if (dup2(null, STDIN_FILENO) < 0)
		goto err;
	if (dup2(null, STDOUT_FILENO) < 0)
		goto err;
	if (dup2(null, STDERR_FILENO) < 0)
		goto err;
	close(null);
	return fd[1];
err:
	DSS_EMERG_LOG(("fatal: %s\n", strerror(errno)));
	exit(EXIT_FAILURE);
}

/**
 * fopen() the given file in append mode.
 *
 * \param logfile_name The name of the file to open.
 *
 * \return Either calls exit() or returns a valid file handle.
 */
FILE *open_log(const char *logfile_name)
{
	FILE *logfile;

	assert(logfile_name);
	logfile = fopen(logfile_name, "a");
	if (!logfile) {
		DSS_EMERG_LOG(("can not open %s: %s\n", logfile_name,
			strerror(errno)));
		exit(EXIT_FAILURE);
	}
	setlinebuf(logfile);
	return logfile;
}

/**
 * Close the log file of the daemon.
 *
 * \param logfile The log file handle.
 *
 * It's OK to call this with logfile == \p NULL.
 */
void close_log(FILE* logfile)
{
	if (!logfile)
		return;
	DSS_INFO_LOG(("closing logfile\n"));
	fclose(logfile);
}

/**
 * Log the startup message.
 */
void log_welcome(int loglevel)
{
	DSS_INFO_LOG(("***** welcome to dss ******\n"));
	DSS_DEBUG_LOG(("using loglevel %d\n", loglevel));
}