[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Readonly files



Steven Fullmer wrote:
Using chmod, chown and chgrp, a user can get permissions to modify files. Is there a way to 'lock' files in use as well? I know of a couple programs that create an entry into a .lock file where, when someone opens the same file, it is opened read-only. Is this a feature unique to this program, or is it a RH standard, so no two people can modify certain files? If there is, is there a command that is sent to lock and/or unlock the files/directories in question?


The creation of a lock file is unique to certain programs, and it's normally used to prevent two programs from trying to fight over a device (such as a modem or printer).


Record locking (where a program acquires a write lock on a file so that only it can modify it) is and has always been a part of Linux. The programs using it must both adhere to the standard (called "cooperative locking"). It's a part of the standard file I/O library and is accessed via the fcntl() system call (see "man fcntl", specifically the parts about "F_GETLK", "F_SETLK" and "F_SETLKW".


Meaning, then, that if a program I run doesn't support cooperative locking, it has the potential of accessing and modifying the file in question while another (which does support this standard) is in the middle of using it?

Yup, that's what it means. Other lock manipulators are flock() and lockf().

The kernel does prevent simultaneous writes to any given _block_, but
it's on a first-come, first-served basis and potential clobbers can
occur.  'Tis always good to:

	...
	fd = open(filename, O_RDWR);
	...
	lockf(fd, F_LOCK);
	x = write(fd, buff, size);
	lockf(fd, F_UNLOCK);

just to prevent these problems.  I usually have that set up in a macro
somewhere in my code.
----------------------------------------------------------------------
- Rick Stevens, Senior Systems Engineer     rstevens vitalstream com -
- VitalStream, Inc.                       http://www.vitalstream.com -
-                                                                    -
-         "The Schizophrenic: An Unauthorized Autobiography"         -
----------------------------------------------------------------------




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]