open ("/dev/my_dev",O_RDWR): Operation not permitted
Linux - Embedded & Single-board computerThis forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
open ("/dev/my_dev",O_RDWR): Operation not permitted
I'm running Vanilla 2.6.30.1 with busybox.
/dev contains a device file called my_dev. It was created using:
mknod my_dev c 248 0
I have a multithreaded application. When I try to open /dev/my_dev using:
rc=open ("/dev/my_dev",O_RDWR)
in the main thread (just after main), I get rc=6 which is OK.
But when I try to open my_dev withing a thread I get:
rc=-1, errno=1 (Operation not permitted).
How can I debug this problem ?
What can cause: Operation not permitted ?
This is the EPERM error on the open, which would indicate that the thread does not have permission to open the file. It would be worth checking how you have created the thread (because that is where the permissions are first set).
This is the EPERM error on the open, which would indicate that the thread does not have permission to open the file. It would be worth checking how you have created the thread (because that is where the permissions are first set).
Thank you for your reply. Here is pthread_attr_t:
typedef struct __pthread_attr_s
{
int __detachstate;
int __schedpolicy;
struct __sched_param __schedparam;
int __inheritsched;
int __scope;
size_t __guardsize;
int __stackaddr_set;
void *__stackaddr;
size_t __stacksize;
} pthread_attr_t;
Which of the fields in the structure can cause the open error ?
here is some sample code (the open succeeds in both the main thread and the secondary thread).
Code:
#include <pthread.h>
#include <fcntl.h>
#include <iostream>
void *threadFn(void *p)
{
// open device in second thread
int handle = open("/dev/dsp",O_RDWR);
std::cout << "secondary thread " << handle << std::endl;
if (handle > 0) close(handle);
}
int main()
{
// open device in main thread
int handle = open("/dev/dsp",O_RDWR);
std::cout << "main thread " << handle << std::endl;
if (handle > 0) close(handle);
// start second thread
pthread_t thread;
pthread_create( &thread, 0, threadFn, 0);
pthread_join(thread, 0);
}
Note that you cannot open a handle for writing from both threads at the same time (and you don't need to, since they share open handles, though you do need to arbitrate access to the handle).
The example assumes that the user is in the dsp group (audio).
here is some sample code (the open succeeds in both the main thread and the secondary thread).
Code:
#include <pthread.h>
#include <fcntl.h>
#include <iostream>
void *threadFn(void *p)
{
// open device in second thread
int handle = open("/dev/dsp",O_RDWR);
std::cout << "secondary thread " << handle << std::endl;
if (handle > 0) close(handle);
}
int main()
{
// open device in main thread
int handle = open("/dev/dsp",O_RDWR);
std::cout << "main thread " << handle << std::endl;
if (handle > 0) close(handle);
// start second thread
pthread_t thread;
pthread_create( &thread, 0, threadFn, 0);
pthread_join(thread, 0);
}
Note that you cannot open a handle for writing from both threads at the same time (and you don't need to, since they share open handles, though you do need to arbitrate access to the handle).
The example assumes that the user is in the dsp group (audio).
It was a silliy mistake. I tried to open the file when it was already opened. Thank you for your time and effort.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.