home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sources / misc / 4216 / client_lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-18  |  5.8 KB  |  208 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "client_def.h"
  11.  
  12. #ifndef NOLOCKING
  13. static char key_string[sizeof(KEY_PREFIX)+32];
  14.  
  15. static char code_str[] =
  16.     "0123456789:_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  17.  
  18. static make_key_string(server_addr,server_port)
  19.     unsigned long  server_addr; 
  20.     unsigned short server_port;
  21. {
  22.     unsigned long v1, v2;
  23.     char *p;
  24.  
  25.     strcpy(key_string,KEY_PREFIX);
  26.     for(p = key_string; *p; p++);
  27.     v1 = server_addr;
  28.     v2 = server_port;
  29.  
  30.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  31.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  32.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6; v1 = v1 | (v2 << (32-3*6));
  33.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  34.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  35.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  36.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  37.     *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  38.     *p   = 0;
  39. }
  40. #endif
  41.  
  42. /********************************************************************/
  43. /******* For those systems that has flock function call *************/
  44. /********************************************************************/
  45. #ifdef USE_FLOCK
  46.  
  47. #include <sys/file.h>
  48.  
  49. int key_persists = 1;
  50. static unsigned int lock_fd;
  51. static unsigned short okey;
  52.  
  53. client_get_key()
  54. {
  55.     if(flock(lock_fd,LOCK_EX)           == -1) { perror("flock"); exit(1); }
  56.     if(read(lock_fd,&okey,sizeof(okey)) == -1) { perror("readk"); exit(1); }
  57.     if(lseek(lock_fd,0L,0)              == -1) { perror("seek"); exit(1); }
  58.     return(okey);
  59. }
  60.  
  61. client_put_key(key)
  62.     unsigned short key;
  63. {
  64.     if(write(lock_fd,&key,sizeof(key)) == -1) { perror("write"); exit(1); }
  65.     if(lseek(lock_fd,0L,0)             == -1) { perror("seek"); exit(1); }
  66.     if(flock(lock_fd,LOCK_UN)          == -1) { perror("unflock"); exit(1); }
  67. }
  68.  
  69. client_init_key(server_addr,server_port,key)
  70.     unsigned long  server_addr; 
  71.     unsigned short server_port;
  72.     unsigned short key;
  73. {
  74.     unsigned long omask;
  75.     okey = key;
  76.  
  77.     make_key_string(server_addr,server_port);
  78.  
  79.     omask = umask(0);
  80.     lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  81.     umask(omask);
  82. }
  83.  
  84. #endif
  85. /********************************************************************/
  86. /******* For those systems that has lockf function call *************/
  87. /********************************************************************/
  88. #ifdef USE_LOCKF
  89.  
  90. #include <unistd.h>
  91.  
  92. int key_persists = 1;
  93. static unsigned int lock_fd;
  94. static unsigned short okey;
  95.  
  96. client_get_key()
  97. {
  98.     if(lockf(lock_fd,F_LOCK,sizeof(okey)) == -1) { perror("lockf"); exit(1); }
  99.     if(read(lock_fd,&okey,sizeof(okey))   == -1) { perror("readlk"); exit(1); }
  100.     if(lseek(lock_fd,0L,0)                == -1) { perror("seek"); exit(1); }
  101.     return(okey);
  102. }
  103.  
  104. client_put_key(key)
  105.     unsigned short key;
  106. {
  107.     if(write(lock_fd,&key,sizeof(key))    == -1) { perror("write"); exit(1); }
  108.     if(lseek(lock_fd,0L,0)                == -1) { perror("seek"); exit(1); }
  109.     if(lockf(lock_fd,F_ULOCK,sizeof(key)) == -1) { perror("unlockf"); exit(1); }
  110. }
  111.  
  112. client_init_key(server_addr,server_port,key)
  113.     unsigned long  server_addr; 
  114.     unsigned short server_port;
  115.     unsigned short key;
  116. {
  117.     unsigned long omask;
  118.     okey = key;
  119.  
  120.     make_key_string(server_addr,server_port);
  121.  
  122.     omask = umask(0);
  123.     lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  124.     umask(omask);
  125. }
  126.  
  127. #endif
  128. /********************************************************************/
  129. /******* For those systems that has SysV shared memory + lockf ******/
  130. /********************************************************************/
  131. #ifdef USE_SHAREMEM_AND_LOCKF
  132.  
  133. #include <unistd.h>
  134. #include <sys/ipc.h>
  135. extern char *shmat();
  136.  
  137. int key_persists = 0;
  138. static unsigned short *share_key;
  139. static unsigned int lock_fd;
  140.  
  141. client_get_key()
  142. {
  143.     if(lockf(lock_fd,F_LOCK,2) == -1) { perror("lockf"); exit(1); }
  144.     return(*share_key);
  145. }
  146.  
  147. client_put_key(key)
  148.     unsigned short key;
  149. {
  150.     *share_key = key;
  151.     if(lockf(lock_fd,F_ULOCK,2) == -1) { perror("unlockf"); exit(1); }
  152. }
  153.  
  154. client_init_key(server_addr,server_port,key)
  155.     unsigned long  server_addr; 
  156.     unsigned short server_port;
  157.     unsigned short key;
  158. {
  159.     unsigned long omask;
  160.     key_t lock_key;
  161.     int   lock_shm;
  162.  
  163.     make_key_string(server_addr,server_port);
  164.  
  165.     omask = umask(0);
  166.     lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  167.     umask(omask);
  168.  
  169.     if((lock_key = ftok(key_string,3432)) == -1){ perror("ftok"); exit(1); }
  170.     if((lock_shm = shmget(lock_key,sizeof(short),IPC_CREAT|0666)) == -1)
  171.                         { perror("shmget"); exit(1); }
  172.     if(!(share_key = (unsigned short *) shmat(lock_shm,(char*)0,0)))
  173.                         { perror("shmat"); exit(1); }
  174. }
  175.  
  176. #endif
  177. /********************************************************************/
  178. /******* For those who does not want to use locking *****************/
  179. /********************************************************************/
  180. #ifdef NOLOCKING
  181.  
  182. int key_persists = 0;
  183. static unsigned short okey;
  184.  
  185. client_get_key()
  186. {
  187.     return(okey);
  188. }
  189.  
  190. client_put_key(key)
  191.     unsigned short key;
  192. {
  193.     okey = key;
  194. }
  195.  
  196. client_init_key(server_addr,server_port,key)
  197.     unsigned long  server_addr; 
  198.     unsigned short server_port;
  199.     unsigned short key;
  200. {
  201.     okey = key;
  202. }
  203.  
  204. #endif
  205. /********************************************************************/
  206. /********************************************************************/
  207. /********************************************************************/
  208.