Welcome to NYCU CSIT Mirror site

Locking: libmount Reference Manual

Locking

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

Functions

Types and Values

struct libmnt_lock

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.

Functions

mnt_free_lock ()

void
mnt_free_lock (struct libmnt_lock *ml);

Deallocates mnt_lock.

Parameters

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 fcntl(2)) b) link() failed: wait (max 30s) on F_SETLKW lock, goto 2.

Note that when the lock is used by mnt_update_table() interface then libmount uses flock() for private library file /run/mount/utab. The fcntl(2) is used only for backwardly compatible stuff like /etc/mtab.

Parameters

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);

Parameters

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)).

Parameters

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.

Parameters

ml

struct libmnt_lock handler

 

enable

TRUE/FALSE

 

Returns

<0 on error, 0 on success.

Types and Values

struct libmnt_lock

struct libmnt_lock;

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