Changeset 3649
- Timestamp:
- Apr 3, 2005, 9:11:15 PM (21 years ago)
- Location:
- trunk/Ohana/src
- Files:
-
- 22 edited
-
addstar/src/gimages.c (modified) (1 diff)
-
addstar/src/image-db.c (modified) (1 diff)
-
imregister/Makefile (modified) (2 diffs)
-
imregister/detrend/entry.c (modified) (1 diff)
-
imregister/detrend/output.c (modified) (1 diff)
-
imregister/src/detregister.c (modified) (4 diffs)
-
imregister/src/detsearch.c (modified) (3 diffs)
-
imregister/src/imregister.c (modified) (3 diffs)
-
imregister/src/imregtable.c (modified) (3 diffs)
-
imregister/src/imsearch.c (modified) (3 diffs)
-
imregister/src/imstatreg.c (modified) (3 diffs)
-
imregister/src/photreg.c (modified) (3 diffs)
-
imregister/src/photsearch.c (modified) (2 diffs)
-
imregister/src/spregister.c (modified) (4 diffs)
-
imregister/src/spsearch.c (modified) (3 diffs)
-
libohana/doc/glockfile.txt (modified) (1 diff)
-
libohana/include/ohana.h (modified) (2 diffs)
-
libohana/src/fits_db.c (modified) (2 diffs)
-
libohana/src/glockfile.c (modified) (10 diffs)
-
libohana/src/phot_catalog.c (modified) (1 diff)
-
misc/Makefile (modified) (4 diffs)
-
misc/src/glockfile.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/addstar/src/gimages.c
r3645 r3649 17 17 18 18 /* 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 */ 20 22 f = GetDB (&dbstate); 21 23 if (dbstate == LCK_EMPTY) { -
trunk/Ohana/src/addstar/src/image-db.c
r3376 r3649 36 36 check_permissions (ImageCat); 37 37 f = fsetlockfile (ImageCat, 3600.0, LOCK, &dbstate); 38 if ( (f == (FILE *) NULL) && (dbstate != LCK_EMPTY)) {38 if (f == NULL) { 39 39 fprintf (stderr, "ERROR: can't lock image catalog\n"); 40 40 exit (1); -
trunk/Ohana/src/imregister/Makefile
r3606 r3649 21 21 INCS = -I$(INC) -I$(LINC) -I$(XINC) 22 22 LIBS = -L$(LLIB) -lohana -lFITS -lm 23 # CFLAGS = $(INCS) -Wall -Werror24 23 CFLAGS = $(INCS) 25 24 LFLAGS = $(LIBS) … … 160 159 rm -f $(BIN)/$*.$(ARCH) 161 160 161 %.rebuild : 162 rm -f $(BIN)/$*.$(ARCH) 163 make $(DESTBIN)/$* 164 162 165 %.install: 163 166 make $(DESTBIN)/$* -
trunk/Ohana/src/imregister/detrend/entry.c
r3648 r3649 77 77 sprintf (fullname, "%s/%s", dBPath, filename); 78 78 f = fsetlockfile (fullname, 2.0, LCK_XCLD, &filestate); 79 /* if there is an error, it may just mean file is locked. */ 79 80 if (filestate == LCK_EMPTY) { 80 81 found = TRUE; -
trunk/Ohana/src/imregister/detrend/output.c
r3606 r3649 15 15 PrintSubset (image, match, Nmatch); 16 16 if (output.verbose) fprintf (stderr, "SUCCESS\n"); 17 18 17 exit (0); 19 18 } -
trunk/Ohana/src/imregister/src/detregister.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "detrend.h" 3 static char *version = "detregister $Revision: 3. 2$";3 static char *version = "detregister $Revision: 3.3 $"; 4 4 5 5 int main (int argc, char **argv) { … … 24 24 dBFile = set_dBFile (); 25 25 if (!fits_db_lock (&db, dBFile)) { 26 fprintf (stderr, "ERROR: failure to lock db\n"); 26 27 fits_db_close (&db); 27 28 exit (1); … … 32 33 } else { 33 34 if (!fits_db_load (&db)) { 35 fprintf (stderr, "ERROR: failure to load db\n"); 34 36 fits_db_close (&db); 35 37 exit (1); … … 37 39 } 38 40 39 /** we may later want to pull this out and put it elsewhere **/40 41 fits_convert_DetReg (&newdata, sizeof (DetReg), 1); 41 42 fits_table_to_vtable (&db.ftable, &vtable, 0, 0); -
trunk/Ohana/src/imregister/src/detsearch.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "detrend.h" 3 static char *version = "detsearch $Revision: 3. 5$";3 static char *version = "detsearch $Revision: 3.6 $"; 4 4 5 5 int main (int argc, char **argv) { … … 23 23 dBFile = set_dBFile (); 24 24 if (!fits_db_lock (&db, dBFile)) { 25 fprintf (stderr, "ERROR: failure to lock db\n"); 25 26 fits_db_close (&db); 26 27 exit (1); … … 28 29 29 30 if (!fits_db_load (&db)) { 31 fprintf (stderr, "ERROR: failure to load db\n"); 30 32 fits_db_close (&db); 31 33 exit (1); -
trunk/Ohana/src/imregister/src/imregister.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "imreg.h" 3 static char *version = "imregister $Revision: 3. 5$";3 static char *version = "imregister $Revision: 3.6 $"; 4 4 5 5 int main (int argc, char **argv) { … … 23 23 24 24 if (!fits_db_lock (&db, ImageDB)) { 25 fprintf (stderr, "ERROR: failure to lock db\n"); 25 26 fits_db_close (&db); 26 27 exit (1); … … 31 32 } else { 32 33 if (!fits_db_load (&db)) { 34 fprintf (stderr, "ERROR: failure to load db\n"); 33 35 fits_db_close (&db); 34 36 exit (1); -
trunk/Ohana/src/imregister/src/imregtable.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "imreg.h" 3 static char *version = "imregtable $Revision: 3. 5$";3 static char *version = "imregtable $Revision: 3.6 $"; 4 4 5 5 int main (int argc, char **argv) { … … 42 42 43 43 if (!fits_db_lock (&db, ImageDB)) { 44 fprintf (stderr, "ERROR: failure to lock db\n"); 44 45 fits_db_close (&db); 45 46 exit (1); … … 50 51 } else { 51 52 if (!fits_db_load (&db)) { 53 fprintf (stderr, "ERROR: failure to load db\n"); 52 54 fits_db_close (&db); 53 55 exit (1); -
trunk/Ohana/src/imregister/src/imsearch.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "imreg.h" 3 static char *version = "imsearch $Revision: 3. 4$";3 static char *version = "imsearch $Revision: 3.5 $"; 4 4 5 5 int main (int argc, char **argv) { … … 17 17 18 18 if (!fits_db_lock (&db, ImageDB)) { 19 fprintf (stderr, "ERROR: failure to lock db\n"); 19 20 fits_db_close (&db); 20 21 exit (1); 21 22 } 22 23 if (!fits_db_load (&db)) { 24 fprintf (stderr, "ERROR: failure to load db\n"); 23 25 fits_db_close (&db); 24 26 exit (1); … … 36 38 37 39 OutputSubset (image, Nimage, match, Nmatch); 38 39 40 exit (0); 40 41 } -
trunk/Ohana/src/imregister/src/imstatreg.c
r3648 r3649 1 1 # include "imregister.h" 2 2 # include "imreg.h" 3 static char *version = "imstatreg $Revision: 3.1 1$";3 static char *version = "imstatreg $Revision: 3.12 $"; 4 4 5 5 int main (int argc, char **argv) { … … 58 58 if (temp_db.dbstate == LCK_EMPTY) { 59 59 fprintf (stderr, "temporary database empty\n"); 60 fits_db_close (&temp_db); 60 61 goto next; 61 62 } 62 63 if (!fits_db_load (&temp_db)) { 63 64 fprintf (stderr, "error reading temp db\n"); 64 goto next;65 exit (1); 65 66 } 66 image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);67 subset = fits_table_get_RegImage (&temp_db.ftable, &Nsubset); 67 68 fprintf (stderr, "temporary database read\n"); 68 69 69 /* delete and unlock temporary database */ 70 /* DELETE and unlock temporary database */ 71 truncate or delete_db; /****** *****/ 70 72 fits_db_close (&temp_db); 71 73 fprintf (stderr, "temporary database closed\n"); … … 80 82 fprintf (stderr, "main database empty\n"); 81 83 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"); 82 92 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"); 100 96 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); 104 104 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); 120 108 if (match != NULL) free (match); 121 109 if (image != NULL) free (image); 122 110 123 /* clear temp DB hardlock */124 unlink (TempDB);125 clearlockfile2 (TempDB, -1, LCK_HARD, &status);126 127 111 next: 128 112 fflush (stderr); -
trunk/Ohana/src/imregister/src/photreg.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "photreg.h" 3 static char *version = "photreg $Revision: 1. 6$";3 static char *version = "photreg $Revision: 1.7 $"; 4 4 5 5 int main (int argc, char **argv) { … … 20 20 if (!strcmp (output.db, "trans")) filename = TransDB; 21 21 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 } 24 27 if (db.dbstate == LCK_EMPTY) { 25 28 fits_db_create (&db); … … 31 34 } else { 32 35 if (!fits_db_load (&db)) { 36 fprintf (stderr, "ERROR: failure to load db\n"); 33 37 fits_db_close (&db); 34 38 exit (1); -
trunk/Ohana/src/imregister/src/photsearch.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "photreg.h" 3 static char *version = "photsearch $Revision: 1. 6$";3 static char *version = "photsearch $Revision: 1.7 $"; 4 4 5 5 int main (int argc, char **argv) { … … 25 25 } 26 26 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 } 28 32 29 33 if (!fits_db_load (&db)) { 34 fprintf (stderr, "ERROR: failure to load db\n"); 30 35 fits_db_close (&db); 31 36 exit (1); -
trunk/Ohana/src/imregister/src/spregister.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "spreg.h" 3 static char *version = "spregister $Revision: 1. 6$";3 static char *version = "spregister $Revision: 1.7 $"; 4 4 5 5 int main (int argc, char **argv) { … … 23 23 24 24 if (!fits_db_lock (&db, SpectrumDB)) { 25 fprintf (stderr, "ERROR: failure to lock db\n"); 25 26 fits_db_close (&db); 26 27 exit (1); … … 31 32 } else { 32 33 if (!fits_db_load (&db)) { 34 fprintf (stderr, "ERROR: failure to load db\n"); 33 35 fits_db_close (&db); 34 36 exit (1); … … 44 46 fits_db_free (&db); 45 47 48 fprintf (stderr, "SUCCESS\n"); 46 49 exit (0); 47 50 } -
trunk/Ohana/src/imregister/src/spsearch.c
r3606 r3649 1 1 # include "imregister.h" 2 2 # include "spreg.h" 3 static char *version = "spsearch $Revision: 1. 5$";3 static char *version = "spsearch $Revision: 1.6 $"; 4 4 5 5 int main (int argc, char **argv) { … … 17 17 18 18 if (!fits_db_lock (&db, SpectrumDB)) { 19 fprintf (stderr, "ERROR: failure to lock db\n"); 19 20 fits_db_close (&db); 20 21 exit (1); 21 22 } 22 23 if (!fits_db_load (&db)) { 24 fprintf (stderr, "ERROR: failure to load db\n"); 23 25 fits_db_close (&db); 24 26 exit (1); … … 36 38 37 39 OutputSubset (spectrum, Nspectrum, match, Nmatch); 38 39 40 exit (0); 40 41 } -
trunk/Ohana/src/libohana/doc/glockfile.txt
r3538 r3649 32 32 Error Conditions: 33 33 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) 36 38 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 45 55 -
trunk/Ohana/src/libohana/include/ohana.h
r3522 r3649 36 36 # define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec)) 37 37 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 */ 38 enum { 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 */ 43 48 44 # define LCK_EMPTY 5 /* locked file isempty */45 # define LCK_FULL 6 /* locked file isnot empty */46 # define LCK_UNKNOWN 7 /* can't stat file toget 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 */ 47 52 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 }; 51 57 52 58 # ifndef M_PI … … 126 132 FILE *fsetlockfile (char *filename, double timeout, int type, int *state); 127 133 int 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);130 134 131 135 /* in config.c */ -
trunk/Ohana/src/libohana/src/fits_db.c
r3609 r3649 41 41 db[0].f = fsetlockfile (filename, db[0].timeout, db[0].lockstate, &db[0].dbstate); 42 42 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 } 44 48 return (FALSE); 45 49 } … … 148 152 /* close the db files (close open file & unlock) */ 149 153 int fits_db_close (FITS_DB *db) { 150 if (db[0]. dbstate == LCK_UNLOCK) return (TRUE);154 if (db[0].f == NULL) return (TRUE); 151 155 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); */157 156 return (TRUE); 158 157 } 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 5 5 int i, done, nbytes, status; 6 6 char *lockname, buffer[64]; 7 char *file, *path ;7 char *file, *path, mode[3]; 8 8 int fd; 9 FILE *f, flock;9 FILE *f, *flock; 10 10 struct stat filestat; 11 11 struct flock filelock; 12 12 struct timeval now, then; 13 13 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; 16 16 17 17 /* define lock type */ … … 24 24 case LCK_XCLD: 25 25 filelock.l_type = F_WRLCK; /* set an exclusive lock */ 26 strcpy (mode, "r+"); 26 27 break; 27 28 case LCK_SOFT: 28 29 filelock.l_type = F_RDLCK; /* set a shared lock */ 30 strcpy (mode, "r"); 29 31 break; 30 32 default: 31 fprintf (stderr, "invalid lock type %d\n", type);33 *state = LCK_INVALID; 32 34 goto failure; 33 35 } 34 36 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; 40 43 goto failure; 41 44 } 45 /* otherwise, we need to be able to create file */ 46 strcpy (mode, "w+"); 42 47 } 43 48 44 49 /* try to open file (create if it does not exist) */ 45 f = fopen (filename, "w+");50 f = fopen (filename, mode); 46 51 if (f == NULL) { 47 52 *state = LCK_ACCESS; … … 66 71 got_lock: 67 72 73 /* check if blocking hardlock exists */ 68 74 if (type == LCK_HARD) { 69 /* we've locked filename, now check lockfile */70 71 75 /* set up name to lockfile */ 72 76 path = pathname (filename); … … 75 79 sprintf (lockname, "%s/.%s.lck", path, file); 76 80 81 status = stat (lockname, &filestat); 82 if ((status == -1) && (errno == ENOENT)) { 83 strcpy (mode, "w+"); 84 } else { 85 strcpy (mode, "r+"); 86 } 87 77 88 /* try to open lockfile */ 78 flock = fopen (lockname, "w+");89 flock = fopen (lockname, mode); 79 90 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; 83 93 } 84 94 fd = fileno (flock); … … 88 98 for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000); 89 99 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; 93 102 } 94 103 … … 98 107 buffer[4] = 0; 99 108 if (!strcmp (buffer, "BUSY")) { 100 *state = LCK_HARDL CK + 20;109 *state = LCK_HARDLOCKHARD; 101 110 goto failure; 102 /*** lock file is still103 111 } 104 112 /* note that we don't care if the lockfile has random garbage */ 105 113 } 106 114 } 115 116 /* set blocking hardlock */ 117 if (type == LCK_HARD) { 107 118 /* 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; 122 132 } 123 133 124 134 /* check if file is empty or not */ 135 fd = fileno (f); 125 136 if (fstat (fd, &filestat)) { 126 137 *state = LCK_UNKNOWN; … … 133 144 } 134 145 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); 139 151 140 152 failure: 141 if (f d != -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); 147 159 } 148 160 … … 150 162 int fclearlockfile (char *filename, FILE *f, int type, int *state) { 151 163 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; 171 167 struct stat filestat; 172 168 struct flock filelock; … … 180 176 filelock.l_len = 0; 181 177 filelock.l_pid = getpid (); 182 filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;183 178 184 179 /* first clear hard lockfile */ … … 191 186 sprintf (lockname, "%s/.%s.lck", path, file); 192 187 193 /* check for lockfile exist ence */188 /* check for lockfile existance */ 194 189 status = stat (lockname, &filestat); 195 190 if (status == -1) { 196 *state = LCK_H UNLOCK;191 *state = LCK_HARDLOCK; 197 192 goto failure; 198 193 } 199 194 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); 206 202 207 203 /* try a few times to lock lockfile */ 208 204 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); 212 206 if (i == 20) { 213 *state = LCK_H UNLOCK;207 *state = LCK_HARDLOCKHARD; 214 208 goto failure; 215 209 } 216 210 217 211 /* 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; 231 224 } 232 225 233 226 /* 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); 240 228 241 229 *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); 244 232 if (lockname != NULL) free (lockname); 245 233 return (1); 246 234 247 235 failure: 248 if (path != NULL) free (path);249 if (file != NULL) free (file);236 if (path != NULL) free (path); 237 if (file != NULL) free (file); 250 238 if (lockname != NULL) free (lockname); 251 239 return (0); 252 253 240 } -
trunk/Ohana/src/libohana/src/phot_catalog.c
r3466 r3649 13 13 14 14 /* set lock on database, create stream f */ 15 catalog[0].f = (FILE *)NULL;15 catalog[0].f = NULL; 16 16 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); 19 20 20 21 fseek (catalog[0].f, 0, SEEK_SET); -
trunk/Ohana/src/misc/Makefile
r3648 r3649 16 16 # 17 17 INCS = -I$(LINC) -I$(XINC) 18 LIBS = -L$(LLIB) -l FITS -lohana-lm19 CFLAGS = 20 CCFLAGS = $(INCS) $(LIBS) 18 LIBS = -L$(LLIB) -lohana -lFITS -lm 19 CFLAGS = $(INCS) 20 LFLAGS = $(LIBS) 21 21 22 22 PROGRAMS = gconfig mknames fhead ftable fields list_astro glockfile \ … … 24 24 medianfilter mefhead mkfringetable gtfringetable ckfits applyscat fiximg 25 25 26 CFHT = cfhtlog27 28 $(CFHT):29 make $(BIN)/$@.$(ARCH)30 31 $(PROGRAMS):32 make $(BIN)/$@.$(ARCH)33 34 26 misc: $(PROGRAMS) 35 36 clean:37 for i in $(PROGRAMS); do make $$i.clean; done38 rm -f */*~39 rm -f */#*40 rm -f *~41 rm -f #*42 43 install:44 for i in $(PROGRAMS); do make $$i.install; done45 27 46 28 FAKESRC = $(SRC)/fakestars.$(ARCH).o $(SRC)/random.$(ARCH).o … … 50 32 $(CC) -o $(BIN)/fakestars.$(ARCH) $(FAKESRC) $(LIBS) $(CCFLAGS) 51 33 52 fakestars: $(BIN)/fakestars.$(ARCH) 53 cp $(BIN)/fakestars.$(ARCH) $(DESTBIN)/fakestars 34 fakestars: $(DESTBIN)/fakestars 54 35 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) 56 40 @if [ ! -d $(DESTBIN) ]; then mkdir -p $(DESTBIN); fi 57 41 cp $(SRC)/cfhtlog.$(ARCH) $(DESTBIN)/ 58 42 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 43 cfhtlog: 44 make $(DESTBIN)/cfhtlog 62 45 63 # RULES for making & installing 46 $(PROGRAMS): % : $(BIN)/%.$(ARCH) 47 48 # dependancy rules for binary code ######################### 49 .PRECIOUS: %.$(ARCH).o 50 .PRECIOUS: $(BIN)/%.$(ARCH) 64 51 65 52 %.$(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 $@ 70 54 71 55 $(BIN)/%.$(ARCH) : $(SRC)/%.$(ARCH).o 72 56 @if [ ! -d $(BIN) ]; then mkdir -p $(BIN); fi 73 $(CC) $^ -o $@ $(L IBS)57 $(CC) $^ -o $@ $(LFLAGS) 74 58 75 59 $(DESTBIN)/%: $(BIN)/%.$(ARCH) … … 84 68 rm -f $(SRC)/$*.$(ARCH).o 85 69 rm -f $(BIN)/$*.$(ARCH) 70 71 # utilities ################################################# 72 73 install: 74 for i in $(PROGRAMS); do make $$i.install; done 75 76 clean: 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 14 14 15 15 filename = argv[1]; 16 timeout = 120.0;16 timeout = 30.0; 17 17 18 18 holdtime = atoi (argv[3]); … … 30 30 31 31 f = fsetlockfile (filename, timeout, type, &state); 32 if (f d== NULL) {32 if (f == NULL) { 33 33 fprintf (stderr, "ERROR: can't lock file %s\n", filename); 34 34 exit (1); 35 35 } 36 36 37 fprintf (stderr, "file is locked\n"); 37 38 sleep (holdtime); 38 39 fclearlockfile (filename, f, type, &state);
Note:
See TracChangeset
for help on using the changeset viewer.
