C中的打开功能说无法打开设备:权限被拒绝
Open function in C says unable to open device: permission denied
我一直在摆弄 hidraw.h
看看我可以用我的 USB 做什么,但是当我尝试打开 /dev/hidraw0
它说 Unable to open device: Permission denied
,我知道我可以做点什么喜欢 system("sudo open /dev/hidraw0");
但当然不会有同样的效果。我也在 Kali Linux。这是我的代码:
/* Linux */
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
/* Unix */
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
using namespace std;
int main()
{
int fd;
int i, res, desc_size = 0;
char buf[256];
struct hidraw_report_descriptor rpt_desc;
struct hidraw_devinfo info;
fd = open("/dev/hidraw0", O_RDWR);
if(fd < 0)
{
perror("Unable to open device");
return 1;
}
else
{
cout << "Something happend!" << endl;
return 0;
}
}
如果可能,请先尝试为该设备授予更多权限
sudo chmod 777 /dev/hidraw0
然后运行你编程。如果需要,也可以尝试通过 Chown
命令更改其所有者。
您的程序无权打开 /dev/hidraw0
。所以你需要运行它有权限的方式。
正如 ForceBru 的评论所建议的那样,最简单的方法是 运行 sudo
下的程序本身。 (你不能 运行 只是 sudo
下的 open
函数;open
是函数,不是程序。)
您还可以通过使用 chmod u+s
或 chmod u+gs
.
设置其 setuid
标志,使程序 运行 具有 root 权限
考虑到设备拥有它所拥有的权限可能是出于充分的理由。小心。
我一直在摆弄 hidraw.h
看看我可以用我的 USB 做什么,但是当我尝试打开 /dev/hidraw0
它说 Unable to open device: Permission denied
,我知道我可以做点什么喜欢 system("sudo open /dev/hidraw0");
但当然不会有同样的效果。我也在 Kali Linux。这是我的代码:
/* Linux */
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
/* Unix */
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
using namespace std;
int main()
{
int fd;
int i, res, desc_size = 0;
char buf[256];
struct hidraw_report_descriptor rpt_desc;
struct hidraw_devinfo info;
fd = open("/dev/hidraw0", O_RDWR);
if(fd < 0)
{
perror("Unable to open device");
return 1;
}
else
{
cout << "Something happend!" << endl;
return 0;
}
}
如果可能,请先尝试为该设备授予更多权限
sudo chmod 777 /dev/hidraw0
然后运行你编程。如果需要,也可以尝试通过 Chown
命令更改其所有者。
您的程序无权打开 /dev/hidraw0
。所以你需要运行它有权限的方式。
正如 ForceBru 的评论所建议的那样,最简单的方法是 运行 sudo
下的程序本身。 (你不能 运行 只是 sudo
下的 open
函数;open
是函数,不是程序。)
您还可以通过使用 chmod u+s
或 chmod u+gs
.
setuid
标志,使程序 运行 具有 root 权限
考虑到设备拥有它所拥有的权限可能是出于充分的理由。小心。