File Locking for editing ??

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
kccoleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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
kccoleAuthor Commented:
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
dhughes111797Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ahoffmannCommented:
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
kccoleAuthor Commented:
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
kccoleAuthor Commented:
Adjusted points to 75
0
kccoleAuthor Commented:
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
dhughes111797Commented:
never heard of it.. Are you sure it's not a local thing? (It's not
on any of our Solaris2 boxes)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.