os/ossrv/genericopenlibs/openenvcore/include/sys/msg.dosc
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /** @file  ../include/sys/msg.h
     2 @internalComponent
     3 */
     4 
     5 /** @fn  msgget(key_t key, int msgflg)
     6 @param key
     7 @param msgflg
     8 @return   Upon successful completion a positive message queue identifier is returned.
     9 Otherwise, -1 is returned and the global variable errno is set to indicate the error.
    10 
    11   The msgget function
    12 returns the message queue identifier associated with key. A message queue identifier is a unique integer greater than zero.
    13 
    14  A message queue is created if either key is equal to IPC_PRIVATE, or key does not have a message queue identifier associated with it and 
    15   the IPC_CREAT bit is set in msgflg.
    16 
    17  If a new message queue is created, the data structure associated with it (the msqid_ds structure, see msgctl is initialized as follows: msg_perm.cuid and msg_perm.uid are set to the effective uid of the calling process.
    18 In the current implementation, uid is set to root. msg_perm.gid and msg_perm.cgid are set to the effective gid of the calling process.
    19 In the current implementation, gid is set to root. msg_perm.mode is set to the lower 9 bits of msgflg. msg_cbytes, msg_qnum, msg_lspid, msg_lrpid, msg_rtime, and msg_stime are set to 0. msg_qbytes is set to the system wide maximum value for the number of bytes in a queue (Dv MSGMNB). msg_ctime is set to the current time.
    20 
    21 Examples:
    22 
    23  Example code to create a message queue using msgget
    24  
    25 @code
    26 #include <sys/types.h>
    27 #include <sys/ipc.h>
    28 #include <sys/msg.h>
    29 #include <stdio.h>
    30 #include <string.h>
    31 #include <errno.h>
    32 
    33 #define MESSAGE_Q_KEY 1000
    34 
    35 int main(void)
    36 {
    37    int msq_id, len;
    38    struct {
    39        long mtype;
    40        char mtext[128];
    41    } msg_buf;
    42    /*
    43     * Create a message queue with a given key
    44     */
    45     if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
    46        printf("Message Q create failed with errno %d
    47 ", errno);
    48        return -1;
    49     }
    50     msg_buf.mtype = 1; /* message identifier */
    51     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
    52     len = strlen(msg_buf.mtext)+1;
    53     /*
    54      * Put the message in the queue
    55      */
    56     if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
    57         printf("Message Q send failed with errno %d
    58 ", errno);
    59     }
    60     return 0;
    61 }
    62 
    63 @endcode
    64  Example code to return an existing message queue with msgget
    65 @code
    66 #include <sys/types.h>
    67 #include <sys/ipc.h>
    68 #include <sys/msg.h>
    69 #include <stdio.h>
    70 #include <string.h>
    71 #include <errno.h>
    72 
    73 #define MESSAGE_Q_KEY 1000
    74 
    75 int main(void)
    76 {
    77    int msq_id;
    78    int msg_len = 128;
    79    int msg_type = 0; /* Any type of message */
    80    struct {
    81        long mtype;
    82        char mtext[128];
    83    } msg_buf;
    84    /*
    85     * Get the message queue id for the given key
    86     */
    87     if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
    88        printf("Message Q  get id failed with errno %d
    89 ", errno);
    90        return -1;
    91     }
    92     /*
    93      * Get the message from the queue
    94      */
    95     if (msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0) == -1) {
    96         printf("Message Q recv failed with errno %d
    97 ", errno);
    98     }
    99     /*
   100      * Remove the message queue
   101      */
   102      if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
   103        printf("Message Q  delete failed with errno %d
   104 ", errno);
   105        return -1;
   106      }
   107      return 0;
   108 }
   109 
   110 @endcode
   111 @see msgctl()
   112 @see msgrcv()
   113 @see msgsnd()
   114 
   115 
   116  
   117 
   118 @publishedAll
   119 @externallyDefinedApi
   120 */
   121 
   122 /** @fn  msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
   123 @param msqid
   124 @param msgp
   125 @param msgsz
   126 @param msgflg
   127 
   128 @return   The msgsnd function returns the value 0 if successful; otherwise the
   129 value -1 is returned and errno is set to indicate the error.
   130 
   131 The msgsnd function sends a message to the message queue specified in msqid. The msgp argument points to a structure containing the message. This structure should consist of the following members: 
   132 @code
   133 long mtype;    /* message type */
   134 char mtext[1]; /* body of message */
   135 @endcode
   136 
   137 mtype is an integer greater than 0 that can be used for selecting messages (see msgrcv mtext is an array of bytes, with a size up to that of the system limit (Dv MSGMAX). 
   138 
   139 If the number of bytes already on the message queue plus msgsz is bigger than the maximum number of bytes on the message queue (Va msg_qbytes, see msgctl or the number of messages on all queues system-wide is already equal to the system limit, msgflg determines the action of msgsnd. If msgflg has IPC_NOWAIT mask set in it, the call will return immediately. If msgflg does not have IPC_NOWAIT set in it, the call will block until: 
   140 
   141 The condition which caused the call to block does no longer exist. The message will be sent. 
   142 The message queue is removed, in which case -1 will be returned, and errno is set to EINVAL. 
   143 After a successful call, the data structure associated with the message queue is updated in the following way: 
   144 
   145 msg_cbytes is incremented by the size of the message. 
   146 msg_qnum is incremented by 1. 
   147 msg_lspid is set to the pid of the calling process. 
   148 msg_stime is set to the current time.   
   149   
   150   
   151 Examples:
   152 @code
   153 #include <sys/types.h>
   154 #include <sys/ipc.h>
   155 #include <sys/msg.h>
   156 #include <stdio.h>
   157 #include <string.h>
   158 #include <errno.h>
   159 
   160 #define MESSAGE_Q_KEY 1000
   161 
   162 int main(void)
   163 {
   164    int msq_id, len;
   165    struct {
   166        long mtype;
   167        char mtext[128];
   168    } msg_buf;
   169    /*
   170     * Create a message queue with the given key
   171     */
   172     if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
   173        printf("Message Q create failed with errno %d
   174 ", errno);
   175        return -1;
   176     }
   177     msg_buf.mtype = 1; /* message identifier */
   178     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
   179     len = strlen(msg_buf.mtext)+1;
   180     /*
   181      * Put the message in the queue
   182      */
   183     if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
   184         printf("Message Q send failed with errno %d
   185 ", errno);
   186     }
   187     return 0;
   188 }
   189 
   190 @endcode
   191  
   192 
   193 @publishedAll
   194 @externallyDefinedApi
   195 */
   196 
   197 /** @fn  msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
   198 @param msqid
   199 @param msgp
   200 @param msgsz
   201 @param msgtyp
   202 @param msgflg
   203 @return   Upon successful completion, msgrcv returns the number of bytes received into the mtext field of the structure pointed to by msgp. Otherwise, -1 is returned, and errno set to indicate the error.
   204 
   205   The msgrcv function receives a message from the message queue specified 
   206 in msqid and places it into the structure pointed to by msgp. This structure should consist of the following members: 
   207 @code
   208 long mtype;    /* message type */
   209 char mtext[1]; /* body of message */
   210 @endcode
   211 
   212  mtype is an integer greater than 0 that can be used for selecting messages, mtext is an array of bytes with a size up to that of the system limit (Dv MSGMAX).
   213 
   214  The value of msgtyp has one of the following meanings: The msgtyp argument
   215 is greater than 0.
   216 The first message of type msgtyp will be received. The msgtyp argument
   217 is equal to 0.
   218 The first message on the queue (of any message type) will be received. The msgtyp argument
   219 is less than 0.
   220 The first message of the lowest message type that is
   221 less than or equal to the absolute value of msgtyp will be received.
   222 
   223  The msgsz argument
   224 specifies the maximum length of the requested message.
   225 If the received
   226 message has a length greater than msgsz it will be silently truncated if the MSG_NOERROR flag is set in msgflg, otherwise an error will be returned.
   227 
   228  If no matching message is present on the message queue specified by msqid, the behavior of msgrcv depends on whether the IPC_NOWAIT flag is set in msgflg or not. If IPC_NOWAIT is set, msgrcv will immediately return a value of -1 and set errno to ENOMSG. If IPC_NOWAIT is not set, the calling process will be blocked until: A message of the requested type becomes available on the message queue. The message queue is removed, in which case -1 will be returned and errno set to EINVAL.
   229 
   230  If a message is successfully received, the data structure associated with msqid is updated as follows: msg_cbytes is decremented by the size of the message. msg_lrpid is set to the pid of the caller. msg_lrtime is set to the current time. msg_qnum is decremented by 1.
   231 
   232 Examples:
   233 
   234  Receive a message from the queue
   235  
   236 @code
   237 #include <sys/types.h>
   238 #include <sys/ipc.h>
   239 #include <sys/msg.h>
   240 #include <stdio.h>
   241 #include <string.h>
   242 #include <errno.h>
   243 
   244 #define MESSAGE_Q_KEY 1000
   245 
   246 int main(void)
   247 {
   248    int msq_id;
   249    int msg_len = 128;
   250    int msg_type = 0; /* Any type of message */
   251    struct {
   252        long mtype;
   253        char mtext[128];
   254    } msg_buf;
   255    int ret_val = -1;
   256    /*
   257     * Get the message queue id for the given key
   258     */
   259     if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT)) == -1) {
   260        printf("Message Q  get id failed with errno %d
   261 ", errno);
   262        return -1;
   263     }
   264     /*
   265      * Get the message from the queue
   266      */
   267     if ((ret_val = msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0)) == -1) {
   268         printf("Message Q recv failed with errno %d
   269 ", errno);
   270     }
   271     /*
   272      * Remove the message queue
   273      */
   274      if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
   275        printf("Message Q  delete failed with errno %d
   276 ", errno);
   277        return -1;
   278      }
   279      return ret_val;
   280 }
   281 
   282 
   283  Send a message to the queue
   284 
   285 #include <sys/types.h>
   286 #include <sys/ipc.h>
   287 #include <sys/msg.h>
   288 #include <stdio.h>
   289 #include <string.h>
   290 #include <errno.h>
   291 
   292 #define MESSAGE_Q_KEY 1000
   293 
   294 int main(void)
   295 {
   296    int msq_id, len;
   297    struct {
   298        long mtype;
   299        char mtext[128];
   300    } msg_buf;
   301    /*
   302     * Create a message queue with the given key
   303     */
   304     if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
   305        printf("Message Q create failed with errno %d0, errno);
   306        return -1;
   307     }
   308     msg_buf.mtype = 1; /* message identifier */
   309     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
   310     len = strlen(msg_buf.mtext)+1;
   311     /*
   312      * Put the message in the queue
   313      */
   314     if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
   315         printf("Message Q send failed with errno %d
   316 ", errno);
   317     }
   318     return 0;
   319 }
   320 
   321 @endcode
   322 @see msgctl()
   323 @see msgget()
   324 @see msgsnd()
   325 
   326 
   327  
   328 
   329 @publishedAll
   330 @externallyDefinedApi
   331 */
   332 
   333 /** @fn  msgctl(int msqid, int cmd, struct msqid_ds *buf)
   334 @param msqid
   335 @param cmd
   336 @param buf
   337 @return   The msgctl function returns the value 0 if successful; otherwise the
   338 value -1 is returned and errno is set to indicate the error.
   339 
   340 The msgctl system call performs some control operations on the message queue specified by msqid. 
   341 Each message queue has a data structure associated with it, parts of which may be altered by msgctl and parts of which determine the actions of msgctl. The data structure is defined in \<sys/msg.h\> and contains (amongst others) the following members: 
   342 
   343 @code
   344 struct msqid_ds {
   345         struct  ipc_perm msg_perm;      /* msg queue permission bits */
   346         struct  msg *msg_first; /* first message in the queue */
   347         struct  msg *msg_last;  /* last message in the queue */
   348         u_long  msg_cbytes;     /* number of bytes in use on the queue */
   349         u_long  msg_qnum;       /* number of msgs in the queue */
   350         u_long  msg_qbytes;     /* max # of bytes on the queue */
   351         pid_t   msg_lspid;      /* pid of last msgsnd() */
   352         pid_t   msg_lrpid;      /* pid of last msgrcv() */
   353         time_t  msg_stime;      /* time of last msgsnd() */
   354         long    msg_pad1;
   355         time_t  msg_rtime;      /* time of last msgrcv() */
   356         long    msg_pad2;
   357         time_t  msg_ctime;      /* time of last msgctl() */
   358         long    msg_pad3;
   359         long    msg_pad4[4];
   360 };
   361 @endcode
   362 
   363 The ipc_perm structure used inside the shmid_ds structure is defined in \<sys/ipc.h\> and looks like this: 
   364 @code
   365 struct ipc_perm {
   366         ushort  cuid;   /* creator user id */
   367         ushort  cgid;   /* creator group id */
   368         ushort  uid;    /* user id */
   369         ushort  gid;    /* group id */
   370         ushort  mode;   /* r/w permission */
   371         ushort  seq;    /* sequence # (to generate unique msg/sem/shm id) */
   372         key_t   key;    /* user specified msg/sem/shm key */
   373 };
   374 @endcode
   375 
   376 
   377 
   378 The operation to be performed by msgctl is specified in cmd and is one of: IPC_STAT  Gather information about the message queue and place it in the structure pointed to by buf.  
   379 IPC_SET  Set the value of the msg_perm.uid, msg_perm.gid, msg_perm.mode and msg_qbytes fields in the structure associated with msqid. The values are taken from the corresponding fields in the structure pointed to by buf. Values for msg_qbytes that exceed the system limit (MSGMNB from \<sys/msg.h\>) are silently truncated to that limit.  
   380 IPC_RMID  Remove the message queue specified by msqid and destroy the data associated with it.  
   381 
   382 
   383 
   384 The permission to read from or write to a message queue (see msgsnd and msgrcv is determined by the msg_perm.mode field in the same way as is done with files (see chmod) 
   385 
   386 
   387 
   388 Examples:
   389 @code
   390 #include <sys/types.h>
   391 #include <sys/ipc.h>
   392 #include <sys/msg.h>
   393 #include <stdio.h>
   394 #include <string.h>
   395 #include <errno.h>
   396 
   397 #define MESSAGE_Q_KEY 1000
   398 
   399 int main(void)
   400 {
   401    int msq_id, len;
   402    struct {
   403        long mtype;
   404        char mtext[128];
   405    } msg_buf;
   406    /*
   407     * Create a message queue with a given key
   408     */
   409     if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
   410        printf("Message Q create failed with errno %d
   411 ", errno);
   412        return -1;
   413     }
   414     msg_buf.mtype = 1; /* message identifier */
   415     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
   416     len = strlen(msg_buf.mtext)+1;
   417     /*
   418      * Put the message in the queue
   419      */
   420     if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
   421         printf("Message Q send failed with errno %d
   422 ", errno);
   423     }
   424     return 0;
   425 }
   426 
   427 @endcode
   428 @code
   429 #include <sys/types.h>
   430 #include <sys/ipc.h>
   431 #include <sys/msg.h>
   432 #include <stdio.h>
   433 #include <string.h>
   434 #include <errno.h>
   435 
   436 #define MESSAGE_Q_KEY 1000
   437 
   438 int main(void)
   439 {
   440    int msq_id;
   441    int msg_len = 128;
   442    int msg_type = 0; /* Any type of message */
   443    struct {
   444        long mtype;
   445        char mtext[128];
   446    } msg_buf;
   447    /*
   448     * Get the message queue id for the given key
   449     */
   450     if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
   451        printf("Message Q  get id failed with errno %d
   452 ", errno);
   453        return -1;
   454     }
   455     /*
   456      * Get the message from the queue
   457      */
   458     if (msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0) == -1) {
   459         printf("Message Q recv failed with errno %d
   460 ", errno);
   461     }
   462     /*
   463      * Remove the message queue
   464      */
   465      if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
   466        printf("Message Q  delete failed with errno %d
   467 ", errno);
   468        return -1;
   469      }
   470      return 0;
   471 }
   472 
   473 @endcode
   474 
   475 Note :The below data members of the structure  msqid_ds are internal components.
   476 @code
   477 	struct  msg *msg_first
   478 	struct  msg *msg_last; 
   479 	long    msg_pad1;
   480 	long    msg_pad2;
   481 	long    msg_pad3;
   482 	long    msg_pad4[4];
   483 @endcode
   484 and are not filled if msgctl is called with the flag IPC_STAT.
   485 
   486 @see msgget()
   487 @see msgrcv()
   488 @see msgsnd()
   489 
   490 
   491  
   492 
   493 @publishedAll
   494 @externallyDefinedApi
   495 */
   496 
   497 
   498 /** @struct msqid_ds
   499 
   500 The msqid_ds structure defines a message queue associated with a message queue ID. There is one queue per message queue ID. 
   501 Collectively, the queues are stored as an array, with message queue IDs serving as an index into the array.
   502 Contains the following members,
   503 
   504 @publishedAll
   505 @externallyDefinedApi
   506 */
   507 
   508 /** @var msqid_ds::msg_perm
   509 msg queue permission bits
   510 */
   511 
   512 /** @var msqid_ds::msg_first
   513 first message in the queue
   514 @internalComponent
   515 */
   516 
   517 /** @var msqid_ds::msg_last
   518 last message in the queue
   519 @internalComponent
   520 */
   521 
   522 /** @var msqid_ds::msg_cbytes
   523 number of bytes in use on the queue
   524 */
   525 
   526 /** @var msqid_ds::msg_qnum
   527 number of msgs in the queue 
   528 */
   529 
   530 /** @var msqid_ds::msg_qbytes
   531 max n of bytes on the queue
   532 */
   533 
   534 /** @var msqid_ds::msg_lspid
   535 pid of last msgsnd() 
   536 */
   537 
   538 /** @var msqid_ds::msg_lrpid
   539 pid of last msgrcv()
   540 */
   541 
   542 /** @var msqid_ds::msg_stime
   543 time of last msgsnd()
   544 */
   545 
   546 /** @var msqid_ds::msg_pad1
   547 time of last msgsnd()
   548 @internalComponent
   549 */
   550 
   551 /** @var msqid_ds::msg_rtime
   552 time of last msgrcv()
   553 */
   554 
   555 /** @var msqid_ds::msg_pad2
   556 time of last msgrcv()
   557 @internalComponent
   558 */
   559 
   560 /** @var msqid_ds::msg_ctime
   561 time of last msgctl()
   562 */
   563 
   564 /** @var msqid_ds::msg_pad3
   565 time of last msgctl()
   566 @internalComponent
   567 */
   568 
   569 /** @var msqid_ds::msg_pad4
   570 time of last msgctl()
   571 @internalComponent
   572 */
   573 
   574 
   575 
   576 /** @def MSG_NOERROR
   577 
   578 The MSG_NOERROR identifier value, the msqid_ds struct and the msg struct are as defined by the SV API Intel 386 Processor Supplement.
   579 don't complain about too long msgs.
   580 
   581 @publishedAll
   582 @externallyDefinedApi
   583 */
   584 
   585 
   586 
   587 
   588