Thursday, 9 February 2017

File override protection with lockfile

I recently came across a requirement wherein I needed to ensure that only a single user could make permanent changes to a file at a given point of time and that changes made by other users trying to access the file concurrently should be discarded.

One method of doing this in vim is by turning "lock on" while you are editing the file. This tries to put an exclusive lock on the file. If another user opens up to edit the same file, vim presents them with a warning but lets the users edit the files if they type in "edit anyway". To find a way around it I did some research and came across the lockfile command whose usage I'll demonstrate in this article.

The lockfile command is provided by the procmail package. So, let's install that first.

[root@still ~]# yum install procmail
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
 * updates: mirrors.vhost.vn
Resolving Dependencies
--> Running transaction check
---> Package procmail.x86_64 0:3.22-35.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================================================
 Package                                                   Arch                                                    Version                                                       Repository                                             Size
=========================================================================================================================================================================
Installing:
 procmail                                                  x86_64                                                  3.22-35.el7                                                   base                                                  171 k

Transaction Summary
=========================================================================================================================================================================
Install  1 Package

Total download size: 171 k
Installed size: 349 k
Is this ok [y/d/N]: y
Downloading packages:
procmail-3.22-35.el7.x86_64.rpm                                                                                                                                                                                       | 171 kB  00:00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : procmail-3.22-35.el7.x86_64                                                                                                                                                                                               1/1
  Verifying  : procmail-3.22-35.el7.x86_64                                                                                                                                                                                               1/1

Installed:
  procmail.x86_64 0:3.22-35.el7

Complete!


An in depth discussion of what procmail is and what it does isn't of relevance to this post but still I'll share a brief description of the package which is as follows:
Procmail can be used to create mail-servers, mailing lists, sort your incoming mail into separate folders/files, preprocess your mail, start any programs upon mail arrival (e.g. to generate different chimes on your workstation for different types of mail) or selectively forward certain incoming mail automatically to someone.

The lockfile command is a conditional semaphore creator. When we want to lock a file for a process, we create it's lock file via execution of the lockfile command followed by the filename with the .lock extension. After that we can perform required actions on the file. Once our task with the file is completed, we can remove the lock file via the rm command and it will effectively unlock the file.

Here is a demonstration:

Create test file:
[sa@still ~]$ cat filename
a test file

Lock the file and edit again:
[sa@still ~]$ lockfile filename.lock
[sa@still ~]$ vim filename

Open another instance of vi and try to edit file. The following warning is displayed:



Click edit any way, make changes and save file.

Now write changes and save the original vi session. You see the following prompt:



Press yes and save the file.

View file contents & remove lock file:

[sa@still ~]$ cat filename
a test file
still testing
[sa@still ~]$ rm filename.lock
rm: remove write-protected regular file ‘filename.lock’? y

Note: only the changes which were made by the process that locked the file will be saved!

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...