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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2002 Internet Software Consortium.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* 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.
*/
/* $Id: os.c,v 1.37 2009-08-05 18:43:37 each Exp $ */
#include <config.h>
#include <stdarg.h>
#include <sys/types.h>
#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>
#include <isc/strerror.h>
#include <isc/string.h>
#include <isc/ntpaths.h>
#include <isc/util.h>
#include <isc/win32os.h>
#include <named/main.h>
#include <named/log.h>
#include <named/os.h>
#include <named/globals.h>
#include <named/ntservice.h>
static char *pidfile = NULL;
static int devnullfd = -1;
static BOOL Initialized = FALSE;
static char *version_error =
"named requires Windows 2000 Service Pack 2 or later to run correctly";
void
ns_paths_init() {
if (!Initialized)
isc_ntpaths_init();
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);
ns_g_keyfile = isc_ntpaths_get(RNDC_KEY_PATH);
ns_g_defaultsessionkeyfile = isc_ntpaths_get(SESSION_KEY_PATH);
Initialized = TRUE;
}
/*
* 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);
else
fprintf(stderr, "%s\n", version_error);
}
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);
/*
* 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();
*/
version_check(progname);
}
void
ns_os_daemonize(void) {
/*
* Try to set stdin, stdout, and stderr to /dev/null, but press
* on even if it fails.
*/
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;
}
}
void
ns_os_chroot(const char *root) {
if (root != NULL)
ns_main_earlyfatal("chroot(): isn't supported by Win32 API");
}
void
ns_os_inituserinfo(const char *username) {
}
void
ns_os_changeuser(void) {
}
void
ns_os_adjustnofile(void) {
}
void
ns_os_minprivs(void) {
}
static int
safe_open(const char *filename, int mode, isc_boolean_t append) {
int fd;
struct stat sb;
if (stat(filename, &sb) == -1) {
if (errno != ENOENT)
return (-1);
} else if ((sb.st_mode & S_IFREG) == 0)
return (-1);
if (append)
fd = open(filename, O_WRONLY|O_CREAT|O_APPEND, mode);
else {
(void)unlink(filename);
fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, mode);
}
return (fd);
}
static void
cleanup_pidfile(void) {
if (pidfile != NULL) {
(void)unlink(pidfile);
free(pidfile);
}
pidfile = NULL;
}
FILE *
ns_os_openfile(const char *filename, int mode, isc_boolean_t switch_user) {
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);
}
fp = fdopen(fd, "w");
if (fp == NULL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
ns_main_earlywarning("could not fdopen() file '%s': %s",
filename, strbuf);
close(fd);
}
return (fp);
}
void
ns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
FILE *lockfile;
pid_t pid;
char strbuf[ISC_STRERRORSIZE];
void (*report)(const char *, ...);
/*
* The caller must ensure any required synchronization.
*/
report = first_time ? ns_main_earlyfatal : ns_main_earlywarning;
cleanup_pidfile();
if (filename == NULL)
return;
pidfile = strdup(filename);
if (pidfile == NULL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
(*report)("couldn't strdup() '%s': %s", filename, strbuf);
return;
}
lockfile = ns_os_openfile(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
ISC_FALSE);
if (lockfile == NULL) {
free(pidfile);
pidfile = NULL;
return;
}
pid = getpid();
if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
(*report)("fprintf() to pid file '%s' failed", filename);
(void)fclose(lockfile);
cleanup_pidfile();
return;
}
if (fflush(lockfile) == EOF) {
(*report)("fflush() to pid file '%s' failed", filename);
(void)fclose(lockfile);
cleanup_pidfile();
return;
}
(void)fclose(lockfile);
}
void
ns_os_shutdown(void) {
closelog();
cleanup_pidfile();
ntservice_shutdown(); /* This MUST be the last thing done */
}
isc_result_t
ns_os_gethostname(char *buf, size_t len) {
int n;
n = gethostname(buf, len);
return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
}
void
ns_os_shutdownmsg(char *command, isc_buffer_t *text) {
UNUSED(command);
UNUSED(text);
}
void
ns_os_tzset(void) {
#ifdef HAVE_TZSET
tzset();
#endif
}
void
ns_os_started(void) {
ntservice_init();
}
|