Re: Safe unlocking

Jens.Toerring_at_physik.fu-berlin.de
Date: 11/28/03


Date: 28 Nov 2003 12:26:41 GMT

Vladimir Zolotykh <gsmith@eurocom.od.ua> wrote:
> Suppose my program starts with creating and locking the file as the
> follows:

> if ((fd = open(file_name, O_CREAT | O_WRONLY | O_EXCL, 0644)) < 0
> || flock (fd, LOCK_EX | LOCK_NB) < 0)
> abort();

> Actually this is C++ program and creating and removing lock file and
> its locking implemented as constructor and destructor. When the
> program terminates properly all works fine and lock file successfully
> removed. The problem arises when the program is terminated by a
> signal. For example if the program is terminated by SIGSEGV or SIGABRT
> the destructor is not called and the lock file remains locked after
> the program termination.

Since when flock() does create a lock file? Unless the flock() function
is something you wrote yourself instead of using the system call there
won't be a lock file, the kernel sets some bits in the open file table
entry for the file. And when your program exits (by whatever way) the
file gets closed automatically and the lock vanishes leaving no traces.

If you really have a lock file you need to get rid of register an
atexit() function to be called when the function exits (but, of
course, this won't be called on a SIGKILL signal, but than that's
a case where also a signal handler woudn't help you).

                                     Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.physik.fu-berlin.de/~toerring


Relevant Pages

  • Safe unlocking
    ... Suppose my program starts with creating and locking the file as the ... program terminates properly all works fine and lock file successfully ... For example if the program is terminated by SIGSEGV or SIGABRT ...
    (comp.unix.misc)
  • Re: deleting old mails
    ... I tried mail/archivemail but it cannot create it's lock file ... A few words about locking. ... no mailbox is ever locked. ...
    (freebsd-questions)
  • lckpwdf question: possible bug?
    ... locking works correctly. ... And lckpwdf correctly creates a lock file /etc/.pwd.lock. ... hit enter to quit: ... # echo $? ...
    (comp.os.linux.security)
  • lckpwdf question: possible bug?
    ... As the following shows locking works correctly. ... And lckpwdf correctly creates a lock file /etc/.pwd.lock. ... hit enter to quit: ... # echo $? ...
    (comp.os.linux.development.apps)
  • Re: POP3, lock files and procmail
    ... You are misinterpreting the text in RFC 1939. ... locking mechanism other than the .lock file. ... The UW IMAP toolkit uses the main mailbox, but has its own locking ...
    (comp.os.linux.networking)