bug-bash
[Top][All Lists]
Advanced

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

Re: History file clobbered by multiple simultaneous exits


From: Chet Ramey
Subject: Re: History file clobbered by multiple simultaneous exits
Date: Fri, 19 Jul 2013 08:20:49 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130509 Thunderbird/17.0.6

On 7/19/13 3:40 AM, Geoff Kuenning wrote:

> Now, to clarify: the difficulty isn't that bash overwrites the history
> file.  That's the default behavior, and it's to be expected.  If a user
> opens three shells (in any fashion) and then successively types "exit"
> in each, it's to be expected that only the last one's history would be
> written to HISTFILE.  And that's what I, personally, want to happen.
> 
> But right now, if all three of those shells exit simultaneously--for
> whatever reason--there is a significant probability that the history
> file will end up zero-length.  That's not theoretical; I've experienced
> it multiple times.  And that's a bug, plain and simple.  And I suspect
> that it can be fixed on 99+% of all deployed systems by just adding
> O_EXCL to the open.

Let's think about why and how this could happen. Maybe that will give some
insight into how to solve the problem.

The current (bash-4.3-devel) code works something like this, assuming no
errors (lib/readline/histfile.c:history_do_write()):

rename (histfile, histfile~)
open file with O_CREAT|O_TRUNC
malloc buffer large enough to hold all history data
write all of the history entries in one write(2) call
close file
unlink (histfile~)

The bash-4.2 code works the same way except that it does not back up the
history  file.  Each shell does the same thing when it exits, assuming
histappend is not set, as in your configuration.

There are a couple of ways the history file can end up zero-length: the
malloc can fail, or the write can fail.  In bash-4.2, it's too late to do
anything about the truncated history file at that point.  In bash-4.3, the
previous history file will be restored.

The O_EXCL solution could work, to some extent, in bash-4.3.  It won't do
any good in bash-4.2 because it will cause open() to fail under the
perfectly reasonable circumstance of an existing history file.

What have I missed that could cause file truncation due to a race condition?

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]