Safe unlocking

From: Vladimir Zolotykh (gsmith_at_eurocom.od.ua)
Date: 11/28/03


Date: Fri, 28 Nov 2003 12:14:01 +0200

Hi,

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.

If I knew in advance that the signal would be SIGSEGV, I would use my
signal handler function for SIGSEGV. I think that you'll readily agree
that calling SIGNAL(2) for each possible signal number would be rather
clumsy.

Therefore my question: what is the sure-fire way to remove lock file
come well, come ill?

Thanks in advance

-- 
Vladimir Zolotykh


Relevant Pages

  • Re: Safe unlocking
    ... > its locking implemented as constructor and destructor. ... > program terminates properly all works fine and lock file successfully ... And when your program exits the ...
    (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)