home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES1.ZIP / LIB / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-28  |  8.7 KB  |  267 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.  *    $Id: lock.c 1.9 1993/10/28 12:19:01 ahd Exp $
  17.  *
  18.  *    Revision history:
  19.  *    $Log: lock.c $
  20.  *     Revision 1.9  1993/10/28  12:19:01  ahd
  21.  *     Cosmetic time formatting twiddles and clean ups
  22.  *
  23.  *     Revision 1.8  1993/04/11  00:31:04  ahd
  24.  *     Global edits for year, TEXT, etc.
  25.  *
  26.  *     Revision 1.8  1993/04/11  00:31:04  ahd
  27.  *     Global edits for year, TEXT, etc.
  28.  *
  29.  *     Revision 1.7  1993/03/24  01:57:30  ahd
  30.  *     Add string.h include
  31.  *
  32.  *     Revision 1.6  1993/03/06  22:48:23  ahd
  33.  *     Allow nested locks
  34.  *
  35.  * Revision 1.5  1992/11/28  19:51:16  ahd
  36.  * Issue lock based on lock file existence to avoid FOPEN retry loop
  37.  *
  38.  * Revision 1.4  1992/11/19  02:58:54  ahd
  39.  * drop rcsid
  40.  *
  41.  * Revision 1.3  1992/11/19  02:54:47  ahd
  42.  * Revision 1.2  1992/11/17  13:47:30  ahd
  43.  * Do not buffer lock file
  44.  *
  45.  * Revision 1.1  1992/11/16  05:00:26  ahd
  46.  * Initial revision
  47.  *
  48.  */
  49.  
  50.  
  51. /*--------------------------------------------------------------------*/
  52. /*                   Standard library include files                   */
  53. /*--------------------------------------------------------------------*/
  54.  
  55. #include <stdio.h>
  56. #include <time.h>
  57. #include <sys/types.h>
  58. #include <process.h>
  59. #include <string.h>
  60. #include <io.h>
  61.  
  62. /*--------------------------------------------------------------------*/
  63. /*                    UUPC/extended include files                     */
  64. /*--------------------------------------------------------------------*/
  65.  
  66. #include "lib.h"
  67. #include "dater.h"
  68. #include "stater.h"
  69. #include "lock.h"
  70. #include "hlib.h"
  71. #include "import.h"
  72.  
  73. /*--------------------------------------------------------------------*/
  74. /*                      Define current file name                      */
  75. /*--------------------------------------------------------------------*/
  76.  
  77. currentfile();
  78.  
  79. /*--------------------------------------------------------------------*/
  80. /*                          Local variables                           */
  81. /*--------------------------------------------------------------------*/
  82.  
  83. static FILE *locket = NULL;
  84. static char lname[FILENAME_MAX];
  85.  
  86. boolean locked = FALSE;
  87.  
  88. /*--------------------------------------------------------------------*/
  89. /*    L o c k S y s t e m                                             */
  90. /*                                                                    */
  91. /*    Get the lock for a system                                       */
  92. /*--------------------------------------------------------------------*/
  93.  
  94. boolean LockSystem( const char *system , long program )
  95. {
  96.    time_t age;
  97.    char fname[FILENAME_MAX];
  98.    char *extension;
  99.  
  100. /*--------------------------------------------------------------------*/
  101. /*                Don't lock unless in multitask mode                 */
  102. /*--------------------------------------------------------------------*/
  103.  
  104.    if ( ! bflag[ F_MULTITASK ] )
  105.       return TRUE;
  106.  
  107. /*--------------------------------------------------------------------*/
  108. /*                Verify our lock is not already in use               */
  109. /*--------------------------------------------------------------------*/
  110.  
  111.    if ( locket != NULL )
  112.    {
  113.       printmsg(0,
  114.          "LockSystem: Attempt to lock %s with lock file %s already open",
  115.          system,
  116.          lname );
  117.       panic();
  118.    } /* if */
  119.  
  120. /*--------------------------------------------------------------------*/
  121. /*                        Try to get the lock                         */
  122. /*--------------------------------------------------------------------*/
  123.  
  124.    switch( program )
  125.    {
  126.       case B_NEWS:
  127.          extension = "LCN";
  128.          break;
  129.  
  130.       case B_UUSTAT:
  131.          extension = "LCS";
  132.          break;
  133.  
  134.       case B_UUCICO:
  135.          extension = "LCK";
  136.          break;
  137.  
  138.       case B_UUXQT:
  139.          extension = "LCX";
  140.          break;
  141.  
  142.       default:
  143.          extension = "LCZ";
  144.          break;
  145.    } /* switch */
  146.  
  147.    sprintf( fname,
  148.             "%s/locks.lck/%.8s.%s",
  149.             E_spooldir,
  150.             system,
  151.             extension );
  152.  
  153.    importpath( lname, fname, system );
  154.  
  155. /*--------------------------------------------------------------------*/
  156. /*    Determine if the lock exists, and unlink it if so.  If this     */
  157. /*    fails, we can't get the lock, so we return gracefully.          */
  158. /*    (This allows to bypass the "helpful" FOPEN retries of the       */
  159. /*    failed open.                                                    */
  160. /*--------------------------------------------------------------------*/
  161.  
  162.    if ( access( lname, 0 ) || !unlink( lname ))
  163.       locket = FOPEN( lname, "w",TEXT_MODE );
  164.  
  165.    if ( locket == NULL )
  166.    {
  167.       long size;
  168.  
  169.       age = stater( lname, &size );
  170.  
  171.       printmsg(1, "System %s already locked since %s",
  172.                system,
  173.                (size > 0) ? dater( age, NULL ) : "UNKNOWN" );
  174.       return FALSE;
  175.    }
  176.  
  177.    setvbuf( locket, NULL, _IONBF, 0);
  178.  
  179.    time( &age );
  180.    fprintf( locket, "%s locked by process %ld at %.24s",
  181.                     system, (long) getpid(), ctime( &age ));
  182.    fflush( locket );          /* Force the file to exist on disk  */
  183.  
  184. /*--------------------------------------------------------------------*/
  185. /*     We don't close the file, since the open file *is* the lock     */
  186. /*--------------------------------------------------------------------*/
  187.  
  188.    locked = TRUE;
  189.    return TRUE;
  190.  
  191. } /* LockSystem */
  192.  
  193. /*--------------------------------------------------------------------*/
  194. /*    U n l o c k S y s t e m                                         */
  195. /*                                                                    */
  196. /*    Release a lock created by LockSystem                            */
  197. /*--------------------------------------------------------------------*/
  198.  
  199. void UnlockSystem( void )
  200. {
  201.  
  202. /*--------------------------------------------------------------------*/
  203. /*                Don't lock unless in multitask mode                 */
  204. /*--------------------------------------------------------------------*/
  205.  
  206.    if ( ! bflag[ F_MULTITASK ] )
  207.       return;
  208.  
  209. /*--------------------------------------------------------------------*/
  210. /*                       Verify we hold a lock                        */
  211. /*--------------------------------------------------------------------*/
  212.  
  213.    if ( locket == NULL )
  214.    {
  215.       printmsg(0,"UnlockSystem: No lock held");
  216.       panic();
  217.    } /* if */
  218.  
  219. /*--------------------------------------------------------------------*/
  220. /*                          Release the lock                          */
  221. /*--------------------------------------------------------------------*/
  222.  
  223.    fclose( locket );
  224.    locket = NULL;
  225.    locked = FALSE;
  226.  
  227.    unlink( lname );
  228.  
  229. } /* UnlockSystem */
  230.  
  231. /*--------------------------------------------------------------------*/
  232. /*       P u s h L o c k                                              */
  233. /*                                                                    */
  234. /*       Save lock status in order to perform second lock             */
  235. /*--------------------------------------------------------------------*/
  236.  
  237.  
  238. void PushLock( LOCKSTACK *top )
  239. {
  240.    top->locket = locket;
  241.    if ( locket !=  NULL )
  242.    {
  243.       strcpy( top->lname, lname );
  244.       locket =  NULL;
  245.    }
  246.    locked = FALSE;
  247.  
  248. }
  249.  
  250. /*--------------------------------------------------------------------*/
  251. /*       P o p L o c k                                                */
  252. /*                                                                    */
  253. /*       Restore previous lock information                            */
  254. /*--------------------------------------------------------------------*/
  255.  
  256. void PopLock( LOCKSTACK *top )
  257. {
  258.    locket = top->locket;
  259.  
  260.    if ( locket !=  NULL )
  261.    {
  262.       strcpy( lname, top->lname );
  263.       locked = TRUE;
  264.    }
  265.  
  266. } /* PopLock */
  267.