IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3648


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

cleaning up setlockfile2/fsetlockfile

Location:
trunk/Ohana/src
Files:
3 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/elixir/src/DumpStatus.c

    r2428 r3648  
    44
    55  FILE *f;
    6   int i, fd, state, total, mode;
     6  int i, state, total, mode;
    77  Process *global, **process;
    88  int Nprocess, Nobject, Dynamic;
     
    1616  } else {
    1717    /* lock file */
    18     fd = setlockfile2 (filename, 0.1, LCK_XCLD, &state);
    19     if (fd == -1) return (2);
    20     f = fdopen (fd, "r+");
    21     if (f == (FILE *) NULL) {
    22       mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    23       fchmod (fd, mode);
    24       clearlockfile2 (filename, fd, LCK_XCLD, &state);
    25       return (2);
    26     }
     18    f = fsetlockfile (filename, 0.1, LCK_XCLD, &state);
     19    if (f == NULL) return (2);
    2720    fseek (f, 0, SEEK_END);
    2821  } 
     
    5144  if (f != stderr) {
    5245    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    53     fchmod (fd, mode);
    54     clearlockfile2 (filename, fd, LCK_XCLD, &state);
    55     fclose (f);
     46    chmod (filename, mode);
     47    fclearlockfile (filename, f, LCK_XCLD, &state);
    5648  }
    5749  return (TRUE);
  • trunk/Ohana/src/elixir/src/LoadPending.c

    r2428 r3648  
    1515
    1616  int i, status, depend, Nobjects;
    17   int fd, lockstate, mode;
     17  int lockstate, mode;
    1818  FILE *f;
    1919  char name[7][256], line[1024];
     
    3333   
    3434    /* check lockfile - don't remove list if locked */
    35     fd = setlockfile2 (filename, 1.0, LCK_XCLD, &lockstate);
    36     if (fd == -1) {
    37       *state = 1;
    38       return (Nobjects);
    39     }
    40     f = fdopen (fd, "r+");
    41     if (f == (FILE *) NULL) {
    42       clearlockfile2 (filename, fd, LCK_XCLD, &lockstate);
     35    f = fsetlockfile (filename, 1.0, LCK_XCLD, &lockstate);
     36    if (f == NULL) {
    4337      *state = 1;
    4438      return (Nobjects);
     
    113107  escape:
    114108  if (*dynamic) {
    115     if (ftruncate (fd, 0)) {
     109    if (truncate (filename, 0)) {
    116110      fprintf (stderr, "failed to clear source %s (errno %d)\n", filename, errno);
    117111      Shutdown (1);
     
    120114    /* clean up fifo - set mode to 666, unlock */
    121115    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    122     fchmod (fd, mode);
    123     clearlockfile2 (filename, fd, LCK_XCLD, &lockstate);
     116    chmod (filename, mode);
     117    fclearlockfile (filename, f, LCK_XCLD, &lockstate);
     118  } else {
     119    fclose (f);
    124120  }
    125   fclose (f);
    126121
    127122  if ((*state == 2) && *dynamic) {
  • trunk/Ohana/src/elixir/src/MachineOps.c

    r3400 r3648  
    443443int DumpProcessTimes (char *filename) {
    444444 
    445   int i, j, fd, state, mode;
     445  int i, j, state, mode;
    446446  FILE *f;
    447447  double dt;
    448448
    449   fd = 0;
    450449  if (filename == (char *) NULL) {
    451450    system ("tput clear");
    452451    f = stderr;
    453452  } else {
    454 
    455453    /* check lockfile */
    456     fd = setlockfile2 (filename, 0.1, LCK_XCLD, &state);
    457     if (fd == -1) return (2);
    458     f = fdopen (fd, "r+");
    459     if (f == (FILE *) NULL) {
    460       mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    461       fchmod (fd, mode);
    462       clearlockfile2 (filename, fd, LCK_XCLD, &state);
    463       return (2);
    464     }
     454    f = fsetlockfile (filename, 0.1, LCK_XCLD, &state);
     455    if (f == NULL) return (2);
    465456    fseek (f, 0, SEEK_END);
    466457  } 
     
    486477  if (f != stderr) {
    487478    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    488     fchmod (fd, mode);
    489     clearlockfile2 (filename, fd, LCK_XCLD, &state);
    490     fclose (f);
     479    chmod (filename, mode);
     480    fclearlockfile (filename, f, LCK_XCLD, &state);
    491481  }
    492482  return (1);
  • trunk/Ohana/src/elixir/src/MsgOps.c

    r2428 r3648  
    11# include "controller.h"
    2 
    3 /*** this file should be updated to use fsetlockfile / fclearlockfile ***/
    42
    53int WaitMsg (char *fifo, char **message, double maxdelay) {
     
    4240
    4341  /* check lockfile */
    44   fd = setlockfile2 (fifo, 0.1, LCK_XCLD, &state);
    45   if (fd == -1) return (2);
    46   f = fdopen (fd, "r+");
    47   if (f == (FILE *) NULL) {
    48     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    49     fchmod (fd, mode);
    50     clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    51     return (2);
    52   }
     42  f = fsetlockfile (fifo, 0.1, LCK_XCLD, &state);
     43  if (f == NULL) return (2);
    5344 
    5445  /* if file is empty, return 0 */
    5546  if (state == LCK_EMPTY) {
    5647    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    57     fchmod (fd, mode);
    58     clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    59     fclose (f);
     48    chmod (fifo, mode);
     49    fclearlockfile (fifo, f, LCK_XCLD, &state);
    6050    return (0);
    6151  } 
     
    8070  /* file will remain until unlocked and fclosed below,
    8171     but cannot be written to because it is locked */
    82   if (ftruncate (fd, 0)) {
     72  if (truncate (fifo, 0)) {
    8373    fprintf (stderr, "failed to clear fifo file %s (errno: %d)\n", fifo, errno);
    8474    Shutdown (1);
     
    8676 
    8777  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    88   fchmod (fd, mode);
    89   clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    90   fclose (f);
     78  chmod (f, mode);
     79  fclearlockfile (fifo, f, LCK_XCLD, &state);
    9180 
    9281  if (Nbytes == 0) {
     
    10695
    10796  /* check lockfile */
    108   fd = setlockfile2 (fifo, 0.1, LCK_XCLD, &state);
    109   if (fd == -1) return (2);
    110   f = fdopen (fd, "r+");
    111   if (f == (FILE *) NULL) {
    112     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    113     fchmod (fd, mode);
    114     clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    115     return (2);
    116   }
     97  f = fsetlockfile (fifo, 0.1, LCK_XCLD, &state);
     98  if (f == NULL) return (2);
    11799
    118100  /* write message to end of file */
     
    121103
    122104  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    123   fchmod (fd, mode);
    124   clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    125   fclose (f);
    126 
     105  chmod (fifo, mode);
     106  fclearlockfile (fifo, f, LCK_XCLD, &state);
    127107  return (1);
    128 
    129108}
    130109
    131 
    132110/*  possible return states:
    133 
    134111    0 - no message (fifo file empty or non-existent)
    135112    1 - message received
    136113    2 - busy (fifo file locked)
    137 
    138114*/
  • trunk/Ohana/src/imregister/base/WriteFIFO.c

    r3 r3648  
    11# include "imregister.h"
    2 
    3 static char retchar[] = "\n";
    42
    53int WriteFIFO (char *filename, char *line) {
    64
    75  int state, mode;
    8   int fd;
     6  FILE *f;
    97
    10   fd = setlockfile2 (filename, 20.0, LCK_XCLD, &state);
    11   if (fd == -1) {
    12       fprintf (stderr, "ERROR: can't lock fifo %s\n", filename);
    13       return (FALSE);
     8  f = fsetlockfile (filename, 20.0, LCK_XCLD, &state);
     9  if (f == NULL) {
     10    fprintf (stderr, "ERROR: can't lock fifo %s\n", filename);
     11    return (FALSE);
    1412  }
    1513
    16   lseek (fd, 0, SEEK_END);
    17   write (fd, line, strlen (line));
    18   write (fd, retchar, 1);
     14  fseek (f, 0, SEEK_END);
     15  fprintf (f, "%s\n", line);
    1916
    2017  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    21   fchmod (fd, mode);
    22   clearlockfile2 (filename, fd, LCK_XCLD, &state);
     18  chmod (filename, mode);
     19  fclearlockfile (filename, f, LCK_XCLD, &state);
    2320
    2421  return (TRUE);
     22}
    2523
    26 }
  • trunk/Ohana/src/imregister/detrend/entry.c

    r3319 r3648  
    3030
    3131  int status;
    32   int found, Ntry, fd, filestate;
     32  int found, Ntry, filestate;
    3333  char path[256], fullpath[256], filename[256], rootname[256], fullname[256], line[512];
    3434  char *dBPath;
    3535  char *filter_basename;
    3636  struct stat statbuf;
     37  FILE *f;
    3738
    3839  filter_basename = filterhash[newdata[0].filter];
     
    7576    sprintf (filename, "%s/%s.%02d.fits", path, rootname, Ntry);
    7677    sprintf (fullname, "%s/%s", dBPath, filename);
    77     fd = setlockfile2 (fullname, 2.0, LCK_XCLD, &filestate);
     78    f = fsetlockfile (fullname, 2.0, LCK_XCLD, &filestate);
    7879    if (filestate == LCK_EMPTY) {
    7980        found = TRUE;
     
    105106    exit (1);
    106107  }
    107   clearlockfile2 (fullname, fd, LCK_XCLD, &filestate);
     108  fclearlockfile2 (fullname, f, LCK_XCLD, &filestate);
    108109
    109110  return (TRUE);
  • trunk/Ohana/src/imregister/src/imstatreg.c

    r3646 r3648  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imstatreg $Revision: 3.10 $";
     3static char *version = "imstatreg $Revision: 3.11 $";
    44
    55int main (int argc, char **argv) {
     
    1212  get_version (argc, argv, version);
    1313  args (argc, argv);
    14   init_db ();
     14  SetSignals ();
    1515
    1616  if (CLIENT) imregclient (argv[1], argv[2], argv[3]);
    17 
    18   SetSignals ();
    1917
    2018  /* fork in background */
  • 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) {
  • trunk/Ohana/src/misc/Makefile

    r2510 r3648  
    4444        for i in $(PROGRAMS); do make $$i.install; done
    4545
    46 # special rules for specific targets
    47 dbtest: src/dbtest.$(ARCH).o src/glockfile2.$(ARCH).o
    48         $(CC) -o dbtest src/dbtest.$(ARCH).o src/glockfile2.$(ARCH).o $(LIBS) $(CCFLAGS)
    49 
    5046FAKESRC = $(SRC)/fakestars.$(ARCH).o $(SRC)/random.$(ARCH).o
    5147
  • trunk/Ohana/src/misc/src/glockfile.c

    r2417 r3648  
    55  char *filename;
    66  double timeout;
    7   int holdtime, state, type, fd;
     7  int holdtime, state, type;
     8  FILE *f;
    89
    910  if (argc != 4) {
     
    2829  }
    2930
    30   fd = setlockfile2 (filename, timeout, type, &state);
    31   if (fd == -1) {
     31  f = fsetlockfile (filename, timeout, type, &state);
     32  if (fd == NULL) {
    3233    fprintf (stderr, "ERROR: can't lock file %s\n", filename);
    3334    exit (1);
     
    3536
    3637  sleep (holdtime);
    37  
    38   clearlockfile2 (filename, fd, type, &state);
    39 
     38  fclearlockfile (filename, f, type, &state);
    4039  exit (0);
    4140}
  • trunk/Ohana/src/opihi/dvo/elixir.c

    r3462 r3648  
    6262int WriteMsg (char *fifo, char *message) {
    6363
    64   int fd, state, mode;
     64  int state, mode;
    6565  FILE *f;
    6666
    6767  /* check lockfile */
    68   fd = setlockfile2 (fifo, 2.0, LCK_XCLD, &state);
    69   if (fd == -1) return (0);
    70   f = fdopen (fd, "r+");
    71   if (f == (FILE *) NULL) {
    72     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    73     fchmod (fd, mode);
    74     clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    75     return (0);
    76   }
     68  f = fsetlockfile (fifo, 2.0, LCK_XCLD, &state);
     69  if (f == NULL) return (0);
    7770
    7871  /* write message to end of file */
     
    8174
    8275  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    83   fchmod (fd, mode);
     76  chmod (fifo, mode);
    8477
    85   clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    86   fclose (f);
    87 
     78  fclearlockfile (fifo, f, LCK_XCLD, &state);
    8879  return (1);
    89 
    9080}
    9181
     
    9484  int i, nbytes, Nbytes, NBYTES;
    9585  char *buffer;
    96   int fd, state, mode;
     86  int state, mode;
    9787  FILE *f;
    9888  struct stat filestat;
     
    10898
    10999  /* check lockfile */
    110   fd = setlockfile2 (fifo, 2.0, LCK_XCLD, &state);
    111   if (fd == -1) {
     100  f = fsetlockfile (fifo, 2.0, LCK_XCLD, &state);
     101  if (f == NULL) {
    112102    fprintf (stderr, "message locked (%d)\n", state);
    113     return (0);
    114   }
    115   f = fdopen (fd, "r+");
    116   if (f == (FILE *) NULL) {
    117     fprintf (stderr, "message blocked\n");
    118     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    119     fchmod (fd, mode);
    120     clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    121103    return (0);
    122104  }
     
    125107  if (state == LCK_EMPTY) {
    126108    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    127     fchmod (fd, mode);
    128     clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    129     fclose (f);
     109    chmod (fifo, mode);
     110    fclearlockfile (fifo, f, LCK_XCLD, &state);
    130111    return (0);
    131112  } 
     
    148129
    149130  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    150   fchmod (fd, mode);
    151   clearlockfile2 (fifo, fd, LCK_XCLD, &state);
    152   fclose (f);
     131  chmod (f, mode);
     132  fclearlockfile (fifo, f, LCK_XCLD, &state);
    153133
    154134  if (Nbytes == 0) {
Note: See TracChangeset for help on using the changeset viewer.