Re: ERRNO 9 / Bad File Number after Hours accessing

30 views
Skip to first unread message
Message has been deleted

Dennis Lee Bieber

unread,
Oct 1, 2020, 2:12:03 PM10/1/20
to Beagleboard
On Thu, 1 Oct 2020 00:03:30 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user
alex.huf80-Re5JQ...@public.gmane.org wrote:

>
>
>
>Hi,
>
>the function below ist running for hours but sometimes an error no 9
>occours (which must be "Bad File Number*"). *Does anyone have an idea or


>------------------------------------------------------------------------------------------------------------------------------------
>uint16_t read_raw_adc_ch(int channel)
> {
>
> uint8_t error_ctr;
> char str[MAX_BUF];
> int output_fd;
> extern int errno;
> error_ctr = 0;
> while (error_ctr < 8)
> {
>
> // oeffnen loest eine messung des adc aus
> if (channel==0) output_fd =
>open("/sys/bus/iio/devices/iio:device0/in_voltage0_raw", O_RDONLY |
>O_ASYNC);
> if (channel==1) output_fd =
>open("/sys/bus/iio/devices/iio:device0/in_voltage1_raw", O_RDONLY |
>O_ASYNC);

Pardon, but this set of statements seems rather crude and redundant.

One improvement would be to use a switch/case structure

switch (channel)
{
case 0:
output_fd = ...;
break;
case 1:
...;
default:
//value is not 0..7, dump an error and exit?
}

but that still has the redundancy in the open calls.

if (channel < 0 || channel > 7)
{
//value is not 0..7, dump an error and exit?
}
// path needs to be a character buffer of sufficient length
sprintf(path, "/sys/bus/iio/devices/iio:device0/in_voltage%1d_raw",
channel)
output_fd = open(path, ...)


Unfortunately I have no help for the "bad file number" condition. My
naive thought was that the runtime keeps incrementing the file number of
the fd on each open, and isn't recycling around to reuse file numbers.
However, that doesn't match the description for POSIX compliant open()
call.

Might it be possible to only open the 8 channels at the start of the
program, outside of the loop, and use something like

lseek(channel_fd, 0, SEEK_SET);
//and then read the channel_fd

{I've not tried seeking on the sysfs entries}


--
Dennis L Bieber

Jeremy Trimble

unread,
Oct 8, 2020, 3:27:35 PM10/8/20
to beagl...@googlegroups.com
Alex,

The EBADF may not be your actual problem, because immediately before exiting the loop you attempt to close an invalid file descriptor:  In the "then" part of your if statement, you verify in the condition that the output_fd is less than zero (an invalid file descriptor), then you call close() on that invalid file descriptor (which also returns an error, but you don't see it because you're not checking the return value of close() there).  Once you break out of the loop, errno is set to EBADF from the close() call, not from the open() call.

You should add the printout as the first line inside of the "then" block of your if ( output_fd < 0 ) -- that will tell you what errno was returned from the open() call.

Jeremy Trimble
Principal Research Engineer
Two Six Labs, LLC  :|:::
https://twosixlabs.com/


On Thu, Oct 1, 2020 at 6:59 AM <alex....@gmail.com> wrote:



Hi,

the function below ist running for hours but sometimes an error no 9 occours (which must be "Bad File Number"). Does anyone have an idea or suggestions for other flags? This is the only function in the whole programm that is accessing the ADC. After the exit systemd restarts the programm and it runs fine again for hours. The function is called from a loop while another file with another fd is open (logfile for the adc-data, with a size of about 500bytes then a new logfile is started). The read_raw_adc_ch(0); is called every 10 seconds. As you can see I've tried to write a workaround with the error_ctr loop, but even the wait for one second does not help.

Thanks for any ideas or suggestions.

Alex


$ cat /proc/version
Linux version 4.19.94-ti-r42 (voodoo@x3-am57xx-beagle-x15-2gb) (gcc version 8.3.0 (Debian 8.3.0-6)) #1buster SMP PREEMPT Tue Mar 31 19:38:29 UTC 2020

cat /etc/debian_version
10.3

Board: BeagleBoneBlack Rev. C



------------------------------------------------------------------------------------------------------------------------------------
uint16_t read_raw_adc_ch(int channel)
    {

    uint8_t error_ctr;
    char str[MAX_BUF];
    int output_fd;
    extern int errno;
    error_ctr = 0;
    while (error_ctr < 8)
    {

        // oeffnen loest eine messung des adc aus
        if (channel==0) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage0_raw", O_RDONLY | O_ASYNC);
        if (channel==1) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage1_raw", O_RDONLY | O_ASYNC);
        if (channel==2) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage2_raw", O_RDONLY | O_ASYNC);
        if (channel==3) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage3_raw", O_RDONLY | O_ASYNC);
        if (channel==4) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage4_raw", O_RDONLY | O_ASYNC);
        if (channel==5) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage5_raw", O_RDONLY | O_ASYNC);
        if (channel==6) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage6_raw", O_RDONLY | O_ASYNC);
        if (channel==7) output_fd = open("/sys/bus/iio/devices/iio:device0/in_voltage7_raw", O_RDONLY | O_ASYNC);

        if (output_fd < 0)
        {
        error_ctr++;
        close(output_fd);
        sleep(1);
        }
        else
        {
        read(output_fd, str, 10);
        close(output_fd);
        return atoi(str);
        }

    }


    printf("ERROR OPEN ADC DEVICE, ERRNO: %d", errno);
        exit(EXIT_FAILURE);

return 0;
}
------------------------------------------------------------------------------------------------------------------------------------

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/add6f258-c7f8-48ff-870f-b6f093838902o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages