summaryrefslogtreecommitdiffstats
path: root/agents/virt/server/daemon_init.c
blob: 29b33ad3aab98f9c2a8bdc95445eb539d76849e3 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/** @file
 * daemon_init function, does sanity checks and calls daemon().
 *
 * Author: Jeff Moyer <jmoyer@redhat.com>
 */
/*
 * TODO: Clean this up so that only one function constructs the 
 *       pidfile /var/run/loggerd.PID, and perhaps only one function
 *       forms the /proc/PID/ path.
 *
 *       Also need to add file locking for the pid file.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/errno.h>
#include <libgen.h>
#include <signal.h>
#include <syslog.h>


/*
 * This should ultimately go in a header file.
 */
void daemon_init(const char *prog, const char *pid_file, int nofork);
void daemon_cleanup(void);
int check_process_running(const char *cmd, const char *pid_file, pid_t * pid);

/*
 * Local prototypes.
 */
static void update_pidfile(const char *filename);
static int setup_sigmask(void);
static char pid_filename[PATH_MAX];

static int
check_pid_valid(pid_t pid, const char *prog)
{
	FILE *fp;
	DIR *dir;
	char filename[PATH_MAX];
	char dirpath[PATH_MAX];
	char proc_cmdline[64];	/* yank this from kernel somewhere */
	char *s = NULL;

	memset(filename, 0, PATH_MAX);
	memset(dirpath, 0, PATH_MAX);

	snprintf(dirpath, sizeof (dirpath), "/proc/%d", pid);
	if ((dir = opendir(dirpath)) == NULL) {
		return 0;	/* Pid has gone away. */
	}
	closedir(dir);

	/*
	 * proc-pid directory exists.  Now check to see if this
	 * PID corresponds to the daemon we want to start.
	 */
	snprintf(filename, sizeof (filename), "/proc/%d/cmdline", pid);
	fp = fopen(filename, "r");
	if (fp == NULL) {
		perror("check_pid_valid");
		return 0;	/* Who cares.... Let's boogy on. */
	}

	if (!fgets(proc_cmdline, sizeof (proc_cmdline) - 1, fp)) {
		/*
		 * Okay, we've seen processes keep a reference to a
		 * /proc/PID/stat file and not let go.  Then when
		 * you try to read /proc/PID/cmline, you get either
		 * \000 or -1.  In either case, we can safely assume
		 * the process has gone away.
		 */
		fclose(fp);
		return 0;
	}
	fclose(fp);

	s = &(proc_cmdline[strlen(proc_cmdline)]);
	if (*s == '\n')
		*s = 0;

	/*
	 * Check to see if this is the same executable.
	 */
	if (strstr(proc_cmdline, prog) == NULL) {
		return 0;
	} else {
		return 1;
	}
}


int
check_process_running(const char *cmd, const char *filename, pid_t * pid)
{
	pid_t oldpid;
	FILE *fp = NULL;
	int ret;

	*pid = -1;

	/*
	 * Read the pid from the file.
	 */
	fp = fopen(filename, "r");
	if (fp == NULL) {	/* error */
		return 0;
	}

	ret = fscanf(fp, "%d\n", &oldpid);
	fclose(fp);

	if ((ret == EOF) || (ret != 1))
		return 0;

	if (check_pid_valid(oldpid, cmd)) {
		*pid = oldpid;
		return 1;
	}
	return 0;
}


static void
update_pidfile(const char *filename)
{
	FILE *fp = NULL;

	strncpy(pid_filename, filename, PATH_MAX - 1);

	fp = fopen(pid_filename, "w");
	if (fp == NULL) {
		syslog(LOG_ERR, "daemon_init: Unable to create pidfile %s: %s\n",
			filename, strerror(errno));
		exit(1);
	}

	fprintf(fp, "%d", getpid());
	fclose(fp);
}


static int
setup_sigmask(void)
{
	sigset_t set;

	sigfillset(&set);

	/*
	 * Dont't block signals which would cause us to dump core.
	 */
	sigdelset(&set, SIGQUIT);
	sigdelset(&set, SIGILL);
	sigdelset(&set, SIGTRAP);
	sigdelset(&set, SIGABRT);
	sigdelset(&set, SIGFPE);
	sigdelset(&set, SIGSEGV);
	sigdelset(&set, SIGBUS);

	/*
	 * Don't block SIGTERM or SIGCHLD
	 */
	sigdelset(&set, SIGTERM);
	sigdelset(&set, SIGINT);
	sigdelset(&set, SIGQUIT);
	sigdelset(&set, SIGCHLD);

	return (sigprocmask(SIG_BLOCK, &set, NULL));
}


void
daemon_init(const char *prog, const char *pid_file, int nofork)
{
	pid_t pid;

	if (check_process_running(prog, pid_file, &pid) && (pid != getpid())) {
		syslog(LOG_ERR,
			"daemon_init: Process \"%s\" already running.\n",
			prog);
		exit(1);
	}

	if (setup_sigmask() < 0) {
		syslog(LOG_ERR, "daemon_init: Unable to set signal mask.\n");
		exit(1);
	}

	if (!nofork && daemon(0, 0)) {
		syslog(LOG_ERR, "daemon_init: Unable to daemonize.\n");
		exit(1);
	}

	update_pidfile(pid_file);
}


void
daemon_cleanup(void)
{
	if (strlen(pid_filename))
		unlink(pid_filename);
}