如何使用C代码或Python检查gnome密钥环状态?

时间:2018-07-17 02:02:29

标签: python c gnome gnome-keyring-daemon

当前使用带有流动代码的命令行gkey-lock / gkey-unlock来锁定和解锁gnome默认密钥环,我们如何实现gnome_keyring_info_get_is_locked()中的<gnome-keyring.h>来检查c或c中的锁定状态python

锁定钥匙圈-gkey-lock.c

#include <stdio.h>
#include <gnome-keyring.h>

int main() {
    GnomeKeyringResult lock_result = gnome_keyring_lock_all_sync();
    if (lock_result == GNOME_KEYRING_RESULT_OK) {
        printf("Successfully locked\n");
        return 0;
    } else {
        printf("Error locking keyring: %d\n", lock_result);
        return 1;
    }
}

解锁钥匙圈-gkey-unlock.c

#include <stdio.h>
#include <gnome-keyring.h>

int main() {
    GnomeKeyringResult lock_result = gnome_keyring_unlock_sync(NULL,NULL);
    if (lock_result == GNOME_KEYRING_RESULT_OK) {
        printf("Successfully unlocked\n");
        return 0;
    } else {
        printf("Error unlocking keyring: %d\n", lock_result);
        return 1;
    }
}

1 个答案:

答案 0 :(得分:0)

Gnome Keyring命令行工具(检查/锁定/解锁)

来源与发布:https://github.com/intika/gnome-keyring-tools

检查密钥环-gkey-check.c

#include <stdio.h>
#include <gnome-keyring.h>

int main() {

  GnomeKeyringInfo *info;
  GnomeKeyringResult gkr;

  gkr = gnome_keyring_get_info_sync(NULL, &info);
  if (gkr != GNOME_KEYRING_RESULT_OK) {
    printf("error\n");
    return -1;
  }
  if (gnome_keyring_info_get_is_locked(info)) {
    printf("locked\n");
    return 0;
  }
  else {
    printf("unlocked\n");
    return -1;
  }
  gnome_keyring_info_free(info);

}

使用Python进行检查-gkey-check.py

import gnomekeyring
print gnomekeyring.get_info_sync(gnomekeyring.get_default_keyring_sync()).get_is_locked()

如何构建

cc gkey-check.c -o gkey-check -Wall -I/usr/include/gnome-keyring-1 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -lgnome-keyring -lglib-2.0