[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • Last Modified:

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
0
kccole
Asked:
kccole
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now