/var/lib/dpkg/lockはどのように機能しますか?

/var/lib/dpkg/lockはどのように機能しますか?

/var/lib/dpkg/lock は、「パッケージマネージャが動作している間」ロックを保持するファイルです。しかし、このシステムはどのように機能しますか? Linuxを使用するたびに/var/lib/dpkg/lockがあります。 dpkgのパッケージマネージャの1つを使用しても変更はありません。だから実際に何をしているのかはわかりません。

ベストアンサー1

わかりませんが、flock().system呼び出しを介して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;
      }

DB修正.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;
}

ファイル.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;
}

dpkg.h:

#define LOCKFILE          "lock"

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).

おすすめ記事