File locking and file permissions

File locking and file permissions

Post by Bill Cri » Fri, 30 Jun 2000 04:00:00



I'm not a C programmer so it will be a great help to me if someone
can answer this question. I recently received the following from one
of the programmers where I work:

   unfortunately, linux does not support true file locking, as best I
   can tell. I don't know about the HPUX or the Sun. If you know of a
   way, tell me.

No file locking? Is this possible?
   Currently this programmer is using chmod to prevent other users
and processes from writing to a file, but the chmod fails if the file
is not owned by the user trying to lock it. This encourages users to
all log in as a single user to run this program (uggg!). All in all
it just plain doesn't work right.
   On a machine with the users umask set to 007 and good group
permissions throughout (i.e. directories are 770 and files are 660),
there's just got to be a way for a process owned by "A" to lock a
file owned by "B" while it writes and release it when it's done. Isn't
this exactly the purpose of "flock"? Or if not flock, then something
else more or less as direct?

Bill Cripe

 
 
 

File locking and file permissions

Post by brian moo » Fri, 30 Jun 2000 04:00:00


On Thu, 29 Jun 2000 00:21:29 GMT,

Quote:> I'm not a C programmer so it will be a great help to me if someone
> can answer this question. I recently received the following from one
> of the programmers where I work:

>    unfortunately, linux does not support true file locking, as best I
>    can tell. I don't know about the HPUX or the Sun. If you know of a
>    way, tell me.

> No file locking? Is this possible?
>    Currently this programmer is using chmod to prevent other users
> and processes from writing to a file, but the chmod fails if the file
> is not owned by the user trying to lock it. This encourages users to
> all log in as a single user to run this program (uggg!). All in all
> it just plain doesn't work right.
>    On a machine with the users umask set to 007 and good group
> permissions throughout (i.e. directories are 770 and files are 660),
> there's just got to be a way for a process owned by "A" to lock a
> file owned by "B" while it writes and release it when it's done. Isn't
> this exactly the purpose of "flock"? Or if not flock, then something
> else more or less as direct?

Unix doesn't have 'real' file locking.  It has 'advisory' locks of
various sorts, and so-called 'mandatory' locks.  Neither is -really-
mandatory (ie, even with mandatory locks, you can rm a locked file and
write a new file with the same name -- the directory permissions
control the permission to delete or rename a file, not the file or any
locks on it).

Yes, flock will lock a file: it's an advisory lock, though (ie, anyone
else wanting to access the file will need to use flock() to check to see
if they can do it without annoying another process -- they could just
skip that check and write to it anyway).

There is also the so-called 'mandatory' locks, done with the 'lockf' (or
fcntl) function.  These would stop 'most' programs from being *.
But note that others (such as an editor) may do something like the
following as part of their save:
   .  mv file file.bak
   .  write file
   .  (optionally rm the .bak)

Mandatory locks won't stop that, but would probably suffice in most
cases.

See /usr/src/linux/Documentations/mandatory.txt and locks.txt.

--
Brian Moore                       | Of course vi is God's editor.
      Sysadmin, C/Perl Hacker     | If He used Emacs, He'd still be waiting
      Usenet Vandal               |  for it to load on the seventh day.
      Netscum, Bane of Elves.

 
 
 

1. mail problem: file permissions for lock file

I've recently changed the file permissions on /usr/spool/mail to

drwxrwxrwx   2 bin      mail         512 Aug  3 12:02 /usr/spool/mail/
-rw-rw----   1 kessi    mail         647 Aug  3 12:02 /usr/spool/mail/kessi

to make xmailtool (a public domain X mailer) work. They used to be

drwxrwxr-x   2 bin      mail         512 Aug  3 11:24 /usr/spool/mail/
-rw-rw----   1 kessi    mail           0 Jun 23 12:08 /usr/spool/mail/kessi

before, and xmailtool was unable to create a lock file in /usr/spool/mail.
Now some people who are trying to mail to me have been getting messages back
with the error message

  --- The transcript of the session follows ---
bellmail: cannot create lock file /usr/spool/mail/kessi.lock: The file access
permissions do not allow the specified action.
550 kessi... Insufficient permission

Can anyone tell me what is exactly happening? And how to solve it? ;-)
I suppose the bellmail on my machine which is trying to create the
lock file. What is this lock file exactly good for? Why are the mails
sometimes delivered and sometimes not?

Thanks a ton for any hints! Best regards,

Alain

--

Paul Scherrer Institut, Zuerich, Switzerland

2. INN for OSR5.0.4 - where?

3. Newbie: Unix File Permissions on HTML files?

4. (no subject)

5. Question about file permissions - sharing data files between multiple users

6. ioctl, ENOENT and "quotas file"

7. file permissions & group membership when uploading files via FTP

8. unfriendly sendmail logs

9. Setting default file permissions on ftp'd files.

10. I get "file permissions deny server execution" on gif files in cgi-bin, why?

11. ftpd, default file permissions on creating a file on Solaris, SunOS 5.5.1

12. Seeking KHow do I get the bit value of a file's file access permissions?

13. default file permissions for ftp'd files