]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ipcutils.h
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / ipcutils.h
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
10 * Copyright (C) 2012-2023 Karel Zak <kzak@redhat.com>
11 */
12 #ifndef UTIL_LINUX_IPCUTILS_H
13 #define UTIL_LINUX_IPCUTILS_H
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/ipc.h>
18 #include <sys/msg.h>
19 #include <sys/sem.h>
20 #include <sys/shm.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <grp.h>
25 #include <pwd.h>
26 #include <stdint.h>
27
28 /*
29 * SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside
30 * #ifdef __KERNEL__ ... #endif
31 */
32 #ifndef SHM_DEST
33 /* shm_mode upper byte flags */
34 # define SHM_DEST 01000 /* segment will be destroyed on last detach */
35 # define SHM_LOCKED 02000 /* segment will not be swapped */
36 #endif
37
38 /* For older kernels the same holds for the defines below */
39 #ifndef MSG_STAT
40 # define MSG_STAT 11
41 # define MSG_INFO 12
42 #endif
43
44 #ifndef SHM_STAT
45 # define SHM_STAT 13
46 # define SHM_INFO 14
47 struct shm_info {
48 int used_ids;
49 unsigned long shm_tot; /* total allocated shm */
50 unsigned long shm_rss; /* total resident shm */
51 unsigned long shm_swp; /* total swapped shm */
52 unsigned long swap_attempts;
53 unsigned long swap_successes;
54 };
55 #endif
56
57 #ifndef SEM_STAT
58 # define SEM_STAT 18
59 # define SEM_INFO 19
60 #endif
61
62 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
63 #ifndef IPC_INFO
64 # define IPC_INFO 3
65 #endif
66
67 /*
68 * * The last arg of semctl is a union semun, but where is it defined? X/OPEN
69 * * tells us to define it ourselves, but until recently Linux include files
70 * * would also define it.
71 * */
72 #ifndef HAVE_UNION_SEMUN
73 /* according to X/OPEN we have to define it ourselves */
74 union semun {
75 int val;
76 struct semid_ds *buf;
77 unsigned short int *array;
78 struct seminfo *__buf;
79 };
80 #endif
81
82 /*
83 * X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
84 * glibc-1.09 has no support for sysv ipc.
85 * glibc 2 uses __key, __seq
86 */
87 #if defined (__GLIBC__) && __GLIBC__ >= 2
88 # define KEY __key
89 #else
90 # define KEY key
91 #endif
92
93 /* Size printing in ipcs is using these. */
94 enum {
95 IPC_UNIT_DEFAULT,
96 IPC_UNIT_BYTES,
97 IPC_UNIT_KB,
98 IPC_UNIT_HUMAN
99 };
100
101 struct ipc_limits {
102 uint64_t shmmni; /* max number of segments */
103 uint64_t shmmax; /* max segment size */
104 uint64_t shmall; /* max total shared memory */
105 uint64_t shmmin; /* min segment size */
106
107 int semmni; /* max number of arrays */
108 int semmsl; /* max semaphores per array */
109 int semmns; /* max semaphores system wide */
110 int semopm; /* max ops per semop call */
111 unsigned int semvmx; /* semaphore max value (constant) */
112
113 int msgmni; /* max queues system wide */
114 uint64_t msgmax; /* max size of message */
115 int msgmnb; /* default max size of queue */
116 };
117
118 extern int ipc_msg_get_limits(struct ipc_limits *lim);
119 extern int ipc_sem_get_limits(struct ipc_limits *lim);
120 extern int ipc_shm_get_limits(struct ipc_limits *lim);
121
122 struct ipc_stat {
123 int id;
124 key_t key;
125 uid_t uid; /* current uid */
126 gid_t gid; /* current gid */
127 uid_t cuid; /* creator uid */
128 gid_t cgid; /* creator gid */
129 unsigned int mode;
130 };
131
132 extern void ipc_print_perms(FILE *f, struct ipc_stat *is);
133 extern void ipc_print_size(int unit, char *msg, uint64_t size, const char *end, int width);
134
135 /* See 'struct shmid_kernel' in kernel sources
136 */
137 struct shm_data {
138 struct ipc_stat shm_perm;
139
140 uint64_t shm_nattch;
141 uint64_t shm_segsz;
142 int64_t shm_atim; /* __kernel_time_t is signed long */
143 int64_t shm_dtim;
144 int64_t shm_ctim;
145 pid_t shm_cprid;
146 pid_t shm_lprid;
147 uint64_t shm_rss;
148 uint64_t shm_swp;
149
150 struct shm_data *next;
151 };
152
153 extern int ipc_shm_get_info(int id, struct shm_data **shmds);
154 extern void ipc_shm_free_info(struct shm_data *shmds);
155
156 /* See 'struct sem_array' in kernel sources
157 */
158 struct sem_elem {
159 int semval;
160 int ncount; /* processes waiting on increase semval */
161 int zcount; /* processes waiting on semval set to zero */
162 pid_t pid; /* process last executed semop(2) call */
163 };
164 struct sem_data {
165 struct ipc_stat sem_perm;
166
167 int64_t sem_ctime;
168 int64_t sem_otime;
169 uint64_t sem_nsems;
170
171 struct sem_elem *elements;
172 struct sem_data *next;
173 };
174
175 extern int ipc_sem_get_info(int id, struct sem_data **semds);
176 extern void ipc_sem_free_info(struct sem_data *semds);
177
178 /* See 'struct msg_queue' in kernel sources
179 */
180 struct msg_data {
181 struct ipc_stat msg_perm;
182
183 int64_t q_stime;
184 int64_t q_rtime;
185 int64_t q_ctime;
186 uint64_t q_cbytes;
187 uint64_t q_qnum;
188 uint64_t q_qbytes;
189 pid_t q_lspid;
190 pid_t q_lrpid;
191
192 struct msg_data *next;
193 };
194
195 extern int ipc_msg_get_info(int id, struct msg_data **msgds);
196 extern void ipc_msg_free_info(struct msg_data *msgds);
197
198 #endif /* UTIL_LINUX_IPCUTILS_H */