You are not logged in.
Yesterday I thought I would need to use a mandatory lock and decided to follow the steps on http://www.hackinglinuxexposed.com/arti … 30623.html. It is no longer of immediate concern but I am starting to wonder what I was doing wrong. Guided by http://www.unixguide.net/unix/programming/2.5.shtml I have the following code called "locker.c":
#include <stdio.h>
#include <fcntl.h>
struct flock *file_lock(short type, short whence) {
static struct flock ret;
ret.l_type = type;
ret.l_start = 0;
ret.l_whence = whence;
ret.l_len = 0;
ret.l_pid = getpid();
return &ret;
}
int main(int argc, char **argv) {
if (argc > 1) {
int fd = open(argv[1], O_RDONLY);
fcntl(fd, F_SETLKW, file_lock(F_WRLCK, SEEK_SET));
while (1) {
scanf("%c", NULL);
}
}
}
I first mount my ext2 /boot partition with mandatory locks enabled:
mount -oremount,mand /boot
I then enable mandatory locks on /boot/foo
chmod g+s,g-x /boot/foo
I then try to lock /boot/foo
locker /boot/foo
however while this program is running I can still open foo in a text editor and even save changes to it. Does anyone know how to fix this?
Last edited by ConnorBehan (2011-05-01 22:42:49)
6EA3 F3F3 B908 2632 A9CB E931 D53A 0445 B47A 0DAB
Great things come in tar.xz packages.
Offline
Returning the address of ret from the file_lock() function invokes undefined behaviour. (Sorry, didn't see the "static".) As well, you should be checking the return value of fcntl().
Last edited by tavianator (2011-05-01 22:11:20)
Offline
Ah, thanks for reminding me that it returns a value! Yes, fcntl() was the function that failed and I think that's because it needs write access for a write lock. Changing O_RDONLY to O_WRONLY makes everything work.
6EA3 F3F3 B908 2632 A9CB E931 D53A 0445 B47A 0DAB
Great things come in tar.xz packages.
Offline