This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c1266a33670484683294adc7a3731fbcfa49f532
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 3 14:31:39 2026 +0100

    sched/mqueue: Report all msqid_ds fields in msgctl(IPC_STAT).
    
    msgctl(IPC_STAT) did not set msg_perm.mode, msg_stime, msg_rtime,
    msg_ctime, msg_lspid and msg_lrpid: they kept the values of the
    caller's buffer.  msg_cbytes was the number of messages times
    CONFIG_MQ_MAXMSGSIZE, not the number of bytes in the queue.
    
    Keep these values in the queue, as POSIX describes them:
    
    - msgget() sets the mode to the low 9 bits of msgflg and msg_ctime to
      the current time.
    - msgsnd() sets msg_lspid and msg_stime, msgrcv() sets msg_lrpid and
      msg_rtime.
    - IPC_SET sets the mode and msg_ctime.
    - msg_cbytes counts the bytes of the messages in the queue.
    
    NuttX has no user and group IDs in struct ipc_perm, so there is
    nothing to keep for them.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 sched/mqueue/msg.h    |  7 +++++++
 sched/mqueue/msgctl.c | 13 ++++++++++---
 sched/mqueue/msgget.c |  4 +++-
 sched/mqueue/msgrcv.c |  3 +++
 sched/mqueue/msgsnd.c |  3 +++
 5 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/sched/mqueue/msg.h b/sched/mqueue/msg.h
index 5a03116b2e0..f8726e77283 100644
--- a/sched/mqueue/msg.h
+++ b/sched/mqueue/msg.h
@@ -54,6 +54,13 @@ struct msgq_s
   struct mqueue_cmn_s  cmn;
   struct list_node     msglist;       /* Prioritized message list */
   key_t                key;           /* The msgget() key, or IPC_PRIVATE */
+  time_t               stime;         /* Time of the last msgsnd() */
+  time_t               rtime;         /* Time of the last msgrcv() */
+  time_t               ctime;         /* Time of the last change */
+  struct ipc_perm      perm;          /* Ownership and permissions */
+  pid_t                lspid;         /* PID of the last msgsnd() */
+  pid_t                lrpid;         /* PID of the last msgrcv() */
+  msglen_t             cbytes;        /* Number of bytes in the queue */
   int16_t              id;            /* The message queue identifier */
   int16_t              maxmsgs;       /* Maximum number of messages in the 
queue */
   int16_t              nmsgs;         /* Number of message in the queue */
diff --git a/sched/mqueue/msgctl.c b/sched/mqueue/msgctl.c
index faaf437e2eb..e0bcfb9f65f 100644
--- a/sched/mqueue/msgctl.c
+++ b/sched/mqueue/msgctl.c
@@ -88,8 +88,9 @@ int msgctl(int msqid, int cmd, FAR struct msqid_ds *buf)
               break;
             }
 
-          msgq->maxmsgs = buf->msg_qbytes / CONFIG_MQ_MAXMSGSIZE;
-
+          msgq->maxmsgs   = buf->msg_qbytes / CONFIG_MQ_MAXMSGSIZE;
+          msgq->perm.mode = buf->msg_perm.mode & IPC_MODE;
+          msgq->ctime     = time(NULL);
           break;
         }
 
@@ -101,9 +102,15 @@ int msgctl(int msqid, int cmd, FAR struct msqid_ds *buf)
               break;
             }
 
+          buf->msg_perm   = msgq->perm;
+          buf->msg_stime  = msgq->stime;
+          buf->msg_rtime  = msgq->rtime;
+          buf->msg_ctime  = msgq->ctime;
+          buf->msg_cbytes = msgq->cbytes;
           buf->msg_qnum   = list_length(&msgq->msglist);
-          buf->msg_cbytes = buf->msg_qnum * CONFIG_MQ_MAXMSGSIZE;
           buf->msg_qbytes = msgq->maxmsgs * CONFIG_MQ_MAXMSGSIZE;
+          buf->msg_lspid  = msgq->lspid;
+          buf->msg_lrpid  = msgq->lrpid;
           break;
         }
 
diff --git a/sched/mqueue/msgget.c b/sched/mqueue/msgget.c
index 42aa48a2c1c..34c8d4325b4 100644
--- a/sched/mqueue/msgget.c
+++ b/sched/mqueue/msgget.c
@@ -79,7 +79,9 @@ int msgget(key_t key, int msgflg)
       ret = nxmsg_alloc(&msgq);
       if (ret >= 0)
         {
-          msgq->key = key;
+          msgq->key       = key;
+          msgq->perm.mode = msgflg & IPC_MODE;
+          msgq->ctime     = time(NULL);
         }
     }
 
diff --git a/sched/mqueue/msgrcv.c b/sched/mqueue/msgrcv.c
index e8e9947d232..8f4742b2708 100644
--- a/sched/mqueue/msgrcv.c
+++ b/sched/mqueue/msgrcv.c
@@ -175,6 +175,9 @@ static int msgrcv_wait(FAR struct msgq_s *msgq, FAR struct 
msgbuf_s **rcvmsg,
 
 found:
   msgq->nmsgs--;
+  msgq->cbytes -= newmsg->msize;
+  msgq->lrpid   = nxsched_getpid();
+  msgq->rtime   = time(NULL);
   *rcvmsg = newmsg;
   return OK;
 }
diff --git a/sched/mqueue/msgsnd.c b/sched/mqueue/msgsnd.c
index 14677970480..7a370ba8c1b 100644
--- a/sched/mqueue/msgsnd.c
+++ b/sched/mqueue/msgsnd.c
@@ -234,6 +234,9 @@ int msgsnd(int msqid, FAR const void *msgp, size_t msgsz, 
int msgflg)
       list_add_tail(&msgq->msglist, &msg->node);
 
       msgq->nmsgs++;
+      msgq->cbytes += msgsz;
+      msgq->lspid   = nxsched_getpid();
+      msgq->stime   = time(NULL);
 
       if (msgq->cmn.nwaitnotempty > 0)
         {

Reply via email to