[英]MySQL UDF Plugin cannot Execute Shell Commands (with system() or execl())
我正在為 MySQL 運行用戶定義的 Function 插件。 該程序運行良好,除非您進行系統調用。 我需要進行系統調用才能調用 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
然后我將 that.so 庫文件復制到 mysql 的插件目錄:
sudo cp udf_callpython.so /usr/lib/mysql/plugin/
然后我在 MySQL 中創建 function,如下所示:
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 ;
然后,在 output 上,您將看到 -1:
mysql> CALL example_insert_proc();
ERROR 1123 (HY000): Can't initialize function 'udf_callpython'; -1
(編輯:)我發現 errno 是 13,這是 Permission denied。
謝謝你。 埃里克·W。
我想通了這個問題。 我正在回答這是其他人有同樣問題的情況......
我禁用了 AppArmor(Ubuntu 20.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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.