IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 35754


Ignore:
Timestamp:
Jul 3, 2013, 2:05:11 PM (13 years ago)
Author:
eugene
Message:

add fchecklockfile function

Location:
trunk/Ohana/src/libohana
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/include/ohana.h

    r34460 r35754  
    283283FILE   *fsetlockfile           PROTO((char *filename, double timeout, int type, int *state));
    284284int     fclearlockfile         PROTO((char *filename, FILE *f, int type, int *state));
     285int     fchecklockfile         PROTO((char *filename, int type, int *state));
    285286
    286287/* in config.c */
  • trunk/Ohana/src/libohana/src

  • trunk/Ohana/src/libohana/src/glockfile.c

    r27435 r35754  
    11# include <ohana.h>
     2
     3int fchecklockfile (char *filename, int type, int *state) {
     4 
     5  int status;
     6  char mode[3];
     7  int fd;
     8  FILE *f;
     9  struct stat filestat;
     10  struct flock filelock;
     11
     12  f = NULL;
     13
     14  /* define lock type */
     15  filelock.l_start  = 0;
     16  filelock.l_whence = SEEK_SET;
     17  filelock.l_len    = 0;
     18  filelock.l_pid    = 0;
     19  switch (type) {
     20  case LCK_HARD:
     21  case LCK_XCLD:
     22    filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
     23    strcpy (mode, "r+");
     24    break;
     25  case LCK_SOFT:
     26    filelock.l_type   = F_RDLCK;  /* set a shared lock */
     27    strcpy (mode, "r");
     28    break;
     29  default:
     30    *state = LCK_INVALID;
     31    goto failure;
     32  }
     33
     34  /* check if file exists */
     35  status = stat (filename, &filestat);
     36  if ((status == -1) && (errno == ENOENT)) {
     37    if (type == LCK_SOFT) {
     38      *state = LCK_MISSING;
     39      goto failure;
     40    }
     41    /* otherwise, we need to be able to create file */
     42    strcpy (mode, "w+");
     43  }
     44 
     45  /* try to open file (create if it does not exist) */
     46  f = fopen (filename, mode);
     47  if (f == NULL) {
     48    *state = LCK_ACCESS;
     49    goto failure;
     50  }
     51  fd = fileno (f);
     52
     53  /* check the lock */
     54  status = fcntl (fd, F_GETLK, &filelock);
     55  if (status) {
     56    *state = LCK_ACCESS;
     57    goto failure;
     58  }
     59
     60  if (filelock.l_pid) {
     61    if (filelock.l_type == F_WRLCK) {
     62      fprintf (stderr, "lock (hard) is held by pid %d\n", filelock.l_pid);
     63    } else {
     64      fprintf (stderr, "lock (soft) is held by pid %d\n", filelock.l_pid);
     65    }
     66  } else {
     67    fprintf (stderr, "lock is NOT held\n");
     68  }
     69
     70  fclose (f);
     71  return TRUE;
     72
     73failure:
     74  fprintf (stderr, "failed to check lock\n");
     75  if (f        != NULL) fclose (f);
     76  return FALSE;
     77}
    278
    379FILE *fsetlockfile (char *filename, double timeout, int type, int *state) {
Note: See TracChangeset for help on using the changeset viewer.