home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-14  |  6.6 KB  |  253 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.  
  4.   
  5. */
  6.  
  7. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  8.  
  9.  
  10. #ifndef lint
  11. static char *RCSid = "$Header: /usr/users/freewais/FreeWAIS-0.1/ir/lock.c,v 1.1 1993/02/16 15:05:35 freewais Exp $";
  12. #endif
  13.  
  14. /* Change log:
  15.  * $Log: lock.c,v $
  16.  * Revision 1.1  1993/02/16  15:05:35  freewais
  17.  * Initial revision
  18.  *
  19.  * Revision 1.4  92/05/06  17:33:25  jonathan
  20.  * ?
  21.  * 
  22.  * Revision 1.3  92/04/28  17:05:23  jonathan
  23.  * K&R style definitions.
  24.  * 
  25.  */
  26.  
  27. /* ----------------------------------------------------------------------------
  28.  
  29.     COPYRIGHT   1992 Thinking Machines Corporation
  30.     AUTHOR      M. Tracy Shen
  31.                         Modified version of Gordon Linoff's lock.c
  32.     MODULE      lock.c -- handle lock files all in one place
  33.     INTERFACES
  34.             utlk_using_lock()   - check the existence of a lock file
  35.             utlk_set_lock()     - set a lock file if one doesn't exist
  36.             utlk_unset_lock()   - unset a lock file if one exists
  37.     INTERNAL ROUTINES
  38.     COMMENTS
  39. ---------------------------------------------------------------------------- */
  40.  
  41. #include <stdio.h>
  42. #include <errno.h>      /* errno, EWOULDBLOCK, etc. */
  43. #include <sys/param.h>
  44.  
  45. #ifndef PATH_MAX        /* Fix for prob in Linux include files, LEB */
  46. #ifdef _POSIX_PATH_MAX
  47. #define PATH_MAX _POSIX_PATH_MAX
  48. #else
  49. #define PATH_MAX 255
  50. #endif
  51. #endif 
  52.  
  53. #ifdef Mach
  54. extern int errno;
  55. #endif
  56.  
  57. #define SIGNOP 0        /* for using kill() to check if process is
  58.                    alive */ 
  59.  
  60. #define FALSE 0
  61. #define TRUE  1
  62.  
  63.  
  64. #define LOCK_STORAGE_MODULE
  65. #include "lock.h"
  66. #undef LOCK_STORSGE_MODULE
  67.  
  68. /******************************************************************************
  69.  *
  70.  *  Function Name:  utlk_using_lock_and_get_pid
  71.  *
  72.  *
  73.  *  Purpose:    check if update or query server is running
  74.  *
  75.  *  Returns:    return TRUE if process running, FALSE if not running
  76.  *          NOT_RUNNING.
  77.  *                      If pid is not NULL, sets it to the process id of
  78.  *                      locking process.
  79.  *
  80.  *  Algorithm:  Find a lock file in /tmp for update or query, read
  81.  *          pid from file, be sure process is running with that
  82.  *          pid.
  83.  *  End Algorithm
  84.  */
  85.  
  86. long
  87.     utlk_using_lock_and_get_pid (dbname, lock_type,pid_ptr)
  88. char *dbname;
  89. long    lock_type;
  90. unsigned *pid_ptr;
  91. {
  92.     /* local variables */
  93.     char lockfile_name[MAXPATHLEN]; /* absolute pathname of update lock */
  94.     FILE *file_ptr;         /* for opening lock files */
  95.     long pid;               /* pid of SC or update process */
  96.     long status;
  97.  
  98.     if (! IN_LOCK_RANGE(lock_type)) {
  99.     /* Out of range locks should never happen, but just in case . . . */
  100.     return(FALSE);
  101.         }
  102.  
  103.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  104.  
  105.     file_ptr = fopen(lockfile_name, "r"); /* look for an existing lock file */
  106.  
  107.     if (file_ptr == NULL) {     /* didn't find lock file! */
  108.     return(FALSE);
  109.        }
  110.  
  111.     /* read pid from the lock file */
  112.     status = fscanf(file_ptr, "%d", &pid);
  113.     fclose(file_ptr);
  114.     if (status == 0) {  /* is an empty lock file */
  115.     return(FALSE);
  116.         }
  117.  
  118.     /* Try a dummy kill; if ESRCH, process is not found.  Otherwise, assume
  119.      * it's there. */
  120.     status = kill(pid, SIGNOP);
  121.     if ((status != 0) && (errno == ESRCH)) {
  122.     return(FALSE);
  123.     }
  124.     if (pid_ptr != NULL) *pid_ptr = pid;
  125.     return(TRUE);
  126.  
  127.     }  /* end utlk_using_lock_and_get_pid */
  128.  
  129. /******************************************************************************
  130.  *
  131.  *  Function Name:  utlk_using_lock
  132.  *
  133.  *
  134.  *  Purpose:    check if update or query server is running
  135.  *
  136.  *  Returns:    return TRUE if process running, FALSE if not running
  137.  *          NOT_RUNNING.
  138.  *
  139.  *  Algorithm:  Find a lock file in /tmp for update or query, read
  140.  *          pid from file, be sure process is running with that
  141.  *          pid.
  142.  *  End Algorithm
  143.  */
  144.  
  145. long
  146.     utlk_using_lock (dbname, lock_type)
  147. char *dbname;
  148. long lock_type;
  149. {
  150.     return(utlk_using_lock_and_get_pid(dbname, lock_type, NULL));
  151. }  /* end utlk_using_lock */
  152.  
  153.  
  154.  
  155. /******************************************************************************
  156.  *
  157.  *  Function Name:  utlk_unset_lock
  158.  *
  159.  *
  160.  *  Purpose:    free lock
  161.  *
  162.  *  Returns:    Return TRUE is successful, FALSE if lock cannot be unset
  163.  *
  164.  *  Algorithm:  
  165.  *  End Algorithm
  166.  */
  167.  
  168. long
  169.     utlk_unset_lock (dbname, lock_type)
  170. char *dbname;
  171. long lock_type;
  172. {
  173.     /* local variables */
  174.     char lockfile_name[MAXPATHLEN]; /* absolute pathname of update lock */
  175.     
  176.     /* begin executable unset_lock */
  177.     
  178.     if (! IN_LOCK_RANGE(lock_type)) {
  179.     /* Out of range locks should never happen, but just in case . . . */
  180.     return(TRUE);
  181.     }
  182.     
  183.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  184.     
  185.     return(unlink(lockfile_name) == 0);
  186. }  /* end utlk_unset_lock */
  187.  
  188.  
  189.  
  190. /******************************************************************************
  191.  *
  192.  *  Function Name:  utlk_set_lock_with_pid
  193.  *
  194.  *
  195.  *  Purpose:    set lock, process id is argument
  196.  *
  197.  *  Returns:    Return TRUE is successful, FALSE if lock cannot be set
  198.  *
  199.  *  Algorithm:  
  200.  *  End Algorithm
  201.  */
  202.  
  203. long
  204.     utlk_set_lock_with_pid (dbname, lock_type, pid)
  205. char *dbname;
  206. long lock_type;         /* LOCK_UPDATE, etc. */
  207. long pid;           /* pid of SC or update process */
  208. {
  209.     /* local variables */
  210.     char lockfile_name[MAXPATHLEN]; /* absolute pathname of update lock */
  211.     FILE *file_ptr;         /* for opening lock files */
  212.  
  213.     /* begin executable set_lock */
  214.     
  215.     if (! IN_LOCK_RANGE(lock_type)) {
  216.     /* Out of range locks should never happen, but just in case . . . */
  217.     return(FALSE);
  218.     }
  219.     
  220.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  221.  
  222.     /* Don't look for an existing file -- just write over it */
  223.     file_ptr = fopen(lockfile_name, "w");
  224.     if (file_ptr == NULL) { /* no write permission to db  */
  225.     return(FALSE);
  226.     }
  227.     fprintf(file_ptr, "%d", pid);
  228.     fclose(file_ptr);
  229.     return(TRUE);
  230. }  /* end utlk_set_lock */
  231.  
  232.  
  233. /******************************************************************************
  234.  *
  235.  *  Function Name:  utlk_set_lock
  236.  *
  237.  *
  238.  *  Purpose:    set lock
  239.  *
  240.  *  Returns:    Return TRUE is successful, FALSE if lock cannot be set
  241.  *
  242.  *  Algorithm:  
  243.  *  End Algorithm
  244.  */
  245.  
  246. long
  247.     utlk_set_lock (dbname, lock_type)
  248. char *dbname;
  249. long lock_type;
  250. {
  251.     return(utlk_set_lock_with_pid(dbname, lock_type, getpid()));
  252. }  /* end utlk_set_lock */
  253.