os.c 6.79 KB
Newer Older
Danny Mayer's avatar
Danny Mayer committed
1
/*
Tinderbox User's avatar
Tinderbox User committed
2
 * Copyright (C) 2004, 2005, 2007-2009, 2012-2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2002  Internet Software Consortium.
Danny Mayer's avatar
Danny Mayer committed
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Danny Mayer's avatar
Danny Mayer committed
6
7
8
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
Mark Andrews's avatar
Mark Andrews committed
9
10
11
12
13
14
15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Danny Mayer's avatar
Danny Mayer committed
16
17
 */

Automatic Updater's avatar
Automatic Updater committed
18
/* $Id: os.c,v 1.39 2012/02/06 23:46:44 tbox Exp $ */
Danny Mayer's avatar
Danny Mayer committed
19
20
21
22

#include <config.h>
#include <stdarg.h>

Evan Hunt's avatar
Evan Hunt committed
23
#include <sys/types.h>
Danny Mayer's avatar
Danny Mayer committed
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <sys/stat.h>

#include <ctype.h>
#include <errno.h>
#include <io.h>
#include <process.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

#include <isc/print.h>
#include <isc/result.h>
37
#include <isc/strerror.h>
Danny Mayer's avatar
Danny Mayer committed
38
39
#include <isc/string.h>
#include <isc/ntpaths.h>
40
#include <isc/util.h>
41
#include <isc/win32os.h>
Danny Mayer's avatar
Danny Mayer committed
42
43

#include <named/main.h>
44
#include <named/log.h>
Danny Mayer's avatar
Danny Mayer committed
45
46
47
48
49
50
#include <named/os.h>
#include <named/globals.h>
#include <named/ntservice.h>


static char *pidfile = NULL;
51
static int devnullfd = -1;
Danny Mayer's avatar
Danny Mayer committed
52
53
54

static BOOL Initialized = FALSE;

Automatic Updater's avatar
Automatic Updater committed
55
static char *version_error =
56
57
	"named requires Windows 2000 Service Pack 2 or later to run correctly";

Danny Mayer's avatar
Danny Mayer committed
58
void
59
ns_paths_init(void) {
60
	if (!Initialized)
Danny Mayer's avatar
Danny Mayer committed
61
		isc_ntpaths_init();
62

Danny Mayer's avatar
Danny Mayer committed
63
64
65
66
67
68
	ns_g_conffile = isc_ntpaths_get(NAMED_CONF_PATH);
	lwresd_g_conffile = isc_ntpaths_get(LWRES_CONF_PATH);
	lwresd_g_resolvconffile = isc_ntpaths_get(RESOLV_CONF_PATH);
	ns_g_conffile = isc_ntpaths_get(NAMED_CONF_PATH);
	ns_g_defaultpidfile = isc_ntpaths_get(NAMED_PID_PATH);
	lwresd_g_defaultpidfile = isc_ntpaths_get(LWRESD_PID_PATH);
69
	ns_g_keyfile = isc_ntpaths_get(RNDC_KEY_PATH);
70
	ns_g_defaultsessionkeyfile = isc_ntpaths_get(SESSION_KEY_PATH);
Danny Mayer's avatar
Danny Mayer committed
71
72
73
74

	Initialized = TRUE;
}

75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Due to Knowledge base article Q263823 we need to make sure that
 * Windows 2000 systems have Service Pack 2 or later installed and
 * warn when it isn't.
 */
static void
version_check(const char *progname) {

	if(isc_win32os_majorversion() < 5)
		return;	/* No problem with Version 4.0 */
	if(isc_win32os_versioncheck(5, 0, 2, 0) < 0)
		if (ntservice_isservice())
			NTReportError(progname, version_error);
Automatic Updater's avatar
Automatic Updater committed
88
		else
89
90
			fprintf(stderr, "%s\n", version_error);
}
Danny Mayer's avatar
Danny Mayer committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

static void
setup_syslog(const char *progname) {
	int options;

	options = LOG_PID;
#ifdef LOG_NDELAY
	options |= LOG_NDELAY;
#endif

	openlog(progname, options, LOG_DAEMON);
}

void
ns_os_init(const char *progname) {
	ns_paths_init();
	setup_syslog(progname);
108
109
110
111
112
113
114
115
116
117
	/*
	 * XXXMPA. We may need to split ntservice_init() in two and
	 * just mark as running in ns_os_started().  If we do that
	 * this is where the first part of ntservice_init() should be
	 * called from.
	 *
	 * XXX970 Remove comment if no problems by 9.7.0.
	 *
	 * ntservice_init();
	 */
118
	version_check(progname);
Danny Mayer's avatar
Danny Mayer committed
119
120
121
122
123
124
125
126
}

void
ns_os_daemonize(void) {
	/*
	 * Try to set stdin, stdout, and stderr to /dev/null, but press
	 * on even if it fails.
	 */
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
	if (devnullfd != -1) {
		if (devnullfd != _fileno(stdin)) {
			close(_fileno(stdin));
			(void)_dup2(devnullfd, _fileno(stdin));
		}
		if (devnullfd != _fileno(stdout)) {
			close(_fileno(stdout));
			(void)_dup2(devnullfd, _fileno(stdout));
		}
		if (devnullfd != _fileno(stderr)) {
			close(_fileno(stderr));
			(void)_dup2(devnullfd, _fileno(stderr));
		}
	}
}

void
ns_os_opendevnull(void) {
	devnullfd = open("NUL", O_RDWR, 0);
}

void
ns_os_closedevnull(void) {
	if (devnullfd != _fileno(stdin) &&
	    devnullfd != _fileno(stdout) &&
	    devnullfd != _fileno(stderr)) {
		close(devnullfd);
		devnullfd = -1;
Danny Mayer's avatar
Danny Mayer committed
155
156
157
158
159
	}
}

void
ns_os_chroot(const char *root) {
Mark Andrews's avatar
Mark Andrews committed
160
161
	if (root != NULL)
		ns_main_earlyfatal("chroot(): isn't supported by Win32 API");
Danny Mayer's avatar
Danny Mayer committed
162
163
164
165
166
167
168
169
170
171
}

void
ns_os_inituserinfo(const char *username) {
}

void
ns_os_changeuser(void) {
}

172
173
174
175
void
ns_os_adjustnofile(void) {
}

Danny Mayer's avatar
Danny Mayer committed
176
177
178
179
180
void
ns_os_minprivs(void) {
}

static int
Evan Hunt's avatar
Evan Hunt committed
181
safe_open(const char *filename, int mode, isc_boolean_t append) {
Danny Mayer's avatar
Danny Mayer committed
182
	int fd;
Mark Andrews's avatar
Mark Andrews committed
183
	struct stat sb;
Danny Mayer's avatar
Danny Mayer committed
184

Mark Andrews's avatar
Mark Andrews committed
185
186
	if (stat(filename, &sb) == -1) {
		if (errno != ENOENT)
Danny Mayer's avatar
Danny Mayer committed
187
			return (-1);
Mark Andrews's avatar
Mark Andrews committed
188
	} else if ((sb.st_mode & S_IFREG) == 0)
Danny Mayer's avatar
Danny Mayer committed
189
190
191
		return (-1);

	if (append)
192
		fd = open(filename, O_WRONLY|O_CREAT|O_APPEND, mode);
Danny Mayer's avatar
Danny Mayer committed
193
194
	else {
		(void)unlink(filename);
195
		fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, mode);
Danny Mayer's avatar
Danny Mayer committed
196
197
198
199
200
201
202
203
204
205
206
207
208
	}
	return (fd);
}

static void
cleanup_pidfile(void) {
	if (pidfile != NULL) {
		(void)unlink(pidfile);
		free(pidfile);
	}
	pidfile = NULL;
}

209
FILE *
Evan Hunt's avatar
Evan Hunt committed
210
ns_os_openfile(const char *filename, int mode, isc_boolean_t switch_user) {
211
212
213
214
215
216
217
218
219
220
	char strbuf[ISC_STRERRORSIZE];
	FILE *fp;
	int fd;

	UNUSED(switch_user);
	fd = safe_open(filename, mode, ISC_FALSE);
	if (fd < 0) {
		isc__strerror(errno, strbuf, sizeof(strbuf));
		ns_main_earlywarning("could not open file '%s': %s",
				     filename, strbuf);
221
		return (NULL);
222
223
224
	}

	fp = fdopen(fd, "w");
Evan Hunt's avatar
Evan Hunt committed
225
	if (fp == NULL) {
226
227
228
229
230
231
232
233
234
		isc__strerror(errno, strbuf, sizeof(strbuf));
		ns_main_earlywarning("could not fdopen() file '%s': %s",
				     filename, strbuf);
		close(fd);
	}

	return (fp);
}

Danny Mayer's avatar
Danny Mayer committed
235
void
236
ns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
Danny Mayer's avatar
Danny Mayer committed
237
238
	FILE *lockfile;
	pid_t pid;
239
	char strbuf[ISC_STRERRORSIZE];
240
	void (*report)(const char *, ...);
Danny Mayer's avatar
Danny Mayer committed
241
242
243
244
245

	/*
	 * The caller must ensure any required synchronization.
	 */

246
247
	report = first_time ? ns_main_earlyfatal : ns_main_earlywarning;

Danny Mayer's avatar
Danny Mayer committed
248
249
	cleanup_pidfile();

250
	if (filename == NULL)
251
		return;
252

Evan Hunt's avatar
Evan Hunt committed
253
	pidfile = strdup(filename);
254
	if (pidfile == NULL) {
255
		isc__strerror(errno, strbuf, sizeof(strbuf));
256
		(*report)("couldn't strdup() '%s': %s", filename, strbuf);
257
		return;
258
	}
Danny Mayer's avatar
Danny Mayer committed
259

260
261
262
	lockfile = ns_os_openfile(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
				  ISC_FALSE);
	if (lockfile == NULL) {
263
264
265
		free(pidfile);
		pidfile = NULL;
		return;
266
	}
Danny Mayer's avatar
Danny Mayer committed
267

268
269
	pid = getpid();

Mark Andrews's avatar
Mark Andrews committed
270
271
	if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
		(*report)("fprintf() to pid file '%s' failed", filename);
272
		(void)fclose(lockfile);
273
274
275
		cleanup_pidfile();
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
276
277
	if (fflush(lockfile) == EOF) {
		(*report)("fflush() to pid file '%s' failed", filename);
278
		(void)fclose(lockfile);
279
280
281
		cleanup_pidfile();
		return;
	}
Danny Mayer's avatar
Danny Mayer committed
282
283
284
285
286
287
288
289
290
	(void)fclose(lockfile);
}

void
ns_os_shutdown(void) {
	closelog();
	cleanup_pidfile();
	ntservice_shutdown();	/* This MUST be the last thing done */
}
291
292
293

isc_result_t
ns_os_gethostname(char *buf, size_t len) {
Mark Andrews's avatar
Mark Andrews committed
294
	int n;
295

296
	n = gethostname(buf, (int)len);
297
	return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
298
}
299

300
301
302
303
304
void
ns_os_shutdownmsg(char *command, isc_buffer_t *text) {
	UNUSED(command);
	UNUSED(text);
}
305
306
307
308
309
310
311

void
ns_os_tzset(void) {
#ifdef HAVE_TZSET
	tzset();
#endif
}
312
313
314

void
ns_os_started(void) {
315
	ntservice_init();
316
}