我对C语言比较陌生,不明白为什么这个程序seg会出错。这可能是我犯的一个愚蠢的错误,但似乎找不到答案。
我也知道使用我的嵌入方法是不寻常的,但这是因为我完全熟悉Python3和易用性。
#define PY_SSIZE_T_CLEAN
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
//#define PAM_SM_SESSION
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include </usr/include/python3.6m/Python.h>
/* expected hook */
/*
PAM_EXTERN int pam_sm_setcred( pam_handle_t *pamh, int flags, int argc, const char **argv ) {
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) {
printf("Acct mgmt\n");
return PAM_SUCCESS;
}
*/
/* expected hook, this is where custom stuff happens */
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv )
{
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 != 0){ // 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.
if (ret1 == 1){
Py_Finalize();
return PAM_SUCCESS;
}
else{
Py_Finalize();
return PAM_AUTH_ERR;
}
}
else{
//printf("Error: can't find file!\n");
return 1;
}
Py_Finalize();
return 0;
}
发布于 2020-02-19 15:31:25
您已经定义了指针,但尚未将它们分配给内存地址。
PyObject *mymod, *func1, *ret1;
代码中的这一行创建了一个名为mymod的指针,该指针可以指向包含PyObject的内存,但是您还没有给它提供内存地址。
我不知道调用函数是否会正确返回指针,所以当你试图把任何东西放在那里时,如果你试图将一个变量赋给一个没有内存地址的指针,它会给出分段错误。
在不知道故障发生的情况下,我只能说这么多。尝试将printf语句放在所有3个指针的赋值之前,并查看。
https://stackoverflow.com/questions/60303585
复制