2

I'm writing a C utility for Solaris 5.10 that uses flock() for file synchronization. My goal is to interoperate with an existing legacy system that also invokes flock() under the hood. According to man pages on my box, it is supported in BSD compatibility mode — the legacy system was compiled somehow, after all! (Unfortunately, I only have access to the binary now.)

The problem is, when I try to compile my code, it fails with the following errors:

main.c: In function `main':
main.c:11: warning: implicit declaration of function `flock'
main.c:11: error: `LOCK_EX' undeclared (first use in this function)
main.c:11: error: (Each undeclared identifier is reported only once
main.c:11: error: for each function it appears in.)
main.c:11: error: `LOCK_NB' undeclared (first use in this function)

Here's a short reproducible example (well, reproducible if you have 5.10 at hand...):

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char** argv) {
  int fd;
  if ((fd = open("file.txt", O_CREAT | O_RDWR, 0644) == -1))
    return 1;
  if (flock(fd, LOCK_EX | LOCK_NB) == -1)
    return 1;

  puts("File locked!");
  return 0;
}

I'm using gcc 3.4.3 with the following arguments: -o test -std=gnu99 -Wall main.c. The error messages seem to suggest that I'm including the wrong header files, but the man page for flock() doesn't mention anything besides the standard #include <sys/file.h>.

What compiler flags do I need to specify to get a program using flock() to work on Solaris 5.10?

ahawkthomas
  • 656
  • 3
  • 13
  • 26
  • 1
    Run your application that uses "flock()" under the `truss` or `sotruss` utilities to see what library and system calls it actually makes to lock the file(s) it accesses. You apparently have just the binary and anecdotal stories about what it's doing to lock files, but you need to actually know what's it's really doing if you want to interoperate with it. – Andrew Henle Nov 25 '19 at 15:50
  • @AndrewHenle, thank you for the suggestion, it's true, I should've spend some time to analyze the binary instead of blindly writing code. Syscall tracing sounds like a good start! – ahawkthomas Nov 26 '19 at 07:01

1 Answers1

2

flock() is quite old which isn't supported in Solaris systems, you have to choose other alternative syscall such as fcntl() as explained in here

man page says: Use of these interfaces should be restricted to only applications written on BSD platforms. Use of these interfaces with any of the system libraries or in multi-thread applications is unsupported.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • Hmm, thank you for the link, I'll try to convert my code to `fcntl()` if I don't find a reasonable way to get `flock()` support. One thing I should note, and I may not have made this clear in my question, `flock()` does seem to be supported on my Solaris box (5.10 is quite old). The problem is just getting it to work! – ahawkthomas Nov 25 '19 at 14:49