Quoting from Advanced Programming in the UNIX Environnement
(page 505), Section 13.6:
We need to truncate the file, because the previous instance of the daemon might have had a process ID larger than ours, with a larger string length. For examples , if the previous instance of the daemon was process ID 12345, and the new instance is process ID 9999, when we write the process ID to the file, we will be left with 99995 in the file. Truncating the file prevents data from the previous daemon appearing as if it applies to the current daemon.
This comment was made on this function:
already_running(void)
{
int fd;
char buf[16];
fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
if (fd < 0) {
syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno));
exit(1);
}
if (lockfile(fd) < 0) {
if (errno == EACCES || errno == EAGAIN) {
close(fd);
return(1);
}
syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));
exit(1);
}
ftruncate(fd, 0);
sprintf(buf, "%ld", (long)getpid());
write(fd, buf, strlen(buf)+1);
return 0;
}
I don't understand how is this behavior possible, and how file truncation prevent that behavior from happening. Could somebody explain this?
Thanks for answering!
It is used to extend or reduce the file size. Truncating a file is much quicker and simpler without modifying the permissions and ownership of the file. The truncated size depends on the original size of the file; the extra data will be lost if the file size is greater than the specified size.
Truncation allows you to search the "root" form of a word with all its different endings by adding a symbol to the end of a word. Example: typing in bank* will retrieve results with these words: bank, banks, banking, bankers, bankruptcy The most common truncation symbol is the asterisk * but databases vary.
Python File truncate() Method The truncate() method resizes the file to the given number of bytes. If the size is not specified, the current position will be used.
The truncate function changes the size of filename to length . If length is shorter than the previous length, data at the end will be lost. The file must be writable by the user to perform this operation. If length is longer, holes will be added to the end.
In the above example, the file is initially 5 bytes long. When you open it for writing, and write the string "9999" to it without truncating, it will just overwrite the first 4 bytes, and leave the 5th byte in place. Hence the file will read "99995". Truncating sets the file length to 0, effectively erasing the previous content.
Hellmar has already provided the answer to your question -- but in the interest of shortening the code (who doesn't like code golf?), you could simplify the open call to:
already_running(void)
{
int fd;
char buf[16];
fd = open(LOCKFILE, O_RDWR|O_CREAT|O_TRUNC, LOCKMODE);
...
The addition of O_TRUNC to the flags will cause the file to be truncated. http://linux.die.net/man/2/open
If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With