* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: os.c,v 1.59 2002/07/03 05:09:43 marka Exp $ */
+/* $Id: os.c,v 1.60 2002/07/10 01:13:42 marka Exp $ */
#include <config.h>
#include <stdarg.h>
-#include <sys/types.h> /* dev_t FreeBSD 2.1 */
+#include <sys/types.h> /* dev_t FreeBSD 2.1 */
#include <sys/stat.h>
#include <ctype.h>
/*
* Linux defines:
- * (T) HAVE_LINUXTHREADS
- * (C) HAVE_LINUX_CAPABILITY_H
- * (P) HAVE_SYS_PRCTL_H
+ * (T) HAVE_LINUXTHREADS
+ * (C) HAVE_LINUX_CAPABILITY_H
+ * (P) HAVE_SYS_PRCTL_H
* The possible cases are:
- * none: setuid() normally
- * T: no setuid()
- * C: setuid() normally, drop caps (keep CAP_SETUID)
- * T+C: no setuid(), drop caps (don't keep CAP_SETUID)
- * T+C+P: setuid() early, drop caps (keep CAP_SETUID)
- * C+P: setuid() normally, drop caps (keep CAP_SETUID)
+ * none: setuid() normally
+ * T: no setuid()
+ * C: setuid() normally, drop caps (keep CAP_SETUID)
+ * T+C: no setuid(), drop caps (don't keep CAP_SETUID)
+ * T+C+P: setuid() early, drop caps (keep CAP_SETUID)
+ * C+P: setuid() normally, drop caps (keep CAP_SETUID)
* P: not possible
* T+P: not possible
*
* if (C)
- * caps = BIND_SERVICE + CHROOT + SETGID
- * if ((T && C && P) || !T)
- * caps += SETUID
- * endif
- * capset(caps)
+ * caps = BIND_SERVICE + CHROOT + SETGID
+ * if ((T && C && P) || !T)
+ * caps += SETUID
+ * endif
+ * capset(caps)
* endif
* if (T && C && P && -u)
- * setuid()
+ * setuid()
* else if (T && -u)
- * fail
+ * fail
* --> start threads
* if (!T && -u)
- * setuid()
+ * setuid()
* if (C && (P || !-u))
- * caps = BIND_SERVICE
- * capset(caps)
+ * caps = BIND_SERVICE
+ * capset(caps)
* endif
*
* It will be nice when Linux threads work properly with setuid().
mainpid = getpid();
#endif
- if (setsid() == -1) {
+ if (setsid() == -1) {
isc__strerror(errno, strbuf, sizeof(strbuf));
ns_main_earlyfatal("setsid(): %s", strbuf);
}
static int
safe_open(const char *filename, isc_boolean_t append) {
int fd;
- struct stat sb;
+ struct stat sb;
- if (stat(filename, &sb) == -1) {
- if (errno != ENOENT)
+ if (stat(filename, &sb) == -1) {
+ if (errno != ENOENT)
return (-1);
- } else if ((sb.st_mode & S_IFREG) == 0) {
+ } else if ((sb.st_mode & S_IFREG) == 0) {
errno = EOPNOTSUPP;
return (-1);
}
if (append)
fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,
- S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+ S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
else {
(void)unlink(filename);
fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
- S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+ S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
}
return (fd);
}
void
ns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
- int fd;
+ int fd;
FILE *lockfile;
size_t len;
pid_t pid;
pidfile = malloc(len + 1);
if (pidfile == NULL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
- (*report)("couldn't malloc '%s': %s", filename, strbuf);
+ (*report)("couldn't malloc '%s': %s", filename, strbuf);
return;
}
/* This is safe. */
strcpy(pidfile, filename);
- fd = safe_open(filename, ISC_FALSE);
- if (fd < 0) {
+ fd = safe_open(filename, ISC_FALSE);
+ if (fd < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
- (*report)("couldn't open pid file '%s': %s", filename, strbuf);
+ (*report)("couldn't open pid file '%s': %s", filename, strbuf);
free(pidfile);
pidfile = NULL;
return;
}
- lockfile = fdopen(fd, "w");
- if (lockfile == NULL) {
+ lockfile = fdopen(fd, "w");
+ if (lockfile == NULL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
(*report)("could not fdopen() pid file '%s': %s",
filename, strbuf);
#else
pid = getpid();
#endif
- if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
- (*report)("fprintf() to pid file '%s' failed", filename);
+ 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);
+ if (fflush(lockfile) == EOF) {
+ (*report)("fflush() to pid file '%s' failed", filename);
(void)fclose(lockfile);
cleanup_pidfile();
return;
isc_result_t
ns_os_gethostname(char *buf, size_t len) {
- int n;
+ int n;
n = gethostname(buf, len);
return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: os.c,v 1.14 2002/05/03 05:28:27 marka Exp $ */
+/* $Id: os.c,v 1.15 2002/07/10 01:13:44 marka Exp $ */
#include <config.h>
#include <stdarg.h>
static int
safe_open(const char *filename, isc_boolean_t append) {
int fd;
- struct stat sb;
+ struct stat sb;
- if (stat(filename, &sb) == -1) {
- if (errno != ENOENT)
+ if (stat(filename, &sb) == -1) {
+ if (errno != ENOENT)
return (-1);
- } else if ((sb.st_mode & S_IFREG) == 0)
+ } else if ((sb.st_mode & S_IFREG) == 0)
return (-1);
if (append)
fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,
- S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+ S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
else {
(void)unlink(filename);
fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
- S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+ S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
}
return (fd);
}
void
ns_os_writepidfile(const char *filename, isc_boolean_t first_time) {
- int fd;
+ int fd;
FILE *lockfile;
size_t len;
pid_t pid;
pidfile = malloc(len + 1);
if (pidfile == NULL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
- (*report)("couldn't malloc '%s': %s", filename, strbuf);
+ (*report)("couldn't malloc '%s': %s", filename, strbuf);
return;
}
/* This is safe. */
strcpy(pidfile, filename);
- fd = safe_open(filename, ISC_FALSE);
- if (fd < 0) {
+ fd = safe_open(filename, ISC_FALSE);
+ if (fd < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
- (*report)("couldn't open pid file '%s': %s", filename, strbuf);
+ (*report)("couldn't open pid file '%s': %s", filename, strbuf);
free(pidfile);
pidfile = NULL;
return;
}
- lockfile = fdopen(fd, "w");
- if (lockfile == NULL) {
+ lockfile = fdopen(fd, "w");
+ if (lockfile == NULL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
(*report)("could not fdopen() pid file '%s': %s",
filename, strbuf);
pid = getpid();
- if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
- (*report)("fprintf() to pid file '%s' failed", filename);
+ if (fprintf(lockfile, "%ld\n", (long)pid) < 0) {
+ (*report)("fprintf() to pid file '%s' failed", filename);
(void)fclose(fd);
cleanup_pidfile();
return;
}
- if (fflush(lockfile) == EOF) {
- (*report)("fflush() to pid file '%s' failed", filename);
+ if (fflush(lockfile) == EOF) {
+ (*report)("fflush() to pid file '%s' failed", filename);
(void)fclose(fd);
cleanup_pidfile();
return;
isc_result_t
ns_os_gethostname(char *buf, size_t len) {
- int n;
+ int n;
n = gethostname(buf, len);
return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);