Introduction to Linux lsipc Command

New types of scams are gradually increasing, and everyone should remain vigilant. Remember that you can refuse any of the following:

  • Screen sharing

  • Request for transfer

  • Request for identity information, etc.

Introduction to Linux lsipc Command

1. What is lsipc

http://man.he.net/man1/lsipc

lsipc – Displays information about the IPC facilities used in the current system.

Inter-Process Communication (IPC) is an important concept. To effectively manage and monitor these IPC resources, Linux provides various tools and commands, among which lsipc is one. However, it is important to note that the standard Linux distribution may not have a command directly named lsipc, but what we commonly refer to as lsipc usually refers to a specific usage of the ipcs command or a tool provided by a certain Linux distribution to list information about IPC facilities in the system.

The lsipc (or ipcs) command is used to display information about the currently existing IPC facilities in the Linux system, including message queues, semaphores, and shared memory. These IPC facilities allow different processes to share and exchange information, thus completing complex tasks. Understanding and managing these IPC resources is crucial for system performance tuning, troubleshooting, and security analysis in data processing and analysis.

2. Parameters

[root@localhost ~]# lsipc -h Usage: lsipc [options] displays information about IPC facilities. Resource options: -m, --shmems Shared memory segments -q, --queues Message queues -s, --semaphores Semaphores -g, --global Information about system-wide usage (can be used with -m, -q, and -s) -i, --id <id> Print detailed information about the resource identified by <id> Options: --noheadings Do not print headings --notruncate Do not truncate output --time-format=<type> Display date in short format, full format, or ISO format -b, --bytes Print SIZE in bytes instead of human-readable format -c, --creator Display creator and owner -e, --export Display in an exportable output format -n, --newline Display each piece of information on a new line -l, --list Force list output format (e.g., when used with --id) -o, --output[=<list>] Define the columns to output -P, --numeric-perms Print numeric permissions (PERMS column) -r, --raw Display in raw mode -t, --time Display additional, detached, and change times -h, --help Display this help and exit -V, --version Output version information and exit Common columns: KEY Resource key ID Resource ID OWNER Owner's username or UID PERMS Permissions CUID Creator UID CUSER Creator user CGID Creator GID CGROUP Creator group UID User ID USER Username GID Group ID GROUP Group name CTIME Last change time Shared memory columns (--shmems): SIZE Segment size NATTCH Number of attached processes STATUS Status ATTACH Attachment time DETACH Detachment time COMMAND Creator command line CPID Creator's PID LPID Last user's PID Message queue columns (--queues): USEDBYTES Bytes used MSGS Number of messages SEND Last sent message time RECV Last received message time LSPID Last sent message's PID LRPID Last received message's PID Semaphore columns (--semaphores): NSEMS Number of semaphores OTIME Last operation time Summary columns (--global): RESOURCE Resource name DESCRIPTION Resource description LIMIT System-wide limit USED Currently used USE% Current usage percentage For more details, see lsipc(1).
Exit status: 0 if normal, 1 if incorrect parameters specified, 2 if a serious error occurred.

3. Examples

1. Check which IPC facilities are already available on the system

[root@localhost ~]# lsipc RESOURCE DESCRIPTION                                              LIMIT USED  USE%MSGMNI   Number of message queues                                  7546    0 0.00%MSGMAX   Max size of message (bytes)                               8192    -     -MSGMNB   Default max size of queue (bytes)                        16384    -     -SHMMNI   Shared memory segments                                    4096    1 0.02%SHMALL   Shared memory pages                       18446744073692774399    0 0.00%SHMMAX   Max size of shared memory segment (bytes) 18446744073692774399    -     -SHMMIN   Min size of shared memory segment (bytes)                    1    -     -SEMMNI   Number of semaphore identifiers                            128    0 0.00%SEMMNS   Total number of semaphores                               32000    0 0.00%SEMMSL   Max semaphores per semaphore set.                          250    -     -SEMOPM   Max number of operations per semop(2)                       32    -     -SEMVMX   Semaphore max value                                      32767    -     -

The above shows the current status and limits of system-level inter-process communication (IPC) resources. Below is an explanation of each output field:

RESOURCE: Resource name, indicating the type of IPC resource. DESCRIPTION: Resource description, briefly explaining the purpose or significance of that resource. LIMIT: System-wide limit, indicating the maximum value or quantity limit allowed for that resource. USED: Currently used quantity or size of the resource. USE%: Percentage of used resources, relative to LIMIT.

The specific resource explanations are as follows:

MSGMNI: Maximum number of message queues. Message queues allow processes to exchange messages in an orderly manner. MSGMAX: Maximum size of a single message (bytes). This is the maximum amount of data that can be contained in each message in the message queue. MSGMNB: Default maximum size of a message queue (bytes). This is the default size limit for a newly created message queue when no specific size is specified. SHMMNI: Maximum number of shared memory segments. Shared memory allows processes to share the same memory area for efficient data exchange. SHMALL: Total number of pages available for shared memory in the system. This value is usually very large because it is based on the physical memory size of the system. SHMMAX: Maximum size of a single shared memory segment (bytes). This is the maximum size of the shared memory segment that can be created. SHMMIN: Minimum size of a single shared memory segment (bytes). Although it is usually not mandatory to create shared memory segments larger than this size, this value defines the possible minimum size. SEMMNI: Maximum number of semaphore identifiers. Semaphores are used for synchronization between processes or threads. SEMMNS: Maximum total number of semaphores in the system. This is the maximum number of semaphores that can exist in the system. SEMMSL: Maximum number of semaphores per semaphore set. This is the maximum number of semaphores that can be included in a single semaphore set. SEMOPM: Maximum number of semaphores modified in each semop system call. This is the maximum number of semaphores that can be modified in a single semop call. SEMVMX: Maximum value of a semaphore. This is the maximum integer value that a semaphore can hold.

Use the ipcs command to check the current activity of each subsystem; it can be seen that there are no message or semaphore arrays.

[root@localhost ~]# ipcs ------ Message Queues --------key        msqid      owner      perms      used-bytes   messages    ------ Shared Memory Segments --------key        shmid      owner      perms      bytes      nattch     status      0x0052e2c1 0          postgres   600        56         6                       ------ Semaphore Arrays --------key        semid      owner      perms      nsems 

2. Create a message queue

[root@localhost ~]# ipcmk --queueMessage queue id: 0[root@localhost ~]# ipcmk --queueMessage queue id: 32769[root@localhost ~]# ipcmk --queueMessage queue id: 65538[root@localhost ~]# ipcmk --queueMessage queue id: 98307[root@localhost ~]# ipcmk --queueMessage queue id: 131076

Write a simple IPC message sender; for simplicity, hardcode the queue ID:

int msqid = 98307; // ID from the above ipcmk –queue

strcpy(message.text,“lime.com”) // Customize the message, e.g., lime.com

[root@localhost opt]# cat test.c#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <string.h>struct msgbuffer {  char text[24];} message;int main() {    int msqid = 98307;    strcpy(message.text,"lime.com");    msgsnd(msqid, &message, sizeof(message), 0);    printf("Message: %s\n",message.text);    printf("Queue: %d\n",msqid);    return 0;        } 

<span>Compile</span>

[root@localhost opt]# gcc test.c -o test.bin[root@localhost opt]# ./test.bin  Message: opensource.com Queue: 98307

<span>Check with ipcs, note the difference between 0x2e2c8d8b and 98307</span>

[root@localhost opt]# ./test.bin Message: opensource.com Queue: 98307[root@localhost opt]# ipcs -q ------ Message Queues --------key        msqid      owner      perms      used-bytes   messages    0x32eb5f61 0          root       644        0            0           0x7778b164 32769      root       644        0            0           0xf9830a2d 65538      root       644        0            0           0x2e2c8d8b 98307      root       644        24           1           0x1459927b 131076     root       644        0            0  

Retrieve the message

[root@localhost opt]# cat test2.c #include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>struct msgbuffer {    char text[24];} message;int main() {    int msqid = 98307;    msgrcv(msqid, &message, sizeof(message),0,0);    printf("\nQueue: %d\n",msqid);    printf("Got this message: %s\n", message.text);    msgctl(msqid,IPC_RMID,NULL);    return 0;}[root@localhost opt]# gcc test2.c -o test2.bin[root@localhost opt]# ./test2.bin Queue: 98307Got this message: lime.com

4. Supplement

History: The lsipc tool was developed inspired by the ipcs tool.

Authors: Ondrej Oprala [email protected]Karel Zak [email protected]

Availability: The lsipc command is part of the util-linux package, which can be obtained from the Linux kernel archive https://www.kernel.org/pub/linux/utils/util-linux/.

Notes:

  • Permission issues: Since IPC facilities may involve sensitive system information, ensure you have sufficient permissions when using the lsipc (or ipcs) command. Ordinary users may not be able to view all IPC facilities’ information.

  • Caution: Before deleting or modifying IPC facilities, ensure you understand the function and dependencies of that facility to avoid unnecessary impact on the system.

  • Regular monitoring: For important IPC facilities, it is recommended to regularly monitor and check using lsipc (or ipcs) command to ensure their normal operation and security.

  • Combine with other tools: In addition to lsipc (or ipcs) command, you can combine with other Linux system tools (such as ipcrm, strace, etc.) for deeper analysis and debugging of IPC-related issues.

5. Conclusion

The lsipc command outputs the IPC facility information from the kernel in a human-readable format to the terminal. The main features of this command include:

  • Support for various IPC facilities: Can display information about various IPC facilities such as message queues, semaphores, and shared memory.

  • Detailed output: Provides rich output options to display detailed information about IPC facilities, such as creator, owner, permissions, size, etc.

  • Flexible querying: Supports filtering and querying specific IPC facility information through different parameters and options.

In summary, it is a very useful command for diagnosing certain difficult problems.

Last but not least, feel free to communicate: Follow the public account to leave a message, or leave a message directly below:

Leave a Comment