Reputation: 101
I've been messing with hidraw.h
to see what I could do with my USB but when I try to open /dev/hidraw0
it says Unable to open device: Permission denied
, I know I could do something like system("sudo open /dev/hidraw0");
But of course it wouldn't have the same effect. Also I'm in Kali Linux. Here's my code:
/* 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;
}
}
Upvotes: 1
Views: 3323
Reputation: 263387
Your program doesn't have permission to open /dev/hidraw0
. So you need to run it in such a way that it has permission.
The simplest way to do that, as ForceBru's comment suggests, is to run the program itself under sudo
. (You can't run just the open
function under sudo
; open
is a function, not a program.)
You might also be able to make the program run with root privileges by setting its setuid
flag, using chmod u+s
or chmod u+gs
.
Consider that the device has the permissions it has for what are presumably good reasons. Be careful.
Upvotes: 1
Reputation: 27230
If possible then first try to give some more permission to that device
sudo chmod 777 /dev/hidraw0
then run you program. if required then also try changing its owner by Chown
command.
Upvotes: 0