Changeset 35697
- Timestamp:
- Jun 21, 2013, 3:19:58 PM (13 years ago)
- Location:
- branches/eam_branches/ipp-20130509/Ohana/src/libohana
- Files:
-
- 2 edited
-
include/ohana.h (modified) (1 diff)
-
src/glockfile.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20130509/Ohana/src/libohana/include/ohana.h
r34460 r35697 283 283 FILE *fsetlockfile PROTO((char *filename, double timeout, int type, int *state)); 284 284 int fclearlockfile PROTO((char *filename, FILE *f, int type, int *state)); 285 int fchecklockfile PROTO((char *filename, int type, int *state)); 285 286 286 287 /* in config.c */ -
branches/eam_branches/ipp-20130509/Ohana/src/libohana/src/glockfile.c
r27435 r35697 1 1 # include <ohana.h> 2 3 int 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 73 failure: 74 fprintf (stderr, "failed to check lock\n"); 75 if (f != NULL) fclose (f); 76 return FALSE; 77 } 2 78 3 79 FILE *fsetlockfile (char *filename, double timeout, int type, int *state) {
Note:
See TracChangeset
for help on using the changeset viewer.
