示例#1
0
文件: os.c 项目: 274914765/C
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, first_time);
    if (lockfile == NULL)
    {
        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);
}
示例#2
0
void
ns_os_shutdown(void) {
	closelog();
	cleanup_pidfile();
}
示例#3
0
void
ns_os_shutdown(void) {
	closelog();
	cleanup_pidfile();
	ntservice_shutdown();	/* This MUST be the last thing done */
}
示例#4
0
void
ns_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;
	}

	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);
}
示例#5
0
void
ns_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 *, ...);
	unsigned int mode;
	char *slash;
	int n;

	/*
	 * 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);

	/*
	 * Make the containing directory if it doesn't exist.
	 */
	slash = strrchr(pidfile, '/');
	if (slash != NULL && slash != pidfile) {
		*slash = '\0';
		mode = S_IRUSR | S_IWUSR | S_IXUSR;	/* u=rwx */
		mode |= S_IRGRP | S_IXGRP;		/* g=rx */
		mode |= S_IROTH | S_IXOTH;		/* o=rx */
		n = mkdir(pidfile, mode);
		if (n == -1 && errno != EEXIST) {
			isc__strerror(errno, strbuf, sizeof(strbuf));
			(*report)("couldn't mkdir %s': %s", filename,
				  strbuf);
			free(pidfile);
			pidfile = NULL;
			return;
		}
		*slash = '/';
	}

	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);
}