filelock/i_fcntl_locking.cThis is filelock/i_fcntl_locking.c (Listing 55-2, page 1130), an example from the book, The Linux Programming Interface. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.
|
#include <sys/stat.h> #include <fcntl.h> #include "tlpi_hdr.h" #define MAX_LINE 100
static void displayCmdFmt(void) { printf("\n Format: cmd lock start length [whence]\n\n"); printf(" 'cmd' is 'g' (GETLK), 's' (SETLK), or 'w' (SETLKW)\n"); printf(" 'lock' is 'r' (READ), 'w' (WRITE), or 'u' (UNLOCK)\n"); printf(" 'start' and 'length' specify byte range to lock\n"); printf(" 'whence' is 's' (SEEK_SET, default), 'c' (SEEK_CUR), " "or 'e' (SEEK_END)\n\n"); }
int main(int argc, char *argv[]) { int fd, numRead, cmd, status; char lock, cmdCh, whence, line[MAX_LINE]; struct flock fl; long long len, st; if (argc != 2 || strcmp(argv[1], "--help") == 0) usageErr("%s file\n", argv[0]); fd = open(argv[1], O_RDWR); if (fd == -1) errExit("open (%s)", argv[1]); printf("Enter ? for help\n"); for (;;) { /* Prompt for locking command and carry it out */ printf("PID=%ld> ", (long) getpid()); fflush(stdout); if (fgets(line, MAX_LINE, stdin) == NULL) /* EOF */ exit(EXIT_SUCCESS); line[strlen(line) - 1] = '\0'; /* Remove trailing '\n' */ if (*line == '\0') continue; /* Skip blank lines */ if (line[0] == '?') { displayCmdFmt(); continue; } whence = 's'; /* In case not otherwise filled in */ numRead = sscanf(line, "%c %c %lld %lld %c", &cmdCh, &lock, &st, &len, &whence); fl.l_start = st; fl.l_len = len; if (numRead < 4 || strchr("gsw", cmdCh) == NULL || strchr("rwu", lock) == NULL || strchr("sce", whence) == NULL) { printf("Invalid command!\n"); continue; } cmd = (cmdCh == 'g') ? F_GETLK : (cmdCh == 's') ? F_SETLK : F_SETLKW; fl.l_type = (lock == 'r') ? F_RDLCK : (lock == 'w') ? F_WRLCK : F_UNLCK; fl.l_whence = (whence == 'c') ? SEEK_CUR : (whence == 'e') ? SEEK_END : SEEK_SET; status = fcntl(fd, cmd, &fl); /* Perform request... */ if (cmd == F_GETLK) { /* ... and see what happened */ if (status == -1) { errMsg("fcntl - F_GETLK"); } else { if (fl.l_type == F_UNLCK) printf("[PID=%ld] Lock can be placed\n", (long) getpid()); else /* Locked out by someone else */ printf("[PID=%ld] Denied by %s lock on %lld:%lld " "(held by PID %ld)\n", (long) getpid(), (fl.l_type == F_RDLCK) ? "READ" : "WRITE", (long long) fl.l_start, (long long) fl.l_len, (long) fl.l_pid); } } else { /* F_SETLK, F_SETLKW */ if (status == 0) printf("[PID=%ld] %s\n", (long) getpid(), (lock == 'u') ? "unlocked" : "got lock"); else if (errno == EAGAIN || errno == EACCES) /* F_SETLK */ printf("[PID=%ld] failed (incompatible lock)\n", (long) getpid()); else if (errno == EDEADLK) /* F_SETLKW */ printf("[PID=%ld] failed (deadlock)\n", (long) getpid()); else errMsg("fcntl - F_SETLK(W)"); } } }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.