summaryrefslogtreecommitdiffstats
path: root/sys-utils/ipcutils.h
blob: 8e2bfddfb8fdeb055917b465c5a8b08a63b7c210 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef UTIL_LINUX_IPCUTILS_H
#define UTIL_LINUX_IPCUTILS_H

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>

/*
 * SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside
 * #ifdef __KERNEL__ ... #endif
 */
#ifndef SHM_DEST
  /* shm_mode upper byte flags */
# define SHM_DEST	01000	/* segment will be destroyed on last detach */
# define SHM_LOCKED	02000	/* segment will not be swapped */
#endif

/* For older kernels the same holds for the defines below */
#ifndef MSG_STAT
# define MSG_STAT	11
# define MSG_INFO	12
#endif

#ifndef SHM_STAT
# define SHM_STAT	13
# define SHM_INFO	14
struct shm_info {
	int used_ids;
	ulong shm_tot;		/* total allocated shm */
	ulong shm_rss;		/* total resident shm */
	ulong shm_swp;		/* total swapped shm */
	ulong swap_attempts;
	ulong swap_successes;
};
#endif

#ifndef SEM_STAT
# define SEM_STAT	18
# define SEM_INFO	19
#endif

/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
#ifndef IPC_INFO
# define IPC_INFO	3
#endif

/*
 *  * The last arg of semctl is a union semun, but where is it defined? X/OPEN
 *   * tells us to define it ourselves, but until recently Linux include files
 *    * would also define it.
 *     */
#ifndef HAVE_UNION_SEMUN
/* according to X/OPEN we have to define it ourselves */
union semun {
	int val;
	struct semid_ds *buf;
	unsigned short int *array;
	struct seminfo *__buf;
};
#endif

/*
 * X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
 *	glibc-1.09 has no support for sysv ipc.
 *	glibc 2 uses __key, __seq
 */
#if defined (__GLIBC__) && __GLIBC__ >= 2
# define KEY __key
#else
# define KEY key
#endif

struct ipc_limits {
	uint64_t	shmmni;		/* max number of segments */
	uint64_t	shmmax;		/* max segment size */
	uint64_t	shmall;		/* max total shared memory */
	uint64_t	shmmin;		/* min segment size */

	int		semmni;		/* max number of arrays */
	int		semmsl;		/* max semaphores per array */
	int		semmns;		/* max semaphores system wide */
	int		semopm;		/* max ops per semop call */
	unsigned int	semvmx;		/* semaphore max value (constant) */

	int		msgmni;		/* max queues system wide */
	size_t		msgmax;		/* max size of message */
	int		msgmnb;		/* default max size of queue */
};

extern int ipc_msg_get_limits(struct ipc_limits *lim);
extern int ipc_sem_get_limits(struct ipc_limits *lim);
extern int ipc_shm_get_limits(struct ipc_limits *lim);

struct ipc_stat {
	int		id;
	key_t		key;
	uid_t		uid;    /* current uid */
	gid_t		gid;    /* current gid */
	uid_t		cuid;    /* creator uid */
	gid_t		cgid;    /* creator gid */
	unsigned int	mode;
};

extern void ipc_print_perms(FILE *f, struct ipc_stat *is);

/* See 'struct shmid_kernel' in kernel sources
 */
struct shm_data {
	struct ipc_stat	shm_perm;

	uint64_t	shm_nattch;
	uint64_t	shm_segsz;
	time_t		shm_atim;
	time_t		shm_dtim;
	time_t		shm_ctim;
	pid_t		shm_cprid;
	pid_t		shm_lprid;
	uint64_t	shm_rss;
	uint64_t	shm_swp;

	struct shm_data  *next;
};

extern int ipc_shm_get_info(int maxid, int id, struct shm_data **shmds);
extern void ipc_shm_free_info(struct shm_data *shmds);

#endif /* UTIL_LINUX_IPCUTILS_H */