我正在为MySQL运行一个用户定义的函数插件。程序可以正常工作,除非您执行系统调用。我需要做一个系统调用来调用一个Python3脚本!使用system()或execl(),所有的系统调用都会返回-1并失败。
有人知道如何使我的C库MySQL UDF插件能够执行命令吗?我们完全被困在这里了,谢谢!
下面是我遇到问题的C代码:
# Written in C Language
#include <string.h>
#include <stdio.h>
#include <mysql.h>
bool udf_callpython_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
// None of these work...
int systemResult = system("echo hello");
//int systemResult = execl("/bin/bash", "bash", "-c", "echo hello again...", (char *) NULL);
//int systemResult = execl("/bin/ls", "ls", "-l", (char*)0);
//int systemResult = system("python3 python_script.py")
char str[100];
sprintf(str, "%d", systemResult);
strcpy(message, str);
return 1;
}
char* udf_callpython(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error) {
system("echo test > does_not_work.txt");
strcpy(result, "Hello, World!");
*length = strlen(result);
return result;
}我是这样编译这段代码的:
gcc -o udf_callpython.so udf_callpython.c -I/usr/include/mysql/ -L/usr/include/mysql/ -shared -fPIC然后,我将.so库文件复制到mysql的插件目录中:
sudo cp udf_callpython.so /usr/lib/mysql/plugin/ 然后,我在MySQL中创建如下函数:
CREATE FUNCTION udf_callpython RETURNS STRING SONAME "udf_callpython.so";然后,我使用这个过程来调用这个UDF:
DELIMITER $$
$$
USE test_db
$$
CREATE PROCEDURE example_insert_proc()
BEGIN
DECLARE result VARCHAR(255);
SET result = udf_callpython();
END;$$
DELIMITER ;然后,在输出中,您将看到-1:
mysql> CALL example_insert_proc();
ERROR 1123 (HY000): Can't initialize function 'udf_callpython'; -1(编辑:)我发现errno为13,这是被拒绝的权限。
发布于 2021-12-22 17:54:34
我想出了这个问题!如果其他人也有同样的问题.
我禁用了AppArmor (Ubuntu20.04):
sudo systemctl disable apparmor
sudo reboot now如果然后重新运行存储过程,system()调用将成功:
mysql> CALL example_insert_proc();
ERROR 1123 (HY000): Can't initialize function 'udf_callpython'; 0
mysql>https://stackoverflow.com/questions/70440573
复制相似问题