sweetmr@SCT60A.SUNYCT.EDU (michael sweet) (06/14/91)
In a program we're writing, we have a configuration file which needs to be locked against changes for two reasons: 1) If the information in the configuration file is changed during a run, the program will bomb. 2) If two copies of the program are run, the second copy should sense the lock placed on the config file by the first copy and exit without doing anything. Unfortunately, the lockf(2) function doesn't seem to work properly (at least the way we are doing it...) THe following is a code exerpt from the program: if (fd = open("config_file", O_RDWR, 02644) < 0) { puts("Can't open configuration file!"); exit(1); }; lseek(fd, 0L, 0); if (lockf(fd, F_TLOCK, 0L) < 0) { puts("Configuration file is already locked!"); exit(1); }; Can anyone see anything obviously wrong with the above code? We have tried running two copies simultaneously, and both seem to lock the file successfully. The configuration file has 02644 permissions.... -Mike Sweet (sweetmr@sct60a.sunyct.edu)
pj@sam.wpd.sgi.com (Paul Jackson) (06/14/91)
In article <9106131834.AA08574@sct60a.sunyct.edu>, sweetmr@SCT60A.SUNYCT.EDU (michael sweet) writes: |> ... lockf(2) function doesn't seem to work properly |> if (fd = open("config_file", O_RDWR, 02644) < 0) |> .... |> if (lockf(fd, F_TLOCK, 0L) < 0) Change the open line to have parentheses around (fd = open(...)). Your current code sets fd to be the result of the boolean (open(...) < 0), which if the open succeeds is false. So fd is 0 (for false) and lockf(0,...) succeeds. -- I won't rest till it's the best ... Software Production Engineer Paul Jackson (pj@wpd.sgi.com), x1373