531551
From 9f643efe377d2a39929f19cc09e8890afc74d9a4 Mon Sep 17 00:00:00 2001
531551
From: Karel Zak <kzak@redhat.com>
531551
Date: Fri, 24 Jun 2016 12:57:13 +0200
531551
Subject: [PATCH 78/84] lsipc: backport new command
531551
531551
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1153770
531551
Signed-off-by: Karel Zak <kzak@redhat.com>
531551
---
531551
 bash-completion/lsipc   |   64 +++
531551
 configure.ac            |    6 +
531551
 include/xalloc.h        |    7 +
531551
 sys-utils/Makemodule.am |   10 +
531551
 sys-utils/ipcs.c        |    2 +-
531551
 sys-utils/ipcutils.c    |  116 ++---
531551
 sys-utils/ipcutils.h    |   13 +-
531551
 sys-utils/lsipc.1       |  133 +++++
531551
 sys-utils/lsipc.c       | 1316 +++++++++++++++++++++++++++++++++++++++++++++++
531551
 tests/functions.sh      |   16 +-
531551
 tests/ts/ipcs/limits2   |    9 +-
531551
 11 files changed, 1613 insertions(+), 79 deletions(-)
531551
 create mode 100644 bash-completion/lsipc
531551
 create mode 100644 sys-utils/lsipc.1
531551
 create mode 100644 sys-utils/lsipc.c
531551
531551
diff --git a/bash-completion/lsipc b/bash-completion/lsipc
531551
new file mode 100644
531551
index 0000000..6a87393
531551
--- /dev/null
531551
+++ b/bash-completion/lsipc
531551
@@ -0,0 +1,64 @@
531551
+_lsipc_module()
531551
+{
531551
+	local cur prev OPTS ARG
531551
+	COMPREPLY=()
531551
+	cur="${COMP_WORDS[COMP_CWORD]}"
531551
+	prev="${COMP_WORDS[COMP_CWORD-1]}"
531551
+	case $prev in
531551
+		'-i'|'--id')
531551
+			COMPREPLY=( $(compgen -W "id" -- $cur) )
531551
+			return 0
531551
+			;;
531551
+		'-h'|'--help'|'-V'|'--version')
531551
+			return 0
531551
+			;;
531551
+		'-o'|'--output')
531551
+			local prefix realcur OUTPUT_ALL OUTPUT
531551
+			realcur="${cur##*,}"
531551
+			prefix="${cur%$realcur}"
531551
+			OUTPUT_ALL="GENERAL KEY ID OWNER PERMS CUID
531551
+			CGID UID GID CHANGE MESSAGE USEDBYTES
531551
+			MSGS SEND RECV LSPID LRPID SHARED BYTES
531551
+			NATTCH STATUS ATTACH DETACH CPID LPID NSEMS
531551
+			LASTOP"
531551
+			for WORD in $OUTPUT_ALL; do
531551
+				if ! [[ $prefix == *"$WORD"* ]]; then
531551
+					OUTPUT="$WORD $OUTPUT"
531551
+				fi
531551
+			done
531551
+			compopt -o nospace
531551
+			COMPREPLY=( $(compgen -P "$prefix" -W "$OUTPUT" -S ',' -- $realcur) )
531551
+			return 0
531551
+			;;
531551
+	esac
531551
+	case $cur in
531551
+		-*)
531551
+			OPTS="--id
531551
+				--help
531551
+				--version
531551
+				--shmems
531551
+				--queues
531551
+				--semaphores
531551
+				--colon-separate
531551
+				--creator
531551
+				--export
531551
+				--global
531551
+				--newline
531551
+				--noheadings
531551
+				--notruncate
531551
+				--output
531551
+				--pid
531551
+				--print0
531551
+				--raw
531551
+				--time
531551
+				--time-format"
531551
+			COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
531551
+			return 0
531551
+			;;
531551
+	esac
531551
+	local IFS=$'\n'
531551
+	compopt -o filenames
531551
+	COMPREPLY=( $(compgen -f -- $cur) )
531551
+	return 0
531551
+}
531551
+complete -F _lsipc_module lsipc
531551
diff --git a/configure.ac b/configure.ac
531551
index 5d9ea39..fe0a011 100644
531551
--- a/configure.ac
531551
+++ b/configure.ac
531551
@@ -1038,6 +1038,12 @@ UL_REQUIRES_BUILD([lsns], [libsmartcols])
531551
 AM_CONDITIONAL([BUILD_LSNS], [test "x$build_lsns" = xyes])
531551
 
531551
 
531551
+UL_BUILD_INIT([lsipc], [check])
531551
+UL_REQUIRES_LINUX([lsipc])
531551
+UL_REQUIRES_BUILD([lsipc], [libsmartcols])
531551
+AM_CONDITIONAL([BUILD_LSIPC], [test "x$build_lsipc" = xyes])
531551
+
531551
+
531551
 UL_BUILD_INIT([chcpu], [check])
531551
 UL_REQUIRES_LINUX([chcpu])
531551
 UL_REQUIRES_HAVE([chcpu], [cpu_set_t], [cpu_set_t type])
531551
diff --git a/include/xalloc.h b/include/xalloc.h
531551
index 6342793..1a1799a 100644
531551
--- a/include/xalloc.h
531551
+++ b/include/xalloc.h
531551
@@ -19,6 +19,13 @@
531551
 # define XALLOC_EXIT_CODE EXIT_FAILURE
531551
 #endif
531551
 
531551
+static inline void __err_oom(const char *file, unsigned int line)
531551
+{
531551
+	err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line);
531551
+}
531551
+
531551
+#define err_oom()	__err_oom(__FILE__, __LINE__)
531551
+
531551
 static inline __ul_alloc_size(1)
531551
 void *xmalloc(const size_t size)
531551
 {
531551
diff --git a/sys-utils/Makemodule.am b/sys-utils/Makemodule.am
531551
index 9baf5a3..6badd17 100644
531551
--- a/sys-utils/Makemodule.am
531551
+++ b/sys-utils/Makemodule.am
531551
@@ -22,6 +22,16 @@ ipcs_SOURCES =	sys-utils/ipcs.c \
531551
 ipcs_LDADD = $(LDADD) libcommon.la
531551
 
531551
 
531551
+if BUILD_LSIPC
531551
+usrbin_exec_PROGRAMS += lsipc
531551
+dist_man_MANS += sys-utils/lsipc.1
531551
+lsipc_SOURCES =	sys-utils/lsipc.c \
531551
+		sys-utils/ipcutils.c \
531551
+		sys-utils/ipcutils.h
531551
+lsipc_LDADD = $(LDADD) libcommon.la libsmartcols.la
531551
+lsipc_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
531551
+endif
531551
+
531551
 usrbin_exec_PROGRAMS += renice
531551
 dist_man_MANS += sys-utils/renice.1
531551
 renice_SOURCES = sys-utils/renice.c
531551
diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c
531551
index 14f5f0b..1843cd5 100644
531551
--- a/sys-utils/ipcs.c
531551
+++ b/sys-utils/ipcs.c
531551
@@ -201,7 +201,7 @@ static void do_shm (char format, int unit)
531551
 			       _("max seg size"), lim.shmmax, "\n", 0);
531551
 		ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit,
531551
 			       _("max total shared memory"),
531551
-			       lim.shmall * getpagesize(), "\n", 0);
531551
+			       (uint64_t) lim.shmall * getpagesize(), "\n", 0);
531551
 		ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
531551
 			       _("min seg size"), lim.shmmin, "\n", 0);
531551
 		return;
531551
diff --git a/sys-utils/ipcutils.c b/sys-utils/ipcutils.c
531551
index 62d7428..51fce7b 100644
531551
--- a/sys-utils/ipcutils.c
531551
+++ b/sys-utils/ipcutils.c
531551
@@ -1,4 +1,3 @@
531551
-
531551
 #include <inttypes.h>
531551
 
531551
 #include "c.h"
531551
@@ -54,8 +53,8 @@ int ipc_sem_get_limits(struct ipc_limits *lim)
531551
 
531551
 	}
531551
 
531551
-	if (rc == 4) {
531551
-		struct seminfo seminfo;
531551
+	if (rc != 4) {
531551
+		struct seminfo seminfo = { .semmni = 0 };
531551
 		union semun arg = { .array = (ushort *) &seminfo };
531551
 
531551
 		if (semctl(0, 0, IPC_INFO, arg) < 0)
531551
@@ -82,12 +81,15 @@ int ipc_shm_get_limits(struct ipc_limits *lim)
531551
 		lim->shmmni = path_read_u64(_PATH_PROC_IPC_SHMMNI);
531551
 
531551
 	} else {
531551
-		struct shminfo shminfo;
531551
+		struct shminfo *shminfo;
531551
+		struct shmid_ds shmbuf;
531551
 
531551
-		if (shmctl(0, IPC_INFO, (struct shmid_ds *) &shminfo) < 0)
531551
+		if (shmctl(0, IPC_INFO, &shmbuf) < 0)
531551
 			return 1;
531551
-		lim->shmmni = shminfo.shmmni;
531551
-		lim->shmall = shminfo.shmall;
531551
+		shminfo = (struct shminfo *) &shmbuf;
531551
+		lim->shmmni = shminfo->shmmni;
531551
+		lim->shmall = shminfo->shmall;
531551
+		lim->shmmax = shminfo->shmmax;
531551
 	}
531551
 
531551
 	return 0;
531551
@@ -97,20 +99,24 @@ int ipc_shm_get_info(int id, struct shm_data **shmds)
531551
 {
531551
 	FILE *f;
531551
 	int i = 0, maxid;
531551
+	char buf[BUFSIZ];
531551
 	struct shm_data *p;
531551
-	struct shm_info dummy;
531551
+	struct shmid_ds dummy;
531551
 
531551
 	p = *shmds = xcalloc(1, sizeof(struct shm_data));
531551
 	p->next = NULL;
531551
 
531551
 	f = path_fopen("r", 0, _PATH_PROC_SYSV_SHM);
531551
 	if (!f)
531551
-		goto fallback;
531551
+		goto shm_fallback;
531551
 
531551
 	while (fgetc(f) != '\n');		/* skip header */
531551
 
531551
-	while (feof(f) == 0) {
531551
-		if (fscanf(f,
531551
+	while (fgets(buf, sizeof(buf), f) != NULL) {
531551
+		/* scan for the first 14-16 columns (e.g. Linux 2.6.32 has 14) */
531551
+		p->shm_rss = 0xdead;
531551
+		p->shm_swp = 0xdead;
531551
+		if (sscanf(buf,
531551
 			  "%d %d  %o %"SCNu64 " %u %u  "
531551
 			  "%"SCNu64 " %u %u %u %u %"SCNi64 " %"SCNi64 " %"SCNi64
531551
 			  " %"SCNu64 " %"SCNu64 "\n",
531551
@@ -129,8 +135,8 @@ int ipc_shm_get_info(int id, struct shm_data **shmds)
531551
 			   &p->shm_dtim,
531551
 			   &p->shm_ctim,
531551
 			   &p->shm_rss,
531551
-			   &p->shm_swp) != 16)
531551
-			continue;
531551
+			   &p->shm_swp) < 14)
531551
+			continue; /* invalid line, skipped */
531551
 
531551
 		if (id > -1) {
531551
 			/* ID specified */
531551
@@ -153,28 +159,20 @@ int ipc_shm_get_info(int id, struct shm_data **shmds)
531551
 	return i;
531551
 
531551
 	/* Fallback; /proc or /sys file(s) missing. */
531551
-fallback:
531551
-	i = id < 0 ? 0 : id;
531551
-
531551
-	maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) &dummy);
531551
-	if (maxid < 0)
531551
-		return 0;
531551
+shm_fallback:
531551
+	maxid = shmctl(0, SHM_INFO, &dummy);
531551
 
531551
-	while (i <= maxid) {
531551
+	for (int j = 0; j <= maxid; j++) {
531551
 		int shmid;
531551
 		struct shmid_ds shmseg;
531551
 		struct ipc_perm *ipcp = &shmseg.shm_perm;
531551
 
531551
-		shmid = shmctl(i, SHM_STAT, &shmseg);
531551
-		if (shmid < 0) {
531551
-			if (-1 < id) {
531551
-				free(*shmds);
531551
-				return 0;
531551
-			}
531551
-			i++;
531551
+		shmid = shmctl(j, SHM_STAT, &shmseg);
531551
+		if (shmid < 0 || (id > -1 && shmid != id)) {
531551
 			continue;
531551
 		}
531551
 
531551
+		i++;
531551
 		p->shm_perm.key = ipcp->KEY;
531551
 		p->shm_perm.id = shmid;
531551
 		p->shm_perm.mode = ipcp->mode;
531551
@@ -196,11 +194,12 @@ fallback:
531551
 			p->next = xcalloc(1, sizeof(struct shm_data));
531551
 			p = p->next;
531551
 			p->next = NULL;
531551
-			i++;
531551
 		} else
531551
-			return 1;
531551
+			break;
531551
 	}
531551
 
531551
+	if (i == 0)
531551
+		free(*shmds);
531551
 	return i;
531551
 }
531551
 
531551
@@ -299,30 +298,22 @@ int ipc_sem_get_info(int id, struct sem_data **semds)
531551
 	return i;
531551
 
531551
 	/* Fallback; /proc or /sys file(s) missing. */
531551
- sem_fallback:
531551
-	i = id < 0 ? 0 : id;
531551
-
531551
+sem_fallback:
531551
 	arg.array = (ushort *) (void *)&dummy;
531551
 	maxid = semctl(0, 0, SEM_INFO, arg);
531551
-	if (maxid < 0)
531551
-		return 0;
531551
 
531551
-	while (i <= maxid) {
531551
+	for (int j = 0; j <= maxid; j++) {
531551
 		int semid;
531551
 		struct semid_ds semseg;
531551
 		struct ipc_perm *ipcp = &semseg.sem_perm;
531551
 		arg.buf = (struct semid_ds *)&semseg;
531551
 
531551
-		semid = semctl(i, 0, SEM_STAT, arg);
531551
-		if (semid < 0) {
531551
-			if (-1 < id) {
531551
-				free(*semds);
531551
-				return 0;
531551
-			}
531551
-			i++;
531551
+		semid = semctl(j, 0, SEM_STAT, arg);
531551
+		if (semid < 0 || (id > -1 && semid != id)) {
531551
 			continue;
531551
 		}
531551
 
531551
+		i++;
531551
 		p->sem_perm.key = ipcp->KEY;
531551
 		p->sem_perm.id = semid;
531551
 		p->sem_perm.mode = ipcp->mode;
531551
@@ -341,10 +332,12 @@ int ipc_sem_get_info(int id, struct sem_data **semds)
531551
 			i++;
531551
 		} else {
531551
 			get_sem_elements(p);
531551
-			return 1;
531551
+			break;
531551
 		}
531551
 	}
531551
 
531551
+	if (i == 0)
531551
+		free(*semds);
531551
 	return i;
531551
 }
531551
 
531551
@@ -398,10 +391,6 @@ int ipc_msg_get_info(int id, struct msg_data **msgds)
531551
 		if (id > -1) {
531551
 			/* ID specified */
531551
 			if (id == p->msg_perm.id) {
531551
-				/*
531551
-				 * FIXME: q_qbytes are not in /proc
531551
-				 *
531551
-				 */
531551
 				if (msgctl(id, IPC_STAT, &msgseg) != -1)
531551
 					p->q_qbytes = msgseg.msg_qbytes;
531551
 				i = 1;
531551
@@ -422,27 +411,19 @@ int ipc_msg_get_info(int id, struct msg_data **msgds)
531551
 	return i;
531551
 
531551
 	/* Fallback; /proc or /sys file(s) missing. */
531551
- msg_fallback:
531551
-	i = id < 0 ? 0 : id;
531551
-
531551
-	maxid = msgctl(id, MSG_STAT, &dummy);
531551
-	if (maxid < 0)
531551
-		return 0;
531551
+msg_fallback:
531551
+	maxid = msgctl(0, MSG_INFO, &dummy);
531551
 
531551
-	while (i <= maxid) {
531551
+	for (int j = 0; j <= maxid; j++) {
531551
 		int msgid;
531551
 		struct ipc_perm *ipcp = &msgseg.msg_perm;
531551
 
531551
-		msgid = msgctl(i, MSG_STAT, &msgseg);
531551
-		if (msgid < 0) {
531551
-			if (-1 < id) {
531551
-				free(*msgds);
531551
-				return 0;
531551
-			}
531551
-			i++;
531551
+		msgid = msgctl(j, MSG_STAT, &msgseg);
531551
+		if (msgid < 0 || (id > -1 && msgid != id)) {
531551
 			continue;
531551
 		}
531551
 
531551
+		i++;
531551
 		p->msg_perm.key = ipcp->KEY;
531551
 		p->msg_perm.id = msgid;
531551
 		p->msg_perm.mode = ipcp->mode;
531551
@@ -463,11 +444,12 @@ int ipc_msg_get_info(int id, struct msg_data **msgds)
531551
 			p->next = xcalloc(1, sizeof(struct msg_data));
531551
 			p = p->next;
531551
 			p->next = NULL;
531551
-			i++;
531551
 		} else
531551
-			return 1;
531551
+			break;
531551
 	}
531551
 
531551
+	if (i == 0)
531551
+		free(*msgds);
531551
 	return i;
531551
 }
531551
 
531551
@@ -508,10 +490,10 @@ void ipc_print_perms(FILE *f, struct ipc_stat *is)
531551
 		fprintf(f, " %-10u\n", is->gid);
531551
 }
531551
 
531551
-void ipc_print_size(int unit, char *msg, size_t size, const char *end,
531551
+void ipc_print_size(int unit, char *msg, uint64_t size, const char *end,
531551
 		    int width)
531551
 {
531551
-	char format[16];
531551
+	char format[32];
531551
 
531551
 	if (!msg)
531551
 		/* NULL */ ;
531551
@@ -527,11 +509,11 @@ void ipc_print_size(int unit, char *msg, size_t size, const char *end,
531551
 	switch (unit) {
531551
 	case IPC_UNIT_DEFAULT:
531551
 	case IPC_UNIT_BYTES:
531551
-		sprintf(format, "%%%dzu", width);
531551
+		sprintf(format, "%%%dju", width);
531551
 		printf(format, size);
531551
 		break;
531551
 	case IPC_UNIT_KB:
531551
-		sprintf(format, "%%%dzu", width);
531551
+		sprintf(format, "%%%dju", width);
531551
 		printf(format, size / 1024);
531551
 		break;
531551
 	case IPC_UNIT_HUMAN:
531551
diff --git a/sys-utils/ipcutils.h b/sys-utils/ipcutils.h
531551
index d2e5972..444065a 100644
531551
--- a/sys-utils/ipcutils.h
531551
+++ b/sys-utils/ipcutils.h
531551
@@ -12,6 +12,7 @@
531551
 #include <unistd.h>
531551
 #include <grp.h>
531551
 #include <pwd.h>
531551
+#include <stdint.h>
531551
 
531551
 /*
531551
  * SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside
531551
@@ -34,11 +35,11 @@
531551
 # define SHM_INFO	14
531551
 struct shm_info {
531551
 	int used_ids;
531551
-	ulong shm_tot;		/* total allocated shm */
531551
-	ulong shm_rss;		/* total resident shm */
531551
-	ulong shm_swp;		/* total swapped shm */
531551
-	ulong swap_attempts;
531551
-	ulong swap_successes;
531551
+	unsigned long shm_tot;		/* total allocated shm */
531551
+	unsigned long shm_rss;		/* total resident shm */
531551
+	unsigned long shm_swp;		/* total swapped shm */
531551
+	unsigned long swap_attempts;
531551
+	unsigned long swap_successes;
531551
 };
531551
 #endif
531551
 
531551
@@ -118,7 +119,7 @@ struct ipc_stat {
531551
 };
531551
 
531551
 extern void ipc_print_perms(FILE *f, struct ipc_stat *is);
531551
-extern void ipc_print_size(int unit, char *msg, size_t size, const char *end, int width);
531551
+extern void ipc_print_size(int unit, char *msg, uint64_t size, const char *end, int width);
531551
 
531551
 /* See 'struct shmid_kernel' in kernel sources
531551
  */
531551
diff --git a/sys-utils/lsipc.1 b/sys-utils/lsipc.1
531551
new file mode 100644
531551
index 0000000..98449cb
531551
--- /dev/null
531551
+++ b/sys-utils/lsipc.1
531551
@@ -0,0 +1,133 @@
531551
+.\" Copyright 2015 Ondrej Oprala(ooprala@redhat.com)
531551
+.\" May be distributed under the GNU General Public License
531551
+.TH LSIPC "1" "November 2015" "util-linux" "User Commands"
531551
+.SH NAME
531551
+lsipc \- show information on IPC facilities currently employed in the system
531551
+.SH SYNOPSIS
531551
+.B lsipc
531551
+[options]
531551
+.SH DESCRIPTION
531551
+.B lsipc
531551
+shows information on the inter-process communication facilities
531551
+for which the calling process has read access.
531551
+.SH OPTIONS
531551
+.TP
531551
+\fB\-i\fR, \fB\-\-id\fR \fIid\fR
531551
+Show full details on just the one resource element identified by
531551
+.IR id .
531551
+This option needs to be combined with one of the three resource options:
531551
+.BR \-m ,
531551
+.BR \-q " or"
531551
+.BR \-s .
531551
+It is possible to override the default output format for this option with the
531551
+\fB\-\-list\fR, \fB\-\-raw\fR, \fB\-\-json\fR or \fB\-\-export\fR option.
531551
+.TP
531551
+\fB\-g\fR, \fB\-\-global\fR
531551
+Show system-wide usage and limits of IPC resources.
531551
+This option may be combined with one of the three resource options:
531551
+.BR \-m ,
531551
+.BR \-q " or"
531551
+.BR \-s .
531551
+The default is to show information about all resources.
531551
+.TP
531551
+\fB\-h\fR, \fB\-\-help\fR
531551
+Display help text and exit.
531551
+.TP
531551
+\fB\-V\fR, \fB\-\-version\fR
531551
+Display version information and exit.
531551
+.SS "Resource options"
531551
+.TP
531551
+\fB\-m\fR, \fB\-\-shmems\fR
531551
+Write information about active shared memory segments.
531551
+.TP
531551
+\fB\-q\fR, \fB\-\-queues\fR
531551
+Write information about active message queues.
531551
+.TP
531551
+\fB\-s\fR, \fB\-\-semaphores\fR
531551
+Write information about active semaphore sets.
531551
+.SS "Output formatting"
531551
+.TP
531551
+\fB\-c\fR, \fB\-\-creator\fR
531551
+Show creator and owner.
531551
+.TP
531551
+\fB\-e\fR, \fB\-\-export\fR
531551
+Output data in the format of NAME=VALUE.
531551
+.TP
531551
+\fB\-l\fR, \fB\-\-list\fR
531551
+Use the list output format.  This is the default, except when \fB\-\-id\fR
531551
+is used.
531551
+.TP
531551
+\fB\-n\fR, \fB\-\-newline\fR
531551
+Display each piece of information on a separate line.
531551
+.TP
531551
+\fB\-\-noheadings\fR
531551
+Do not print a header line.
531551
+.TP
531551
+\fB\-\-notruncate\fR
531551
+Don't truncate output.
531551
+.TP
531551
+\fB\-o\fR, \fB\-\-output \fIlist\fP
531551
+Specify which output columns to print.  Use
531551
+.B \-\-help
531551
+to get a list of all supported columns.
531551
+.TP
531551
+\fB\-p\fR, \fB\-\-pid\fR
531551
+Show PIDs of creator and last operator.
531551
+.TP
531551
+\fB\-r\fR, \fB\-\-raw\fR
531551
+Raw output (no columnation).
531551
+.TP
531551
+\fB\-t\fR, \fB\-\-time\fR
531551
+Write time information.  The time of the last control operation that changed
531551
+the access permissions for all facilities, the time of the last
531551
+.I msgsnd()
531551
+and
531551
+.I msgrcv()
531551
+operations on message queues, the time of the last
531551
+.I shmat()
531551
+and
531551
+.I shmdt()
531551
+operations on shared memory, and the time of the last
531551
+.I semop()
531551
+operation on semaphores.
531551
+.TP
531551
+\fB\-\-time\-format\fR \fItype\fP
531551
+Display dates in short, full or iso format.  The default is short, this time
531551
+format is designed to be space efficient and human readable.
531551
+
531551
+.SH EXIT STATUS
531551
+.TP
531551
+0
531551
+if OK,
531551
+.TP
531551
+1
531551
+if incorrect arguments specified,
531551
+.TP
531551
+2
531551
+if a serious error occurs.
531551
+.SH SEE ALSO
531551
+.BR ipcrm (1),
531551
+.BR ipcmk (1),
531551
+.BR msgrcv (2),
531551
+.BR msgsnd (2),
531551
+.BR semget (2),
531551
+.BR semop (2),
531551
+.BR shmat (2),
531551
+.BR shmdt (2),
531551
+.BR shmget (2)
531551
+.SH HISTORY
531551
+The \fBlsipc\fP utility is inspired by the \fBipcs\fP utility.
531551
+.SH AUTHORS
531551
+.MT ooprala@redhat.com
531551
+Ondrej Oprala
531551
+.ME
531551
+.br
531551
+.MT kzak@redhat.com
531551
+Karel Zak
531551
+.ME
531551
+
531551
+.SH AVAILABILITY
531551
+The lsipc command is part of the util-linux package and is available from
531551
+.UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
531551
+Linux Kernel Archive
531551
+.UE .
531551
diff --git a/sys-utils/lsipc.c b/sys-utils/lsipc.c
531551
new file mode 100644
531551
index 0000000..0be9d91
531551
--- /dev/null
531551
+++ b/sys-utils/lsipc.c
531551
@@ -0,0 +1,1316 @@
531551
+/*
531551
+ * lsipc - List information about IPC instances employed in the system
531551
+ *
531551
+ * Copyright (C) 2015 Ondrej Oprala <ooprala@redhat.com>
531551
+ * Copyright (C) 2015 Karel Zak <ooprala@redhat.com>
531551
+ *
531551
+ * This program is free software; you can redistribute it and/or modify
531551
+ * it under the terms of the GNU General Public License as published by
531551
+ * the Free Software Foundation; either version 2 of the License, or
531551
+ * (at your option) any later version.
531551
+ *
531551
+ * This program is distributed in the hope that it would be useful,
531551
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
531551
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
531551
+ * GNU General Public License for more details.
531551
+ *
531551
+ * You should have received a copy of the GNU General Public License along
531551
+ * with this program; if not, write to the Free Software Foundation, Inc.,
531551
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
531551
+ *
531551
+ *
531551
+ * lsipc is inspired by the ipcs utility. The aim is to create
531551
+ * a utility unencumbered by a standard to provide more flexible
531551
+ * means of controlling the output.
531551
+ */
531551
+
531551
+#include <errno.h>
531551
+#include <getopt.h>
531551
+#include <sys/time.h>
531551
+#include <unistd.h>
531551
+
531551
+#include <libsmartcols.h>
531551
+
531551
+#include "c.h"
531551
+#include "nls.h"
531551
+#include "closestream.h"
531551
+#include "strutils.h"
531551
+#include "optutils.h"
531551
+#include "xalloc.h"
531551
+#include "procutils.h"
531551
+#include "ipcutils.h"
531551
+
531551
+/*
531551
+ * time modes
531551
+ * */
531551
+enum {
531551
+	TIME_INVALID = 0,
531551
+	TIME_SHORT,
531551
+	TIME_FULL,
531551
+	TIME_ISO
531551
+};
531551
+
531551
+/*
531551
+ * IDs
531551
+ */
531551
+enum {
531551
+	/* generic */
531551
+	COLDESC_IDX_GEN_FIRST = 0,
531551
+		COL_KEY = COLDESC_IDX_GEN_FIRST,
531551
+		COL_ID,
531551
+		COL_OWNER,
531551
+		COL_PERMS,
531551
+		COL_CUID,
531551
+		COL_CUSER,
531551
+		COL_CGID,
531551
+		COL_CGROUP,
531551
+		COL_UID,
531551
+		COL_USER,
531551
+		COL_GID,
531551
+		COL_GROUP,
531551
+		COL_CTIME,
531551
+	COLDESC_IDX_GEN_LAST = COL_CTIME,
531551
+
531551
+	/* msgq-specific */
531551
+	COLDESC_IDX_MSG_FIRST,
531551
+		COL_USEDBYTES = COLDESC_IDX_MSG_FIRST,
531551
+		COL_MSGS,
531551
+		COL_SEND,
531551
+		COL_RECV,
531551
+		COL_LSPID,
531551
+		COL_LRPID,
531551
+	COLDESC_IDX_MSG_LAST = COL_LRPID,
531551
+
531551
+	/* shm-specific */
531551
+	COLDESC_IDX_SHM_FIRST,
531551
+		COL_SIZE = COLDESC_IDX_SHM_FIRST,
531551
+		COL_NATTCH,
531551
+		COL_STATUS,
531551
+		COL_ATTACH,
531551
+		COL_DETACH,
531551
+		COL_COMMAND,
531551
+		COL_CPID,
531551
+		COL_LPID,
531551
+	COLDESC_IDX_SHM_LAST = COL_LPID,
531551
+
531551
+	/* sem-specific */
531551
+	COLDESC_IDX_SEM_FIRST,
531551
+		COL_NSEMS = COLDESC_IDX_SEM_FIRST,
531551
+		COL_OTIME,
531551
+	COLDESC_IDX_SEM_LAST = COL_OTIME,
531551
+
531551
+	/* summary (--global) */
531551
+	COLDESC_IDX_SUM_FIRST,
531551
+		COL_RESOURCE = COLDESC_IDX_SUM_FIRST,
531551
+		COL_DESC,
531551
+		COL_LIMIT,
531551
+		COL_USED,
531551
+		COL_USEPERC,
531551
+	COLDESC_IDX_SUM_LAST = COL_USEPERC
531551
+};
531551
+
531551
+/* not all columns apply to all options, so we specify a legal range for each */
531551
+static size_t LOWER, UPPER;
531551
+
531551
+/*
531551
+ * output modes
531551
+ */
531551
+enum {
531551
+	OUT_EXPORT = 1,
531551
+	OUT_NEWLINE,
531551
+	OUT_RAW,
531551
+	OUT_PRETTY,
531551
+	OUT_LIST
531551
+};
531551
+
531551
+struct lsipc_control {
531551
+	int outmode;
531551
+	unsigned int noheadings : 1,		/* don't print header line */
531551
+		     notrunc : 1,		/* don't truncate columns */
531551
+		     bytes : 1,			/* SIZE in bytes */
531551
+		     numperms : 1,		/* numeric permissions */
531551
+		     time_mode : 2;
531551
+};
531551
+
531551
+struct lsipc_coldesc {
531551
+	const char *name;
531551
+	const char *help;
531551
+	const char *pretty_name;
531551
+
531551
+	double whint;	/* width hint */
531551
+	long flag;
531551
+};
531551
+
531551
+static const struct lsipc_coldesc coldescs[] =
531551
+{
531551
+	/* common */
531551
+	[COL_KEY]	= { "KEY",	N_("Resource key"), N_("Key"), 1},
531551
+	[COL_ID]	= { "ID",	N_("Resource ID"), N_("ID"), 1},
531551
+	[COL_OWNER]	= { "OWNER",	N_("Owner's username or UID"), N_("Owner"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_PERMS]	= { "PERMS",	N_("Permissions"), N_("Permissions"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_CUID]	= { "CUID",	N_("Creator UID"), N_("Creator UID"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_CUSER]     = { "CUSER",    N_("Creator user"), N_("Creator user"), 1 },
531551
+	[COL_CGID]	= { "CGID",	N_("Creator GID"), N_("Creator GID"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_CGROUP]    = { "CGROUP",   N_("Creator group"), N_("Creator group"), 1 },
531551
+	[COL_UID]	= { "UID",	N_("User ID"), N_("UID"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_USER]	= { "USER",	N_("User name"), N_("User name"), 1},
531551
+	[COL_GID]	= { "GID",	N_("Group ID"), N_("GID"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_GROUP]	= { "GROUP",	N_("Group name"), N_("Group name"), 1},
531551
+	[COL_CTIME]	= { "CTIME",	N_("Time of the last change"), N_("Last change"), 1, SCOLS_FL_RIGHT},
531551
+
531551
+	/* msgq-specific */
531551
+	[COL_USEDBYTES]	= { "USEDBYTES",N_("Bytes used"), N_("Bytes used"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_MSGS]	= { "MSGS",	N_("Number of messages"), N_("Messages"), 1},
531551
+	[COL_SEND]	= { "SEND",	N_("Time of last msg sent"), N_("Msg sent"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_RECV]	= { "RECV",	N_("Time of last msg received"), N_("Msg received"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_LSPID]	= { "LSPID",	N_("PID of the last msg sender"), N_("Msg sender"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_LRPID]	= { "LRPID",	N_("PID of the last msg receiver"), N_("Msg receiver"), 1, SCOLS_FL_RIGHT},
531551
+
531551
+	/* shm-specific */
531551
+	[COL_SIZE]	= { "SIZE",	N_("Segment size"), N_("Segment size"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_NATTCH]	= { "NATTCH",	N_("Number of attached processes"), N_("Attached processes"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_STATUS]	= { "STATUS",	N_("Status"), N_("Status"), 1, SCOLS_FL_NOEXTREMES},
531551
+	[COL_ATTACH]	= { "ATTACH",	N_("Attach time"), N_("Attach time"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_DETACH]	= { "DETACH",	N_("Detach time"), N_("Detach time"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_COMMAND]	= { "COMMAND",  N_("Creator command line"), N_("Creator command"), 0, SCOLS_FL_TRUNC},
531551
+	[COL_CPID]	= { "CPID",	N_("PID of the creator"), N_("Creator PID"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_LPID]	= { "LPID",	N_("PID of last user"), N_("Last user PID"), 1, SCOLS_FL_RIGHT},
531551
+
531551
+	/* sem-specific */
531551
+	[COL_NSEMS]	= { "NSEMS",	N_("Number of semaphores"), N_("Semaphores"), 1, SCOLS_FL_RIGHT},
531551
+	[COL_OTIME]	= { "OTIME",	N_("Time of the last operation"), N_("Last operation"), 1, SCOLS_FL_RIGHT},
531551
+
531551
+	/* cols for summarized information */
531551
+	[COL_RESOURCE]  = { "RESOURCE", N_("Resource name"), N_("Resource"), 1 },
531551
+	[COL_DESC]      = { "DESCRIPTION",N_("Resource description"), N_("Description"), 1 },
531551
+	[COL_USED]      = { "USED",     N_("Currently used"), N_("Used"), 1, SCOLS_FL_RIGHT },
531551
+	[COL_USEPERC]	= { "USE%",     N_("Currently use percentage"), N_("Use"), 1, SCOLS_FL_RIGHT },
531551
+	[COL_LIMIT]     = { "LIMIT",    N_("System-wide limit"), N_("Limit"), 1, SCOLS_FL_RIGHT },
531551
+};
531551
+
531551
+
531551
+/* columns[] array specifies all currently wanted output column. The columns
531551
+ * are defined by coldescs[] array and you can specify (on command line) each
531551
+ * column twice. That's enough, dynamically allocated array of the columns is
531551
+ * unnecessary overkill and over-engineering in this case */
531551
+static int columns[ARRAY_SIZE(coldescs) * 2];
531551
+static size_t ncolumns;
531551
+
531551
+static inline size_t err_columns_index(size_t arysz, size_t idx)
531551
+{
531551
+	if (idx >= arysz)
531551
+		errx(EXIT_FAILURE, _("too many columns specified, "
531551
+				     "the limit is %zu columns"),
531551
+				arysz - 1);
531551
+	return idx;
531551
+}
531551
+
531551
+#define add_column(ary, n, id)	\
531551
+		((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
531551
+
531551
+static int column_name_to_id(const char *name, size_t namesz)
531551
+{
531551
+	size_t i;
531551
+
531551
+	for (i = 0; i < ARRAY_SIZE(coldescs); i++) {
531551
+		const char *cn = coldescs[i].name;
531551
+
531551
+		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) {
531551
+			if (i > COL_CTIME) {
531551
+				if (i >= LOWER && i <= UPPER)
531551
+					return i;
531551
+				else {
531551
+					warnx(_("column %s does not apply to the specified IPC"), name);
531551
+					return -1;
531551
+				}
531551
+			} else
531551
+				return i;
531551
+		}
531551
+	}
531551
+	warnx(_("unknown column: %s"), name);
531551
+	return -1;
531551
+}
531551
+
531551
+static char *get_username(struct passwd **pw, uid_t id)
531551
+{
531551
+	if (!*pw || (*pw)->pw_uid != id)
531551
+		*pw = getpwuid(id);
531551
+
531551
+	return *pw ? xstrdup((*pw)->pw_name) : NULL;
531551
+}
531551
+
531551
+static char *get_groupname(struct group **gr, gid_t id)
531551
+{
531551
+	if (!*gr || (*gr)->gr_gid != id)
531551
+		*gr = getgrgid(id);
531551
+
531551
+	return *gr ? xstrdup((*gr)->gr_name) : NULL;
531551
+}
531551
+
531551
+static int parse_time_mode(const char *optarg)
531551
+{
531551
+	struct lsipc_timefmt {
531551
+		const char *name;
531551
+		const int val;
531551
+	};
531551
+	static const struct lsipc_timefmt timefmts[] = {
531551
+		{"iso", TIME_ISO},
531551
+		{"full", TIME_FULL},
531551
+		{"short", TIME_SHORT},
531551
+	};
531551
+	size_t i;
531551
+
531551
+	for (i = 0; i < ARRAY_SIZE(timefmts); i++) {
531551
+		if (strcmp(timefmts[i].name, optarg) == 0)
531551
+			return timefmts[i].val;
531551
+	}
531551
+	errx(EXIT_FAILURE, _("unknown time format: %s"), optarg);
531551
+}
531551
+
531551
+static void __attribute__ ((__noreturn__)) usage(FILE * out)
531551
+{
531551
+	size_t i;
531551
+
531551
+	fputs(USAGE_HEADER, out);
531551
+	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
531551
+
531551
+	fputs(USAGE_SEPARATOR, out);
531551
+	fputs(_("Show information on IPC facilities.\n"), out);
531551
+
531551
+	fputs(USAGE_SEPARATOR, out);
531551
+	fputs(_("Resource options:\n"), out);
531551
+	fputs(_(" -m, --shmems      shared memory segments\n"), out);
531551
+	fputs(_(" -q, --queues      message queues\n"), out);
531551
+	fputs(_(" -s, --semaphores  semaphores\n"), out);
531551
+	fputs(_(" -g, --global      info about system-wide usage (may be used with -m, -q and -s)\n"), out);
531551
+	fputs(_(" -i, --id <id>     print details on resource identified by <id>\n"), out);
531551
+
531551
+	fputs(USAGE_OPTIONS, out);
531551
+	fputs(_("     --noheadings         don't print headings\n"), out);
531551
+	fputs(_("     --notruncate         don't truncate output\n"), out);
531551
+	fputs(_("     --time-format=<type> display dates in short, full or iso format\n"), out);
531551
+	fputs(_(" -b, --bytes              print SIZE in bytes rather than in human readable format\n"), out);
531551
+	fputs(_(" -c, --creator            show creator and owner\n"), out);
531551
+	fputs(_(" -e, --export             display in an export-able output format\n"), out);
531551
+	fputs(_(" -n, --newline            display each piece of information on a new line\n"), out);
531551
+	fputs(_(" -l, --list               force list output format (for example with --id)\n"), out);
531551
+	fputs(_(" -o, --output[=<list>]    define the columns to output\n"), out);
531551
+	fputs(_(" -P, --numeric-perms      print numeric permissions (PERMS column)\n"), out);
531551
+	fputs(_(" -r, --raw                display in raw mode\n"), out);
531551
+	fputs(_(" -t, --time               show attach, detach and change times\n"), out);
531551
+
531551
+	fputs(USAGE_SEPARATOR, out);
531551
+	fputs(USAGE_HELP, out);
531551
+	fputs(USAGE_VERSION, out);
531551
+
531551
+	fprintf(out, _("\nGeneric columns:\n"));
531551
+	for (i = COLDESC_IDX_GEN_FIRST; i <= COLDESC_IDX_GEN_LAST; i++)
531551
+		fprintf(out, " %14s  %s\n", coldescs[i].name, _(coldescs[i].help));
531551
+
531551
+	fprintf(out, _("\nShared-memory columns (--shmems):\n"));
531551
+	for (i = COLDESC_IDX_SHM_FIRST; i <= COLDESC_IDX_SHM_LAST; i++)
531551
+		fprintf(out, " %14s  %s\n", coldescs[i].name, _(coldescs[i].help));
531551
+
531551
+	fprintf(out, _("\nMessage-queue columns (--queues):\n"));
531551
+	for (i = COLDESC_IDX_MSG_FIRST; i <= COLDESC_IDX_MSG_LAST; i++)
531551
+		fprintf(out, " %14s  %s\n", coldescs[i].name, _(coldescs[i].help));
531551
+
531551
+	fprintf(out, _("\nSemaphore columns (--semaphores):\n"));
531551
+	for (i = COLDESC_IDX_SEM_FIRST; i <= COLDESC_IDX_SEM_LAST; i++)
531551
+		fprintf(out, " %14s  %s\n", coldescs[i].name, _(coldescs[i].help));
531551
+
531551
+	fprintf(out, _("\nSummary columns (--global):\n"));
531551
+	for (i = COLDESC_IDX_SUM_FIRST; i <= COLDESC_IDX_SUM_LAST; i++)
531551
+		fprintf(out, " %14s  %s\n", coldescs[i].name, _(coldescs[i].help));
531551
+
531551
+	fprintf(out, USAGE_MAN_TAIL("lsipc(1)"));
531551
+	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
531551
+}
531551
+
531551
+static struct libscols_table *new_table(struct lsipc_control *ctl)
531551
+{
531551
+	struct libscols_table *table = scols_new_table();
531551
+
531551
+	if (!table)
531551
+		errx(EXIT_FAILURE, _("failed to initialize output table"));
531551
+	if (ctl->noheadings)
531551
+		scols_table_enable_noheadings(table, 1);
531551
+
531551
+	switch(ctl->outmode) {
531551
+	case OUT_NEWLINE:
531551
+		scols_table_set_column_separator(table, "\n");
531551
+		/* fallthrough */
531551
+	case OUT_EXPORT:
531551
+		scols_table_enable_export(table, 1);
531551
+		break;
531551
+	case OUT_RAW:
531551
+		scols_table_enable_raw(table, 1);
531551
+		break;
531551
+	case OUT_PRETTY:
531551
+		scols_table_enable_noheadings(table, 1);
531551
+		break;
531551
+	default:
531551
+		break;
531551
+	}
531551
+	return table;
531551
+}
531551
+
531551
+static struct libscols_table *setup_table(struct lsipc_control *ctl)
531551
+{
531551
+	struct libscols_table *table = new_table(ctl);
531551
+	size_t n;
531551
+
531551
+	for (n = 0; n < ncolumns; n++) {
531551
+		int flags = coldescs[columns[n]].flag;
531551
+
531551
+		if (ctl->notrunc)
531551
+			flags &= ~SCOLS_FL_TRUNC;
531551
+
531551
+		if (!scols_table_new_column(table,
531551
+				coldescs[columns[n]].name,
531551
+				coldescs[columns[n]].whint,
531551
+				flags))
531551
+			goto fail;
531551
+	}
531551
+	return table;
531551
+fail:
531551
+	scols_unref_table(table);
531551
+	return NULL;
531551
+}
531551
+
531551
+static int print_pretty(struct libscols_table *table)
531551
+{
531551
+	struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
531551
+	struct libscols_column *col;
531551
+	struct libscols_cell *data;
531551
+	struct libscols_line *ln;
531551
+	const char *hstr, *dstr;
531551
+	int n = 0;
531551
+
531551
+	ln = scols_table_get_line(table, 0);
531551
+	while (!scols_table_next_column(table, itr, &col)) {
531551
+
531551
+		data = scols_line_get_cell(ln, n);
531551
+
531551
+		hstr = N_(coldescs[columns[n]].pretty_name);
531551
+		dstr = scols_cell_get_data(data);
531551
+
531551
+		if (dstr)
531551
+			printf("%s:%*c%-36s\n", hstr, 35 - (int)strlen(hstr), ' ', dstr);
531551
+		++n;
531551
+	}
531551
+
531551
+	/* this is used to pretty-print detailed info about a semaphore array */
531551
+	if (ln) {
531551
+		struct libscols_table *subtab = scols_line_get_userdata(ln);
531551
+		if (subtab) {
531551
+			printf(_("Elements:\n\n"));
531551
+			scols_print_table(subtab);
531551
+		}
531551
+	}
531551
+
531551
+	scols_free_iter(itr);
531551
+	return 0;
531551
+
531551
+}
531551
+
531551
+static int print_table(struct lsipc_control *ctl, struct libscols_table *tb)
531551
+{
531551
+	if (ctl->outmode == OUT_PRETTY)
531551
+		print_pretty(tb);
531551
+	else
531551
+		scols_print_table(tb);
531551
+	return 0;
531551
+}
531551
+static struct timeval now;
531551
+
531551
+static int date_is_today(time_t t)
531551
+{
531551
+	if (now.tv_sec == 0)
531551
+		gettimeofday(&now, NULL);
531551
+	return t / (3600 * 24) == now.tv_sec / (3600 * 24);
531551
+}
531551
+
531551
+static int date_is_thisyear(time_t t)
531551
+{
531551
+	if (now.tv_sec == 0)
531551
+		gettimeofday(&now, NULL);
531551
+	return t / (3600 * 24 * 365) == now.tv_sec / (3600 * 24 * 365);
531551
+}
531551
+
531551
+static char *make_time(int mode, time_t time)
531551
+{
531551
+	char *s;
531551
+	struct tm tm;
531551
+	char buf[64] = {0};
531551
+
531551
+	localtime_r(&time, &tm;;
531551
+
531551
+	switch(mode) {
531551
+	case TIME_FULL:
531551
+		asctime_r(&tm, buf);
531551
+		if (*(s = buf + strlen(buf) - 1) == '\n')
531551
+			*s = '\0';
531551
+		break;
531551
+	case TIME_SHORT:
531551
+		if (date_is_today(time))
531551
+			strftime(buf, sizeof(buf), "%H:%M", &tm;;
531551
+		else if (date_is_thisyear(time))
531551
+			strftime(buf, sizeof(buf), "%b%d", &tm;;
531551
+		else
531551
+			strftime(buf, sizeof(buf), "%Y-%b%d", &tm;;
531551
+		break;
531551
+	case TIME_ISO:
531551
+		strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", &tm;;
531551
+		break;
531551
+	default:
531551
+		errx(EXIT_FAILURE, _("unsupported time type"));
531551
+	}
531551
+	return xstrdup(buf);
531551
+}
531551
+
531551
+static void global_set_data(struct libscols_table *tb, const char *resource,
531551
+			    const char *desc, uintmax_t used, uintmax_t limit, int usage)
531551
+{
531551
+	struct libscols_line *ln;
531551
+	size_t n;
531551
+
531551
+	ln = scols_table_new_line(tb, NULL);
531551
+	if (!ln)
531551
+		err_oom();
531551
+
531551
+	for (n = 0; n < ncolumns; n++) {
531551
+		int rc = 0;
531551
+		char *arg = NULL;
531551
+
531551
+		switch (columns[n]) {
531551
+		case COL_RESOURCE:
531551
+			rc = scols_line_set_data(ln, n, resource);
531551
+			break;
531551
+		case COL_DESC:
531551
+			rc = scols_line_set_data(ln, n, desc);
531551
+			break;
531551
+		case COL_USED:
531551
+			if (usage) {
531551
+				xasprintf(&arg, "%ju", used);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+			} else
531551
+				rc = scols_line_set_data(ln, n, "-");
531551
+			break;
531551
+		case COL_USEPERC:
531551
+			if (usage) {
531551
+				xasprintf(&arg, "%2.2f%%", (double) used / limit * 100);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+			} else
531551
+				rc = scols_line_set_data(ln, n, "-");
531551
+			break;
531551
+		case COL_LIMIT:
531551
+			xasprintf(&arg, "%ju", limit);
531551
+			rc = scols_line_refer_data(ln, n, arg);
531551
+			break;
531551
+		}
531551
+
531551
+		if (rc != 0)
531551
+			err(EXIT_FAILURE, _("failed to set data"));
531551
+	}
531551
+}
531551
+
531551
+static void setup_sem_elements_columns(struct libscols_table *tb)
531551
+{
531551
+	if (!scols_table_new_column(tb, "SEMNUM", 0, SCOLS_FL_RIGHT))
531551
+		err_oom();
531551
+	if (!scols_table_new_column(tb, "VALUE", 0, SCOLS_FL_RIGHT))
531551
+		err_oom();
531551
+	if (!scols_table_new_column(tb, "NCOUNT", 0, SCOLS_FL_RIGHT))
531551
+		err_oom();
531551
+	if (!scols_table_new_column(tb, "ZCOUNT", 0, SCOLS_FL_RIGHT))
531551
+		err_oom();
531551
+	if (!scols_table_new_column(tb, "PID", 0, SCOLS_FL_RIGHT))
531551
+		err_oom();
531551
+	if (!scols_table_new_column(tb, "COMMAND", 0, SCOLS_FL_RIGHT))
531551
+		err_oom();
531551
+}
531551
+
531551
+static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
531551
+{
531551
+	struct libscols_line *ln;
531551
+	struct passwd *pw = NULL, *cpw = NULL;
531551
+	struct group *gr = NULL, *cgr = NULL;
531551
+	struct sem_data *semds, *semdsp;
531551
+	char *arg = NULL;
531551
+
531551
+	if (ipc_sem_get_info(id, &semds) < 1) {
531551
+		if (id > -1)
531551
+			warnx(_("id %d not found"), id);
531551
+		return;
531551
+	}
531551
+	for (semdsp = semds;  semdsp->next != NULL || id > -1; semdsp = semdsp->next) {
531551
+		size_t n;
531551
+		ln = scols_table_new_line(tb, NULL);
531551
+
531551
+		for (n = 0; n < ncolumns; n++) {
531551
+			int rc = 0;
531551
+			switch (columns[n]) {
531551
+			case COL_KEY:
531551
+				xasprintf(&arg, "0x%08x",semdsp->sem_perm.key);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_ID:
531551
+				xasprintf(&arg, "%d",semdsp->sem_perm.id);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_OWNER:
531551
+				arg = get_username(&pw, semdsp->sem_perm.uid);
531551
+				if (!arg)
531551
+					xasprintf(&arg, "%u", semdsp->sem_perm.uid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_PERMS:
531551
+				if (ctl->numperms)
531551
+					xasprintf(&arg, "%#o", semdsp->sem_perm.mode & 0777);
531551
+				else {
531551
+					arg = xmalloc(11);
531551
+					strmode(semdsp->sem_perm.mode & 0777, arg);
531551
+				}
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CUID:
531551
+				xasprintf(&arg, "%u", semdsp->sem_perm.cuid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CUSER:
531551
+				arg = get_username(&cpw, semdsp->sem_perm.cuid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CGID:
531551
+				xasprintf(&arg, "%u", semdsp->sem_perm.cgid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CGROUP:
531551
+				arg = get_groupname(&cgr, semdsp->sem_perm.cgid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_UID:
531551
+				xasprintf(&arg, "%u", semdsp->sem_perm.uid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_USER:
531551
+				arg = get_username(&pw, semdsp->sem_perm.uid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_GID:
531551
+				xasprintf(&arg, "%u", semdsp->sem_perm.gid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_GROUP:
531551
+				arg = get_groupname(&gr, semdsp->sem_perm.gid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CTIME:
531551
+				if (semdsp->sem_ctime != 0) {
531551
+					rc = scols_line_refer_data(ln, n,
531551
+							make_time(ctl->time_mode,
531551
+							  (time_t)semdsp->sem_ctime));
531551
+				}
531551
+				break;
531551
+			case COL_NSEMS:
531551
+				xasprintf(&arg, "%ju", semdsp->sem_nsems);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_OTIME:
531551
+				if (semdsp->sem_otime != 0) {
531551
+					rc = scols_line_refer_data(ln, n,
531551
+							make_time(ctl->time_mode,
531551
+							  (time_t)semdsp->sem_otime));
531551
+				}
531551
+				break;
531551
+			}
531551
+			if (rc != 0)
531551
+				err(EXIT_FAILURE, _("failed to set data"));
531551
+			arg = NULL;
531551
+		}
531551
+
531551
+		if (id > -1 && semds->sem_nsems) {
531551
+			/* Create extra table with ID specific semaphore elements */
531551
+			struct libscols_table *sub = new_table(ctl);
531551
+			size_t i;
531551
+			int rc = 0;
531551
+
531551
+			scols_table_enable_noheadings(sub, 0);
531551
+			setup_sem_elements_columns(sub);
531551
+
531551
+			for (i = 0; i < semds->sem_nsems; i++) {
531551
+				struct sem_elem *e = &semds->elements[i];
531551
+				struct libscols_line *sln = scols_table_new_line(sub, NULL);
531551
+
531551
+				/* SEMNUM */
531551
+				xasprintf(&arg, "%zu", i);
531551
+				rc = scols_line_refer_data(sln, 0, arg);
531551
+				if (rc)
531551
+					break;
531551
+
531551
+				/* VALUE */
531551
+				xasprintf(&arg, "%d", e->semval);
531551
+				rc = scols_line_refer_data(sln, 1, arg);
531551
+				if (rc)
531551
+					break;
531551
+
531551
+				/* NCOUNT */
531551
+				xasprintf(&arg, "%d", e->ncount);
531551
+				rc = scols_line_refer_data(sln, 2, arg);
531551
+				if (rc)
531551
+					break;
531551
+
531551
+				/* ZCOUNT */
531551
+				xasprintf(&arg, "%d", e->zcount);
531551
+				rc = scols_line_refer_data(sln, 3, arg);
531551
+				if (rc)
531551
+					break;
531551
+
531551
+				/* PID */
531551
+				xasprintf(&arg, "%d", e->pid);
531551
+				rc = scols_line_refer_data(sln, 4, arg);
531551
+				if (rc)
531551
+					break;
531551
+
531551
+				/* COMMAND */
531551
+				arg = proc_get_command(e->pid);
531551
+				rc = scols_line_refer_data(sln, 5, arg);
531551
+				if (rc)
531551
+					break;
531551
+			}
531551
+
531551
+			if (rc != 0)
531551
+				err(EXIT_FAILURE, _("failed to set data"));
531551
+
531551
+			scols_line_set_userdata(ln, (void *)sub);
531551
+			break;
531551
+		}
531551
+	}
531551
+	ipc_sem_free_info(semds);
531551
+}
531551
+
531551
+static void do_sem_global(struct libscols_table *tb)
531551
+{
531551
+	struct sem_data *semds, *semdsp;
531551
+	struct ipc_limits lim;
531551
+	int nsems = 0, nsets = 0;
531551
+
531551
+	ipc_sem_get_limits(&lim);
531551
+
531551
+	if (ipc_sem_get_info(-1, &semds) > 0) {
531551
+		for (semdsp = semds; semdsp->next != NULL; semdsp = semdsp->next) {
531551
+			++nsets;
531551
+			nsems += semds->sem_nsems;
531551
+		}
531551
+		ipc_sem_free_info(semds);
531551
+	}
531551
+
531551
+	global_set_data(tb, "SEMMNI", _("Number of semaphore identifiers"), nsets, lim.semmni, 1);
531551
+	global_set_data(tb, "SEMMNS", _("Total number of semaphores"), nsems, lim.semmns, 1);
531551
+	global_set_data(tb, "SEMMSL", _("Max semaphores per semaphore set."), 0, lim.semmsl, 0);
531551
+	global_set_data(tb, "SEMOPM", _("Max number of operations per semop(2)"), 0, lim.semopm, 0);
531551
+	global_set_data(tb, "SEMVMX", _("Semaphore max value"), 0, lim.semvmx, 0);
531551
+}
531551
+
531551
+static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
531551
+{
531551
+	struct libscols_line *ln;
531551
+	struct passwd *pw = NULL;
531551
+	struct group *gr = NULL;
531551
+	struct msg_data *msgds, *msgdsp;
531551
+	char *arg = NULL;
531551
+
531551
+	if (ipc_msg_get_info(id, &msgds) < 1) {
531551
+		if (id > -1)
531551
+			warnx(_("id %d not found"), id);
531551
+		return;
531551
+	}
531551
+
531551
+	for (msgdsp = msgds; msgdsp->next != NULL || id > -1 ; msgdsp = msgdsp->next) {
531551
+		size_t n;
531551
+		ln = scols_table_new_line(tb, NULL);
531551
+
531551
+		/* no need to call getpwuid() for the same user */
531551
+		if (!(pw && pw->pw_uid == msgdsp->msg_perm.uid))
531551
+			pw = getpwuid(msgdsp->msg_perm.uid);
531551
+
531551
+		/* no need to call getgrgid() for the same user */
531551
+		if (!(gr && gr->gr_gid == msgdsp->msg_perm.gid))
531551
+			gr = getgrgid(msgdsp->msg_perm.gid);
531551
+
531551
+		for (n = 0; n < ncolumns; n++) {
531551
+			int rc = 0;
531551
+
531551
+			switch (columns[n]) {
531551
+			case COL_KEY:
531551
+				xasprintf(&arg, "0x%08x",msgdsp->msg_perm.key);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_ID:
531551
+				xasprintf(&arg, "%d",msgdsp->msg_perm.id);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_OWNER:
531551
+				arg = get_username(&pw, msgdsp->msg_perm.uid);
531551
+				if (!arg)
531551
+					xasprintf(&arg, "%u", msgdsp->msg_perm.uid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_PERMS:
531551
+				if (ctl->numperms)
531551
+					xasprintf(&arg, "%#o", msgdsp->msg_perm.mode & 0777);
531551
+				else {
531551
+					arg = xmalloc(11);
531551
+					strmode(msgdsp->msg_perm.mode & 0777, arg);
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				}
531551
+				break;
531551
+			case COL_CUID:
531551
+				xasprintf(&arg, "%u", msgdsp->msg_perm.cuid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CUSER:
531551
+				arg = get_username(&pw, msgdsp->msg_perm.cuid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CGID:
531551
+				xasprintf(&arg, "%u", msgdsp->msg_perm.cuid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CGROUP:
531551
+				arg = get_groupname(&gr, msgdsp->msg_perm.cgid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_UID:
531551
+				xasprintf(&arg, "%u", msgdsp->msg_perm.uid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_USER:
531551
+				arg = get_username(&pw, msgdsp->msg_perm.uid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_GID:
531551
+				xasprintf(&arg, "%u", msgdsp->msg_perm.gid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_GROUP:
531551
+				arg = get_groupname(&gr,msgdsp->msg_perm.gid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CTIME:
531551
+				if (msgdsp->q_ctime != 0)
531551
+					rc = scols_line_refer_data(ln, n,
531551
+						make_time(ctl->time_mode,
531551
+							  (time_t)msgdsp->q_ctime));
531551
+				break;
531551
+			case COL_USEDBYTES:
531551
+				xasprintf(&arg, "%ju", msgdsp->q_cbytes);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_MSGS:
531551
+				xasprintf(&arg, "%ju", msgdsp->q_qnum);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_SEND:
531551
+				if (msgdsp->q_stime != 0)
531551
+					rc = scols_line_refer_data(ln, n,
531551
+						make_time(ctl->time_mode,
531551
+							  (time_t)msgdsp->q_stime));
531551
+				break;
531551
+			case COL_RECV:
531551
+				if (msgdsp->q_rtime != 0)
531551
+					rc = scols_line_refer_data(ln, n,
531551
+						make_time(ctl->time_mode,
531551
+							  (time_t)msgdsp->q_rtime));
531551
+				break;
531551
+			case COL_LSPID:
531551
+				xasprintf(&arg, "%u", msgdsp->q_lspid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_LRPID:
531551
+				xasprintf(&arg, "%u", msgdsp->q_lrpid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			}
531551
+			if (rc != 0)
531551
+				err(EXIT_FAILURE, _("failed to set data"));
531551
+			arg = NULL;
531551
+		}
531551
+		if (id > -1)
531551
+			break;
531551
+	}
531551
+	ipc_msg_free_info(msgds);
531551
+}
531551
+
531551
+
531551
+static void do_msg_global(struct libscols_table *tb)
531551
+{
531551
+	struct msg_data *msgds, *msgdsp;
531551
+	struct ipc_limits lim;
531551
+	int msgqs = 0;
531551
+
531551
+	ipc_msg_get_limits(&lim);
531551
+
531551
+	/* count number of used queues */
531551
+	if (ipc_msg_get_info(-1, &msgds) > 0) {
531551
+		for (msgdsp = msgds; msgdsp->next != NULL; msgdsp = msgdsp->next)
531551
+			++msgqs;
531551
+		ipc_msg_free_info(msgds);
531551
+	}
531551
+
531551
+	global_set_data(tb, "MSGMNI", _("Number of message queues"), msgqs, lim.msgmni, 1);
531551
+	global_set_data(tb, "MSGMAX", _("Max size of message (bytes)"),	0, lim.msgmax, 0);
531551
+	global_set_data(tb, "MSGMNB", _("Default max size of queue (bytes)"), 0, lim.msgmnb, 0);
531551
+}
531551
+
531551
+
531551
+static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
531551
+{
531551
+	struct libscols_line *ln;
531551
+	struct passwd *pw = NULL;
531551
+	struct group *gr = NULL;
531551
+	struct shm_data *shmds, *shmdsp;
531551
+	char *arg = NULL;
531551
+
531551
+	if (ipc_shm_get_info(id, &shmds) < 1) {
531551
+		if (id > -1)
531551
+			warnx(_("id %d not found"), id);
531551
+		return;
531551
+	}
531551
+
531551
+	for (shmdsp = shmds; shmdsp->next != NULL || id > -1 ; shmdsp = shmdsp->next) {
531551
+		size_t n;
531551
+		ln = scols_table_new_line(tb, NULL);
531551
+		if (!ln)
531551
+			err_oom();
531551
+
531551
+		for (n = 0; n < ncolumns; n++) {
531551
+			int rc = 0;
531551
+
531551
+			switch (columns[n]) {
531551
+			case COL_KEY:
531551
+				xasprintf(&arg, "0x%08x",shmdsp->shm_perm.key);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_ID:
531551
+				xasprintf(&arg, "%d",shmdsp->shm_perm.id);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_OWNER:
531551
+				arg = get_username(&pw, shmdsp->shm_perm.uid);
531551
+				if (!arg)
531551
+					xasprintf(&arg, "%u", shmdsp->shm_perm.uid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_PERMS:
531551
+				if (ctl->numperms)
531551
+					xasprintf(&arg, "%#o", shmdsp->shm_perm.mode & 0777);
531551
+				else {
531551
+					arg = xmalloc(11);
531551
+					strmode(shmdsp->shm_perm.mode & 0777, arg);
531551
+				}
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CUID:
531551
+				xasprintf(&arg, "%u", shmdsp->shm_perm.cuid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CUSER:
531551
+				arg = get_username(&pw, shmdsp->shm_perm.cuid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CGID:
531551
+				xasprintf(&arg, "%u", shmdsp->shm_perm.cuid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CGROUP:
531551
+				arg = get_groupname(&gr, shmdsp->shm_perm.cgid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_UID:
531551
+				xasprintf(&arg, "%u", shmdsp->shm_perm.uid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_USER:
531551
+				arg = get_username(&pw, shmdsp->shm_perm.uid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_GID:
531551
+				xasprintf(&arg, "%u", shmdsp->shm_perm.gid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_GROUP:
531551
+				arg = get_groupname(&gr, shmdsp->shm_perm.gid);
531551
+				if (arg)
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_CTIME:
531551
+				if (shmdsp->shm_ctim != 0)
531551
+					rc = scols_line_refer_data(ln, n,
531551
+						make_time(ctl->time_mode,
531551
+							  (time_t)shmdsp->shm_ctim));
531551
+				break;
531551
+			case COL_SIZE:
531551
+				if (ctl->bytes)
531551
+					xasprintf(&arg, "%ju", shmdsp->shm_segsz);
531551
+				else
531551
+					arg = size_to_human_string(SIZE_SUFFIX_1LETTER, shmdsp->shm_segsz);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_NATTCH:
531551
+				xasprintf(&arg, "%ju", shmdsp->shm_nattch);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_STATUS: {
531551
+					int comma = 0;
531551
+					size_t offt = 0;
531551
+
531551
+					free(arg);
531551
+					arg = xcalloc(1, sizeof(char) * strlen(_("dest"))
531551
+							+ strlen(_("locked"))
531551
+							+ strlen(_("hugetlb"))
531551
+							+ strlen(_("noreserve")) + 4);
531551
+#ifdef SHM_DEST
531551
+					if (shmdsp->shm_perm.mode & SHM_DEST) {
531551
+						offt += sprintf(arg, "%s", _("dest"));
531551
+						comma++;
531551
+					}
531551
+#endif
531551
+#ifdef SHM_LOCKED
531551
+					if (shmdsp->shm_perm.mode & SHM_LOCKED) {
531551
+						if (comma)
531551
+							arg[offt++] = ',';
531551
+						offt += sprintf(arg + offt, "%s", _("locked"));
531551
+					}
531551
+#endif
531551
+#ifdef SHM_HUGETLB
531551
+					if (shmdsp->shm_perm.mode & SHM_HUGETLB) {
531551
+						if (comma)
531551
+							arg[offt++] = ',';
531551
+						offt += sprintf(arg + offt, "%s", _("hugetlb"));
531551
+					}
531551
+#endif
531551
+#ifdef SHM_NORESERVE
531551
+					if (shmdsp->shm_perm.mode & SHM_NORESERVE) {
531551
+						if (comma)
531551
+							arg[offt++] = ',';
531551
+						sprintf(arg + offt, "%s", _("noreserve"));
531551
+					}
531551
+#endif
531551
+					rc = scols_line_refer_data(ln, n, arg);
531551
+				}
531551
+				break;
531551
+			case COL_ATTACH:
531551
+				if (shmdsp->shm_atim != 0)
531551
+					rc = scols_line_refer_data(ln, n,
531551
+							make_time(ctl->time_mode,
531551
+							  (time_t)shmdsp->shm_atim));
531551
+				break;
531551
+			case COL_DETACH:
531551
+				if (shmdsp->shm_dtim != 0)
531551
+					rc = scols_line_refer_data(ln, n,
531551
+							make_time(ctl->time_mode,
531551
+							  (time_t)shmdsp->shm_dtim));
531551
+				break;
531551
+			case COL_CPID:
531551
+				xasprintf(&arg, "%u", shmdsp->shm_cprid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_LPID:
531551
+				xasprintf(&arg, "%u", shmdsp->shm_lprid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			case COL_COMMAND:
531551
+				arg = proc_get_command(shmdsp->shm_cprid);
531551
+				rc = scols_line_refer_data(ln, n, arg);
531551
+				break;
531551
+			}
531551
+			if (rc != 0)
531551
+				err(EXIT_FAILURE, _("failed to set data"));
531551
+			arg = NULL;
531551
+		}
531551
+		if (id > -1)
531551
+			break;
531551
+	}
531551
+	ipc_shm_free_info(shmds);
531551
+}
531551
+
531551
+static void do_shm_global(struct libscols_table *tb)
531551
+{
531551
+	struct shm_data *shmds, *shmdsp;
531551
+	uint64_t nsegs = 0, sum_segsz = 0;
531551
+	struct ipc_limits lim;
531551
+
531551
+	ipc_shm_get_limits(&lim);
531551
+
531551
+	if (ipc_shm_get_info(-1, &shmds) > 0) {
531551
+		for (shmdsp = shmds; shmdsp->next != NULL; shmdsp = shmdsp->next) {
531551
+			++nsegs;
531551
+			sum_segsz += shmdsp->shm_segsz;
531551
+		}
531551
+		ipc_shm_free_info(shmds);
531551
+	}
531551
+
531551
+	global_set_data(tb, "SHMMNI", _("Shared memory segments"), nsegs, lim.shmmni, 1);
531551
+	global_set_data(tb, "SHMALL", _("Shared memory pages"), sum_segsz / getpagesize(), lim.shmall, 1);
531551
+	global_set_data(tb, "SHMMAX", _("Max size of shared memory segment (bytes)"), 0, lim.shmmax, 0);
531551
+	global_set_data(tb, "SHMMIN", _("Min size of shared memory segment (bytes)"), 0, lim.shmmin, 0);
531551
+}
531551
+
531551
+int main(int argc, char *argv[])
531551
+{
531551
+	int opt, msg = 0, sem = 0, shm = 0, id = -1;
531551
+	int show_time = 0, show_creat = 0, global = 0;
531551
+	size_t i;
531551
+	struct lsipc_control *ctl = xcalloc(1, sizeof(struct lsipc_control));
531551
+	static struct libscols_table *tb;
531551
+	char *outarg = NULL;
531551
+
531551
+	/* long only options. */
531551
+	enum {
531551
+		OPT_NOTRUNC = CHAR_MAX + 1,
531551
+		OPT_NOHEAD,
531551
+		OPT_TIME_FMT
531551
+	};
531551
+
531551
+	static const struct option longopts[] = {
531551
+		{ "bytes",          no_argument,        0, 'b' },
531551
+		{ "creator",        no_argument,	0, 'c' },
531551
+		{ "export",         no_argument,	0, 'e' },
531551
+		{ "global",         no_argument,	0, 'g' },
531551
+		{ "help",           no_argument,	0, 'h' },
531551
+		{ "id",             required_argument,	0, 'i' },
531551
+		{ "list",           no_argument,        0, 'l' },
531551
+		{ "newline",        no_argument,	0, 'n' },
531551
+		{ "noheadings",     no_argument,	0, OPT_NOHEAD },
531551
+		{ "notruncate",     no_argument,	0, OPT_NOTRUNC },
531551
+		{ "numeric-perms",  no_argument,	0, 'P' },
531551
+		{ "output",         required_argument,	0, 'o' },
531551
+		{ "pid",            no_argument,	0, 'p' },
531551
+		{ "queues",         no_argument,	0, 'q' },
531551
+		{ "raw",            no_argument,	0, 'r' },
531551
+		{ "semaphores",     no_argument,	0, 's' },
531551
+		{ "shmems",         no_argument,	0, 'm' },
531551
+		{ "time",           no_argument,	0, 't' },
531551
+		{ "time-format",    required_argument,	0, OPT_TIME_FMT },
531551
+		{ "version",        no_argument,	0, 'V' },
531551
+		{NULL, 0, NULL, 0}
531551
+	};
531551
+
531551
+	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
531551
+		{ 'J', 'e', 'l', 'n', 'r' },
531551
+		{ 'g', 'i' },
531551
+		{ 'c', 'o', 't' },
531551
+		{ 'm', 'q', 's' },
531551
+		{ 0 }
531551
+	};
531551
+	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
531551
+
531551
+	setlocale(LC_ALL, "");
531551
+	bindtextdomain(PACKAGE, LOCALEDIR);
531551
+	textdomain(PACKAGE);
531551
+	atexit(close_stdout);
531551
+
531551
+	ctl->time_mode = 0;
531551
+
531551
+	scols_init_debug(0);
531551
+
531551
+	while ((opt = getopt_long(argc, argv, "bceghi:lmno:PqrstuV", longopts, NULL)) != -1) {
531551
+
531551
+		err_exclusive_options(opt, longopts, excl, excl_st);
531551
+
531551
+		switch (opt) {
531551
+		case 'b':
531551
+			ctl->bytes = 1;
531551
+			break;
531551
+		case 'i':
531551
+			id = strtos32_or_err(optarg, _("failed to parse IPC identifier"));
531551
+			break;
531551
+		case 'e':
531551
+			ctl->outmode = OUT_EXPORT;
531551
+			break;
531551
+		case 'r':
531551
+			ctl->outmode = OUT_RAW;
531551
+			break;
531551
+		case 'o':
531551
+			outarg = optarg;
531551
+			break;
531551
+		case 'g':
531551
+			global = 1;
531551
+			break;
531551
+		case 'q':
531551
+			msg = 1;
531551
+			add_column(columns, ncolumns++, COL_KEY);
531551
+			add_column(columns, ncolumns++, COL_ID);
531551
+			add_column(columns, ncolumns++, COL_PERMS);
531551
+			add_column(columns, ncolumns++, COL_OWNER);
531551
+			add_column(columns, ncolumns++, COL_USEDBYTES);
531551
+			add_column(columns, ncolumns++, COL_MSGS);
531551
+			add_column(columns, ncolumns++, COL_LSPID);
531551
+			add_column(columns, ncolumns++, COL_LRPID);
531551
+			LOWER = COLDESC_IDX_MSG_FIRST;
531551
+			UPPER = COLDESC_IDX_MSG_LAST;
531551
+			break;
531551
+		case 'l':
531551
+			ctl->outmode = OUT_LIST;
531551
+			break;
531551
+		case 'm':
531551
+			shm = 1;
531551
+			add_column(columns, ncolumns++, COL_KEY);
531551
+			add_column(columns, ncolumns++, COL_ID);
531551
+			add_column(columns, ncolumns++, COL_PERMS);
531551
+			add_column(columns, ncolumns++, COL_OWNER);
531551
+			add_column(columns, ncolumns++, COL_SIZE);
531551
+			add_column(columns, ncolumns++, COL_NATTCH);
531551
+			add_column(columns, ncolumns++, COL_STATUS);
531551
+			add_column(columns, ncolumns++, COL_CTIME);
531551
+			add_column(columns, ncolumns++, COL_CPID);
531551
+			add_column(columns, ncolumns++, COL_LPID);
531551
+			add_column(columns, ncolumns++, COL_COMMAND);
531551
+			LOWER = COLDESC_IDX_SHM_FIRST;
531551
+			UPPER = COLDESC_IDX_SHM_LAST;
531551
+			break;
531551
+		case 'n':
531551
+			ctl->outmode = OUT_NEWLINE;
531551
+			break;
531551
+		case 'P':
531551
+			ctl->numperms = 1;
531551
+			break;
531551
+		case 's':
531551
+			sem = 1;
531551
+			add_column(columns, ncolumns++, COL_KEY);
531551
+			add_column(columns, ncolumns++, COL_ID);
531551
+			add_column(columns, ncolumns++, COL_PERMS);
531551
+			add_column(columns, ncolumns++, COL_OWNER);
531551
+			add_column(columns, ncolumns++, COL_NSEMS);
531551
+			LOWER = COLDESC_IDX_SEM_FIRST;
531551
+			UPPER = COLDESC_IDX_SEM_LAST;
531551
+			break;
531551
+		case OPT_NOTRUNC:
531551
+			ctl->notrunc = 1;
531551
+			break;
531551
+		case OPT_NOHEAD:
531551
+			ctl->noheadings = 1;
531551
+			break;
531551
+		case OPT_TIME_FMT:
531551
+			ctl->time_mode = parse_time_mode(optarg);
531551
+			break;
531551
+		case 't':
531551
+			show_time = 1;
531551
+			break;
531551
+		case 'c':
531551
+			show_creat = 1;
531551
+			break;
531551
+		case 'h':
531551
+			usage(stdout);
531551
+		case 'V':
531551
+			printf(UTIL_LINUX_VERSION);
531551
+			return EXIT_SUCCESS;
531551
+		default:
531551
+			usage(stderr);
531551
+		}
531551
+	}
531551
+
531551
+	/* default is global */
531551
+	if (msg + shm + sem == 0) {
531551
+		msg = shm = sem = global = 1;
531551
+		if (show_time || show_creat || id != -1)
531551
+			errx(EXIT_FAILURE, _("--global is mutually exclusive with --creator, --id and --time"));
531551
+	}
531551
+	if (global) {
531551
+		add_column(columns, ncolumns++, COL_RESOURCE);
531551
+		add_column(columns, ncolumns++, COL_DESC);
531551
+		add_column(columns, ncolumns++, COL_LIMIT);
531551
+		add_column(columns, ncolumns++, COL_USED);
531551
+		add_column(columns, ncolumns++, COL_USEPERC);
531551
+		LOWER = COLDESC_IDX_SUM_FIRST;
531551
+		UPPER = COLDESC_IDX_SUM_LAST;
531551
+	}
531551
+
531551
+	/* default to pretty-print if --id specified */
531551
+	if (id != -1 && !ctl->outmode)
531551
+		ctl->outmode = OUT_PRETTY;
531551
+
531551
+	if (!ctl->time_mode)
531551
+		ctl->time_mode = ctl->outmode == OUT_PRETTY ? TIME_FULL : TIME_SHORT;
531551
+
531551
+	if (ctl->outmode == OUT_PRETTY && !(optarg || show_creat || show_time)) {
531551
+		/* all columns for lsipc --<RESOURCE> --id <ID> */
531551
+		for (ncolumns = 0, i = 0; i < ARRAY_SIZE(coldescs); i++)
531551
+			 columns[ncolumns++] = i;
531551
+	} else {
531551
+		if (show_creat) {
531551
+			add_column(columns, ncolumns++, COL_CUID);
531551
+			add_column(columns, ncolumns++, COL_CGID);
531551
+			add_column(columns, ncolumns++, COL_UID);
531551
+			add_column(columns, ncolumns++, COL_GID);
531551
+		}
531551
+		if (msg && show_time) {
531551
+			add_column(columns, ncolumns++, COL_SEND);
531551
+			add_column(columns, ncolumns++, COL_RECV);
531551
+			add_column(columns, ncolumns++, COL_CTIME);
531551
+		}
531551
+		if (shm && show_time) {
531551
+			/* keep "COMMAND" as last column */
531551
+			size_t cmd = columns[ncolumns - 1] == COL_COMMAND;
531551
+
531551
+			if (cmd)
531551
+				ncolumns--;
531551
+			add_column(columns, ncolumns++, COL_ATTACH);
531551
+			add_column(columns, ncolumns++, COL_DETACH);
531551
+			if (cmd)
531551
+				add_column(columns, ncolumns++, COL_COMMAND);
531551
+		}
531551
+		if (sem && show_time) {
531551
+			add_column(columns, ncolumns++, COL_OTIME);
531551
+			add_column(columns, ncolumns++, COL_CTIME);
531551
+		}
531551
+	}
531551
+
531551
+	if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
531551
+					 (int *) &ncolumns, column_name_to_id) < 0)
531551
+		return EXIT_FAILURE;
531551
+
531551
+	tb = setup_table(ctl);
531551
+	if (!tb)
531551
+		return EXIT_FAILURE;
531551
+
531551
+	if (msg) {
531551
+		if (global)
531551
+			do_msg_global(tb);
531551
+		else
531551
+			do_msg(id, ctl, tb);
531551
+	}
531551
+	if (shm) {
531551
+		if (global)
531551
+			do_shm_global(tb);
531551
+		else
531551
+			do_shm(id, ctl, tb);
531551
+	}
531551
+	if (sem) {
531551
+		if (global)
531551
+			do_sem_global(tb);
531551
+		else
531551
+			do_sem(id, ctl, tb);
531551
+	}
531551
+
531551
+	print_table(ctl, tb);
531551
+
531551
+	scols_unref_table(tb);
531551
+	free(ctl);
531551
+
531551
+	return EXIT_SUCCESS;
531551
+}
531551
+
531551
diff --git a/tests/functions.sh b/tests/functions.sh
531551
index 123f6c3..b2d493c 100644
531551
--- a/tests/functions.sh
531551
+++ b/tests/functions.sh
531551
@@ -50,16 +50,25 @@ function ts_skip_nonroot {
531551
 }
531551
 
531551
 function ts_failed_subtest {
531551
+	local msg="FAILED"
531551
+	local ret=1
531551
+	if [ "$TS_KNOWN_FAIL" = "yes" ]; then
531551
+		msg="KNOWN FAILED"
531551
+		ret=0
531551
+	fi
531551
+
531551
 	if [ x"$1" == x"" ]; then
531551
-		echo " FAILED ($TS_NS)"
531551
+		echo " $msg ($TS_NS)"
531551
 	else
531551
-		echo " FAILED ($1)"
531551
+		echo " $msg ($1)"
531551
 	fi
531551
+
531551
+	return $ret
531551
 }
531551
 
531551
 function ts_failed {
531551
 	ts_failed_subtest "$1"
531551
-	exit 1
531551
+	exit $?
531551
 }
531551
 
531551
 function ts_ok_subtest {
531551
@@ -150,6 +159,7 @@ function ts_init_env {
531551
 	TS_SUBDIR=$(dirname $TS_SCRIPT)
531551
 	TS_TESTNAME=$(basename $TS_SCRIPT)
531551
 	TS_COMPONENT=$(basename $TS_SUBDIR)
531551
+	TS_KNOWN_FAIL="no"
531551
 
531551
 	TS_NSUBTESTS=0
531551
 	TS_NSUBFAILED=0
531551
diff --git a/tests/ts/ipcs/limits2 b/tests/ts/ipcs/limits2
531551
index f99a354..63f834d 100755
531551
--- a/tests/ts/ipcs/limits2
531551
+++ b/tests/ts/ipcs/limits2
531551
@@ -16,15 +16,20 @@
531551
 # GNU General Public License for more details.
531551
 #
531551
 
531551
-TS_TOPDIR="$(dirname $0)/../.."
531551
+TS_TOPDIR="${0%/*}/../.."
531551
 TS_DESC="basic limits"
531551
 
531551
 . $TS_TOPDIR/functions.sh
531551
 ts_init "$*"
531551
-type bc >/dev/null 2>&1 || ts_skip "cannot find bc command"
531551
 
531551
 . $TS_SELF/functions.sh
531551
 
531551
+# TODO https://github.com/karelzak/util-linux/issues/51
531551
+SHMALL=$(
531551
+if [ $(bc <<<"(2^64 / $PAGE_SIZE) <= $SHMALL") -eq 1 ]; then
531551
+	TS_KNOWN_FAIL="yes"
531551
+fi
531551
+
531551
 ts_log "check for difference between kernel and IPC"
531551
 ipcs_limits_check >> $TS_OUTPUT
531551
 
531551
-- 
531551
2.7.4
531551