⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os.c

📁 bind 源码 最新实现 linux/unix/windows平台
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifdef HAVE_LINUXTHREADS	mainpid = getpid();#endif	if (setsid() == -1) {		isc__strerror(errno, strbuf, sizeof(strbuf));		ns_main_earlyfatal("setsid(): %s", strbuf);	}	/*	 * 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.	 */	if (devnullfd != -1) {		if (devnullfd != STDIN_FILENO) {			(void)close(STDIN_FILENO);			(void)dup2(devnullfd, STDIN_FILENO);		}		if (devnullfd != STDOUT_FILENO) {			(void)close(STDOUT_FILENO);			(void)dup2(devnullfd, STDOUT_FILENO);		}		if (devnullfd != STDERR_FILENO) {			(void)close(STDERR_FILENO);			(void)dup2(devnullfd, STDERR_FILENO);		}	}}voidns_os_started(void) {	char buf = 0;	/*	 * Signal to the parent that we stated successfully.	 */	if (dfd[0] != -1 && dfd[1] != -1) {		write(dfd[1], &buf, 1);		close(dfd[1]);		dfd[0] = dfd[1] = -1;	}}voidns_os_opendevnull(void) {	devnullfd = open("/dev/null", O_RDWR, 0);}voidns_os_closedevnull(void) {	if (devnullfd != STDIN_FILENO &&	    devnullfd != STDOUT_FILENO &&	    devnullfd != STDERR_FILENO) {		close(devnullfd);		devnullfd = -1;	}}static isc_boolean_tall_digits(const char *s) {	if (*s == '\0')		return (ISC_FALSE);	while (*s != '\0') {		if (!isdigit((*s)&0xff))			return (ISC_FALSE);		s++;	}	return (ISC_TRUE);}voidns_os_chroot(const char *root) {	char strbuf[ISC_STRERRORSIZE];#ifdef HAVE_LIBSCF	ns_smf_chroot = 0;#endif	if (root != NULL) {		if (chroot(root) < 0) {			isc__strerror(errno, strbuf, sizeof(strbuf));			ns_main_earlyfatal("chroot(): %s", strbuf);		}		if (chdir("/") < 0) {			isc__strerror(errno, strbuf, sizeof(strbuf));			ns_main_earlyfatal("chdir(/): %s", strbuf);		}#ifdef HAVE_LIBSCF		/* Set ns_smf_chroot flag on successful chroot. */		ns_smf_chroot = 1;#endif	}}voidns_os_inituserinfo(const char *username) {	char strbuf[ISC_STRERRORSIZE];	if (username == NULL)		return;	if (all_digits(username))		runas_pw = getpwuid((uid_t)atoi(username));	else		runas_pw = getpwnam(username);	endpwent();	if (runas_pw == NULL)		ns_main_earlyfatal("user '%s' unknown", username);	if (getuid() == 0) {		if (initgroups(runas_pw->pw_name, runas_pw->pw_gid) < 0) {			isc__strerror(errno, strbuf, sizeof(strbuf));			ns_main_earlyfatal("initgroups(): %s", strbuf);		}	}}voidns_os_changeuser(void) {	char strbuf[ISC_STRERRORSIZE];	if (runas_pw == NULL || done_setuid)		return;	done_setuid = ISC_TRUE;#ifdef HAVE_LINUXTHREADS#ifdef HAVE_LINUX_CAPABILITY_H	if (!non_root_caps)		ns_main_earlyfatal("-u with Linux threads not supported: "				   "requires kernel support for "				   "prctl(PR_SET_KEEPCAPS)");#else	ns_main_earlyfatal("-u with Linux threads not supported: "			   "no capabilities support or capabilities "			   "disabled at build time");#endif#endif	if (setgid(runas_pw->pw_gid) < 0) {		isc__strerror(errno, strbuf, sizeof(strbuf));		ns_main_earlyfatal("setgid(): %s", strbuf);	}	if (setuid(runas_pw->pw_uid) < 0) {		isc__strerror(errno, strbuf, sizeof(strbuf));		ns_main_earlyfatal("setuid(): %s", strbuf);	}#if defined(HAVE_LINUX_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)	linux_minprivs();#endif}voidns_os_minprivs(void) {#ifdef HAVE_SYS_PRCTL_H	linux_keepcaps();#endif#ifdef HAVE_LINUXTHREADS	ns_os_changeuser(); /* Call setuid() before threads are started */#endif#if defined(HAVE_LINUX_CAPABILITY_H) && defined(HAVE_LINUXTHREADS)	linux_minprivs();#endif}static intsafe_open(const char *filename, 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) {		errno = EOPNOTSUPP;		return (-1);	}	if (append)		fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,			  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);	}	return (fd);}static voidcleanup_pidfile(void) {	if (pidfile != NULL) {		(void)unlink(pidfile);		free(pidfile);	}	pidfile = NULL;}voidns_os_writepidfile(const char *filename, isc_boolean_t first_time) {	int fd;	FILE *lockfile;	size_t len;	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;	len = strlen(filename);	pidfile = malloc(len + 1);	if (pidfile == NULL) {		isc__strerror(errno, strbuf, sizeof(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) {		isc__strerror(errno, strbuf, sizeof(strbuf));		(*report)("couldn't open pid file '%s': %s", filename, strbuf);		free(pidfile);		pidfile = NULL;		return;	}	lockfile = fdopen(fd, "w");	if (lockfile == NULL) {		isc__strerror(errno, strbuf, sizeof(strbuf));		(*report)("could not fdopen() pid file '%s': %s",			  filename, strbuf);		(void)close(fd);		cleanup_pidfile();		return;	}#ifdef HAVE_LINUXTHREADS	pid = mainpid;#else	pid = getpid();#endif	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);}voidns_os_shutdown(void) {	closelog();	cleanup_pidfile();}isc_result_tns_os_gethostname(char *buf, size_t len) {	int n;	n = gethostname(buf, len);	return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);}static char *next_token(char **stringp, const char *delim) {	char *res;	do {		res = strsep(stringp, delim);		if (res == NULL)			break;	} while (*res == '\0');	return (res);}voidns_os_shutdownmsg(char *command, isc_buffer_t *text) {	char *input, *ptr;	unsigned int n;	pid_t pid;	input = command;	/* Skip the command name. */	ptr = next_token(&input, " \t");	if (ptr == NULL)		return;	ptr = next_token(&input, " \t");	if (ptr == NULL)		return;		if (strcmp(ptr, "-p") != 0)		return;#ifdef HAVE_LINUXTHREADS	pid = mainpid;#else	pid = getpid();#endif	n = snprintf((char *)isc_buffer_used(text),		     isc_buffer_availablelength(text),		     "pid: %ld", (long)pid);	/* Only send a message if it is complete. */	if (n < isc_buffer_availablelength(text))		isc_buffer_add(text, n);}voidns_os_tzset(void) {#ifdef HAVE_TZSET	tzset();#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -