Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prussdrv_open() fails if filedescriptor=0 is the first available file descriptor in system. #44

Open
hzeller opened this issue Jan 2, 2016 · 3 comments

Comments

@hzeller
Copy link
Contributor

hzeller commented Jan 2, 2016

If all file-descriptors are closed (say, we're running as daemon and have closed all file descriptors), prussdrv_open() fails.

The reason is a flaw in the assumptions in the implementation of prussdrv.c ( https://github.com/beagleboard/am335x_pru_package/blob/master/pru_sw/app_loader/interface/prussdrv.c ): file-descriptors (prussdrv.mmap_fd and prussdrv.fd[i]) are initialized with zero and considered not opened/invalid when they are zero.

Of course, this assumption fails if all files have been closed before, thus the very first file descriptor in the system is, in fact, zero, so an open("/dev/uio0") returns FD 0. This results in prussdrv_open() to fail.

The whole file has to be changed so that the assumption of 'invalid filedescriptor' is changed to '-1' instead of 0. There are various ways to do that, so I let the maintainers choose what they prefer without suggesting patch for now.

(this was, indeed, a real world problem that surfaced in BeagleG. I've worked around it by re-opening file descriptors
hzeller/beagleg@042d1c4
)

To replicate

Here is a little test-program. With the close(0) call in place, it will fail.


#include <stdio.h>
#include <unistd.h>

#include <prussdrv.h>

int main() {
        // Close file-descriptor 0
        close(0);

        int ret = prussdrv_open(PRU_EVTOUT_0);
        fprintf(stderr, "Return code=%d ", ret);
        if (ret == 0) {
                fprintf(stderr, "SUCCESS\n");
        } else {
                fprintf(stderr, "FAILURE\n");
        }
        return ret;
}
@ghost
Copy link

ghost commented Apr 6, 2017

Hi,

I had the same problem on:

root@beaglebone:# uname -or
4.4.30-ti-r64 GNU/Linux
root@beaglebone:# lsb_release -irc
Distributor ID: Debian
Release: 8.6
Codename: jessie

This link helped me to resolve it: https://groups.google.com/forum/#!topic/beagleboard/1rIV-mR8wYw
Codename: jessie

@hzeller
Copy link
Contributor Author

hzeller commented Apr 6, 2017

The issued talked about in that thread is about 4.x kernels and the device overlay and PRU interfacing changes that come with it @fedorovIgor.

This bug is about using wrong error sentinel values for file descriptors inside the library, so it is not quite related.

@ghost
Copy link

ghost commented Apr 6, 2017

I see, I apologize for not reading carefully your post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants