IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3649


Ignore:
Timestamp:
Apr 3, 2005, 9:11:15 PM (21 years ago)
Author:
eugene
Message:

cleaning up fsetlockfile / fclearlockfile (dropped setlockfile2/clearlockfile2)

Location:
trunk/Ohana/src
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/addstar/src/gimages.c

    r3645 r3649  
    1717 
    1818  /* if image.db is opened successfully, f will not be NULL.
    19      if image.db is empty, dbstate will be LCK_EMPTY */
     19     if image.db is empty, dbstate will be LCK_EMPTY
     20     if image.db is not locked & opened successfully, we will not reach here!
     21  */
    2022  f = GetDB (&dbstate);
    2123  if (dbstate == LCK_EMPTY) {
  • trunk/Ohana/src/addstar/src/image-db.c

    r3376 r3649  
    3636  check_permissions (ImageCat);
    3737  f = fsetlockfile (ImageCat, 3600.0, LOCK, &dbstate);
    38   if ((f == (FILE *) NULL) && (dbstate != LCK_EMPTY)) {
     38  if (f == NULL) {
    3939    fprintf (stderr, "ERROR: can't lock image catalog\n");
    4040    exit (1);
  • trunk/Ohana/src/imregister/Makefile

    r3606 r3649  
    2121INCS    =       -I$(INC) -I$(LINC) -I$(XINC)
    2222LIBS    =       -L$(LLIB) -lohana -lFITS -lm
    23 # CFLAGS        =       $(INCS) -Wall -Werror
    2423CFLAGS  =       $(INCS)
    2524LFLAGS  =       $(LIBS)
     
    160159        rm -f $(BIN)/$*.$(ARCH)
    161160
     161%.rebuild :
     162        rm -f $(BIN)/$*.$(ARCH)
     163        make $(DESTBIN)/$*
     164
    162165%.install:
    163166        make $(DESTBIN)/$*
  • trunk/Ohana/src/imregister/detrend/entry.c

    r3648 r3649  
    7777    sprintf (fullname, "%s/%s", dBPath, filename);
    7878    f = fsetlockfile (fullname, 2.0, LCK_XCLD, &filestate);
     79    /* if there is an error, it may just mean file is locked. */
    7980    if (filestate == LCK_EMPTY) {
    8081        found = TRUE;
  • trunk/Ohana/src/imregister/detrend/output.c

    r3606 r3649  
    1515  PrintSubset (image, match, Nmatch);
    1616  if (output.verbose) fprintf (stderr, "SUCCESS\n");
    17 
    1817  exit (0);
    1918}
  • trunk/Ohana/src/imregister/src/detregister.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "detrend.h"
    3 static char *version = "detregister $Revision: 3.2 $";
     3static char *version = "detregister $Revision: 3.3 $";
    44
    55int main (int argc, char **argv) {
     
    2424  dBFile = set_dBFile ();
    2525  if (!fits_db_lock (&db, dBFile)) {
     26    fprintf (stderr, "ERROR: failure to lock db\n");
    2627    fits_db_close (&db);
    2728    exit (1);
     
    3233  } else { 
    3334    if (!fits_db_load (&db)) {
     35      fprintf (stderr, "ERROR: failure to load db\n");
    3436      fits_db_close (&db);
    3537      exit (1);
     
    3739  }
    3840
    39   /** we may later want to pull this out and put it elsewhere **/
    4041  fits_convert_DetReg (&newdata, sizeof (DetReg), 1);
    4142  fits_table_to_vtable (&db.ftable, &vtable, 0, 0);
  • trunk/Ohana/src/imregister/src/detsearch.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "detrend.h"
    3 static char *version = "detsearch $Revision: 3.5 $";
     3static char *version = "detsearch $Revision: 3.6 $";
    44
    55int main (int argc, char **argv) {
     
    2323  dBFile = set_dBFile ();
    2424  if (!fits_db_lock (&db, dBFile)) {
     25    fprintf (stderr, "ERROR: failure to lock db\n");
    2526    fits_db_close (&db);
    2627    exit (1);
     
    2829 
    2930  if (!fits_db_load (&db)) {
     31    fprintf (stderr, "ERROR: failure to load db\n");
    3032    fits_db_close (&db);
    3133    exit (1);
  • trunk/Ohana/src/imregister/src/imregister.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imregister $Revision: 3.5 $";
     3static char *version = "imregister $Revision: 3.6 $";
    44
    55int main (int argc, char **argv) {
     
    2323
    2424  if (!fits_db_lock (&db, ImageDB)) {
     25    fprintf (stderr, "ERROR: failure to lock db\n");
    2526    fits_db_close (&db);
    2627    exit (1);
     
    3132  } else { 
    3233    if (!fits_db_load (&db)) {
     34      fprintf (stderr, "ERROR: failure to load db\n");
    3335      fits_db_close (&db);
    3436      exit (1);
  • trunk/Ohana/src/imregister/src/imregtable.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imregtable $Revision: 3.5 $";
     3static char *version = "imregtable $Revision: 3.6 $";
    44
    55int main (int argc, char **argv) {
     
    4242
    4343  if (!fits_db_lock (&db, ImageDB)) {
     44    fprintf (stderr, "ERROR: failure to lock db\n");
    4445    fits_db_close (&db);
    4546    exit (1);
     
    5051  } else { 
    5152    if (!fits_db_load (&db)) {
     53      fprintf (stderr, "ERROR: failure to load db\n");
    5254      fits_db_close (&db);
    5355      exit (1);
  • trunk/Ohana/src/imregister/src/imsearch.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imsearch $Revision: 3.4 $";
     3static char *version = "imsearch $Revision: 3.5 $";
    44
    55int main (int argc, char **argv) {
     
    1717
    1818  if (!fits_db_lock (&db, ImageDB)) {
     19    fprintf (stderr, "ERROR: failure to lock db\n");
    1920    fits_db_close (&db);
    2021    exit (1);
    2122  }
    2223  if (!fits_db_load (&db)) {
     24    fprintf (stderr, "ERROR: failure to load db\n");
    2325    fits_db_close (&db);
    2426    exit (1);
     
    3638
    3739  OutputSubset (image, Nimage, match, Nmatch);
    38 
    3940  exit (0);
    4041}
  • trunk/Ohana/src/imregister/src/imstatreg.c

    r3648 r3649  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imstatreg $Revision: 3.11 $";
     3static char *version = "imstatreg $Revision: 3.12 $";
    44
    55int main (int argc, char **argv) {
     
    5858    if (temp_db.dbstate == LCK_EMPTY) {
    5959      fprintf (stderr, "temporary database empty\n");
     60      fits_db_close (&temp_db);
    6061      goto next;
    6162    }
    6263    if (!fits_db_load (&temp_db)) {
    6364      fprintf (stderr, "error reading temp db\n");
    64       goto next;
     65      exit (1);
    6566    }
    66     image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);
     67    subset = fits_table_get_RegImage (&temp_db.ftable, &Nsubset);
    6768    fprintf (stderr, "temporary database read\n");
    6869
    69     /* delete and unlock temporary database */
     70    /* DELETE and unlock temporary database */
     71    truncate or delete_db; /****** *****/
    7072    fits_db_close (&temp_db);
    7173    fprintf (stderr, "temporary database closed\n");
     
    8082      fprintf (stderr, "main database empty\n");
    8183      fits_db_close (&image_db);
     84      fits_db_free (&temp_db);
     85      goto next;
     86      /* this is a type of error: we read entries from the
     87         temp db, but there were no entries to match in the main db
     88         we will just drop the temp db data */
     89    }
     90    image = fits_table_get_RegImage (&image_db.ftable, &Nimage);
     91    fprintf (stderr, "main database read\n");
    8292
    83       /* add temp data back to temp database */
    84       fprintf (stderr, "resave temporary data\n");
    85       if (!fits_db_lock (&temp_db, TempDB)) {
    86         fits_db_close (&temp_db);
    87         fprintf (stderr, "error locking temp db (path missing? access permission?)\n");
    88         exit (1);
    89       }
    90       if (temp_db.dbstate == LCK_EMPTY) {
    91         fprintf (stderr, "temporary database empty\n");
    92         goto next;
    93       }
    94       if (!fits_db_load (&temp_db)) {
    95         fprintf (stderr, "error reading temp db\n");
    96         goto next;
    97       }
    98       image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);
    99       fprintf (stderr, "temporary database read\n");
     93    /* match temp image with main images */
     94    match = match_images (image, Nimage, subset, Nsubset, &Nmatch);
     95    if (Nmatch != Nsubset) fprintf (stderr, "WARNING: some images missed\n");
    10096
    101       fits_convert_Spectrum (spectrum, sizeof (Spectrum), 1);
    102       fits_table_to_vtable (&db.ftable, &vtable, 0, 0);
    103       fits_vadd_rows (&vtable, (char *) spectrum, 1, sizeof(Spectrum));
     97    /* update entries in main db */
     98    fits_vtable_from_ftable (&image_db.ftable, &vtable, match, Nmatch);
     99    for (i = 0; i < Nmatch; i++) {
     100      fits_convert_RegImage ((RegImage *) vtable.buffer[i], sizeof (RegImage), 1);
     101    }
     102    fits_db_update (&image_db, &vtable);
     103    fits_db_close (&image_db);
    104104
    105       fits_db_update (&db, &vtable);
    106       fits_db_close (&db);
    107       fits_db_free (&db);
    108 
    109       print_db_status ("temporary database resaved");
    110       goto next;
    111     }
    112 
    113     /************* this is kind of convoluted, need to simplify a bit ****/
    114 
    115     /* match temp image with main images / update main DB */
    116     match = match_images (image, Nentry, &Nmatch);
    117     if (Nmatch != Nentry) fprintf (stderr, "WARNING: some images missed\n");
    118     print_db_status ("main database read");
    119     update_db (match, Nmatch);
     105    fits_db_free (&image_db);
     106    fits_db_free (&temp_db);
     107    fits_free_vtable (&vtable);
    120108    if (match != NULL) free (match);
    121109    if (image != NULL) free (image);
    122110     
    123     /* clear temp DB hardlock */
    124     unlink (TempDB);
    125     clearlockfile2 (TempDB, -1, LCK_HARD, &status);
    126 
    127111  next:
    128112    fflush (stderr);
  • trunk/Ohana/src/imregister/src/photreg.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "photreg.h"
    3 static char *version = "photreg $Revision: 1.6 $";
     3static char *version = "photreg $Revision: 1.7 $";
    44
    55int main (int argc, char **argv) {
     
    2020  if (!strcmp (output.db, "trans")) filename = TransDB;
    2121
    22   if (!fits_db_lock (&db, filename)) exit (1);
    23 
     22  if (!fits_db_lock (&db, filename)) {
     23    fprintf (stderr, "ERROR: failure to lock db\n");
     24    fits_db_close (&db);
     25    exit (1);
     26  }
    2427  if (db.dbstate == LCK_EMPTY) {
    2528    fits_db_create (&db);
     
    3134  } else { 
    3235    if (!fits_db_load (&db)) {
     36      fprintf (stderr, "ERROR: failure to load db\n");
    3337      fits_db_close (&db);
    3438      exit (1);
  • trunk/Ohana/src/imregister/src/photsearch.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "photreg.h"
    3 static char *version = "photsearch $Revision: 1.6 $";
     3static char *version = "photsearch $Revision: 1.7 $";
    44
    55int main (int argc, char **argv) {
     
    2525  }
    2626
    27   if (!fits_db_lock (&db, filename)) exit (1);
     27  if (!fits_db_lock (&db, filename)) {
     28    fprintf (stderr, "ERROR: failure to lock db\n");
     29    fits_db_close (&db);
     30    exit (1);
     31  }
    2832 
    2933  if (!fits_db_load (&db)) {
     34    fprintf (stderr, "ERROR: failure to load db\n");
    3035    fits_db_close (&db);
    3136    exit (1);
  • trunk/Ohana/src/imregister/src/spregister.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "spreg.h"
    3 static char *version = "spregister $Revision: 1.6 $";
     3static char *version = "spregister $Revision: 1.7 $";
    44
    55int main (int argc, char **argv) {
     
    2323
    2424  if (!fits_db_lock (&db, SpectrumDB)) {
     25    fprintf (stderr, "ERROR: failure to lock db\n");
    2526    fits_db_close (&db);
    2627    exit (1);
     
    3132  } else { 
    3233    if (!fits_db_load (&db)) {
     34      fprintf (stderr, "ERROR: failure to load db\n");
    3335      fits_db_close (&db);
    3436      exit (1);
     
    4446  fits_db_free (&db);
    4547
     48  fprintf (stderr, "SUCCESS\n");
    4649  exit (0);
    4750}
  • trunk/Ohana/src/imregister/src/spsearch.c

    r3606 r3649  
    11# include "imregister.h"
    22# include "spreg.h"
    3 static char *version = "spsearch $Revision: 1.5 $";
     3static char *version = "spsearch $Revision: 1.6 $";
    44
    55int main (int argc, char **argv) {
     
    1717
    1818  if (!fits_db_lock (&db, SpectrumDB)) {
     19    fprintf (stderr, "ERROR: failure to lock db\n");
    1920    fits_db_close (&db);
    2021    exit (1);
    2122  }
    2223  if (!fits_db_load (&db)) {
     24    fprintf (stderr, "ERROR: failure to load db\n");
    2325    fits_db_close (&db);
    2426    exit (1);
     
    3638
    3739  OutputSubset (spectrum, Nspectrum, match, Nmatch);
    38 
    3940  exit (0);
    4041}
  • trunk/Ohana/src/libohana/doc/glockfile.txt

    r3538 r3649  
    3232 Error Conditions:
    3333
    34  on error, fsetlockfile returns NULL and clears the lock state of the
    35  file
     34 on error, fsetlockfile closes the file, closes the lockfile, and
     35 returns NULL.  In come cases, an empty file may be left behind
     36 (LCK_TIMEOUT, LCK_HARDOPEN, LCK_HARDLOCK, LCK_HARDLOCKHARD,
     37 LCK_HARDLOCKCLOSE)
    3638
    37  - invalid type
    38  - SOFT and file does not exist
    39  - file not accessible (cannot be opened)
    40  - lockfile not accessible
    41  - cannot lock lockfile (held by another user)
    42  - lockfile says 'BUSY' (previous holder crashed)
    43  - cannot unlock lockfile (file system error)
    44  - error writing 'BUSY' to lockfile
     39 - invalid type (LCK_INVALID)
     40   * no file created, no lock file created : OK
     41 - SOFT and file does not exist (LCK_EMPTY)
     42   * no file created, no lock file created : OK
     43 - file not accessible, cannot be opened (LCK_ACCESS)
     44   * no file created, no lock file created : OK
     45 - FS lock timed out (LCK_TIMEOUT)
     46   * new file is created, file is open, file is unlocked, lock file is not created : OK
     47 - lockfile not accessible (LCK_HARDOPEN)
     48   * new file is created, file is open, file is locked, lock file is not created : OK
     49 - cannot lock lockfile, held by another user (LCK_HARDLOCK)
     50   * new file is created, file is open, file is locked, lock file is created : OK
     51 - lockfile says 'BUSY', previous holder crashed (LCK_HARDLOCKHARD)
     52   * new file is created, file is open, file is locked, lock file exists, lock file locked : OK
     53 - error writing 'BUSY' to lockfile (LCK_HARDCLOSE)
     54   * new file is created, file is open, file is locked, lock file exists, lock file locked
    4555
  • trunk/Ohana/src/libohana/include/ohana.h

    r3522 r3649  
    3636# define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))
    3737
    38 # define LCK_UNLOCK  0   /* file is unlocked */
    39 # define LCK_ACCESS  1   /* can't get access to file */
    40 # define LCK_TIMEOUT 2   /* timeout setting lock */
    41 # define LCK_HARDLCK 3   /* error setting hard lockfile */
    42 # define LCK_HUNLOCK 4   /* error clearing hard lockfile */
     38enum {
     39 LCK_UNLOCK,        /* file is unlocked */
     40 LCK_INVALID,       /* invalid locktype requested */
     41 LCK_MISSING,       /* soft requested and file missing (error) */
     42 LCK_ACCESS,        /* can't get access to file */
     43 LCK_TIMEOUT,       /* timeout setting lock */
     44 LCK_HARDOPEN,      /* cannot open hard lockfile */
     45 LCK_HARDLOCK,      /* cannot lock hard lockfile */
     46 LCK_HARDLOCKHARD,  /* hard lockfile is locked */
     47 LCK_HARDCLOSE,     /* cannot write to hardlock */
    4348
    44 # define LCK_EMPTY   5   /* locked file is empty */
    45 # define LCK_FULL    6   /* locked file is not empty */
    46 # define LCK_UNKNOWN 7   /* can't stat file to get size */
     49 LCK_EMPTY,         /* file is locked and empty */
     50 LCK_FULL,          /* file is locked and not empty */
     51 LCK_UNKNOWN,       /* file is locked, but can't get size */
    4752
    48 # define LCK_SOFT    10  /* soft lock */
    49 # define LCK_HARD    11  /* hard lock */
    50 # define LCK_XCLD    12  /* exclusive soft lock */
     53 LCK_SOFT,          /* soft lock */
     54 LCK_HARD,          /* hard lock */
     55 LCK_XCLD,          /* exclusive soft lock */
     56};
    5157
    5258# ifndef M_PI
     
    126132FILE *fsetlockfile (char *filename, double timeout, int type, int *state);
    127133int   fclearlockfile (char *filename, FILE *f, int type, int *state);
    128 int   setlockfile2 (char *filename, double timeout, int type, int *state);
    129 int   clearlockfile2 (char *filename, int fd, int type, int *state);
    130134
    131135/* in config.c */
  • trunk/Ohana/src/libohana/src/fits_db.c

    r3609 r3649  
    4141  db[0].f = fsetlockfile (filename, db[0].timeout, db[0].lockstate, &db[0].dbstate);
    4242  if (db[0].f == NULL) {
    43     fprintf (stderr, "cannot set lock on db file %s\n", filename);
     43    if (db[0].dbstate == LCK_MISSING) {
     44      fprintf (stderr, "no data in db %s\n", filename);
     45    } else {
     46      fprintf (stderr, "cannot set lock on db file %s\n", filename);
     47    }
    4448    return (FALSE);
    4549  }
     
    148152/* close the db files (close open file & unlock) */
    149153int fits_db_close (FITS_DB *db) {
    150   if (db[0].dbstate == LCK_UNLOCK) return (TRUE);
     154  if (db[0].f == NULL) return (TRUE);
    151155  fclearlockfile (db[0].filename, db[0].f, db[0].lockstate, &db[0].dbstate);
    152   /* fclearlockfile also performs fclose (f) */
    153 
    154   /* for this to be atomic, need to unlink before we unlock */
    155   /* unlink file here if resulting file is empty? */
    156   /* if (db[0].Nrow == 0) && (lockstate != LCK_SOFT)) unlink (dBFile); */
    157156  return (TRUE);
    158157
     158
     159/* for this to be atomic, need to unlink before we unlock */
     160/* unlink file here if resulting file is empty? */
     161/* if (db[0].Nrow == 0) && (lockstate != LCK_SOFT)) unlink (dBFile); */
  • trunk/Ohana/src/libohana/src/glockfile.c

    r3648 r3649  
    55  int i, done, nbytes, status;
    66  char *lockname, buffer[64];
    7   char *file, *path;
     7  char *file, *path, mode[3];
    88  int fd;
    9   FILE *f, flock;
     9  FILE *f, *flock;
    1010  struct stat filestat;
    1111  struct flock filelock;
    1212  struct timeval now, then;
    1313
    14   /* initial values at -1, close if the are set to another value */
    15   file = path = lockname = (char *) NULL;
     14  f = flock = NULL;
     15  file = path = lockname = NULL;
    1616
    1717  /* define lock type */
     
    2424  case LCK_XCLD:
    2525    filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
     26    strcpy (mode, "r+");
    2627    break;
    2728  case LCK_SOFT:
    2829    filelock.l_type   = F_RDLCK;  /* set a shared lock */
     30    strcpy (mode, "r");
    2931    break;
    3032  default:
    31     fprintf (stderr, "invalid lock type %d\n", type);
     33    *state = LCK_INVALID;
    3234    goto failure;
    3335  }
    3436
    35   /* if soft & file does not exist, return LCK_ACCESS */
    36   if (type == LCK_SOFT) {
    37     status = stat (filename, &filestat);
    38     if ((status == -1) && (errno == ENOENT)) {
    39       *state = LCK_EMPTY;
     37  /* check if file exists */
     38  status = stat (filename, &filestat);
     39  if ((status == -1) && (errno == ENOENT)) {
     40    /* if soft, return LCK_ACCESS */
     41    if (type == LCK_SOFT) {
     42      *state = LCK_MISSING;
    4043      goto failure;
    4144    }
     45    /* otherwise, we need to be able to create file */
     46    strcpy (mode, "w+");
    4247  }
    4348 
    4449  /* try to open file (create if it does not exist) */
    45   f = fopen (filename, "w+");
     50  f = fopen (filename, mode);
    4651  if (f == NULL) {
    4752    *state = LCK_ACCESS;
     
    6671got_lock:
    6772
     73  /* check if blocking hardlock exists */
    6874  if (type == LCK_HARD) {
    69     /* we've locked filename, now check lockfile */
    70 
    7175    /* set up name to lockfile */
    7276    path = pathname (filename);
     
    7579    sprintf (lockname, "%s/.%s.lck", path, file);
    7680
     81    status = stat (lockname, &filestat);
     82    if ((status == -1) && (errno == ENOENT)) {
     83      strcpy (mode, "w+");
     84    } else {
     85      strcpy (mode, "r+");
     86    }
     87
    7788    /* try to open lockfile */
    78     flock = fopen (lockname, "w+");
     89    flock = fopen (lockname, mode);
    7990    if (flock == NULL) {
    80       *state = LCK_HARDLCK;
    81       goto failure;
    82       /***** this one has left the file locked ****/
     91      *state = LCK_HARDOPEN;
     92      goto failure;
    8393    }
    8494    fd = fileno (flock);
     
    8898    for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000);
    8999    if (i == 20) {
    90       *state = LCK_HARDLCK + 10;
    91       goto failure;
    92       /***** this one has left the file locked ****/
     100      *state = LCK_HARDLOCK;
     101      goto failure;
    93102    }
    94103   
     
    98107      buffer[4] = 0;
    99108      if (!strcmp (buffer, "BUSY")) {
    100         *state = LCK_HARDLCK + 20;
     109        *state = LCK_HARDLOCKHARD;
    101110        goto failure;
    102         /*** lock file is still
    103111      }
    104112      /* note that we don't care if the lockfile has random garbage */
    105113    }
    106    
     114  }
     115   
     116  /* set blocking hardlock */
     117  if (type == LCK_HARD) {
    107118    /* we've really got the lock, write BUSY to protect it */
    108     lseek (flock, 0, SEEK_SET);
    109     write (flock, "BUSY", 4);
    110    
    111     /* now unlock lockfile */
    112     filelock.l_type = F_UNLCK;
    113     if (fcntl (flock, F_SETLK, &filelock) == -1) {
    114       *state = LCK_HARDLCK + 30;
    115       goto failure;
    116     }
    117     if (close (flock)) {
    118       fprintf (stderr, "error closing lock\n");
    119       goto failure;
    120     }
    121     flock = -1;
     119    fseek (flock, 0, SEEK_SET);
     120    nbytes = fprintf (flock, "BUSY\n");
     121    if (nbytes != 5) {
     122      *state = LCK_HARDCLOSE;
     123      goto failure;
     124    }
     125   
     126    /* now fclose lockfile (also unlocks file) */
     127    if (fclose (flock)) {
     128      *state = LCK_HARDCLOSE;
     129      goto failure;
     130    }
     131    flock = NULL;
    122132  }
    123133
    124134  /* check if file is empty or not */
     135  fd = fileno (f);
    125136  if (fstat (fd, &filestat)) {
    126137    *state = LCK_UNKNOWN;
     
    133144  }
    134145
    135   if (path != NULL) free (path);
    136   if (file != NULL) free (file);
    137   if (lockname != NULL) free (lockname);
    138   return (fd);
     146  if (path     != NULL) free (path);
     147  if (file     != NULL) free (file);
     148  if (lockname != NULL) free (lockname);
     149  if (flock    != NULL) fclose (flock);
     150  return (f);
    139151
    140152failure:
    141   if (fd != -1) close (fd);
    142   if (flock != -1) close (flock);
    143   if (path != NULL) free (path);
    144   if (file != NULL) free (file);
    145   if (lockname != NULL) free (lockname);
    146   return (-1);
     153  if (f        != NULL) fclose (f);
     154  if (flock    != NULL) fclose (flock);
     155  if (path     != NULL) free (path);
     156  if (file     != NULL) free (file);
     157  if (lockname != NULL) free (lockname);
     158  return (NULL);
    147159}
    148160 
     
    150162int fclearlockfile (char *filename, FILE *f, int type, int *state) {
    151163
    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 
    164 /* if we pass in -1, all we can do is clear the hardlock */
    165 int clearlockfile2 (char *filename, int fd, int type, int *state) {
    166  
    167   char *lockname;
    168   int i, status;
    169   char *path, *file;
    170   int flock, filemode;
     164  int i, fd, status, nbytes;
     165  char *lockname, *path, *file;
     166  FILE *flock;
    171167  struct stat filestat;
    172168  struct flock filelock;
     
    180176  filelock.l_len    = 0;
    181177  filelock.l_pid    = getpid ();
    182   filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    183178
    184179  /* first clear hard lockfile */
     
    191186    sprintf (lockname, "%s/.%s.lck", path, file);
    192187   
    193     /* check for lockfile existence */
     188    /* check for lockfile existance */
    194189    status = stat (lockname, &filestat);
    195190    if (status == -1) {
    196       *state = LCK_HUNLOCK;
     191      *state = LCK_HARDLOCK;
    197192      goto failure;
    198193    }
    199194 
    200     /* open lockfile */
    201     flock = open (lockname, O_RDWR | O_CREAT, filemode);
    202     if (flock == -1) {
    203       *state = LCK_HUNLOCK;
    204       goto failure;
    205     }
     195    /* try to open lockfile */
     196    flock = fopen (lockname, "w+");
     197    if (flock == NULL) {
     198      *state = LCK_HARDOPEN;
     199      goto failure;
     200    }
     201    fd = fileno (flock);
    206202
    207203    /* try a few times to lock lockfile */
    208204    filelock.l_type = F_WRLCK;
    209     for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
    210       usleep (10000);
    211     }
     205    for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000);
    212206    if (i == 20) {
    213       *state = LCK_HUNLOCK;
     207      *state = LCK_HARDLOCKHARD;
    214208      goto failure;
    215209    }
    216210
    217211    /* set value to IDLE */
    218     lseek (flock, 0, SEEK_SET);
    219     write (flock, "IDLE", 4);
    220 
    221     /* unlock lockfile */
    222     filelock.l_type = F_UNLCK;
    223     if (fcntl (flock, F_SETLK, &filelock) == -1) {
    224       *state = LCK_HUNLOCK;
    225       goto failure;
    226     }
    227     if (close (flock)) {
    228       fprintf (stderr, "error closing lock\n");
    229       goto failure;
    230     }
     212    fseek (flock, 0, SEEK_SET);
     213    nbytes = fprintf (flock, "IDLE\n");
     214    if (nbytes != 5) {
     215      *state = LCK_HARDCLOSE;
     216      goto failure;
     217    }
     218
     219    if (fclose (flock)) {
     220      *state = LCK_HARDCLOSE;
     221      goto failure;
     222    }
     223    flock = NULL;
    231224  }
    232225 
    233226  /* now unlock the file */
    234   if (fd != -1) {
    235     if (fcntl (fd, F_SETLK, &filelock) == -1) {
    236       *state = LCK_HUNLOCK;
    237       goto failure;
    238     }
    239   }
     227  fclose (f);
    240228
    241229  *state = LCK_UNLOCK;
    242   if (path != NULL) free (path);
    243   if (file != NULL) free (file);
     230  if (path != NULL)     free (path);
     231  if (file != NULL)     free (file);
    244232  if (lockname != NULL) free (lockname);
    245233  return (1);
    246234
    247235failure:
    248   if (path != NULL) free (path);
    249   if (file != NULL) free (file);
     236  if (path != NULL)     free (path);
     237  if (file != NULL)     free (file);
    250238  if (lockname != NULL) free (lockname);
    251239  return (0);
    252 
    253240}
  • trunk/Ohana/src/libohana/src/phot_catalog.c

    r3466 r3649  
    1313
    1414  /* set lock on database, create stream f */
    15   catalog[0].f  = (FILE *) NULL;
     15  catalog[0].f = NULL;
    1616  catalog[0].f = fsetlockfile (catalog[0].filename, 3600.0, lockmode, &dbstate);
    17   if (dbstate == LCK_EMPTY) return (2);
    18   if (catalog[0].f == (FILE *) NULL) return (0);
     17  if (dbstate == LCK_MISSING) return (2);
     18  if (dbstate == LCK_EMPTY)   return (2);
     19  if (catalog[0].f == NULL)   return (0);
    1920
    2021  fseek (catalog[0].f, 0, SEEK_SET);
  • trunk/Ohana/src/misc/Makefile

    r3648 r3649  
    1616
    1717INCS    =       -I$(LINC) -I$(XINC)
    18 LIBS    =       -L$(LLIB) -lFITS -lohana -lm
    19 CFLAGS  =       
    20 CCFLAGS =       $(INCS) $(LIBS)
     18LIBS    =       -L$(LLIB) -lohana -lFITS -lm
     19CFLAGS  =       $(INCS)
     20LFLAGS  =       $(LIBS)
    2121
    2222PROGRAMS = gconfig mknames fhead ftable fields list_astro glockfile \
     
    2424medianfilter mefhead mkfringetable gtfringetable ckfits applyscat fiximg
    2525
    26 CFHT = cfhtlog
    27 
    28 $(CFHT):
    29         make $(BIN)/$@.$(ARCH)
    30 
    31 $(PROGRAMS):
    32         make $(BIN)/$@.$(ARCH)
    33 
    3426misc: $(PROGRAMS)
    35 
    36 clean:
    37         for i in $(PROGRAMS); do make $$i.clean; done
    38         rm -f */*~
    39         rm -f */#*
    40         rm -f *~
    41         rm -f #*
    42 
    43 install:
    44         for i in $(PROGRAMS); do make $$i.install; done
    4527
    4628FAKESRC = $(SRC)/fakestars.$(ARCH).o $(SRC)/random.$(ARCH).o
     
    5032        $(CC) -o $(BIN)/fakestars.$(ARCH) $(FAKESRC) $(LIBS) $(CCFLAGS)
    5133
    52 fakestars: $(BIN)/fakestars.$(ARCH)
    53         cp $(BIN)/fakestars.$(ARCH) $(DESTBIN)/fakestars
     34fakestars: $(DESTBIN)/fakestars
    5435
    55 $(DESTBIN)/cfhtlog: $(SRC)/cfhtlog.$(ARCH)
     36$(BIN)/cfhtlog.$(ARCH): $(SRC)/cfhtlog.c
     37        $(CC) -I/cfht/include -L/cfht/lib -o $(BIN)/cfhtlog.$(ARCH) $(SRC)/cfhtlog.c -lcfht
     38
     39$(DESTBIN)/cfhtlog: $(BIN)/cfhtlog.$(ARCH)
    5640        @if [ ! -d $(DESTBIN) ]; then mkdir -p $(DESTBIN); fi
    5741        cp $(SRC)/cfhtlog.$(ARCH) $(DESTBIN)/
    5842
    59 cfhtlog: $(SRC)/cfhtlog.$(ARCH)
    60 $(SRC)/cfhtlog.$(ARCH): $(SRC)/cfhtlog.c
    61         $(CC) -I/cfht/include -L/cfht/lib -o $(SRC)/cfhtlog.$(ARCH) $(SRC)/cfhtlog.c -lcfht
     43cfhtlog:
     44        make $(DESTBIN)/cfhtlog
    6245
    63 # RULES for making & installing
     46$(PROGRAMS): % : $(BIN)/%.$(ARCH)
     47
     48# dependancy rules for binary code #########################
     49.PRECIOUS: %.$(ARCH).o
     50.PRECIOUS: $(BIN)/%.$(ARCH)
    6451
    6552%.$(ARCH).o : %.c
    66         $(CC) $(INCS) -o $*.$(ARCH).o -c $<
    67 
    68 $(SRC)/%.$(ARCH).o : %.c
    69         $(CC) $(INCS) -o $*.$(ARCH).o -c $<
     53        $(CC) $(CFLAGS) -c $< -o $@
    7054
    7155$(BIN)/%.$(ARCH) : $(SRC)/%.$(ARCH).o
    7256        @if [ ! -d $(BIN) ]; then mkdir -p $(BIN); fi
    73         $(CC) $^ -o $@ $(LIBS)
     57        $(CC) $^ -o $@ $(LFLAGS)
    7458
    7559$(DESTBIN)/%: $(BIN)/%.$(ARCH)
     
    8468        rm -f $(SRC)/$*.$(ARCH).o
    8569        rm -f $(BIN)/$*.$(ARCH)
     70
     71# utilities #################################################
     72
     73install:
     74        for i in $(PROGRAMS); do make $$i.install; done
     75
     76clean:
     77        for i in $(PROGRAMS); do make $$i.clean; done
     78        rm -f */*~
     79        rm -f */#*
     80        rm -f *~
     81        rm -f #*
     82
  • trunk/Ohana/src/misc/src/glockfile.c

    r3648 r3649  
    1414
    1515  filename = argv[1];
    16   timeout = 120.0;
     16  timeout = 30.0;
    1717
    1818  holdtime = atoi (argv[3]);
     
    3030
    3131  f = fsetlockfile (filename, timeout, type, &state);
    32   if (fd == NULL) {
     32  if (f == NULL) {
    3333    fprintf (stderr, "ERROR: can't lock file %s\n", filename);
    3434    exit (1);
    3535  }
    3636
     37  fprintf (stderr, "file is locked\n");
    3738  sleep (holdtime);
    3839  fclearlockfile (filename, f, type, &state);
Note: See TracChangeset for help on using the changeset viewer.