IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 1, 2005, 6:32:51 PM (21 years ago)
Author:
eugene
Message:

cleaning up setlockfile2/fsetlockfile

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/src/glockfile.c

    r3538 r3648  
    11# include <ohana.h>
    22
    3 /* set a lock on the given file */
    43FILE *fsetlockfile (char *filename, double timeout, int type, int *state) {
    5  
    6   int fd;
    7   FILE *f;
    8 
    9   fd = setlockfile2 (filename, timeout, type, state);
    10   if (fd == -1) return ((FILE *) NULL);
    11 
    12   /* setlockfile2 returns a pointer to a vile open for update */
    13   f = fdopen (fd, "r+");
    14   if (f == (FILE *) NULL) {
    15     clearlockfile2 (filename, fd, type, state);
    16     fprintf (stderr, "can't make stream from descriptor\n");
    17     return ((FILE *) NULL);
    18   }
    19   return (f);
    20 }
    21 
    22 /* clears lock. removes hardlock even if file pointer is not supplied */
    23 int fclearlockfile (char *filename, FILE *f, int type, int *state) {
    24 
    25   int fd, status;
    26 
    27   fd = -1;
    28   if (f != NULL) {
    29     fflush (f);
    30     fd = fileno (f);
    31   }
    32   status = clearlockfile2 (filename, fd, type, state);
    33   if (f != NULL) fclose (f);
    34   return (status);
    35 }
    36 
    37 /* should eventually rewrite these two to use a stream as the basic
    38    element and only grab the fileno when needed by fcntl
    39    (fileno cannot fail) */
    40 
    41 int setlockfile2 (char *filename, double timeout, int type, int *state) {
    424 
    435  int i, done, nbytes, status;
    446  char *lockname, buffer[64];
    457  char *file, *path;
    46   int fd, flock;
    47   int filemode;
     8  int fd;
     9  FILE *f, flock;
    4810  struct stat filestat;
    4911  struct flock filelock;
     
    5113
    5214  /* initial values at -1, close if the are set to another value */
    53   fd = flock = -1;
    54   filemode = S_IRUSR | S_IRGRP | S_IROTH;
    5515  file = path = lockname = (char *) NULL;
    56   gettimeofday (&then, (void *) NULL);
    5716
    5817  /* define lock type */
     
    6524  case LCK_XCLD:
    6625    filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
    67     filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    6826    break;
    6927  case LCK_SOFT:
    7028    filelock.l_type   = F_RDLCK;  /* set a shared lock */
    71     filemode = S_IRUSR | S_IRGRP | S_IROTH;
    7229    break;
    7330  default:
     
    8542  }
    8643 
    87   /* try to open file */
    88   fd = open (filename, O_RDWR | O_CREAT, filemode);
    89   if (fd == -1) {
     44  /* try to open file (create if it does not exist) */
     45  f = fopen (filename, "w+");
     46  if (f == NULL) {
    9047    *state = LCK_ACCESS;
    9148    goto failure;
    9249  }
     50  fd = fileno (f);
    9351
    9452  /* we first try to set a FS level lock on the file */
    95   for (done = 0; !done; ) {
     53  gettimeofday (&then, (void *) NULL);
     54  while (1) {
     55    /* try to lock file */
     56    if (fcntl (fd, F_SETLK, &filelock) != -1) goto got_lock;
    9657
    9758    /* check for timeout */
     
    10162      goto failure;
    10263    }
    103     usleep (10000); /* 10 ms is min */
    104 
    105     /* try to lock file */
    106     if (fcntl (fd, F_SETLK, &filelock) != -1) done = 1;
    107   }
     64    usleep (10000); /* 10 ms is min utime */
     65  }
     66got_lock:
    10867
    10968  if (type == LCK_HARD) {
    110     /* we've locked filename, now check lockname */
    111 
    112     /* locking the lockfile before checking / setting contents will
    113        ensure the data is synced across NFS */
    114    
     69    /* we've locked filename, now check lockfile */
     70
    11571    /* set up name to lockfile */
    11672    path = pathname (filename);
     
    12076
    12177    /* try to open lockfile */
    122     flock = open (lockname, O_RDWR | O_CREAT, filemode);
    123     if (flock == -1) {
     78    flock = fopen (lockname, "w+");
     79    if (flock == NULL) {
    12480      *state = LCK_HARDLCK;
    12581      goto failure;
    126     }
    127    
    128     /* try a few times to lock lockfile */
    129     for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
    130       usleep (10000);
    131     }
     82      /***** this one has left the file locked ****/
     83    }
     84    fd = fileno (flock);
     85   
     86    /* try a few times to lock lockfile (locking the lockfile before checking
     87       the contents will ensure the data is synced across NFS) */
     88    for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000);
    13289    if (i == 20) {
    13390      *state = LCK_HARDLCK + 10;
    13491      goto failure;
    135     }
    136    
    137     /* we've locked the file, now read the contents */
    138     nbytes = read (flock, buffer, 4);
     92      /***** this one has left the file locked ****/
     93    }
     94   
     95    /* we've locked the lockfile, now read the contents */
     96    nbytes = fread (buffer, 1, 4, flock);
    13997    if (nbytes == 4) { /* lock file has a word in it */
    14098      buffer[4] = 0;
     
    142100        *state = LCK_HARDLCK + 20;
    143101        goto failure;
     102        /*** lock file is still
    144103      }
     104      /* note that we don't care if the lockfile has random garbage */
    145105    }
    146106   
     
    187147}
    188148 
     149/* clears lock. removes hardlock even if file pointer is not supplied */
     150int fclearlockfile (char *filename, FILE *f, int type, int *state) {
     151
     152  int fd, status;
     153
     154  fd = -1;
     155  if (f != NULL) {
     156    fflush (f);
     157    fd = fileno (f);
     158  }
     159  status = clearlockfile2 (filename, fd, type, state);
     160  if (f != NULL) fclose (f);
     161  return (status);
     162}
     163
    189164/* if we pass in -1, all we can do is clear the hardlock */
    190165int clearlockfile2 (char *filename, int fd, int type, int *state) {
Note: See TracChangeset for help on using the changeset viewer.