








                     Locking in C News


                       Henry Spencer

                      Dept. of Zoology
                   University of Toronto





Several parts of C News need some way of  locking  parts  of
the  news  subsystem  against concurrent execution.  Various
system-specific locking system calls exist, but none of them
is  truly  portable, and most of them provide far more func-
tionality than we need.

C News locking uses the _l_i_n_k(2) system call  and  pre-agreed
names.  _L_i_n_k has the necessary characteristic for safe lock-
ing: it is an atomic test-and-set  operation.   Furthermore,
it exists in all Unixes.

All locks are created in the NEWSCTL directory  (see  _C_o_n_f_i_-
_g_u_r_a_t_i_o_n _M_e_c_h_a_n_i_s_m_s _i_n _C _N_e_w_s for where this directory is to
be found and how programs can determine this) and have names
starting  with  `LOCK'.   To  acquire a lock, first create a
temporary file in NEWSCTL with a name  of  the  form  `L._n',
where  _n  is  your  process id.  You are urged to also write
your process id, in decimal ASCII,  into  this  file.   Then
attempt  to  link  the temporary file to `LOCK_x', where _x is
chosen based on what sort of locking you wish to do.  Exist-
ing lock names are:

center; ll.   LOCK    relaynews,  modifications  to  control
files  LOCKinput       input  subsystem  processing  spooled
input     LOCKbatch       batcher     preparing      batches
LOCKexpire      expire expiring articles

If the link fails, sleep and try  again.   If  it  succeeds,
proceed.   The  temporary file may be removed then or at the
same time as the lock is removed.  Programs are expected  to
make a determined effort to remove lock files when they ter-
minate, normally or as a result of signals.

Shell programs have an additional problem in that  System  V
has  broken _l_n(1) so that it removes a pre-existing destina-
tion file.  C News therefore provides a pure, simple locking
program  under the name NEWSBIN/newslock (if the recommenda-
tions in _D_i_r_e_c_t_o_r_y _L_a_y_o_u_t _a_n_d _P_A_T_H _i_n _C _N_e_w_s  are  followed,
this  will  automatically  be  in  the  search path of shell



                      22 April 1989





                           - 2 -


programs).   Usage  is  `newslock tempfile lockfile';   exit
status  is  0 for success, 1 for failure, 2 for wrong number
of arguments.  No messages are printed for  normal  failure,
so no redirection of output is needed.

A suitable locking procedure for  a  shell  file  using  the
standard configuration facilities is:

        lock="$NEWSCTL/LOCKxxx"         # modify name as appropriate
        ltemp="$NEWSCTL/L.$$"
        echo $$ >$ltemp
        trap "rm -f $ltemp ; exit 0" 0 1 2 15
        while true
        do
                if newslock $ltemp $lock
                then
                        trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
                        break
                fi
                sleep 30
        done

A  template  of  this  form  can  be  found  in   the   file
_n_e_w_s_l_o_c_k._s_h.

Although there are various thorny questions associated  with
breaking  locks by dead programs, reboot is a time when sur-
viving locks are definitely invalid.   (Although  there  are
problems  even  here if a networked group of systems are not
rebooted as a unit.) For this and other  reasons,  a  system
running C News should execute NEWSCTL/bin/newsboot at reboot
time (e.g. from /_e_t_c/_r_c).

























                      22 April 1989


