/var/lib/dpkg/lock 是在“包管理器正在工作”时持有锁的文件。但是这个系统是如何运作的呢?每次运行 Linux 时,我都有 /var/lib/dpkg/lock 。当我使用 dpkg 的包管理器之一时,我没有任何更改。所以我看不到它在行动。
我不确定,但这很可能是通过flock()
. 该flock()
系统调用创建一个文件的咨询锁。如果另一个应用程序试图获得对文件的锁定,内核将阻塞直到原始锁定消失,或者EWOULDBLOCK
如果LOCK_NB
给出选项则返回。这种锁定机制将允许使用锁定文件而无需删除和重新创建它。
更新:检查源并验证它是建议锁定,但它不flock()
直接使用。fcntl
用来:
查询.c:
if (modstatdb_is_locked())
puts(_(
"Another process has locked the database for writing, and might currently be\n"
"modifying it, some of the following problems might just be due to that.\n"));
head_running = true;
}
Run Code Online (Sandbox Code Playgroud)
dbmodify.c:
modstatdb_is_locked(void)
{
int lockfd;
bool locked;
if (dblockfd == -1) {
lockfd = open(lockfile, O_RDONLY);
if (lockfd == -1)
ohshite(_("unable to open lock file %s for testing"), lockfile);
} else {
lockfd = dblockfd;
}
locked = file_is_locked(lockfd, lockfile);
/* We only close the file if there was no lock open, otherwise we would
* release the existing lock on close. */
if (dblockfd == -1)
close(lockfd);
return locked;
}
Run Code Online (Sandbox Code Playgroud)
文件.c:
file_is_locked(int lockfd, const char *filename)
{
struct flock fl;
file_lock_setup(&fl, F_WRLCK);
if (fcntl(lockfd, F_GETLK, &fl) == -1)
ohshit(_("unable to check file '%s' lock status"), filename);
if (fl.l_type == F_WRLCK && fl.l_pid != getpid())
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
dpkg.h:
#define LOCKFILE "lock"
Run Code Online (Sandbox Code Playgroud)
从fcntl
联机帮助页:
Advisory locking
F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test for the existence of record locks (also known as file-segment or file-region locks). The third
argument, lock, is a pointer to a structure that has at least the following fields (in unspecified order).
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
955 次 |
最近记录: |