gpt4 book ai didi

mysql UDF : fopen = permission denied

转载 作者:行者123 更新时间:2023-11-29 02:07:48 25 4
gpt4 key购买 nike

编辑:我已经重写了这个问题,因为我没有得到答案,我目前正在努力缩小问题的范围。

我正在尝试创建一个 mysql UDF function检查服务器端是否存在文件。此函数调用“打开/关闭”。即使文件可读,它也不起作用。

我把这个函数的代码放在下面:

#include <mysql.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

/* The initialization function */
my_bool fileExists_init(
UDF_INIT *initid,
UDF_ARGS *args,
char *message
)
{
/* check the args */
if (!(args->arg_count == 1 &&
args->arg_type[0] == STRING_RESULT
))
{
strncpy(message,"Bad parameter expected a string",MYSQL_ERRMSG_SIZE);
return 1;
}
initid->maybe_null=1;
initid->ptr= NULL;
return 0;
}

/* The deinitialization function */
void fileExists_deinit(UDF_INIT *initid)
{
}

#define MAX_RESULT_LENGTH 250
char *fileExists(
UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *length, char *is_null, char *error)
{
//bad filename
if(args->args[0]==NULL || args->lengths[0]==0 || args->lengths[0] >= FILENAME_MAX )
{
strncpy(result,"#BAD_INPUT",MAX_RESULT_LENGTH);
}
else
{
char filename[FILENAME_MAX+1];
int err;
int in;
//create a NULL terminated string
memcpy(filename,args->args[0],args->lengths[0]);
filename[args->lengths[0]]=0;
errno=0;
in=open(filename,O_RDONLY|O_NDELAY);
err=errno;
if(in<0)
{
snprintf(result,MAX_RESULT_LENGTH,"#ERR:\"%s\":\"%s\".",strerror(err),filename);
}
else
{
close(in);
snprintf(result,MAX_RESULT_LENGTH,"OK:\"%s\".",filename);
}
}
*length=strlen(result);
return result;
}

制作:

gcc -Wall -DMYSQL_VERSION -fPIC -shared `mysql_config --cflags` -o `mysql_config --plugindir`/libfileexists.so udffileexists.c  `mysql_config --libs `

测试:

ok,mysql可以打开一些文件了:

mysql>  create function fileExists RETURNS STRING SONAME 'libfileexists.so'; select fileExists("/etc/mysql/my.cnf"); drop function fileExists;
Query OK, 0 rows affected (0.00 sec)

+---------------------------------+
| fileExists("/etc/mysql/my.cnf") |
+---------------------------------+
| OK:"/etc/mysql/my.cnf". |
+---------------------------------+
1 row in set (0.00 sec)



mysql> create function fileExists RETURNS STRING SONAME 'libfileexists.so'; select fileExists("/tmp/file.txt"); drop function fileExists;
Query OK, 0 rows affected (0.00 sec)

+-------------------------------+
| fileExists("/tmp/file.txt") |
+-------------------------------+
| OK:"/tmp/file.txt". |
+-------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

好的,没问题,它可以工作,我可以在/tmp/ 中打开() file.txt

ls -la /tmp 
drwxrwxrwt 16 root root 4096 2010-05-28 15:45 .
-rw-r--r-- 1 lindenb lindenb 0 2010-05-28 15:25 file.txt

但是当我想测试/data中的文件时:

ls -la /data
drwxrwxrwx 4 root root 4096 2010-05-28 16:11 .
-rw-r--r-- 1 lindenb lindenb 0 2010-05-28 15:25 file.txt

我得到了:

mysql>  create function fileExists RETURNS STRING SONAME 'libfileexists.so'; select fileExists("/data/file.txt"); drop function fileExists;
Query OK, 0 rows affected (0.00 sec)

+--------------------------------------------+
| fileExists("/data/file.txt") |
+--------------------------------------------+
| #ERR:"Permission denied":"/data/file.txt". |
+--------------------------------------------+
1 row in set (0.00 sec)

有什么想法吗?

谢谢!

最佳答案

mysqld 受 apparmor 保护.

AppArmor represents one of several possible approaches to the problem of restricting the actions that installed software can take.

我加了

/data/** r,

在结束时

/etc/apparmor.d/usr.sbin.mysqld

apparmor 已重启:

/etc/init.d/apparmor restart

现在我的 UDF 工作正常了! :-)

关于mysql UDF : fopen = permission denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2911028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com