]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ipcutils.c
ipcs: cleanup jumplabel stlyes
[thirdparty/util-linux.git] / sys-utils / ipcutils.c
1
2 #include <inttypes.h>
3
4 #include "c.h"
5 #include "nls.h"
6 #include "xalloc.h"
7 #include "path.h"
8 #include "pathnames.h"
9 #include "ipcutils.h"
10 #include "strutils.h"
11
12 #ifndef SEMVMX
13 # define SEMVMX 32767 /* <= 32767 semaphore maximum value */
14 #endif
15 #ifndef SHMMIN
16 # define SHMMIN 1 /* min shared segment size in bytes */
17 #endif
18
19
20 int ipc_msg_get_limits(struct ipc_limits *lim)
21 {
22 if (path_exist(_PATH_PROC_IPC_MSGMNI) &&
23 path_exist(_PATH_PROC_IPC_MSGMNB) &&
24 path_exist(_PATH_PROC_IPC_MSGMAX)) {
25
26 lim->msgmni = path_read_s32(_PATH_PROC_IPC_MSGMNI);
27 lim->msgmnb = path_read_s32(_PATH_PROC_IPC_MSGMNB);
28 lim->msgmax = path_read_s32(_PATH_PROC_IPC_MSGMAX);
29 } else {
30 struct msginfo msginfo;
31
32 if (msgctl(0, IPC_INFO, (struct msqid_ds *) &msginfo) < 0)
33 return 1;
34 lim->msgmni = msginfo.msgmni;
35 lim->msgmnb = msginfo.msgmnb;
36 lim->msgmax = msginfo.msgmax;
37 }
38
39 return 0;
40 }
41
42 int ipc_sem_get_limits(struct ipc_limits *lim)
43 {
44 FILE *f;
45 int rc = 0;
46
47 lim->semvmx = SEMVMX;
48
49 f = path_fopen("r", 0, _PATH_PROC_IPC_SEM);
50 if (f) {
51 rc = fscanf(f, "%d\t%d\t%d\t%d",
52 &lim->semmsl, &lim->semmns, &lim->semopm, &lim->semmni);
53 fclose(f);
54
55 }
56
57 if (rc == 4) {
58 struct seminfo seminfo;
59 union semun arg = { .array = (ushort *) &seminfo };
60
61 if (semctl(0, 0, IPC_INFO, arg) < 0)
62 return 1;
63 lim->semmni = seminfo.semmni;
64 lim->semmsl = seminfo.semmsl;
65 lim->semmns = seminfo.semmns;
66 lim->semopm = seminfo.semopm;
67 }
68
69 return 0;
70 }
71
72 int ipc_shm_get_limits(struct ipc_limits *lim)
73 {
74 lim->shmmin = SHMMIN;
75
76 if (path_exist(_PATH_PROC_IPC_SHMALL) &&
77 path_exist(_PATH_PROC_IPC_SHMMAX) &&
78 path_exist(_PATH_PROC_IPC_SHMMNI)) {
79
80 lim->shmall = path_read_u64(_PATH_PROC_IPC_SHMALL);
81 lim->shmmax = path_read_u64(_PATH_PROC_IPC_SHMMAX);
82 lim->shmmni = path_read_u64(_PATH_PROC_IPC_SHMMNI);
83
84 } else {
85 struct shminfo shminfo;
86
87 if (shmctl(0, IPC_INFO, (struct shmid_ds *) &shminfo) < 0)
88 return 1;
89 lim->shmmni = shminfo.shmmni;
90 lim->shmall = shminfo.shmall;
91 }
92
93 return 0;
94 }
95
96 int ipc_shm_get_info(int id, struct shm_data **shmds)
97 {
98 FILE *f;
99 int i = 0, maxid;
100 struct shm_data *p;
101 struct shm_info dummy;
102
103 p = *shmds = xcalloc(1, sizeof(struct shm_data));
104 p->next = NULL;
105
106 f = path_fopen("r", 0, _PATH_PROC_SYSV_SHM);
107 if (!f)
108 goto shm_fallback;
109
110 while (fgetc(f) != '\n'); /* skip header */
111
112 while (feof(f) == 0) {
113 if (fscanf(f,
114 "%d %d %o %"SCNu64 " %u %u "
115 "%"SCNu64 " %u %u %u %u %"SCNi64 " %"SCNi64 " %"SCNi64
116 " %"SCNu64 " %"SCNu64 "\n",
117 &p->shm_perm.key,
118 &p->shm_perm.id,
119 &p->shm_perm.mode,
120 &p->shm_segsz,
121 &p->shm_cprid,
122 &p->shm_lprid,
123 &p->shm_nattch,
124 &p->shm_perm.uid,
125 &p->shm_perm.gid,
126 &p->shm_perm.cuid,
127 &p->shm_perm.cgid,
128 &p->shm_atim,
129 &p->shm_dtim,
130 &p->shm_ctim,
131 &p->shm_rss,
132 &p->shm_swp) != 16)
133 continue;
134
135 if (id > -1) {
136 /* ID specified */
137 if (id == p->shm_perm.id) {
138 i = 1;
139 break;
140 } else
141 continue;
142 }
143
144 p->next = xcalloc(1, sizeof(struct shm_data));
145 p = p->next;
146 p->next = NULL;
147 i++;
148 }
149
150 if (i == 0)
151 free(*shmds);
152 fclose(f);
153 return i;
154
155 /* Fallback; /proc or /sys file(s) missing. */
156 shm_fallback:
157 i = id < 0 ? 0 : id;
158
159 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) &dummy);
160 if (maxid < 0)
161 return 0;
162
163 while (i <= maxid) {
164 int shmid;
165 struct shmid_ds shmseg;
166 struct ipc_perm *ipcp = &shmseg.shm_perm;
167
168 shmid = shmctl(i, SHM_STAT, &shmseg);
169 if (shmid < 0) {
170 if (-1 < id) {
171 free(*shmds);
172 return 0;
173 }
174 i++;
175 continue;
176 }
177
178 p->shm_perm.key = ipcp->KEY;
179 p->shm_perm.id = shmid;
180 p->shm_perm.mode = ipcp->mode;
181 p->shm_segsz = shmseg.shm_segsz;
182 p->shm_cprid = shmseg.shm_cpid;
183 p->shm_lprid = shmseg.shm_lpid;
184 p->shm_nattch = shmseg.shm_nattch;
185 p->shm_perm.uid = ipcp->uid;
186 p->shm_perm.gid = ipcp->gid;
187 p->shm_perm.cuid = ipcp->cuid;
188 p->shm_perm.cgid = ipcp->cuid;
189 p->shm_atim = shmseg.shm_atime;
190 p->shm_dtim = shmseg.shm_dtime;
191 p->shm_ctim = shmseg.shm_ctime;
192 p->shm_rss = 0xdead;
193 p->shm_swp = 0xdead;
194
195 if (id < 0) {
196 p->next = xcalloc(1, sizeof(struct shm_data));
197 p = p->next;
198 p->next = NULL;
199 i++;
200 } else
201 return 1;
202 }
203
204 return i;
205 }
206
207 void ipc_shm_free_info(struct shm_data *shmds)
208 {
209 while (shmds) {
210 struct shm_data *next = shmds->next;
211 free(shmds);
212 shmds = next;
213 }
214 }
215
216 static void get_sem_elements(struct sem_data *p)
217 {
218 size_t i;
219
220 if (!p || !p->sem_nsems || p->sem_perm.id < 0)
221 return;
222
223 p->elements = xcalloc(p->sem_nsems, sizeof(struct sem_elem));
224
225 for (i = 0; i < p->sem_nsems; i++) {
226 struct sem_elem *e = &p->elements[i];
227 union semun arg = { .val = 0 };
228
229 e->semval = semctl(p->sem_perm.id, i, GETVAL, arg);
230 if (e->semval < 0)
231 err(EXIT_FAILURE, _("%s failed"), "semctl(GETVAL)");
232
233 e->ncount = semctl(p->sem_perm.id, i, GETNCNT, arg);
234 if (e->ncount < 0)
235 err(EXIT_FAILURE, _("%s failed"), "semctl(GETNCNT)");
236
237 e->zcount = semctl(p->sem_perm.id, i, GETZCNT, arg);
238 if (e->zcount < 0)
239 err(EXIT_FAILURE, _("%s failed"), "semctl(GETZCNT)");
240
241 e->pid = semctl(p->sem_perm.id, i, GETPID, arg);
242 if (e->pid < 0)
243 err(EXIT_FAILURE, _("%s failed"), "semctl(GETPID)");
244 }
245 }
246
247 int ipc_sem_get_info(int id, struct sem_data **semds)
248 {
249 FILE *f;
250 int i = 0, maxid;
251 struct sem_data *p;
252 struct seminfo dummy;
253 union semun arg;
254
255 p = *semds = xcalloc(1, sizeof(struct sem_data));
256 p->next = NULL;
257
258 f = path_fopen("r", 0, _PATH_PROC_SYSV_SEM);
259 if (!f)
260 goto sem_fallback;
261
262 while (fgetc(f) != '\n') ; /* skip header */
263
264 while (feof(f) == 0) {
265 if (fscanf(f,
266 "%d %d %o %" SCNu64 " %u %u %u %u %"
267 SCNi64 " %" SCNi64 "\n",
268 &p->sem_perm.key,
269 &p->sem_perm.id,
270 &p->sem_perm.mode,
271 &p->sem_nsems,
272 &p->sem_perm.uid,
273 &p->sem_perm.gid,
274 &p->sem_perm.cuid,
275 &p->sem_perm.cgid,
276 &p->sem_otime,
277 &p->sem_ctime) != 10)
278 continue;
279
280 if (id > -1) {
281 /* ID specified */
282 if (id == p->sem_perm.id) {
283 get_sem_elements(p);
284 i = 1;
285 break;
286 } else
287 continue;
288 }
289
290 p->next = xcalloc(1, sizeof(struct sem_data));
291 p = p->next;
292 p->next = NULL;
293 i++;
294 }
295
296 if (i == 0)
297 free(*semds);
298 fclose(f);
299 return i;
300
301 /* Fallback; /proc or /sys file(s) missing. */
302 sem_fallback:
303 i = id < 0 ? 0 : id;
304
305 arg.array = (ushort *) (void *)&dummy;
306 maxid = semctl(0, 0, SEM_INFO, arg);
307 if (maxid < 0)
308 return 0;
309
310 while (i <= maxid) {
311 int semid;
312 struct semid_ds semseg;
313 struct ipc_perm *ipcp = &semseg.sem_perm;
314 arg.buf = (struct semid_ds *)&semseg;
315
316 semid = semctl(i, 0, SEM_STAT, arg);
317 if (semid < 0) {
318 if (-1 < id) {
319 free(*semds);
320 return 0;
321 }
322 i++;
323 continue;
324 }
325
326 p->sem_perm.key = ipcp->KEY;
327 p->sem_perm.id = semid;
328 p->sem_perm.mode = ipcp->mode;
329 p->sem_nsems = semseg.sem_nsems;
330 p->sem_perm.uid = ipcp->uid;
331 p->sem_perm.gid = ipcp->gid;
332 p->sem_perm.cuid = ipcp->cuid;
333 p->sem_perm.cgid = ipcp->cuid;
334 p->sem_otime = semseg.sem_otime;
335 p->sem_ctime = semseg.sem_ctime;
336
337 if (id < 0) {
338 p->next = xcalloc(1, sizeof(struct sem_data));
339 p = p->next;
340 p->next = NULL;
341 i++;
342 } else {
343 get_sem_elements(p);
344 return 1;
345 }
346 }
347
348 return i;
349 }
350
351 void ipc_sem_free_info(struct sem_data *semds)
352 {
353 while (semds) {
354 struct sem_data *next = semds->next;
355 free(semds->elements);
356 free(semds);
357 semds = next;
358 }
359 }
360
361 int ipc_msg_get_info(int id, struct msg_data **msgds)
362 {
363 FILE *f;
364 int i = 0, maxid;
365 struct msg_data *p;
366 struct msqid_ds dummy;
367 struct msqid_ds msgseg;
368
369 p = *msgds = xcalloc(1, sizeof(struct msg_data));
370 p->next = NULL;
371
372 f = path_fopen("r", 0, _PATH_PROC_SYSV_MSG);
373 if (!f)
374 goto msg_fallback;
375
376 while (fgetc(f) != '\n') ; /* skip header */
377
378 while (feof(f) == 0) {
379 if (fscanf(f,
380 "%d %d %o %" SCNu64 " %" SCNu64
381 " %u %u %u %u %u %u %" SCNi64 " %" SCNi64 " %" SCNi64 "\n",
382 &p->msg_perm.key,
383 &p->msg_perm.id,
384 &p->msg_perm.mode,
385 &p->q_cbytes,
386 &p->q_qnum,
387 &p->q_lspid,
388 &p->q_lrpid,
389 &p->msg_perm.uid,
390 &p->msg_perm.gid,
391 &p->msg_perm.cuid,
392 &p->msg_perm.cgid,
393 &p->q_stime,
394 &p->q_rtime,
395 &p->q_ctime) != 14)
396 continue;
397
398 if (id > -1) {
399 /* ID specified */
400 if (id == p->msg_perm.id) {
401 /*
402 * FIXME: q_qbytes are not in /proc
403 *
404 */
405 if (msgctl(id, IPC_STAT, &msgseg) != -1)
406 p->q_qbytes = msgseg.msg_qbytes;
407 i = 1;
408 break;
409 } else
410 continue;
411 }
412
413 p->next = xcalloc(1, sizeof(struct msg_data));
414 p = p->next;
415 p->next = NULL;
416 i++;
417 }
418
419 if (i == 0)
420 free(*msgds);
421 fclose(f);
422 return i;
423
424 /* Fallback; /proc or /sys file(s) missing. */
425 msg_fallback:
426 i = id < 0 ? 0 : id;
427
428 maxid = msgctl(id, MSG_STAT, &dummy);
429 if (maxid < 0)
430 return 0;
431
432 while (i <= maxid) {
433 int msgid;
434 struct ipc_perm *ipcp = &msgseg.msg_perm;
435
436 msgid = msgctl(i, MSG_STAT, &msgseg);
437 if (msgid < 0) {
438 if (-1 < id) {
439 free(*msgds);
440 return 0;
441 }
442 i++;
443 continue;
444 }
445
446 p->msg_perm.key = ipcp->KEY;
447 p->msg_perm.id = msgid;
448 p->msg_perm.mode = ipcp->mode;
449 p->q_cbytes = msgseg.msg_cbytes;
450 p->q_qnum = msgseg.msg_qnum;
451 p->q_lspid = msgseg.msg_lspid;
452 p->q_lrpid = msgseg.msg_lrpid;
453 p->msg_perm.uid = ipcp->uid;
454 p->msg_perm.gid = ipcp->gid;
455 p->msg_perm.cuid = ipcp->cuid;
456 p->msg_perm.cgid = ipcp->cgid;
457 p->q_stime = msgseg.msg_stime;
458 p->q_rtime = msgseg.msg_rtime;
459 p->q_ctime = msgseg.msg_ctime;
460 p->q_qbytes = msgseg.msg_qbytes;
461
462 if (id < 0) {
463 p->next = xcalloc(1, sizeof(struct msg_data));
464 p = p->next;
465 p->next = NULL;
466 i++;
467 } else
468 return 1;
469 }
470
471 return i;
472 }
473
474 void ipc_msg_free_info(struct msg_data *msgds)
475 {
476 while (msgds) {
477 struct msg_data *next = msgds->next;
478 free(msgds);
479 msgds = next;
480 }
481 }
482
483 void ipc_print_perms(FILE *f, struct ipc_stat *is)
484 {
485 struct passwd *pw;
486 struct group *gr;
487
488 fprintf(f, "%-10d %-10o", is->id, is->mode & 0777);
489
490 if ((pw = getpwuid(is->cuid)))
491 fprintf(f, " %-10s", pw->pw_name);
492 else
493 fprintf(f, " %-10u", is->cuid);
494
495 if ((gr = getgrgid(is->cgid)))
496 fprintf(f, " %-10s", gr->gr_name);
497 else
498 fprintf(f, " %-10u", is->cgid);
499
500 if ((pw = getpwuid(is->uid)))
501 fprintf(f, " %-10s", pw->pw_name);
502 else
503 fprintf(f, " %-10u", is->uid);
504
505 if ((gr = getgrgid(is->gid)))
506 fprintf(f, " %-10s\n", gr->gr_name);
507 else
508 fprintf(f, " %-10u\n", is->gid);
509 }
510
511 void ipc_print_size(int unit, char *msg, uint64_t size, const char *end,
512 int width)
513 {
514 char format[32];
515
516 if (!msg)
517 /* NULL */ ;
518 else if (msg[strlen(msg) - 1] == '=')
519 printf("%s", msg);
520 else if (unit == IPC_UNIT_BYTES)
521 printf(_("%s (bytes) = "), msg);
522 else if (unit == IPC_UNIT_KB)
523 printf(_("%s (kbytes) = "), msg);
524 else
525 printf("%s = ", msg);
526
527 switch (unit) {
528 case IPC_UNIT_DEFAULT:
529 case IPC_UNIT_BYTES:
530 sprintf(format, "%%%dju", width);
531 printf(format, size);
532 break;
533 case IPC_UNIT_KB:
534 sprintf(format, "%%%dju", width);
535 printf(format, size / 1024);
536 break;
537 case IPC_UNIT_HUMAN:
538 sprintf(format, "%%%ds", width);
539 printf(format, size_to_human_string(SIZE_SUFFIX_1LETTER, size));
540 break;
541 default:
542 /* impossible occurred */
543 abort();
544 }
545
546 if (end)
547 printf("%s", end);
548 }