python - 如何使用 c 代码或 python 检查 gnome 密钥环状态?
问题描述
目前使用命令行 gkey-lock/gkey-unlock 和流动代码来锁定和解锁 gnome 默认密钥环,我们如何实现gnome_keyring_info_get_is_locked()
from<gnome-keyring.h>
在 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;
}
}
解决方案
Gnome Keyring Command Line Tools (check/lock/unlock)
Sources & Release : https://github.com/intika/gnome-keyring-tools
Check Keyring - 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);
}
Check With Python - gkey-check.py
import gnomekeyring
print gnomekeyring.get_info_sync(gnomekeyring.get_default_keyring_sync()).get_is_locked()
How to build
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
推荐阅读
- c++ - 提升 ASIO SSL、TLS https 服务器,根据 SNI 使用不同的证书在单个 IP 地址中托管多个 https 站点
- r - 如何从特定列创建具有唯一值的向量?
- javascript - 尝试在对象中的特定键值对上使用 JavaScript 嵌套循环
- dart - 如何初始化静态变量并为其设置各种值?
- python-3.x - 使用字典和基序查找包含特定序列的基因
- python - 如何为输入创建计时器?
- c - 是否有 C 方法来拆分单行输入?
- r - 如何获取一个数据框中的行顺序以匹配另一个数据框中的其他行?
- nestjs - nestjs+nunjucks 如何渲染异步模板
- unity3d - 如何在按钮出现统一之前创建时间延迟