Beispiel #1
0
Log *logfopen(char *name, FILE * fp)
{
	Log *l;

	if (!fp) {
		if (!(l = lookup_logfile(name)))
			return NULL;
		l->opencount++;
		return l;
	}

	if (!(l = malloc(sizeof(Log))))
		return NULL;
	if (!(l->st = malloc(sizeof(struct stat)))) {
		free((char *)l);
		return NULL;
	}

	if (!(l->name = SaveStr(name))) {
		free((char *)l->st);
		free((char *)l);
		return NULL;
	}
	l->fp = fp;
	l->opencount = 1;
	l->writecount = 0;
	l->flushcount = 0;
	changed_logfile(l);

	l->next = logroot;
	logroot = l;
	return l;
}
Beispiel #2
0
int logfflush(Log *l)
{
	int r = 0;

	if (!l)
		for (l = logroot; l; l = l->next) {
			if (stolen_logfile(l) && logfile_reopen(l->name, fileno(l->fp), l))
				return -1;
			r |= fflush(l->fp);
			l->flushcount++;
			changed_logfile(l);
	} else {
		if (stolen_logfile(l) && logfile_reopen(l->name, fileno(l->fp), l))
			return -1;
		r = fflush(l->fp);
		l->flushcount++;
		changed_logfile(l);
	}
	return r;
}
Beispiel #3
0
/*
 * XXX
 * write and flush both *should* check the file's stat, if it disappeared
 * or changed, re-open it.
 */
int logfwrite(Log *l, char *buf, size_t n)
{
	int r;

	if (stolen_logfile(l) && logfile_reopen(l->name, fileno(l->fp), l))
		return -1;
	r = fwrite(buf, n, 1, l->fp);
	l->writecount += l->flushcount + 1;
	l->flushcount = 0;
	changed_logfile(l);
	return r;
}
Beispiel #4
0
static int logfile_reopen(char *name, int wantfd, Log *l)
{
	int got_fd;

	close(wantfd);
	if (((got_fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0) || lf_move_fd(got_fd, wantfd) < 0) {
		logfclose(l);
		return -1;
	}
	changed_logfile(l);
	l->st->st_ino = l->st->st_dev = 0;
	return 0;
}
Beispiel #5
0
static int logfile_reopen(char *name, int wantfd, struct logfile *l)
{
	int got_fd;

	close(wantfd);
	if (((got_fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0) || lf_move_fd(got_fd, wantfd) < 0) {
		logfclose(l);
		debug("logfile_reopen: failed for %s\n", name);
		return -1;
	}
	changed_logfile(l);
	l->st->st_ino = l->st->st_dev = 0;
	debug("logfile_reopen: %d = %s\n", wantfd, name);
	return 0;
}