home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XS.ZIP / LIB / LOCK.C < prev    next >
C/C++ Source or Header  |  1992-11-28  |  7KB  |  190 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    l o c k . c                                                     */
  3. /*                                                                    */
  4. /*    Locking functions for UUPC/extended                             */
  5. /*                                                                    */
  6. /*    Copyright (c) 1992 by Kendra Electronic Wonderworks; all        */
  7. /*    rights reserved except those explicitly granted by the          */
  8. /*    UUPC/extended license.                                          */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*                          RCS Information                           */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*
  16.  *    $Header: E:\src\uupc\LIB\RCS\LOCK.C 1.5 1992/11/28 19:51:16 ahd Exp $
  17.  *
  18.  *    Revision history:
  19.  *    $Log: LOCK.C $
  20.  * Revision 1.5  1992/11/28  19:51:16  ahd
  21.  * Issue lock based on lock file existence to avoid FOPEN retry loop
  22.  *
  23.  * Revision 1.4  1992/11/19  02:58:54  ahd
  24.  * drop rcsid
  25.  *
  26.  * Revision 1.3  1992/11/19  02:54:47  ahd
  27.  * Revision 1.2  1992/11/17  13:47:30  ahd
  28.  * Do not buffer lock file
  29.  *
  30.  * Revision 1.1  1992/11/16  05:00:26  ahd
  31.  * Initial revision
  32.  *
  33.  */
  34.  
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                   Standard library include files                   */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include <stdio.h>
  41. #include <time.h>
  42. #include <sys/types.h>
  43. #include <process.h>
  44. #include <io.h>
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                    UUPC/extended include files                     */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. #include "lib.h"
  51. #include "dater.h"
  52. #include "stater.h"
  53. #include "lock.h"
  54. #include "hlib.h"
  55. #include "import.h"
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                      Define current file name                      */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. currentfile();
  62.  
  63. /*--------------------------------------------------------------------*/
  64. /*                          Local variables                           */
  65. /*--------------------------------------------------------------------*/
  66.  
  67. static FILE *locket = NULL;
  68. static char lname[FILENAME_MAX];
  69.  
  70. boolean locked = FALSE;
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*    L o c k S y s t e m                                             */
  74. /*                                                                    */
  75. /*    Get the lock for a system                                       */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. boolean LockSystem( const char *system , long program )
  79. {
  80.    time_t age;
  81.    char fname[FILENAME_MAX];
  82.  
  83. /*--------------------------------------------------------------------*/
  84. /*                Don't lock unless in multitask mode                 */
  85. /*--------------------------------------------------------------------*/
  86.  
  87.    if ( ! bflag[ F_MULTITASK ] )
  88.       return TRUE;
  89.  
  90. /*--------------------------------------------------------------------*/
  91. /*                Verify our lock is not already in use               */
  92. /*--------------------------------------------------------------------*/
  93.  
  94.    if ( locket != NULL )
  95.    {
  96.       printmsg(0,
  97.          "LockSystem: Attempt to lock %s with lock file %s already open",
  98.          system,
  99.          lname );
  100.       panic();
  101.    } /* if */
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*                        Try to get the lock                         */
  105. /*--------------------------------------------------------------------*/
  106.  
  107.    sprintf( fname,
  108.             "%s/locks.lck/%.8s.%s",
  109.             E_spooldir,
  110.             system,
  111.             (program == B_UUXQT ) ? "LCX" : "LCK" );
  112.  
  113.    importpath( lname, fname, system );
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*    Determine if the lock exists, and unlink it if so.  If this     */
  117. /*    fails, we can't get the lock, so we return gracefully.          */
  118. /*    (This allows to bypass the "helpful" FOPEN retries of the       */
  119. /*    failed open.                                                    */
  120. /*--------------------------------------------------------------------*/
  121.  
  122.    if ( access( lname, 0 ) || !unlink( lname ))
  123.       locket = FOPEN( lname, "w", TEXT );
  124.  
  125.    if ( locket == NULL )
  126.    {
  127.       long size;
  128.  
  129.       age = stater( lname, &size );
  130.  
  131.       printmsg(1, "System %s already locked since %s",
  132.                system,
  133.                (size > 0) ? dater( age, NULL ) : "UNKNOWN" );
  134.       return FALSE;
  135.    }
  136.  
  137.    setvbuf( locket, NULL, _IONBF, 0);
  138.  
  139.    time( &age );
  140.    fprintf( locket, "%s locked by process %ld at %s",
  141.                     system, (long) getpid(), ctime( &age ));
  142.    fflush( locket );          /* Force the file to exist on disk  */
  143.  
  144. /*--------------------------------------------------------------------*/
  145. /*     We don't close the file, since the open file *is* the lock     */
  146. /*--------------------------------------------------------------------*/
  147.  
  148.    locked = TRUE;
  149.    return TRUE;
  150.  
  151. } /* LockSystem */
  152.  
  153. /*--------------------------------------------------------------------*/
  154. /*    U n l o c k S y s t e m                                         */
  155. /*                                                                    */
  156. /*    Release a lock created by LockSystem                            */
  157. /*--------------------------------------------------------------------*/
  158.  
  159. void UnlockSystem( void )
  160. {
  161.  
  162. /*--------------------------------------------------------------------*/
  163. /*                Don't lock unless in multitask mode                 */
  164. /*--------------------------------------------------------------------*/
  165.  
  166.    if ( ! bflag[ F_MULTITASK ] )
  167.       return;
  168.  
  169. /*--------------------------------------------------------------------*/
  170. /*                       Verify we hold a lock                        */
  171. /*--------------------------------------------------------------------*/
  172.  
  173.    if ( locket == NULL )
  174.    {
  175.       printmsg(0,"UnlockSystem: No lock held");
  176.       panic();
  177.    } /* if */
  178.  
  179. /*--------------------------------------------------------------------*/
  180. /*                          Release the lock                          */
  181. /*--------------------------------------------------------------------*/
  182.  
  183.    fclose( locket );
  184.    locket = NULL;
  185.    locked = FALSE;
  186.  
  187.    unlink( lname );
  188.  
  189. } /* UnlockSystem */
  190.