Changeset 3648 for trunk/Ohana/src/libohana/src/glockfile.c
- Timestamp:
- Apr 1, 2005, 6:32:51 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/libohana/src/glockfile.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/libohana/src/glockfile.c
r3538 r3648 1 1 # include <ohana.h> 2 2 3 /* set a lock on the given file */4 3 FILE *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 basic38 element and only grab the fileno when needed by fcntl39 (fileno cannot fail) */40 41 int setlockfile2 (char *filename, double timeout, int type, int *state) {42 4 43 5 int i, done, nbytes, status; 44 6 char *lockname, buffer[64]; 45 7 char *file, *path; 46 int fd , flock;47 int filemode;8 int fd; 9 FILE *f, flock; 48 10 struct stat filestat; 49 11 struct flock filelock; … … 51 13 52 14 /* initial values at -1, close if the are set to another value */ 53 fd = flock = -1;54 filemode = S_IRUSR | S_IRGRP | S_IROTH;55 15 file = path = lockname = (char *) NULL; 56 gettimeofday (&then, (void *) NULL);57 16 58 17 /* define lock type */ … … 65 24 case LCK_XCLD: 66 25 filelock.l_type = F_WRLCK; /* set an exclusive lock */ 67 filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;68 26 break; 69 27 case LCK_SOFT: 70 28 filelock.l_type = F_RDLCK; /* set a shared lock */ 71 filemode = S_IRUSR | S_IRGRP | S_IROTH;72 29 break; 73 30 default: … … 85 42 } 86 43 87 /* try to open file */88 f d = open (filename, O_RDWR | O_CREAT, filemode);89 if (f d == -1) {44 /* try to open file (create if it does not exist) */ 45 f = fopen (filename, "w+"); 46 if (f == NULL) { 90 47 *state = LCK_ACCESS; 91 48 goto failure; 92 49 } 50 fd = fileno (f); 93 51 94 52 /* 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; 96 57 97 58 /* check for timeout */ … … 101 62 goto failure; 102 63 } 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 } 66 got_lock: 108 67 109 68 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 115 71 /* set up name to lockfile */ 116 72 path = pathname (filename); … … 120 76 121 77 /* 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) { 124 80 *state = LCK_HARDLCK; 125 81 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); 132 89 if (i == 20) { 133 90 *state = LCK_HARDLCK + 10; 134 91 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); 139 97 if (nbytes == 4) { /* lock file has a word in it */ 140 98 buffer[4] = 0; … … 142 100 *state = LCK_HARDLCK + 20; 143 101 goto failure; 102 /*** lock file is still 144 103 } 104 /* note that we don't care if the lockfile has random garbage */ 145 105 } 146 106 … … 187 147 } 188 148 149 /* clears lock. removes hardlock even if file pointer is not supplied */ 150 int 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 189 164 /* if we pass in -1, all we can do is clear the hardlock */ 190 165 int clearlockfile2 (char *filename, int fd, int type, int *state) {
Note:
See TracChangeset
for help on using the changeset viewer.
