9front - general discussion about 9front
 help / color / mirror / Atom feed
From: cinap_lenrek@felloff.net
To: 9front@9front.org
Subject: Re: [9front] Re: [9front] Re: [9front] Dealing with log files…
Date: Mon, 18 Oct 2021 21:40:33 +0200	[thread overview]
Message-ID: <E27A20967E3BEAFF238356DEB777B2ED@felloff.net> (raw)
In-Reply-To: <B693CFAA-4337-4B08-9F42-7CD9B227E3D4@quintile.net>

> i dont get this, plan9 services all (to my knowledge) open and
> close the logfile on each message so you can just rename the logfile
> and touch and chmod a new one.

interesting point.

according to the code, it keeps the fd around, but it checks 
for each message if the file name or qid changed... but apparently
only for some kind of race condition when the fd got closed and
reopand after a rfork(RFCFD)

if the dirstat fails, it will also reopen, but leak the old file
descriptors (as it assumes it is the fd realloc race).

the result is indeed that the file gets reopend when you rename
the old file and create a new one.

--
cinap

PS: /sys/src/libc/9sys/syslog.c

/*
 * Print
 *  sysname: time: mesg
 * on /sys/log/logname.
 * If cons or log file can't be opened, print on the system console, too.
 */
void
syslog(int cons, char *logname, char *fmt, ...)
{
	char buf[1024];
	char *ctim, *p;
	va_list arg;
	int n;
	Dir *d;
	char err[ERRMAX];

	err[0] = '\0';
	errstr(err, sizeof err);
	lock(&sl);

	/*
	 *  paranoia makes us stat to make sure a fork+close
	 *  hasn't broken our fd's
	 */
	d = dirfstat(sl.fd);
	if(sl.fd < 0 || sl.name == nil || strcmp(sl.name, logname) != 0 ||
	   !eqdirdev(d, sl.d)){
		free(sl.name);
		sl.name = strdup(logname);
		if(sl.name == nil)
			cons = 1;
		else{
			free(sl.d);
			sl.d = nil;
			_syslogopen();
			if(sl.fd < 0)
				cons = 1;
			else
				sl.d = dirfstat(sl.fd);
		}
	}
	free(d);
	if(cons){
		d = dirfstat(sl.consfd);
		if(sl.consfd < 0 || !eqdirdev(d, sl.consd)){
			free(sl.consd);
			sl.consd = nil;
			sl.consfd = open("#c/cons", OWRITE|OCEXEC);
			if(sl.consfd >= 0)
				sl.consd = dirfstat(sl.consfd);
		}
		free(d);
	}

	if(fmt == nil){
		unlock(&sl);
		return;
	}

	ctim = ctime(time(0));
	p = buf + snprint(buf, sizeof(buf)-1, "%s ", sysname());
	strncpy(p, ctim+4, 15);
	p += 15;
	*p++ = ' ';
	errstr(err, sizeof err);
	va_start(arg, fmt);
	p = vseprint(p, buf+sizeof(buf)-1, fmt, arg);
	va_end(arg);
	*p++ = '\n';
	n = p - buf;

	if(sl.fd >= 0){
		seek(sl.fd, 0, 2);
		write(sl.fd, buf, n);
	}

	if(cons && sl.consfd >=0)
		write(sl.consfd, buf, n);

	unlock(&sl);
}

  reply	other threads:[~2021-10-19  9:18 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-16 20:06 igor
2021-10-16 20:52 ` umbraticus
2021-10-16 21:01 ` [9front] " Stanley Lieber
2021-10-17  8:29 ` Lyndon Nerenberg (VE7TFX/VE6BBM)
2021-10-17  9:06   ` umbraticus
2021-10-17 10:30   ` igor
2021-10-17 14:19   ` [9front] " Stanley Lieber
2021-10-17 12:29 ` Rodrigo G. López
2021-10-17 13:50   ` [9front] " hiro
2021-10-18  7:03     ` rgl
2021-10-18  8:51       ` sirjofri
2021-10-18 18:49         ` Steve Simon
2021-10-18 19:40           ` cinap_lenrek [this message]
2021-10-17 18:15 ` smj
2021-10-18  7:35   ` [9front] " Silas McCroskey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E27A20967E3BEAFF238356DEB777B2ED@felloff.net \
    --to=cinap_lenrek@felloff.net \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).