---
name: "msgget_secure"
section: "2"
platform: "linux"
package: "libselinux-dev"
version: "2002031409-4"
release: "Debian 3.0 (Woody)"
platforms: ["linux"]
url: "https://new.carta.tech/msgget/2"
---

# msgget\_secure(2)

get a message queue identifier

Part of [msgget(2)](/msgget/2.md).

## NAME

msgget, msgget\_secure - get a message queue identifier

## SYNOPSIS

```
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/msg.h>
# include <linux/flask/flask_types.h> /* for msgget_secure */
```

**int msgget ( key\_t** *key***,** **int** *msgflg* **)**

**int msgget\_secure ( key\_t** *key***,** **int** *msgflg***,** *security\_id\_t* **sid** *)*

## DESCRIPTION

The function returns the message queue identifier associated to the value of the *key* argument. A new message queue is created if *key* has value **IPC\_PRIVATE** or *key* isn't **IPC\_PRIVATE**, no existing message queue is associated to *key*, and **IPC\_CREAT** is asserted in *msgflg* (i.e. *msgflg***&***IPC\_CREAT* is nonzero). The presence in *msgflg* of the fields **IPC\_CREAT** and **IPC\_EXCL** plays the same role, with respect to the existence of the message queue, as the presence of **O\_CREAT** and **O\_EXCL** in the mode argument of the [**open**(2)](/open/2.md) system call: i.e. the **msgget** function fails if *msgflg* asserts both **IPC\_CREAT** and **IPC\_EXCL** and a message queue already exists for *key*.

Upon creation, the lower 9 bits of the argument *msgflg* define the access permissions of the message queue. These permission bits have the same format and semantics as the access permissions parameter in  [**open**(2)](/open/2.md) or [**creat**(2)](/creat/2.md) system calls.  (The execute permissions are not used.)

Furthermore, while creating, the system call initializes the system message queue data structure **msqid\_ds** as follows:

-

  **msg\_perm.cuid** and **msg\_perm.uid** are set to the effective user-ID of the calling process.

-

  **msg\_perm.cgid** and **msg\_perm.gid** are set to the effective group-ID of the calling process.

-

  The lowest order 9 bits of **msg\_perm.mode** are set to the lowest order 9 bit of *msgflg*.

-

  **msg\_qnum**, **msg\_lspid**, **msg\_lrpid**, **msg\_stime** and **msg\_rtime** are set to 0.

-

  **msg\_ctime** is set to the current time.

-

  **msg\_qbytes** is set to the system limit **MSGMNB**.

If the message queue already exists the access permissions are verified, and a check is made to see if it is marked for destruction. If [**msgget\_secure**(2)](/msgget_secure/2.md) is used the *sid* argument must match the value of the existing message queue in addition to the access permissions being checked.

## RETURN VALUE

If successful, the return value will be the message queue identifier (a nonnegative integer), otherwise **-1** with **errno** indicating the error.

## ERRORS

For a failing return, **errno** will be set to one among the following values:

- **EACCES**

  A message queue exists for *key*, but the calling process has no access permissions to the queue or the calling process lacks permission to create a message queue with the specified *sid.*

- **EEXIST**

  A message queue exists for **key** and *msgflg* was asserting both **IPC\_CREAT** and **IPC\_EXCL**.

- **EIDRM**

  The message queue is marked for removal.

- **ENOENT**

  No message queue exists for *key* and *msgflg* wasn't asserting **IPC\_CREAT**.

- **ENOMEM**

  A message queue has to be created but the system has not enough memory for the new data structure.

- **ENOSPC**

  A message queue has to be created but the system limit for the maximum number of message queues (**MSGMNI**) would be exceeded.

## NOTES

**IPC\_PRIVATE** isn't a flag field but a **key\_t** type. If this special value is used for *key*, the system call ignores everything but the lowest order 9 bits of *msgflg* and creates a new message queue (on success).

The following is a system limit on message queue resources affecting a **msgget** call:

- **MSGMNI**

  System wide maximum number of message queues: policy dependent.

## BUGS

Use of **IPC\_PRIVATE** does not actually prohibit other processes from getting access to the allocated message queue.

As for the files, there is currently no intrinsic way for a process to ensure exclusive access to a message queue. Asserting both **IPC\_CREAT** and **IPC\_EXCL** in *msgflg* only ensures (on success) that a new message queue will be created, it doesn't imply exclusive access to the message queue.

## CONFORMING TO

SVr4, SVID.  SVr4 does not document the EIDRM error code.

## SEE ALSO

[**ftok**(3)](/ftok/3.md), [**ipc**(5)](/ipc/5.md), [**msgget**(2)](/msgget/2.md), [**msgget\_secure**(2)](/msgget_secure/2.md), [**msgsid**(2)](/msgsid/2.md), [**msgctl**(2)](/msgctl/2.md), [**msgsnd**(2)](/msgsnd/2.md), [**msgsnd\_secure**(2)](/msgsnd_secure/2.md), [**msgrcv**(2)](/msgrcv/2.md), [**msgrcv\_secure**(2)](/msgrcv_secure/2.md), [**msgop**(2)](/msgop/2.md).
