os.c revision 76786c2904942b708d8a7a4659df74da5dc9446e
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington/*
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * Copyright (C) 1999-2002 Internet Software Consortium.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington *
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * Permission to use, copy, modify, and/or distribute this software for any
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * purpose with or without fee is hereby granted, provided that the above
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * copyright notice and this permission notice appear in all copies.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington *
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * PERFORMANCE OF THIS SOFTWARE.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington */
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews/* $Id: os.c,v 1.34 2009/06/12 02:33:21 each Exp $ */
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <config.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <stdarg.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <sys/stat.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence#include <ctype.h>
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence#include <errno.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <io.h>
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence#include <process.h>
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence#include <fcntl.h>
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence#include <stdio.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <stdlib.h>
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence#include <syslog.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/print.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/result.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/strerror.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/string.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/ntpaths.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/util.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <isc/win32os.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <named/main.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <named/log.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <named/os.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <named/globals.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#include <named/ntservice.h>
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic char *pidfile = NULL;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic int devnullfd = -1;
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrence
7332e47e11ceb87928f801b925269aa6a91838b1David Lawrencestatic BOOL Initialized = FALSE;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic char *version_error =
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington "named requires Windows 2000 Service Pack 2 or later to run correctly";
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_paths_init() {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (!Initialized)
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington isc_ntpaths_init();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ns_g_conffile = isc_ntpaths_get(NAMED_CONF_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington lwresd_g_conffile = isc_ntpaths_get(LWRES_CONF_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington lwresd_g_resolvconffile = isc_ntpaths_get(RESOLV_CONF_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ns_g_conffile = isc_ntpaths_get(NAMED_CONF_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ns_g_defaultpidfile = isc_ntpaths_get(NAMED_PID_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington lwresd_g_defaultpidfile = isc_ntpaths_get(LWRESD_PID_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ns_g_keyfile = isc_ntpaths_get(RNDC_KEY_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ns_g_defaultddnskeyfile = isc_ntpaths_get(DDNS_KEY_PATH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington Initialized = TRUE;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington/*
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Due to Knowledge base article Q263823 we need to make sure that
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Windows 2000 systems have Service Pack 2 or later installed and
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * warn when it isn't.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic void
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonversion_check(const char *progname) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if(isc_win32os_majorversion() < 5)
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return; /* No problem with Version 4.0 */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if(isc_win32os_versioncheck(5, 0, 2, 0) < 0)
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (ntservice_isservice())
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington NTReportError(progname, version_error);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington else
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington fprintf(stderr, "%s\n", version_error);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
532989b206894bdaf6de6cb883d2e31169c4bfacAndreas Gustafsson
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic void
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonsetup_syslog(const char *progname) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington int options;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington options = LOG_PID;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#ifdef LOG_NDELAY
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington options |= LOG_NDELAY;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#endif
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington openlog(progname, options, LOG_DAEMON);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
532989b206894bdaf6de6cb883d2e31169c4bfacAndreas Gustafssonvoid
532989b206894bdaf6de6cb883d2e31169c4bfacAndreas Gustafssonns_os_init(const char *progname) {
532989b206894bdaf6de6cb883d2e31169c4bfacAndreas Gustafsson ns_paths_init();
532989b206894bdaf6de6cb883d2e31169c4bfacAndreas Gustafsson setup_syslog(progname);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington /*
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * XXXMPA. We may need to split ntservice_init() in two and
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington * just mark as running in ns_os_started(). If we do that
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * this is where the first part of ntservice_init() should be
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * called from.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington *
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * XXX970 Remove comment if no problems by 9.7.0.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington *
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * ntservice_init();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington version_check(progname);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_daemonize(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington /*
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Try to set stdin, stdout, and stderr to /dev/null, but press
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * on even if it fails.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (devnullfd != -1) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (devnullfd != _fileno(stdin)) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington close(_fileno(stdin));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)_dup2(devnullfd, _fileno(stdin));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (devnullfd != _fileno(stdout)) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington close(_fileno(stdout));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)_dup2(devnullfd, _fileno(stdout));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (devnullfd != _fileno(stderr)) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington close(_fileno(stderr));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)_dup2(devnullfd, _fileno(stderr));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_opendevnull(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington devnullfd = open("NUL", O_RDWR, 0);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_closedevnull(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (devnullfd != _fileno(stdin) &&
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington devnullfd != _fileno(stdout) &&
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington devnullfd != _fileno(stderr)) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington close(devnullfd);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington devnullfd = -1;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_chroot(const char *root) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (root != NULL)
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ns_main_earlyfatal("chroot(): isn't supported by Win32 API");
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_inituserinfo(const char *username) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_changeuser(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_adjustnofile(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_minprivs(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic int
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellingtonsafe_open(const char *filename, isc_boolean_t append) {
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington int fd;
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington struct stat sb;
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington if (stat(filename, &sb) == -1) {
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington if (errno != ENOENT)
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington return (-1);
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington } else if ((sb.st_mode & S_IFREG) == 0)
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington return (-1);
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (append)
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington else {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)unlink(filename);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return (fd);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic void
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtoncleanup_pidfile(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (pidfile != NULL) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)unlink(pidfile);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington free(pidfile);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington pidfile = NULL;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington int fd;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington FILE *lockfile;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington size_t len;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington pid_t pid;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington char strbuf[ISC_STRERRORSIZE];
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington void (*report)(const char *, ...);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington /*
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * The caller must ensure any required synchronization.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington report = first_time ? ns_main_earlyfatal : ns_main_earlywarning;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington cleanup_pidfile();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (filename == NULL)
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington len = strlen(filename);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington pidfile = malloc(len + 1);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (pidfile == NULL) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington isc__strerror(errno, strbuf, sizeof(strbuf));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (*report)("couldn't malloc '%s': %s", filename, strbuf);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington /* This is safe. */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington strcpy(pidfile, filename);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington fd = safe_open(filename, ISC_FALSE);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (fd < 0) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington isc__strerror(errno, strbuf, sizeof(strbuf));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (*report)("couldn't open pid file '%s': %s", filename, strbuf);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington free(pidfile);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington pidfile = NULL;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington lockfile = fdopen(fd, "w");
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (lockfile == NULL) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington isc__strerror(errno, strbuf, sizeof(strbuf));
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (*report)("could not fdopen() pid file '%s': %s",
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington filename, strbuf);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)close(fd);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington cleanup_pidfile();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington pid = getpid();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (*report)("fprintf() to pid file '%s' failed", filename);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)fclose(lockfile);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington cleanup_pidfile();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington if (fflush(lockfile) == EOF) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (*report)("fflush() to pid file '%s' failed", filename);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)fclose(lockfile);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington cleanup_pidfile();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington }
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington (void)fclose(lockfile);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_shutdown(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington closelog();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington cleanup_pidfile();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ntservice_shutdown(); /* This MUST be the last thing done */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonisc_result_t
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_gethostname(char *buf, size_t len) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington int n;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington n = gethostname(buf, len);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington}
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellingtonvoid
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellingtonns_os_shutdownmsg(char *command, isc_buffer_t *text) {
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington UNUSED(command);
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington UNUSED(text);
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington}
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_tzset(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#ifdef HAVE_TZSET
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington tzset();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#endif
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonvoid
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonns_os_started(void) {
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington ntservice_init();
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington}
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington