tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Updatding mtime on truncating zero sized files



I ran into a strange issue: when starting firefox I got greeted with
a "It looks like you have not used firefox for a while ..." message,
welcoming me back and offering to prune the profile.

This is supposed to happen when firefox has not been used for 60 days,
but it had been used minutes ago.

Looking at the firefox code I came up with the sample program below.
Firefox assumes that the open or fcntl will update the mtime, but in
-current it does not happen.

Output on netbsd-7:

[/tmp] martin@night-porter > c99 -Wall -O2 test.c
[/tmp] martin@night-porter > ./a.out 
lock file: /tmp/test17943.lock
last lock: 0
last lock: 1483303778
last lock: 1483303780
last lock: 1483303782
last lock: 1483303784
last lock: 1483303786
last lock: 1483303788
last lock: 1483303790
last lock: 1483303792
last lock: 1483303794

and on -current:

lock file: /tmp/test6138.lock
last lock: 0
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716
last lock: 1483303716


Not updating mtime may have some justification in this case, but we better
check standards and avoid being the odd one out for no real good reason.

Martin
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>

time_t
lock_it(const char *file)
{
	struct stat st;
	struct flock lock;

	if (stat(file, &st) != 0)
		st.st_mtime = 0;

	int f = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if (f == -1) return 0;

	memset(&lock, 0, sizeof lock);
	lock.l_type = F_WRLCK;
	lock.l_whence = SEEK_SET;
	fcntl(f, F_SETLK, &lock);
	close(f);

	return st.st_mtime;
}

int
main(int argc, char **argv)
{
	char buf[1000];

	sprintf(buf, "/tmp/test%d.lock", getpid());

	printf("lock file: %s\n", buf);

	for (int i = 0; i < 10; i++) {
		time_t l = lock_it(buf);
		printf("last lock: %ld\n", (long)l);
		sleep(2);
	}

	unlink(buf);

	return 0;
}


Home | Main Index | Thread Index | Old Index