
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Introduction to File Locking in Linux
Introduction
File locking is a mechanism used to restrict access to a file to only one process or user at a time. It is essential in multi-user systems to avoid conflicts when multiple processes try to access the same file simultaneously. In Linux, file locking is implemented through the use of locks. Lock prevents other processes from accessing a file until the lock is released.
In this article, we will explore file locking in Linux and how to implement it using C code. We will discuss the different types of file locks, how to create and release locks, and how to handle errors that may arise during the locking process.
Types of File Locks
In Linux, there are two types of file locks −
Advisory locks
Mandatory locks.
Implementing Advisory Locks Using fcntl()
Advisory locks are file locks that allow processes to request a lock on a file, but they do not prevent other processes from accessing or modifying the file. Advisory locks are helpful when multiple processes need to access a file, but each process needs to ensure that it has exclusive access to a particular section of the file.
The fcntl() function is used to set advisory locks on files. The fcntl() function can be used to acquire a lock on a particular section of a file, or on the entire file. The following code example shows how to set an advisory lock on a file using the fcntl() function −
Example
#include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { int fd = open(argv[1], O_RDWR); struct flock fl; fl.l_type = F_WRLCK; // Set a write lock fl.l_whence = SEEK_SET; // Start at beginning of file fl.l_start = 0; // Offset from beginning of file fl.l_len = 0; // Lock entire file fl.l_pid = getpid(); // Set process ID fcntl(fd, F_SETLKW, &fl); // Set the lock printf("File locked
"); sleep(10); // Sleep for 10 seconds fl.l_type = F_UNLCK; // Release the lock fcntl(fd, F_SETLK, &fl); // Unlock the file printf("File unlocked
"); close(fd); return 0; }
The above code sets a write lock on a file specified by the command-line argument. The fcntl() function is called with the F_SETLKW flag to acquire the lock. The process then sleeps for 10 seconds to simulate some work being done on the file. Finally, the lock is released using the F_SETLK flag.
cd Desktop/ $ touch filename.c $ gcc filename.c -o test $ ./test
Output
File locked File unlocked
Implementing Mandatory locks
Mandatory locks are file locks that prevent other processes from accessing or modifying a file until the lock is released. Mandatory locks are set by the kernel and cannot be overridden by processes. Mandatory locks are helpful when a file is critical and must not be modified by any other process.
The fcntl() function is also used to set mandatory locks on files. The F_SETLK flag is used to set a mandatory lock on a file. The following code example shows how to set a mandatory lock on a file using the fcntl() function −
Example
#include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { int fd = open(argv[1], O_RDWR); struct flock fl; fl.l_type = F_WRLCK; // Set a write lock fl.l_whence = SEEK_SET; // Start at beginning of file fl.l_start = 0; // Offset from beginning of file fl.l_len = 0; // Lock entire file fl.l_pid = getpid(); // Set process ID fcntl(fd, F_SETLK, &fl); // Set the lock printf("File locked
"); sleep(10); // Sleep for 10 }
Output
File locked File unlocked
All Locks in a Linux System
There are two ways to see all locks in a Linux System
Islocks Command
cat /proc/locks
Using Islocks Command
Before using Islocks command lets understand the work of Islocks command .So, with thw help of Islocks command you can see all current file locks in your Linux system.
$ lslocks COMMAND PID TYPE SIZE MODE M START END PATH cron 752 FLOCK WRITE 0 0 0 /run... nautilus 2164 POSIX 3.9M READ 0 1073741826 1073742335 /home/papan/.cache/tracker/meta.db nautilus 2164 POSIX 32K READ 0 128 128 /home/papan/.cache/tracker/meta.db-shm whoopsie 955 FLOCK WRITE 0 0 0 /run/lock... snapd 787 FLOCK WRITE 0 0 0 /... tracker-miner-f 1480 POSIX 3.9M READ 0 1073741826 1073742335 /home/papan/.cache/tracker/meta.db tracker-miner-f 1480 POSIX 32K READ 0 128 128 /home/papan/.cache/tracker/meta.db-shm update-notifier 2049 FLOCK WRITE 0 0 0 /run/user/1000/update-notifier.pid
The list above displays information on currently locked files within the system, including the lock type, file path, and the process responsible for holding the lock.
Using cat /proc/lock
In addition to using the flock system call in our programs, we can also view the locks that are currently held on files in the Linux operating system using the /proc/locks file. The /proc/locks file is a virtual file system that provides information about the locks that are held on files in the system.
To view the contents of the /proc/locks file, we can simply open it in a text editor or use the cat command in the terminal −
$ cat /proc/locks 1: FLOCK ADVISORY WRITE 2049 00:37:94 0 EOF 2: POSIX ADVISORY READ 1480 08:05:280388 128 128 3: POSIX ADVISORY READ 1480 08:05:280384 1073741826 1073742335 4: FLOCK ADVISORY WRITE 787 08:05:703530 0 EOF 5: FLOCK ADVISORY WRITE 955 00:1b:5 0 EOF 6: FLOCK ADVISORY WRITE 752 00:19:1123 0 EOF
Conclusion
File locking is a critical mechanism in Linux that helps to prevent data corruption and ensure data consistency in multi-process environments. In this article, we have explored the basics of file locking in Linux, including the different types of locks, how to use file locking in C programs, and how to view information about file locks using the lslocks command.
- Related Articles
- Introduction to File MIME Types on Linux
- Introduction to fzf command in Linux
- Introduction to Bash Array in Linux
- Introduction to tee Command in Linux
- Introduction to Bash Globbing on Linux
- Append Lines to a File in Linux
- How to Copy File Permissions and Ownership to Another File in Linux?
- Advanced File Permissions in Linux
- How to Find Out File Types in Linux
- Multiple Granularity Locking in DBMS
- File globbing in Linux in C++
- How to sort a file in-place in Linux?
- How to Randomize Lines in a File in Linux
- Extracting a WAR File in Linux
- Creating a Temporary File in Linux
