Solved

File Locking for editing ??

Posted on 1997-11-26
8
549 Views
Last Modified: 2013-12-27
Hello,

    I'm running on a Sun SPARCstation 5 (SunOS 5.5.1) can
someone please tell me the best way to control this situation: There are several files that several people need to change concurrently (using vi and emacs).  These 3 people
are members of a unique unix group.  How can we ensure that
we are not editing the same file at the same time? This is not source code and we don't want to use SCCS or any other
source control system - We're looking for some file locking
mechanism; e.g., John is ready to edit file "foo.doc" he
executes <unix.file.locking.command> he then edits the document; after finished with edits he unlocks the file <unix.file.locking.command -u>.

Thanks !

kcc
0
Comment
Question by:kccole
8 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
lock="$NEWSCTL/LOCKxxx"         # modify name as appropriate
ltemp="$NEWSCTL/L.$$"
echo $$ >$ltemp
trap "rm -f $ltemp ; exit 0" 0 1 2 15
while true
do
        if ln $ltemp $lock
        then
                trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
                break
        fi
        sleep 30
done

0
 

Author Comment

by:kccole
Comment Utility
Thanx for your reply but unfortunately I'm left with too many questions as to what each line of code does exactly and how it can be used in the example I supplied (in other words please
make it more idiot proof! with comments for code and an explanation of how to implement/execute when needing to edit the
 file.

Thanx Much!
kcc
0
 
LVL 1

Accepted Solution

by:
dhughes111797 earned 70 total points
Comment Utility
I wrote something like this in C that works. All you have to
do is set an alias for your editor of choice (emacs, vi)
that runs this program instead. Then your editor environment variable is set to your real editor.
e.g.
setenv EDITOR vi
alias vi /usr/bin/vi-lock

Try it out and see if it works for you.

#include <stdio.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
 
extern int errno;
 
main(int argc, char *argv[])
{
        int fd, rc;
        char answer[2];
        char edit_string[80];
        struct flock fl;
        char *editor;
        char *user;
 
        fl.l_type = F_WRLCK;
        fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = 0;

if (argc < 2) {
perror("usage vi-lock file [ file2 [ file3 ...]]");
exit(-5);
}

if ((editor = getenv("EDITOR")) == NULL)
editor = "vi";
if ((user = getenv("USER")) == NULL)
user = "root";


while (--argc > 0) {
*argv++;        /* skip argv[0] */

fd = open(*argv, O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
fprintf(stderr, "Creating new file:  %s\n", *argv);
sleep(1);
} else {
close(fd);
}

fd = open(*argv, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Couldn't open file %s for writing\n", *
argv);
exit(-2);
}
if (((rc = fcntl(fd, F_SETLK, &fl)) < 0) && (errno != EINVAL)) {
perror("Locking");
fcntl(fd, F_GETLK, &fl);
printf("File %s is currently locked by pid %d. Wait? (Y/
N) ",
*argv, fl.l_pid);
scanf("%c",answer);
if (*answer == 'Y' || *answer == 'y') {
rc = fcntl(fd, F_SETLKW, &fl);
} else {
continue;
}
                } else if (rc < 0 && errno != EINVAL) {
                        perror("Locking problem");
                        printf("errno %d fd %d\n", errno, fd);
                        exit(1);
                }
 
 
                sprintf(edit_string, "%s %s\n", editor, *argv);
                rc = system(edit_string);
                if (rc < 0) {
                        perror("couldn't exec vi");
                        exit(-3);
                }
 
                fl.l_type = F_UNLCK;
                rc = fcntl(fd, F_SETLK, &fl);
                if (rc < 0) {
                        perror("couldn't unlock file");
                        exit(-4);
                }
        }
 
}

0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
alias vi to vi-lock (for example)
shell script for vi-lock (not security proofed):

#! /bin/sh
file=$1
chmod g-w $file
chmod o-w $file
/bin/vi $file
chmod g+w $file
chmod o+w $file
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:kccole
Comment Utility
ahoffman,

    Doesn't your script work only if the owner of the file isn't
a possible editor of the file ? e.g.,

    unix id=sammi   group id=sammi

Sammi decides to add 3 other user's (unix id's) to his group:

    group sammi ==>  sammi, franki, noel, pransor

If franki executes your script and edits a common file
having the permission of 775 (before executing it) then
what will prevent Sammi from editing the same file at the same
time, thus corrupting the changes franki has made?

thanx!
kcc
0
 

Author Comment

by:kccole
Comment Utility
Adjusted points to 75
0
 

Author Comment

by:kccole
Comment Utility
Hello All,
 
    I've just learned of a utility called "led" on my system
that allows the very thing I need to do.  I just thought I'd poll the readers to see if anyone else knew of its existence too and
if so why do home grown stuff when something is already there to
use??

Thanx Again.,
kcc
0
 
LVL 1

Expert Comment

by:dhughes111797
Comment Utility
never heard of it.. Are you sure it's not a local thing? (It's not
on any of our Solaris2 boxes)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now