leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

lockfile.c File Reference
#include "leafnode.h"
#include "ln_log.h"
#include "critmem.h"
#include "validatefqdn.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
Include dependency graph for lockfile.c:

Go to the source code of this file.

Functions

static nlink_t fd_st_nlink (const int fd)
 
static int lock_is_stale (const char *const name, const int quiet)
 
int safe_mkstemp (char *templ)
 
int try_lock (unsigned long timeout)
 
int handover_lock (pid_t pid)
 

Detailed Description

library module to safely create a lock file.

Author
Matthias Andree
Date
2001 - 2006

Copyright (C) 2001 - 2006 Matthias Andree matth.nosp@m.ias..nosp@m.andre.nosp@m.e@gm.nosp@m.x.de

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Definition in file lockfile.c.

Function Documentation

◆ fd_st_nlink()

static nlink_t fd_st_nlink ( const int  fd)
static

get hard link count of open file pointed to by filedes. uses fstat(2)

Returns
0 in case of trouble (which is also logged), the count of hard links otherwise

NOTE: you cannot distinguish "fstat error" from "deleted file".

Parameters
fdopen file descriptor

Definition at line 52 of file lockfile.c.

References ln_log(), LNLOG_CTOP, and LNLOG_SERR.

Referenced by try_lock().

◆ handover_lock()

int handover_lock ( pid_t  pid)

Tries to hand over lock to the process with the given pid.

Returns
0 for success, nonzero for failure – check errno for details in case of error.

Definition at line 327 of file lockfile.c.

References critmalloc(), fqdn, lockfile, and writes().

Referenced by main().

◆ lock_is_stale()

static int lock_is_stale ( const char *const  name,
const int  quiet 
)
static

Check if the lock file given by "name" is stale and if so, erase it. A lock is stale when the process to which it belongs, is dead.

Bug:
cannot detect if lock files held by other hosts are stale.
Returns
  • -1 for error "failure"
  • 0 if lock file is still in use or held by another host "failure"
  • 1 if lock file is stale and has been erased "success"
Parameters
namefile name of lock file
quietquiet flag

Definition at line 74 of file lockfile.c.

References fqdn, len, ln_log(), LNLOG_CTOP, LNLOG_SERR, LNLOG_SNOTICE, name, and quiet.

Referenced by try_lock().

◆ safe_mkstemp()

int safe_mkstemp ( char *  templ)

Safe mkstemp replacement.
Ensures the file is only read- and writable by its owner; some systems create these with 0777 or 0666 permissions.

Parameters
templtemplate to build filename upon, as for mkstemp

Definition at line 163 of file lockfile.c.

References mkstemp().

Referenced by buildpseudoart(), and try_lock().

◆ try_lock()

int try_lock ( unsigned long  timeout)

Try to set a lockfile, blocking or non-blocking. Checks if the lockfile exists and is active.

requires: atomic link(2) syscall.

features:

  • NFS safe (but leafnode does not work distributed)
  • stale detection by PID in lock file (may be fooled)
Bug:
Cannot remove stale lock on other machine.
Bug:
Stale detection may be fooled if another process has been assigned the PID that the last caller had.
Returns
  • 0: if locking succeeded
  • 1: if locking failed because the lock is held by someone else and isn't stale
  • -1: for other errors
Parameters
timeoutTimeout, if nonzero, wait at most this many seconds.

Definition at line 200 of file lockfile.c.

References critmalloc(), debugmode, fd_st_nlink(), fqdn, is_validfqdn(), ln_log(), LNLOG_CTOP, LNLOG_SCRIT, LNLOG_SERR, lock_is_stale(), lockfile, quiet, safe_mkstemp(), timeout, verbose, and writes().

Referenced by main().