2001-07-18 03:43:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1999-2001 Internet Software Consortium.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and 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 INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM 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.
|
|
|
|
*/
|
|
|
|
|
2002-07-10 01:13:44 +00:00
|
|
|
/* $Id: os.c,v 1.15 2002/07/10 01:13:44 marka Exp $ */
|
2001-07-18 03:43:18 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdarg.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>
|
2001-11-21 05:07:25 +00:00
|
|
|
#include <isc/strerror.h>
|
2001-07-18 03:43:18 +00:00
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/ntpaths.h>
|
|
|
|
|
|
|
|
#include <named/main.h>
|
|
|
|
#include <named/os.h>
|
|
|
|
#include <named/globals.h>
|
|
|
|
#include <named/ntservice.h>
|
|
|
|
|
|
|
|
|
|
|
|
static char *pidfile = NULL;
|
|
|
|
|
|
|
|
static BOOL Initialized = FALSE;
|
|
|
|
|
|
|
|
void
|
2001-07-18 18:42:13 +00:00
|
|
|
ns_paths_init() {
|
|
|
|
if (!Initialized)
|
2001-07-18 03:43:18 +00:00
|
|
|
isc_ntpaths_init();
|
2001-07-18 18:42:13 +00:00
|
|
|
|
2001-07-18 03:43:18 +00:00
|
|
|
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);
|
2001-08-09 23:44:13 +00:00
|
|
|
ns_g_keyfile = isc_ntpaths_get(RNDC_KEY_PATH);
|
2001-07-18 03:43:18 +00:00
|
|
|
|
|
|
|
Initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
ntservice_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_daemonize(void) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to set stdin, stdout, and stderr to /dev/null, but press
|
|
|
|
* on even if it fails.
|
|
|
|
*
|
|
|
|
* XXXMLG The close() calls here are unneeded on all but NetBSD, but
|
|
|
|
* are harmless to include everywhere. dup2() is supposed to close
|
|
|
|
* the FD if it is in use, but unproven-pthreads-0.16 is broken
|
|
|
|
* and will end up closing the wrong FD. This will be fixed eventually,
|
|
|
|
* and these calls will be removed.
|
|
|
|
*/
|
|
|
|
fd = open("NUL", O_RDWR, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
close(_fileno(stdin));
|
|
|
|
(void)_dup2(fd, _fileno(stdin));
|
|
|
|
close(_fileno(stdout));
|
|
|
|
(void)_dup2(fd, _fileno(stdout));
|
|
|
|
close(_fileno(stderr));
|
|
|
|
(void)_dup2(fd, _fileno(stderr));
|
|
|
|
if (fd != _fileno(stdin) &&
|
|
|
|
fd != _fileno(stdout) &&
|
|
|
|
fd != _fileno(stderr))
|
|
|
|
(void)close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_chroot(const char *root) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_inituserinfo(const char *username) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_changeuser(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_minprivs(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
safe_open(const char *filename, isc_boolean_t append) {
|
|
|
|
int fd;
|
2002-07-10 01:13:44 +00:00
|
|
|
struct stat sb;
|
2001-07-18 03:43:18 +00:00
|
|
|
|
2002-07-10 01:13:44 +00:00
|
|
|
if (stat(filename, &sb) == -1) {
|
|
|
|
if (errno != ENOENT)
|
2001-07-18 03:43:18 +00:00
|
|
|
return (-1);
|
2002-07-10 01:13:44 +00:00
|
|
|
} else if ((sb.st_mode & S_IFREG) == 0)
|
2001-07-18 03:43:18 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (append)
|
|
|
|
fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,
|
2002-07-10 01:13:44 +00:00
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
2001-07-18 03:43:18 +00:00
|
|
|
else {
|
|
|
|
(void)unlink(filename);
|
|
|
|
fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
|
2002-07-10 01:13:44 +00:00
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
2001-07-18 03:43:18 +00:00
|
|
|
}
|
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_pidfile(void) {
|
|
|
|
if (pidfile != NULL) {
|
|
|
|
(void)unlink(pidfile);
|
|
|
|
free(pidfile);
|
|
|
|
}
|
|
|
|
pidfile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-05-03 05:28:29 +00:00
|
|
|
ns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
|
2002-07-10 01:13:44 +00:00
|
|
|
int fd;
|
2001-07-18 03:43:18 +00:00
|
|
|
FILE *lockfile;
|
|
|
|
size_t len;
|
|
|
|
pid_t pid;
|
2001-11-21 05:07:25 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2002-05-03 05:28:29 +00:00
|
|
|
void (*report)(const char *, ...);
|
2001-07-18 03:43:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller must ensure any required synchronization.
|
|
|
|
*/
|
|
|
|
|
2002-05-03 05:28:29 +00:00
|
|
|
report = first_time ? ns_main_earlyfatal : ns_main_earlywarning;
|
|
|
|
|
2001-07-18 03:43:18 +00:00
|
|
|
cleanup_pidfile();
|
|
|
|
|
2001-10-12 05:40:41 +00:00
|
|
|
if (strcmp(filename, "none") == 0)
|
|
|
|
return;
|
2001-07-18 03:43:18 +00:00
|
|
|
len = strlen(filename);
|
|
|
|
pidfile = malloc(len + 1);
|
2001-11-22 03:11:01 +00:00
|
|
|
if (pidfile == NULL) {
|
2001-11-21 05:07:25 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2002-07-10 01:13:44 +00:00
|
|
|
(*report)("couldn't malloc '%s': %s", filename, strbuf);
|
2002-05-03 05:28:29 +00:00
|
|
|
return;
|
2001-11-22 03:11:01 +00:00
|
|
|
}
|
2001-07-18 03:43:18 +00:00
|
|
|
/* This is safe. */
|
|
|
|
strcpy(pidfile, filename);
|
|
|
|
|
2002-07-10 01:13:44 +00:00
|
|
|
fd = safe_open(filename, ISC_FALSE);
|
|
|
|
if (fd < 0) {
|
2001-11-21 05:07:25 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2002-07-10 01:13:44 +00:00
|
|
|
(*report)("couldn't open pid file '%s': %s", filename, strbuf);
|
2002-05-03 05:28:29 +00:00
|
|
|
free(pidfile);
|
|
|
|
pidfile = NULL;
|
|
|
|
return;
|
2001-11-22 03:11:01 +00:00
|
|
|
}
|
2002-07-10 01:13:44 +00:00
|
|
|
lockfile = fdopen(fd, "w");
|
|
|
|
if (lockfile == NULL) {
|
2001-11-21 05:07:25 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2002-05-03 05:28:29 +00:00
|
|
|
(*report)("could not fdopen() pid file '%s': %s",
|
|
|
|
filename, strbuf);
|
|
|
|
(void)close(fd);
|
|
|
|
cleanup_pidfile();
|
|
|
|
return;
|
2001-11-22 03:11:01 +00:00
|
|
|
}
|
2001-07-18 03:43:18 +00:00
|
|
|
|
2002-05-03 05:28:29 +00:00
|
|
|
pid = getpid();
|
|
|
|
|
2002-07-10 01:13:44 +00:00
|
|
|
if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
|
|
|
|
(*report)("fprintf() to pid file '%s' failed", filename);
|
2002-05-03 05:28:29 +00:00
|
|
|
(void)fclose(fd);
|
|
|
|
cleanup_pidfile();
|
|
|
|
return;
|
|
|
|
}
|
2002-07-10 01:13:44 +00:00
|
|
|
if (fflush(lockfile) == EOF) {
|
|
|
|
(*report)("fflush() to pid file '%s' failed", filename);
|
2002-05-03 05:28:29 +00:00
|
|
|
(void)fclose(fd);
|
|
|
|
cleanup_pidfile();
|
|
|
|
return;
|
|
|
|
}
|
2001-07-18 03:43:18 +00:00
|
|
|
(void)fclose(lockfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_shutdown(void) {
|
|
|
|
closelog();
|
|
|
|
cleanup_pidfile();
|
|
|
|
ntservice_shutdown(); /* This MUST be the last thing done */
|
|
|
|
}
|
2001-10-08 07:46:11 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_os_gethostname(char *buf, size_t len) {
|
2002-07-10 01:13:44 +00:00
|
|
|
int n;
|
2001-11-18 03:03:42 +00:00
|
|
|
|
|
|
|
n = gethostname(buf, len);
|
|
|
|
return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
|
2001-10-08 07:46:11 +00:00
|
|
|
}
|
2001-11-18 03:03:42 +00:00
|
|
|
|
2001-12-01 00:34:27 +00:00
|
|
|
void
|
|
|
|
ns_os_shutdownmsg(char *command, isc_buffer_t *text) {
|
|
|
|
UNUSED(command);
|
|
|
|
UNUSED(text);
|
|
|
|
}
|