python - C子进程中的当前目录
问题描述
我正在编写一个自定义 PAM 模块。
我已经按照 Linux-PAM 的要求编写了一个共享对象 (.so) 文件。这个 .so 文件的作用是调用嵌入式 Python 来打开我的面部识别,并且根据结果,将返回 PAM_SUCCESS 或 PAM_AUTH_ERR
在 /etc/pam.d/sudo 文件中,我告诉 PAM 该文件位于(/home/berns/2020-ca326-cberns-fileencryption-with-opencv/PAM/pam_authnew.so)中。这没关系,当输入 sudo 时,我可以看到我自己的个人错误声明正在打印,说它无法加载 Python 文件。
我遇到的问题是,面部识别代码与 .so 文件所在的目录完全不同。(../代码/面部)。我在我的 C 代码中使用了 chdir 命令,但它似乎没有将目录更改回 Python 面部文件所在的位置以提供成功与否。
有什么我想念的吗?
C代码如下:
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define GetCurrentDir getcwd
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include </usr/include/python3.6m/Python.h>
int main(int argc, char** argv){
}
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv ){
char *result;
chdir("../code/facial"); // this changes it to the correct directory to execute
//dlopen("/usr/lib/x86_64-linux-gnu/libpython3.6m.so",RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); // Starts python interpreter
PyRun_SimpleString("import os\nimport sys\nsys.path.append(os.getcwd())"); // lets python know where we are
PyObject *mymod, *func1, *ret1;
mymod = PyImport_ImportModule("pam_detect"); // This is the .py
if (mymod != NULL){ // check if the file file was loaded
func1 = PyObject_GetAttrString(mymod, "detect"); // hel is the function name in the file you declared earlier
ret1 = PyObject_CallObject(func1, NULL); // Null because the function doesnt take an argument.
result = PyUnicode_AsUTF8(ret1);
//printf("%s\n", result);
if (strcmp(result, "success") == 0){
Py_Finalize();
return PAM_SUCCESS;
}
else{
Py_Finalize();
return PAM_AUTH_ERR;
}
}
else{
printf("Error: can't find file!\n");
}
Py_Finalize();
return 0;
}
来自下方终端的错误响应
这是由于子进程没有更改自己运行空间之外的目录吗?
同样值得注意的是,如果我编译 C 代码而不将其设为 .so ,我可以完美地加载 Python 文件并识别我的脸。
解决方案
推荐阅读
- reactjs - 如何更新 MobX 中的嵌套存储
- python - 如何从单个 Flask 路由执行两个 SQL 命令
- javascript - 回显的 HTML 表单不调用 onsubmit 函数
- jenkins - 如何在jenkins中动态更改sonarqube projectVersion号
- android - Android:无法通过文本或其他应用程序从原始文件夹发送音频文件
- networking - 码头工人网络
- ionic-framework - Ionic - 滚动到特定元素 Virtual Scroll
- node.js - 无法安装最新版本的nodejs
- sql - 如何在 postgres 中在其中一列的值为“true”的两列上添加唯一约束
- scikit-learn - 如何预测离群值比例的最佳值?