make msgsnd(2) more posix

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

make msgsnd(2) more posix

Moritz Buhl-3
Hi,

while running some ported tests I noticed that msgsnd(2) did not error
when passing a message with mtype < 1, even tho the manual page states:
> mtype is an integer greater than 0 that can be used

POSIX says:
https://pubs.opengroup.org/onlinepubs/9699919799/
[EINVAL]
    The value of msqid is not a valid message queue identifier, or the value of mtype is less than 1; or the value of msgsz is greater than the system-imposed limit.

Here is a minimal test:

#include <sys/msg.h>

struct msg {
        long             mtype;
        char             buf[3];
};

int
main(void)
{
        struct msg msg = { 0, { 'a', 'b', 'c' } };
        int id;

        id = msgget(1234, IPC_CREAT | 0600);

        // man 2 msgsnd: mtype is an integer greater than 0 ...
        if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == -1)
                return 0;
        return 1;
}

and the patch follows.

greetings,
mbuhl


Index: sys/kern/sysv_msg.c
===================================================================
RCS file: /cvs/src/sys/kern/sysv_msg.c,v
retrieving revision 1.34
diff -u -p -r1.34 sysv_msg.c
--- sys/kern/sysv_msg.c 5 Dec 2018 15:42:45 -0000 1.34
+++ sys/kern/sysv_msg.c 12 Jul 2019 15:03:16 -0000
@@ -588,7 +588,7 @@ msg_copyin(struct msg *msg, const char *
  return (error);
  }
 
- if (msg->msg_type < 0) {
+ if (msg->msg_type <= 0) {
  msg_free(msg);
  return (EINVAL);
  }

Reply | Threaded
Open this post in threaded view
|

Re: make msgsnd(2) more posix

Klemens Nanni-2
On Fri, Jul 12, 2019 at 10:34:33PM +0200, Moritz Buhl wrote:
> while running some ported tests I noticed that msgsnd(2) did not error
> when passing a message with mtype < 1, even tho the manual page states:
> > mtype is an integer greater than 0 that can be used
We also fail to mention that condition in the ERRORS section.
 
> Here is a minimal test:
>
> #include <sys/msg.h>
>
> struct msg {
What msgsnd(2) send takes should be of type `struct mymsg`, but our
manual page lackes the struct's name, so this could be improved as well.

> Index: sys/kern/sysv_msg.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/sysv_msg.c,v
> retrieving revision 1.34
> diff -u -p -r1.34 sysv_msg.c
> --- sys/kern/sysv_msg.c 5 Dec 2018 15:42:45 -0000 1.34
> +++ sys/kern/sysv_msg.c 12 Jul 2019 15:03:16 -0000
> @@ -588,7 +588,7 @@ msg_copyin(struct msg *msg, const char *
>   return (error);
>   }
>  
> - if (msg->msg_type < 0) {
> + if (msg->msg_type <= 0) {
OK kn, although I'd go with `< 1' as that matches the specification's
wording as well as what NetBSD and FreeBSD have in sysv_msg.c.

>   msg_free(msg);
>   return (EINVAL);
>   }
>

Reply | Threaded
Open this post in threaded view
|

Re: make msgsnd(2) more posix

Todd C. Miller-3
On Sun, 14 Jul 2019 14:57:54 +0200, Klemens Nanni wrote:

> What msgsnd(2) send takes should be of type `struct mymsg`, but our
> manual page lackes the struct's name, so this could be improved as well.

The struct name is intentionally omitted because the standard doesn't
specify one.  Callers need to make their own struct with those
elements.  Any code that relies on "struct mymsg" in sys/msg.h will
not be portable.

 - todd

Reply | Threaded
Open this post in threaded view
|

Re: make msgsnd(2) more posix

Alexander Bluhm
In reply to this post by Klemens Nanni-2
On Sun, Jul 14, 2019 at 02:57:54PM +0200, Klemens Nanni wrote:
> We also fail to mention that condition in the ERRORS section.

Moritz, can you create a man page ERRORS diff?

> > - if (msg->msg_type < 0) {
> > + if (msg->msg_type <= 0) {
> OK kn, although I'd go with `< 1' as that matches the specification's
> wording as well as what NetBSD and FreeBSD have in sysv_msg.c.

I have commited it with < 1.  For both variants llvm creates the
same object with cmp 0.

bluhm

Reply | Threaded
Open this post in threaded view
|

Re: make msgsnd(2) more posix

Moritz Buhl-3
In reply to this post by Moritz Buhl-3
> Moritz, can you create a man page ERRORS diff?

Is the attached diff ok?

mbuhl

Index: lib/libc/sys/msgsnd.2
===================================================================
RCS file: /cvs/src/lib/libc/sys/msgsnd.2,v
retrieving revision 1.19
diff -u -p -r1.19 msgsnd.2
--- lib/libc/sys/msgsnd.2 10 Dec 2014 19:19:00 -0000 1.19
+++ lib/libc/sys/msgsnd.2 18 Jul 2019 11:21:48 -0000
@@ -127,6 +127,9 @@ will fail if:
 .Fa msqid
 is not a valid message queue identifier.
 .Pp
+.Va mtype
+is less than 1.
+.Pp
 .Fa msgsz
 is greater than
 .Va msg_qbytes .

Reply | Threaded
Open this post in threaded view
|

Re: make msgsnd(2) more posix

Ingo Schwarze
Hi Moritz,

Moritz Buhl wrote on Thu, Jul 18, 2019 at 01:38:49PM +0200:
> bluhm@ wrote:

>> Moritz, can you create a man page ERRORS diff?

> Is the attached diff ok?

Yes, committed with some additional tweaks.

Thank you,
  Ingo


CVSROOT: /cvs
Module name: src
Changes by: [hidden email] 2019/07/18 06:57:12

Modified files:
        lib/libc/sys   : msgsnd.2

Log message:
State that mtype < 1 causes EINVAL as required by POSIX
and as implemented by OpenBSD since sysv_msg.c rev. 1.35.
Diff from Moritz Buhl <mbuhl at moritzbuhl dot de> requested by bluhm@.

While here, add STANDARDS, improve HISTORY,
and use the customary .Fa for struct fields rather than .Va.


Index: msgsnd.2
===================================================================
RCS file: /cvs/src/lib/libc/sys/msgsnd.2,v
retrieving revision 1.19
diff -u -r1.19 msgsnd.2
--- msgsnd.2 10 Dec 2014 19:19:00 -0000 1.19
+++ msgsnd.2 18 Jul 2019 12:57:04 -0000
@@ -53,10 +53,10 @@
 char mtext[1]; /* body of message */
 .Ed
 .Pp
-.Va mtype
+.Fa mtype
 is an integer greater than 0 that can be used for selecting messages (see
 .Xr msgrcv 2 ) ;
-.Va mtext
+.Fa mtext
 is an array of
 .Fa msgsz
 bytes, with a size between 0 and that of the system limit
@@ -65,7 +65,7 @@
 If the number of bytes already on the message queue plus
 .Fa msgsz
 is bigger than the maximum number of bytes on the message queue
-.Po Va msg_qbytes ,
+.Po Fa msg_qbytes ,
  see
 .Xr msgctl 2
 .Pc ,
@@ -105,16 +105,16 @@
 queue is updated in the following way:
 .Bl -bullet
 .It
-.Va msg_cbytes
+.Fa msg_cbytes
 is incremented by the size of the message.
 .It
-.Va msg_qnum
+.Fa msg_qnum
 is incremented by 1.
 .It
-.Va msg_lspid
+.Fa msg_lspid
 is set to the pid of the calling process.
 .It
-.Va msg_stime
+.Fa msg_stime
 is set to the current time.
 .El
 .Sh RETURN VALUES
@@ -127,9 +127,12 @@
 .Fa msqid
 is not a valid message queue identifier.
 .Pp
+.Fa mtype
+is less than 1.
+.Pp
 .Fa msgsz
 is greater than
-.Va msg_qbytes .
+.Fa msg_qbytes .
 .It Bq Er EACCES
 The calling process does not have write access to the message queue.
 .It Bq Er EAGAIN
@@ -153,6 +156,13 @@
 .Xr msgctl 2 ,
 .Xr msgget 2 ,
 .Xr msgrcv 2
+.Sh STANDARDS
+The
+.Fn msgsnd
+function conforms to the X/Open System Interfaces option of
+.St -p1003.1-2008 .
 .Sh HISTORY
-Message queues appeared in
-.At V.1 .
+Message queues first appeared in
+.At V.1
+and have been available since
+.Nx 1.0 .