Welcome to NYCU CSIT Mirror site

Locking

Locking

Locking — locking methods for /etc/mtab or another libmount files

Synopsis

struct              libmnt_lock;
void                mnt_free_lock                       (struct libmnt_lock *ml);
int                 mnt_lock_file                       (struct libmnt_lock *ml);
struct libmnt_lock * mnt_new_lock                       (const char *datafile,
                                                         pid_t id);
void                mnt_unlock_file                     (struct libmnt_lock *ml);
int                 mnt_lock_block_signals              (struct libmnt_lock *ml,
                                                         int enable);

Description

The mtab lock is backwards compatible with the standard linux /etc/mtab locking. Note, it's necessary to use the same locking schema in all applications that access the file.

Details

struct libmnt_lock

struct libmnt_lock;

Stores information about the locked file (e.g. /etc/mtab)


mnt_free_lock ()

void                mnt_free_lock                       (struct libmnt_lock *ml);

Deallocates mnt_lock.

ml :

struct libmnt_lock handler

mnt_lock_file ()

int                 mnt_lock_file                       (struct libmnt_lock *ml);

Creates a lock file (e.g. /etc/mtab~). Note that this function may use alarm().

Your application always has to call mnt_unlock_file() before exit.

Traditional mtab locking scheme:

1. create linkfile (e.g. /etc/mtab~.$PID) 2. link linkfile --> lockfile (e.g. /etc/mtab~.$PID --> /etc/mtab~) 3. a) link() success: setups F_SETLK lock (see fcnlt(2)) b) link() failed: wait (max 30s) on F_SETLKW lock, goto 2.

ml :

pointer to struct libmnt_lock instance

Returns :

0 on success or negative number in case of error (-ETIMEOUT is case of stale lock file).

mnt_new_lock ()

struct libmnt_lock * mnt_new_lock                       (const char *datafile,
                                                         pid_t id);

datafile :

the file that should be covered by the lock

id :

unique linkfile identifier or 0 (default is getpid())

Returns :

newly allocated lock handler or NULL on case of error.

mnt_unlock_file ()

void                mnt_unlock_file                     (struct libmnt_lock *ml);

Unlocks the file. The function could be called independently of the lock status (for example from exit(3)).

ml :

lock struct

mnt_lock_block_signals ()

int                 mnt_lock_block_signals              (struct libmnt_lock *ml,
                                                         int enable);

Block/unblock signals when the lock is locked, the signals are not blocked by default.

ml :

struct libmnt_lock handler

enable :

TRUE/FALSE

Returns :

<0 on error, 0 on success.