Urs Beeli <firstname.lastname@example.org> wrote:
I am fairly new to QNX and have a question regarding resource managers.
I am writing a resource manager for a hardware device. In our setup, it does
not make sense, that the file representing the hardware can be opened more
than once as the clients would be contesting over the same device. I realise
that the clients could use O_EXCL in their open call to avoid this but to be
on the safe side I want to enforce this in the resource manager.
O_EXCL would not do what you want. O_EXCL is only meaningful if
combined with O_CREAT (and means to fail the open for creation if
the file already exists), but I’m pretty sure you’re not doing a file system,
where you’ll be dynamically creating entries.
What I have done is:
#define IOFUNC_ATTR_T struct DevAttr
typedef struct DevAttr
int IoOpen(resmgr_context_t *ctp, io_open_t *msg, DevAttr *attr, void
int result = EINVAL;
if (attr != NULL)
if (attr->attr.count == 0)
printf(“Opened connection number %d\n”,attr->attr.count);
result = iofunc_open_default(ctp, msg, &attr->attr, extra);
printf(“Cannot open device - already opened %d
result = EBUSY;
int IoCloseOcb (resmgr_context_t *ctp, void *reserved, RESMGR_OCB_T *ocb)
if (ocb->ocb.attr->attr.count > 0)
printf(“Decreased counter to %d\n”, ocb->ocb.attr->attr.count);
printf(“Count is %d\n”, ocb->ocb.attr->attr.count);
return (iofunc_close_ocb_default (ctp, reserved, &ocb->ocb));
This works like a charm, as soon as I have opened the file once, any try to
open it again fails with the error ‘file busy’.
Note: you may have to take over io_dup and fail those as well – otherwise
someone might inherit an fd to your device. (Or, maybe, you will control
the code that talks to your device and not need to worry.)
However, unfortunately, it also means, that whenever I do an ls on the
directory which contains the file I get an error, indicating that my device
is busy as well.
I assume you mean either an “ls -l”, or you have ls aliased to something
other than the default (e.g. “ls -F” which requires stat information).
What I would like is to disallow file open calls to the file for
reading/writing but to allow opening it for stat() etc, so it will show
properly in the directory. Is there any way to do this?
In the completely general case, no.
There are a number of ways to go though.
Most people who open a device for collecting information (e.g. stty, ls)
will not open for write access, and if your main client does need/want
write access, you can allow only one open for write, and any number for
read. You’d need to check the ioflag entry in the connect message.
(Experiment with this; IIRC, the mapping from RD/WR/RD-WR to ioflag
is a bit odd.)
Another approach is to take advantage of the fact that most things that
want information about a file call stat() [they could call open(), fstat(),
close() – but don’t usually]. stat() generates what we call a “combine”
message, that is a request that includes headers for more than one
request, e.g. it will be something like an IO_OPEN followed by an IO_STAT.
If you allow through any open that has a combine flag set, but fail the
others you will be mostly ok. By that, I mean you will break the
open(), fstat(), close() case which is not generally used. To test
for this, you would check the subtype in the connect message, and
if it is either _IO_CONNECT_COMBINE or _IO_CONNECT_COMBINE_CLOSE, you
would allow it through (and, of course, not increment your access count).
Take a look at <sys/iomsg.h> for the structures and defines involved.
Hope this helps,
QNX Training Services