【发布时间】:2022-01-23 04:15:30
【问题描述】:
我正在为 MySQL 运行用户定义函数插件。该程序运行良好,除非您进行系统调用。我需要进行系统调用才能调用 Python3 脚本! 我使用 system() 或 execl() 执行的所有系统调用都返回 -1 并失败。
有人知道如何让我的 C 库 MySQL UDF 插件能够执行 shell 命令吗?我们完全坚持这一点,谢谢!
这是我遇到问题的 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,就是Permission denied。
【问题讨论】:
标签: python mysql c plugins triggers