Пример #1
0
void cfg_p11path (char *item, int itemno, char *value) {
#ifdef DEBUG
	fprintf (stdout, "DEBUG: DECLARE %s AS %s\n", item, value);
#endif
	cfg_setvar (item, itemno, value);
	//TODO:WHY?// free_p11pin ();
}
Пример #2
0
void cfg_pidfile (char *item, int itemno, char *value) {
	static int fh = 0;
	if (fh) {
		fprintf (stderr, "You can specify only one PID file\n");
		exit (1);
	}
	cfg_setvar (item, CFGVAR_DAEMON_PIDFILE, value);
	atexit (unlink_pidfile);
	fh = open (value, O_RDWR | O_CREAT, 0664);
	char pidbuf [10];
	if (fh < 0) {
		perror ("Failed to open PID file");
		exit (1);
	}
retry:
	if (flock (fh, LOCK_EX | LOCK_NB) != 0) {
		if (errno == EWOULDBLOCK) {
			pid_t oldpid;
			bzero (pidbuf, sizeof (pidbuf));
			read (fh, pidbuf, sizeof (pidbuf)-1);
			oldpid = atoi (pidbuf);
			if (kill_old_pid) {
				if (kill (oldpid, SIGHUP) == 0) {
					fprintf (stderr, "Sent hangup to old daemon with PID %s\n", pidbuf);
					sleep (1);
				}
				lseek (fh, 0, SEEK_SET);
				errno = 0;
				goto retry;
			}
			if (kill (oldpid, 0) != -1) {
				fprintf (stderr, "Another daemon owns the PID file: process %s", pidbuf);
				exit (1);
			}
		} else {
			perror ("Failed to own the PID file");
			exit (1);
		}
	}
	snprintf (pidbuf, sizeof (pidbuf)-1, "%d\n", getpid ());
	if (write (fh, pidbuf, strlen (pidbuf)) != strlen (pidbuf)) {
		perror ("Failed to write all bytes to PID file");
		exit (1);
	}
	ftruncate (fh, strlen (pidbuf));
	fsync (fh);
	//
	// Note: The file remains open -- to sustain the flock on it
	//
}