Ejemplo n.º 1
0
/* Stevens. */
void daemonize(void)
{
	pid_t pid;

	/* Separate from our parent via fork, so init inherits us. */
	if ((pid = fork()) < 0)
		barf_perror("Failed to fork daemon");
	if (pid != 0)
		exit(0);

	/* Session leader so ^C doesn't whack us. */
	setsid();

	/* Let session leader exit so child cannot regain CTTY */
	if ((pid = fork()) < 0)
		barf_perror("Failed to fork daemon");
	if (pid != 0)
		exit(0);

	/* Move off any mount points we might be in. */
	if (chdir("/") == -1)
		barf_perror("Failed to chdir");

	/* Discard our parent's old-fashioned umask prejudices. */
	umask(0);
}
Ejemplo n.º 2
0
void write_pidfile(const char *pidfile)
{
	char buf[100];
	int len;
	int fd;

	fd = open(pidfile, O_RDWR | O_CREAT, 0600);
	if (fd == -1)
		barf_perror("Opening pid file %s", pidfile);

	/* We exit silently if daemon already running. */
	if (lockf(fd, F_TLOCK, 0) == -1)
		exit(0);

	len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
	if (write(fd, buf, len) != len)
		barf_perror("Writing pid file %s", pidfile);
}
Ejemplo n.º 3
0
void finish_daemonize(void)
{
	int devnull = open("/dev/null", O_RDWR);
	if (devnull == -1)
		barf_perror("Could not open /dev/null\n");
	dup2(devnull, STDIN_FILENO);
	dup2(devnull, STDOUT_FILENO);
	dup2(devnull, STDERR_FILENO);
	close(devnull);
	xprintf = trace;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
	TDB_DATA key;
	TDB_CONTEXT *tdb;

	if (argc != 2)
		barf("Usage: xs_tdb_dump <tdbfile>");

	tdb = tdb_open(talloc_strdup(NULL, argv[1]), 0, 0, O_RDONLY, 0);
	if (!tdb)
		barf_perror("Could not open %s", argv[1]);

	key = tdb_firstkey(tdb);
	while (key.dptr) {
		TDB_DATA data;
		struct record_hdr *hdr;

		data = tdb_fetch(tdb, key);
		hdr = (void *)data.dptr;
		if (data.dsize < sizeof(*hdr))
			fprintf(stderr, "%.*s: BAD truncated\n",
				(int)key.dsize, key.dptr);
		else if (data.dsize != total_size(hdr))
			fprintf(stderr, "%.*s: BAD length %i for %i/%i/%i (%i)\n",
				(int)key.dsize, key.dptr, (int)data.dsize,
				hdr->num_perms, hdr->datalen,
				hdr->childlen, total_size(hdr));
		else {
			unsigned int i;
			char *p;

			printf("%.*s: ", (int)key.dsize, key.dptr);
			for (i = 0; i < hdr->num_perms; i++)
				printf("%s%c%i",
				       i == 0 ? "" : ",",
				       perm_to_char(hdr->perms[i].perms),
				       hdr->perms[i].id);
			p = (void *)&hdr->perms[hdr->num_perms];
			printf(" %.*s\n", hdr->datalen, p);
			p += hdr->datalen;
			for (i = 0; i < hdr->childlen; i += strlen(p+i)+1)
				printf("\t-> %s\n", p+i);
		}
		key = tdb_nextkey(tdb, key);
	}
	return 0;
}
Ejemplo n.º 5
0
void init_pipe(int reopen_log_pipe[2])
{
	if (pipe(reopen_log_pipe)) {
		barf_perror("pipe");
	}
}