1. Overview
- Three types of System v ipc: System V message queue, System V semaphore, and System V shared memory Zone
- System v ipc shares many similar points between the functions that access them and the information maintained by the kernel for them. This chapter describes all these common attributes.
- Summarizes all System v ipc functions.
2. key_t key and ftok Function
- The header file SYS/types. h defines the data type key_t as an integer, usually a 32-bit integer.
#include <sys/ipc.h>
key_t ftok (const char *pathname, int id);
Returns: IPC key if OK, -1 on error
- The ftok function converts an existing path name and an integer identifier into a key_t value, which is called the IPC key.
- If the pathname does not exist or is inaccessible to the calling process, ftok returns-1
- There is no guarantee that the combination of two different pathnames and the same ID produces different keys.
3. ipc_perm Structure
- The kernel maintains an information structure for each IPC object, which is similar to the information maintained by the kernel for files.
struct ipcqerm {
uid_t uid; /* owner‘s user id */
gid_t gid; /* owner‘s group id */
uid_t cuid; /* creator‘s user id */
gid_t cgid; /* creator‘s group id */
mode_t mode; /* read - write permissions */
ulong_t seq; /* slot usage sequence number */
key_t key; /* IPC key */
};
4. Create and open an IPC Channel
- The application has two options for the key value.
- Call ftok and pass the pathname and ID to it.
- Specify the key as ipc_private. This ensures that a new and unique IPC object is created.
- All three xxxget functions have a parameter named Oflag, which specifies the read/write permission bit of the IPC object (the mode Member of the ipc_perm structure) and selects to create a new IPC object, or access an existing IPC object.
- System v ipc defines its own ipc_xxx constant value, instead of using the o_creat and o_excl constant values as standard open functions and posix ipc functions do.
- Note: The System v ipc function combines the common root permission bits of ipc_xxx to a single Oflag parameter. The open function has a parameter named Oflag and mode.
5. IPC permission
- Each time you use a xxxget function to create a new IPC object, the following information is saved to the ipc_perm structure of the object.
- Some bits in the Oflag parameter initialize the mode Member of the ipc_perm structure. The suffix A of sem_a indicates alter change.
- In the ipc_perm structure, the cuid and cgid members are set to the valid user ID and valid group ID of the calling process. These two members are collectively called the Creator ID.
- The UID and GID members in the ipc_perm structure are also set to the valid user ID and the valid group ID of the calling process.
- Although a process can call the xxxctl function to modify the owner ID, the Creator Id never changes. The xxxctl function also allows a process to modify the mode member of an IPC object.
- Every time a process accesses an IPC object, IPC performs two-level checks. When the object is opened (xxxget function), it is executed once, and will be executed once every time this object is used.
6. identifier Reuse
- The ipc_perm structure contains a seq variable, which is a slot usage serial number. This variable is a counter maintained by the kernel for each potential IPC object in the system. Each time an IPC object is deleted, the kernel increments the corresponding slot number. If it overflows, the cycle is 0.
- There are two reasons for this counter:
- Consider the file descriptor maintained by the kernel for opening files. They are small integers that only make sense within a single process and are process-specific values. However, the system v ipc identifier is system-specific, not process-specific.
- To avoid reusing the System v ipc identifier in a short time
7. IPCS and ipcrm programs
- It can only be used for System v ipc, but not for POSIX IPC
- IPCS-Q-m-S
- Ipcrm -- all = msg
8. kernel restrictions
UNIX network programming 2 Reading Notes Chapter 2-System V IPC