How to Lock a Directory in C on a Linux Machine

c++ solution to locking a directory - linux

Not possible in standard C++ at all.

Under linux, use setuid permissions on the executable, so it runs in context of its owner. Then you can lock down access permissions for the directory, so it is only accessible to the owner of the executable.

Of course, this doesn't stop users using your program from messing with your cache. You need to design your program so it prevents inappropriate actions by users. And make sure the owner account (which can be set up specifically for your application) does not have more privileges than it needs.

Locking files in linux with c/c++

Yes, this is possible.

The Unix way to do this is via fcntl or lockf.
Whatever you choose, make sure to use only it and not mix the two. Have a look at this question (with answer) about it: fcntl, lockf, which is better to use for file locking?.

If you can, have a look at section 14.3 in Advanced Programming in the UNIX Environment.

Placing exclusive lock on read-only files in Linux

Any access (read or write) will suffice for a Linux flock syscall to place a lock on file, unlike fcntl lock which requires read access for a read lock and write for a write lock.

You might be using libc that emulates flock on top of fcntl. To get what you need, invoke system call directly through syscall:

#include <sys/syscall.h>
#include <unistd.h>

// from include/uapi/asm-generic/fcntl.h
#define SYS_LOCK_SH 1
#define SYS_LOCK_EX 2
#define SYS_LOCK_UN 8

static int sys_flock(int fd, int op)
{
return (int) syscall(SYS_flock, fd, op);
}

As the result, the following program must succeed:

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>

#define SYS_LOCK_EX 2

static long sys_flock(int fd, int flags)
{
return (int) syscall(SYS_flock, fd, flags);
}

int main(void)
{
int fd = open("/etc/hosts", O_RDONLY);
int ret = sys_flock(fd, SYS_LOCK_EX);

if (ret) {
errno = -ret;
perror("flock");
return 1;
}
}

Why does producer need to lock directory to create new files in Linux?

Producing a file is not atomic. First, an empty file is created. Then its content is written to it, possibly over multiple writes. Each write modifies the file, so your claim the file is never modified is false.

A lock is being used to ensure that the consumer only picks up complete files.[1]

There are alternative approaches to locking that could be used. For example, the producer could create files suffixed with .tmp, then rename (not copy) the file to the correct name when the file is complete. If the consumer ignores files ending in .tmp, then it will only pick up complete files.[2]



  1. Complete in the sense that the producer will never add to it again. But that doesn't mean the file contains all that it should. If the producer crashes, and if that automatically releases the lock, the consumer may receive only a part of the intended contents.

  2. Truly complete. Not only will the producer never add to it again, but everything the producer intended to write to the file was written to it. (Short of a power failure at exactly the wrong moment or a similarly drastic event.)



Related Topics



Leave a reply



Submit