Skip to contentSkip to conversation

pipe fun

Xenomai-help Applied Ignacio García Pérez 3 participants 7 messages Nov 21, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

This has to be something really simple but I'm hitting a wall. I have a rt module that creates a pipe and periodically writes some data to it. In the following program, the read fails with "No space left on device":

int main (void) {

    int r, fd; u_char c;

    fd = open("/proc/xenomai/registry/pipes/rt2event", O_RDWR);
    if (fd < 0) {
        fprintf(stderr, "ERROR opening pipe: %s\n", strerror(errno));
        return -1;
    }

    for (;;) {
        r = read(fd, &c, sizeof(c));
        if (r == 0) { fprintf(stderr, "ERROR reading (returned 0)\n");
break; }
        if (r < 0) { fprintf(stderr, "ERROR reading: %s\n",
strerror(errno)); break; }
        fprintf(stderr, "%02hX ", c);
    }

    close(fd);

    return 0;
}

However, the following program works fine!!!

int main (void) {

    int r; u_char c;

    FILE *f;

    f = fopen("/proc/xenomai/registry/pipes/rt2event", "r+b");
    if (f == NULL) {
        fprintf(stderr, "ERROR opening pipe: %s\n", strerror(errno));
        return -1;
    }

    for (;;) {
        r = fread(&c, sizeof(c), 1, f);
        if (r == 0) { fprintf(stderr, "ERROR reading (returned 0)\n");
break; }
        if (r < 0) { fprintf(stderr, "ERROR reading: %s\n",
strerror(errno)); break; }
        fprintf(stderr, "%02hX ", c);
    }

    fclose(f);

    return 0;
}

Any clues?

By the way, I noticed a "cat /proc/xenomai/registry/pipes/rt2event | xxd" won't work, perhaps due to the same reason?

#1 lore raw

[email protected] wrote on 21.11.2005 11:11:52:

Quoted 4 lines

This has to be something really simple but I'm hitting a wall. I have a rt module that creates a pipe and periodically writes some data to it. In the following program, the read fails with "No space left on device":

The reason is as follows:

from rt_pipe_send() docs

* This service writes a complete message to be received from the
* associated special device. rt_pipe_send() always preserves message
* boundaries, which means that all data sent through a single call of
* this service will be gathered in a single read(2) operation from
* the special device.

Data is written as a size-bounded message so a reader must provide a big-enough buffer for the message to be read as a whole. It's possible to send data as a stream (rt_pipe_stream()) but not possible (well, should it be so?) to read it as a stream (actually, it's possible but as a side effect - that's why fread() works).

[code]

nucleus::pipe.c::xnpipe_read()

...
ret = (ssize_t)xnpipe_m_size(mh); /* Cannot be zero */ state->ionrd -= ret;

xnlock_put_irqrestore(&nklock,s);

if (ret <= count) <---- Is a provided buffer big enough to host
the whole message?

            {
            if (__copy_to_user(buf,xnpipe_m_data(mh),ret))
                ret = -EFAULT;
            }
        else
            /* Return buffer is too small - message is lost. */
            ret = -ENOSPC;    <---- That's the error you are getting.

...

[/code]

Why does fread() work? It just uses an additional buffering (by default) and provides a big enough internal buffer for read(). So the whole message is read into that buffer upon the first fread() call from your side and a few subsequent fread() calls just return data from that internal buffer. So it looks like data being read as a stream but that's only a consequence of additional buffering being made by glibc::fread().

Quoted 58 lines

int main (void) {

    int r, fd; u_char c;

    fd = open("/proc/xenomai/registry/pipes/rt2event", O_RDWR);
    if (fd < 0) {
        fprintf(stderr, "ERROR opening pipe: %s\n", strerror(errno));
        return -1;
    }

    for (;;) {
        r = read(fd, &c, sizeof(c));
        if (r == 0) { fprintf(stderr, "ERROR reading (returned 0)\n");
break; }
        if (r < 0) { fprintf(stderr, "ERROR reading: %s\n",
strerror(errno)); break; }
        fprintf(stderr, "%02hX ", c);
    }

    close(fd);

    return 0;
}

However, the following program works fine!!!

int main (void) {

    int r; u_char c;

    FILE *f;

    f = fopen("/proc/xenomai/registry/pipes/rt2event", "r+b");
    if (f == NULL) {
        fprintf(stderr, "ERROR opening pipe: %s\n", strerror(errno));
        return -1;
    }

    for (;;) {
        r = fread(&c, sizeof(c), 1, f);
        if (r == 0) { fprintf(stderr, "ERROR reading (returned 0)\n");
break; }
        if (r < 0) { fprintf(stderr, "ERROR reading: %s\n",
strerror(errno)); break; }
        fprintf(stderr, "%02hX ", c);
    }

    fclose(f);

    return 0;
}
Quoted 5 lines

Any clues?

By the way, I noticed a "cat /proc/xenomai/registry/pipes/rt2event | xxd" won't work, perhaps due to the same reason?

---
Best regards,
Dmitry

#2 lore raw
Quoted 3 lines · Dmitry Adamushko

/* Return buffer is too small - message is lost. */ ret = -ENOSPC; <---- That's the error you are getting.

Maybe "No space left on device" is a bit misleading. Wouldn't ENOMEM fit better? (or event EINVAL)

#3 lore raw

Ignacio García Pérez <[email protected]> wrote on 21.11.2005 12:24:57:

Quoted 8 lines
Quoted 3 lines · Dmitry Adamushko

/* Return buffer is too small - message is lost. */ ret = -ENOSPC; <---- That's the error you are getting.

Maybe "No space left on device" is a bit misleading. Wouldn't ENOMEM fit better? (or event EINVAL)

Actually, looking now at the linux sources, -ENOSPC is used for describing the same reason (buffer is not big enough) in some places; although there is, at least, one more value -ENOBUFS that's used for the same reason.

#define ENOSPC          28      /* No space left on device */
#define ENOBUFS         105     /* No buffer space available */

So it looks like both may be used in the same context but, I guess, a definition of -ENOBUFS is less confusing.

---
Best regards,
Dmitry

#4 lore raw
Quoted 11 lines · Dmitry Adamushko

Ignacio García Pérez <[email protected]> wrote on 21.11.2005 12:24:57:

Quoted 8 lines
Quoted 3 lines · Dmitry Adamushko

/* Return buffer is too small - message is lost. */ ret = -ENOSPC; <---- That's the error you are getting.

Maybe "No space left on device" is a bit misleading. Wouldn't ENOMEM fit better? (or event EINVAL)

Quoted 8 lines

Actually, looking now at the linux sources, -ENOSPC is used for describing the same reason (buffer is not big enough) in some places; although there is, at least, one more value -ENOBUFS that's used for the same reason.

#define ENOSPC          28      /* No space left on device */
#define ENOBUFS         105     /* No buffer space available */

So it looks like both may be used in the same context but, I guess, a definition of -ENOBUFS is less confusing.

Let's fix this.

Quoted 11 lines

---
Best regards,
Dmitry

------------------------------------------------------------------------

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.
#5 lore raw

Philippe Gerum <[email protected]> wrote on 21.11.2005 13:36:41:

Quoted 9 lines
Quoted 8 lines · Dmitry Adamushko

Ignacio García Pérez <[email protected]> wrote on 21.11.2005 12:24:57:

Quoted 6 lines
Quoted 3 lines · Dmitry Adamushko

/* Return buffer is too small - message is lost. */ ret = -ENOSPC; <---- That's the error you are getting.

Maybe "No space left on device" is a bit misleading. Wouldn't ENOMEM

fit

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

better? (or event EINVAL)

Actually, looking now at the linux sources, -ENOSPC is used for describing the same reason (buffer is not big enough) in some places; although there is, at least, one more value -ENOBUFS that's used for

the

Quoted 5 lines
Quoted 5 lines

same reason.

#define ENOSPC          28      /* No space left on device */
#define ENOBUFS         105     /* No buffer space available */
Quoted 7 lines
Quoted 4 lines

So it looks like both may be used in the same context but, I guess, a definition of -ENOBUFS is less confusing.

Let's fix this.

Enclosed.

One more thing about xnpipe_read() though.

...

            }
        else
            /* Return buffer is too small - message is lost. */
            ret = -ENOBUFS;

So the message is lost in such a case. We may easily avoid that by making a proper check of the size when we are still in the locked section and putting the message back into the queue (prependq() of course). I think that would be more sane.

Quoted 3 lines

--

Philippe.

---
Best regards,
Dmitry

(See attached file: pipe-ENOBUFS.patch)(See attached file: ChangeLog.patch)

pipe.c renamed from pipe.c-old +1 -1
@@ -749,7 +749,7 @@
 	    }
 	else
 	    /* Return buffer is too small - message is lost. */
-	    ret = -ENOSPC;
+	    ret = -ENOBUFS;
 
 	if (handler != NULL)
 	    ret = handler(xnminor_from_state(state),mh,ret,cookie);
ChangeLog renamed from ChangeLog-old +7 -0
@@ -1,4 +1,11 @@
 
+2005-11-21  Dmitry Adamushko <[email protected]>
+
+	* nucleus/pipe.c (xnpipe_send): Return -ENOBUFS instead of -ENOSPC
+	when a given buffer is not big enough for the entire message to be
+	written in. perror() gives a, hopefully, more-precise and less-confusing
+	description for the former one.
+
 2005-11-18  Heikki Lindholm <[email protected]>
 
 	* include/asm-powerpc/system.h, ksrc/arch/powerpc/switch_64.S: Add
#6 lore raw
Quoted 29 lines · Dmitry Adamushko

Philippe Gerum <[email protected]> wrote on 21.11.2005 13:36:41:

Quoted 9 lines
Quoted 8 lines · Dmitry Adamushko

Ignacio García Pérez <[email protected]> wrote on 21.11.2005 12:24:57:

Quoted 6 lines
Quoted 3 lines · Dmitry Adamushko

/* Return buffer is too small - message is lost. */ ret = -ENOSPC; <---- That's the error you are getting.

Maybe "No space left on device" is a bit misleading. Wouldn't

ENOMEM fit

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

better? (or event EINVAL)

Actually, looking now at the linux sources, -ENOSPC is used for describing the same reason (buffer is not big enough) in some places; although there is, at least, one more value -ENOBUFS that's used

for the

Quoted 10 lines
Quoted 9 lines

same reason.

#define ENOSPC          28      /* No space left on device */
#define ENOBUFS         105     /* No buffer space available */

So it looks like both may be used in the same context but, I guess, a definition of -ENOBUFS is less confusing.

Quoted 5 lines
Quoted 2 lines

Let's fix this.

Enclosed.

Applied, thanks.

Quoted 11 lines

One more thing about xnpipe_read() though.

...

           }
       else
           /* Return buffer is too small - message is lost. */
           ret = -ENOBUFS;

So the message is lost in such a case. We may easily avoid that by making a proper check of the size when we are still in the locked section and putting the message back into the queue (prependq() of course). I think that would be more sane.

Problem is that if nobody ends up reading the message because the application always passes invalid args, internal heap memory may be exhausted due to unread messages.

Quoted 11 lines
Quoted 4 lines

--

Philippe.

---
Best regards,
Dmitry

/(See attached file: pipe-ENOBUFS.patch)//(See attached file: ChangeLog.patch)/

Signature · 1 line
Philippe.