home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xblockbuster / patch3.sh / lock.c next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.6 KB  |  190 lines

  1. /*
  2. ** lock.c: this module provides a simple locking mechanism
  3. **
  4. **         LockScoreFile(filename) locks the given file. returns -1 on error.
  5. **         UnlockScoreFile(filename) unlocks the given file.
  6. **         LockFile(fd) locks the given file descriptor using fcntl.
  7. **         UnlockFile(fd) unlocks the given file descriptor.
  8. **
  9. ** the lockfilename is built from the given parameter by adding ".lock" at
  10. ** the end of it. the callers process id will be written in the file.
  11. ** when the file is unlocked again the pid will be read and checked
  12. ** before the file is unlinked.
  13. **
  14. ** LockFile and UnlockFile use fcntl on an open file descriptor.
  15. **
  16. ** Copyright (C) 1993, Gerald Vogt <vogt@isa.de>
  17. ** 
  18. ** This program is free software; you can redistribute it and/or modify
  19. ** it under the terms of the GNU General Public License as published by
  20. ** the Free Software Foundation; either version 1, or (at your option)
  21. ** any later version.
  22. **
  23. ** This program is distributed in the hope that it will be useful,
  24. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. ** GNU General Public License for more details.
  27. **
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <fcntl.h>
  32. #include <malloc.h>
  33. #include "xblockbuster.h"
  34. #include "lock.h"
  35.  
  36. #define MAXTRY 30
  37.  
  38. #ifdef LOCK_FILE
  39. char    *CreateLockFileName(scorefile)
  40. char    *scorefile;
  41. {
  42.     char    *buffer;
  43.  
  44.     buffer = malloc(strlen(scorefile) + 6);
  45.  
  46.     if (buffer != (char *) NULL) {
  47.     strcpy(buffer, scorefile);
  48.     strcat(buffer, ".lock");
  49.     }
  50.  
  51.     return(buffer);
  52. }
  53.  
  54. int LockScoreFile(scorefile)
  55. char    *scorefile;
  56. {
  57.     char    *filename;
  58.     int        ret;
  59.     char    string[10];
  60.     int        tryCount;
  61.     extern char *sys_errlist[];
  62.     extern int errno;
  63.  
  64.     filename = CreateLockFileName(scorefile);
  65.    
  66.     if (filename == (char *) NULL)
  67.       return(-1);
  68.  
  69.     tryCount = 0;
  70.  
  71.     do {
  72.     ret = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0664);
  73.     tryCount++;
  74.     
  75.     if (ret == -1) {
  76.         fprintf(stderr,"Could not open %s:%s\n",filename,sys_errlist[errno]);
  77.  
  78.         if (tryCount == MAXTRY) {
  79.         fprintf(stderr, "Giving up.\n");
  80.         free(filename);
  81.         return(-1);
  82.         }
  83.         sleep(1);
  84.     }
  85.     } while (ret == -1);
  86.  
  87.     sprintf(string, "%d", getpid());
  88.     write(ret, string, strlen(string) + 1);
  89.  
  90.     close(ret);
  91.     free(filename);
  92.     return(0);
  93. }
  94.       
  95. void    UnlockScoreFile(scorefile)
  96. char    *scorefile;
  97. {
  98.     char    *filename;
  99.     int        ret, len;
  100.     char    string[10];
  101.     extern int atoi();
  102.  
  103.     filename = CreateLockFileName(scorefile);
  104.     
  105.     if (filename == (char *) NULL)
  106.       return;
  107.  
  108.     ret = open(filename, O_RDONLY);
  109.  
  110.     if (ret == -1) {
  111.     perror("open score lock file");
  112.     free(filename);
  113.     return;
  114.     }
  115.  
  116.     len = read(ret, string, 10);
  117.  
  118.     if (len == -1) {
  119.     perror("reading score lock file");
  120.     close(ret);
  121.     free(filename);
  122.     return;
  123.     }
  124.  
  125.     len = atoi(string);
  126.  
  127.     close(ret);
  128.  
  129.     if (len == getpid())
  130.       unlink(filename);
  131.     else
  132.       fprintf(stderr, "Score lock pid does not match\n");
  133.  
  134.     free(filename);
  135. }
  136. #endif
  137.  
  138. #ifdef LOCK_FCNTL
  139. int LockFile(fd)
  140. int fd;
  141. {
  142.     int        ret;
  143.     int        tryCount;
  144.     struct flock fl;
  145.  
  146.     tryCount = 0;
  147.  
  148.     do {
  149.     fl.l_type = F_WRLCK;
  150.     fl.l_whence = 0;
  151.     fl.l_start = 0L;
  152.     fl.l_len = 0L;
  153.     
  154.     ret = fcntl(fd, F_SETLK, &fl);
  155.  
  156.     tryCount++;
  157.     
  158.     if (ret == -1) {
  159.         perror("fcntl");
  160.  
  161.         if (tryCount == MAXTRY) {
  162.         fprintf(stderr, "Giving up.\n");
  163.         return(-1);
  164.         }
  165.         sleep(1);
  166.     }
  167.     } while (ret == -1);
  168.  
  169.     return(0);
  170. }
  171.       
  172. void    UnlockFile(fd)
  173. int     fd;
  174. {
  175.     int        ret;
  176.     struct flock fl;
  177.  
  178.     fl.l_type = F_UNLCK;
  179.     fl.l_whence = 0;
  180.     fl.l_start = 0L;
  181.     fl.l_len = 0L;
  182.  
  183.     ret = fcntl(fd, F_SETLK, &fl);
  184.  
  185.     if (ret == -1) {
  186.     perror("fcntl");
  187.     }
  188. }
  189. #endif
  190.