1LOCKDEV(3)                 Linux Programmer's Manual                LOCKDEV(3)
2
3
4

NAME

6       lockdev,  liblockdev,  dev_testlock, dev_lock, dev_relock, dev_unlock -
7       manage device lockfiles
8

SYNOPSIS

10       #include <lockdev.h>
11
12       pid_t dev_testlock( const char * devname);
13       pid_t dev_lock( const char * devname);
14       pid_t dev_relock( const char * devname, pid_t pid);
15       pid_t dev_unlock( const char * devname, pid_t pid);
16
17       cc [ flag ... ] file ... -llockdev [ library ]
18

DESCRIPTION

20       The lockdev functions act on device locks normally located in /var/lock
21       .   The  lock  is  acquired creating a pair of files hardlinked between
22       them and named after the device name (as mandated by  FSSTND)  and  the
23       device's  major  and  minor numbers (as in SVr4 locks). This permits to
24       circumvent a problem using only the FSSTND lock method  when  the  same
25       device  exists  under different names (for convenience or when a device
26       must be accessable by more than one group of users).
27       The  lock  file  names  are  typically  in  the  form  LCK..ttyS1   and
28       LCK.004.065  ,  but  is provided a way to easily modify them to use the
29       library on different architectures. The content of those files  is  the
30       pid of the process who owns the lock.
31
32       The  dev_testlock() function simply checks if the device is in some way
33       locked and if the owner of the  lock  is  still  active  (otherwise  it
34       removes  the  lock).  It recognise a valid lock even if only one of the
35       two lock files exists (and is owned by an existing process), thus  per‐
36       mitting  a  safe  use of this library together with programs using only
37       FSSTND or SVr4 lock style.
38
39       The dev_lock() function first checks if the device  is  already  locked
40       and  then  tries to acquire the lock building the two lock files. First
41       it creates the file which name contains the major and minor numbers (in
42       SVr4 style), then it creates the file with the device name in its name.
43       This order reduces the clashes with other processes trying to lock  the
44       same device (even with a different name) and using this library. It has
45       no problem with processes that uses only the FSSTND algorithm.
46
47       The dev_relock() function changes the owner of an existing lock; if the
48       pid  of  the old owner is provided, then it checks if the lock was cor‐
49       rectly assigned (otherwise  there  is  the  possibility  of  a  process
50       acquiring  a lock which was owned by another unrelated process). If the
51       device was not locked, locks it.
52
53       The dev_unlock() function removes the existing locks on the device.  If
54       the  pid  of  the  owner of the lock is provided, then it checks if the
55       lock is assigned to that process, avoiding to remove locks assigned  to
56       other existing processes.
57
58

RETURN VALUES

60       All the functions in lockdev library return ZERO on successfull comple‐
61       tion of the function (dev_testlock returns zero if there is no lock  on
62       the  device), otherwise, if the device is currently locked by an exist‐
63       ing process, they return the pid of the process owner of the lock. They
64       return a negative number when some kind of error happens. Actually they
65       all return only (-1).
66
67

DEBUGGING

69       The API has symbols used only for debugging purposis
70
71       int liblockdev_debug
72       void liblockdev_incr_debug( void );
73       void liblockdev_reset_debug( void );
74
75       which can be used when the liblockdev library is compiled with  -DDEBUG
76       flag  as  when  using  make install-dbg , which compiles a debug shared
77       library and installs it under /usr/local/lib/debug (or /usr/lib/debug).
78       The value of the global integer is set to 1 by the  DEBUG  define,  and
79       can  be  set  to a different value passing a flag like -DDEBUG=3 during
80       compilation  of  the  library,  or  setting  the  environment  variable
81       LIBLOCKDEV_DEBUG to the wanted value before executing your program.
82       During  execution of your program, the flag's value can be changed from
83       your program or from another terminal, respectively using the  function
84       liblockdev_incr_debug() , or sending SIGUSR1 to the running process, to
85       increment the value of the  integer  by  one,  or  using  the  function
86       liblockdev_reset_debug()  ,  or sending SIGUSR2 to the running process,
87       to set to zero the value of the global integer.
88       Direct manipulation of  the  global  integer  is  strongly  deprecated,
89       because the data structure of the symbol (actually an integer) could be
90       changed later in some way, or even become a macro.
91
92       The library prints on stdout some informations  like  error  conditions
93       (level  of  1),  normal  termination conditions (2) or function calling
94       (3).
95
96       To use the debug shared library, simply define in your environment  the
97       variable  LD_LIBRARY_PATH=/usr/lib/debug  (or  /usr/local/lib/debug  if
98       built using make install-dbg) and call gdb  or  directly  your  program
99       without  any need to recompile it. As you can check with ldd, your pro‐
100       gram will load the debug library instead of the normal one.
101       Beware that if your program is setuid or setgid, you must  become  root
102       to  let  this  work, because ld.so ignores the LD_LIBRARY_PATH variable
103       for security reasons.
104
105       On Debian  GNU/Linux  systems  exists  a  debug  binary  package  named
106       liblockdev1-dbg  which  installs a shared library built with all debug‐
107       ging options (and the -DDEBUG flag) into /usr/lib/debug .
108
109

FILES

111       /var/lock/LCK..<device>
112       /var/lock/LCK.<major>.<minor>
113       /usr/lib/liblockdev.so.1
114       /usr/lib/debug/liblockdev.so.1
115
116

HISTORY

118       (c) 1997 by Fabrizio Polacco <fab@prosa.it>.
119       This program is free software; you can redistribute it and/or modify it
120       under  the  terms of the GNU Lesser General Public License as published
121       by the Free Software Foundation; version 2 dated June, 1991.
122
123
124
125Linux Manpage                     26 Dec 1997                       LOCKDEV(3)
Impressum