Source-Changes-HG archive

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

[src/trunk]: src/bin/ksh Avoid generating a corrupt history file if multiple ...



details:   https://anonhg.NetBSD.org/src/rev/4fcea9642f4d
branches:  trunk
changeset: 748669:4fcea9642f4d
user:      dsl <dsl%NetBSD.org@localhost>
date:      Sat Oct 31 21:54:01 2009 +0000

description:
Avoid generating a corrupt history file if multiple ksh exit together.
Fixes PR/28912

diffstat:

 bin/ksh/history.c |  15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)

diffs (48 lines):

diff -r 2a93eec4e378 -r 4fcea9642f4d bin/ksh/history.c
--- a/bin/ksh/history.c Sat Oct 31 20:41:00 2009 +0000
+++ b/bin/ksh/history.c Sat Oct 31 21:54:01 2009 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: history.c,v 1.9 2005/06/26 19:09:00 christos Exp $     */
+/*     $NetBSD: history.c,v 1.10 2009/10/31 21:54:01 dsl Exp $ */
 
 /*
  * command history
@@ -19,7 +19,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: history.c,v 1.9 2005/06/26 19:09:00 christos Exp $");
+__RCSID("$NetBSD: history.c,v 1.10 2009/10/31 21:54:01 dsl Exp $");
 #endif
 
 
@@ -740,20 +740,27 @@
 hist_finish()
 {
   static int once;
+  int fd;
   FILE *fh;
   register int i;
   register char **hp;
 
   if (once++)
     return;
+  if (hname == NULL || hname[0] == 0)
+    return;
+
   /* check how many we have */
   i = histptr - histlist;
   if (i >= histsize)
     hp = &histptr[-histsize];
   else
     hp = histlist;
-  if (hname && (fh = fopen(hname, "w")))
-  {
+
+  fd = open(hname, O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK, 0777);
+  /* Remove anything written before we got the lock */
+  ftruncate(fd, 0);
+  if (fd >= 0 && (fh = fdopen(fd, "w"))) {
     for (i = 0; hp + i <= histptr && hp[i]; i++)
       fprintf(fh, "%s%c", hp[i], '\0');
     fclose(fh);



Home | Main Index | Thread Index | Old Index