]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/ipcs.c
su: use lib/pty-session.c code for --pty
[thirdparty/util-linux.git] / sys-utils / ipcs.c
index 8ed5698ddbd5d28a573da2a6646164fe0960defb..2d18a13cced6db1640b86f2febf28e2fa4f408ec 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 #include <errno.h>
-#include <features.h>
 #include <getopt.h>
 
 #include "c.h"
@@ -47,23 +46,29 @@ static void print_msg (int id, int unit);
 /* we read time as int64_t from /proc, so cast... */
 #define xctime(_x)     ctime((time_t *) (_x))
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out, USAGE_HEADER);
-       fprintf(out, " %s [resource ...] [output-format]\n", program_invocation_short_name);
-       fprintf(out, " %s [resource] -i <id>\n", program_invocation_short_name);
-       fprintf(out, USAGE_OPTIONS);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       fprintf(out, _(" %1$s [resource-option...] [output-option]\n"
+                      " %1$s -m|-q|-s -i <id>\n"), program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Show information on IPC facilities.\n"), out);
+
+       fputs(USAGE_OPTIONS, out);
        fputs(_(" -i, --id <id>  print details on resource identified by <id>\n"), out);
-       fprintf(out, USAGE_HELP);
-       fprintf(out, USAGE_VERSION);
-       fprintf(out, USAGE_SEPARATOR);
+       printf(USAGE_HELP_OPTIONS(16));
+
+       fputs(USAGE_SEPARATOR, out);
        fputs(_("Resource options:\n"), out);
        fputs(_(" -m, --shmems      shared memory segments\n"), out);
        fputs(_(" -q, --queues      message queues\n"), out);
        fputs(_(" -s, --semaphores  semaphores\n"), out);
        fputs(_(" -a, --all         all (default)\n"), out);
-       fprintf(out, USAGE_SEPARATOR);
-       fputs(_("Output format:\n"), out);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Output options:\n"), out);
        fputs(_(" -t, --time        show attach, detach and change times\n"), out);
        fputs(_(" -p, --pid         show PIDs of creator and last operator\n"), out);
        fputs(_(" -c, --creator     show creator and owner\n"), out);
@@ -71,8 +76,9 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
        fputs(_(" -u, --summary     show status summary\n"), out);
        fputs(_("     --human       show sizes in human-readable format\n"), out);
        fputs(_(" -b, --bytes       show sizes in bytes\n"), out);
-       fprintf(out, USAGE_MAN_TAIL("ipcs(1)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       printf(USAGE_MAN_TAIL("ipcs(1)"));
+
+       exit(EXIT_SUCCESS);
 }
 
 int main (int argc, char **argv)
@@ -102,7 +108,7 @@ int main (int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((opt = getopt_long(argc, argv, options, longopts, NULL)) != -1) {
                switch (opt) {
@@ -143,13 +149,13 @@ int main (int argc, char **argv)
                case 'b':
                        unit = IPC_UNIT_BYTES;
                        break;
+
                case 'h':
-                       usage(stdout);
+                       usage();
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
@@ -192,16 +198,24 @@ static void do_shm (char format, int unit)
        case LIMITS:
        {
                struct ipc_limits lim;
+               uint64_t tmp, pgsz = getpagesize();
 
-               printf (_("------ Shared Memory Limits --------\n"));
-               if (ipc_shm_get_limits(&lim))
+               if (ipc_shm_get_limits(&lim)) {
+                       printf (_("unable to fetch shared memory limits\n"));
                        return;
+               }
+               printf (_("------ Shared Memory Limits --------\n"));
                printf (_("max number of segments = %ju\n"), lim.shmmni);
                ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit,
                               _("max seg size"), lim.shmmax, "\n", 0);
+
+               tmp = (uint64_t) lim.shmall * pgsz;
+               /* overflow handling, at least we don't print ridiculous small values */
+               if (lim.shmall != 0 && tmp / lim.shmall != pgsz) {
+                       tmp = UINT64_MAX - (UINT64_MAX % pgsz);
+               }
                ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit,
-                              _("max total shared memory"),
-                              (uint64_t) lim.shmall * getpagesize(), "\n", 0);
+                              _("max total shared memory"), tmp, "\n", 0);
                ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
                               _("min seg size"), lim.shmmin, "\n", 0);
                return;
@@ -209,9 +223,11 @@ static void do_shm (char format, int unit)
        case STATUS:
        {
                int maxid;
-               struct shm_info shm_info;
+               struct shmid_ds shmbuf;
+               struct shm_info *shm_info;
 
-               maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) &shm_info);
+               maxid = shmctl (0, SHM_INFO, &shmbuf);
+               shm_info =  (struct shm_info *) &shmbuf;
                if (maxid < 0) {
                        printf (_("kernel not configured for shared memory\n"));
                        return;
@@ -234,11 +250,11 @@ static void do_shm (char format, int unit)
                          "pages resident  %ld\n"
                          "pages swapped   %ld\n"
                          "Swap performance: %ld attempts\t %ld successes\n"),
-                       shm_info.used_ids,
-                       shm_info.shm_tot,
-                       shm_info.shm_rss,
-                       shm_info.shm_swp,
-                       shm_info.swap_attempts, shm_info.swap_successes);
+                       shm_info->used_ids,
+                       shm_info->shm_tot,
+                       shm_info->shm_rss,
+                       shm_info->shm_swp,
+                       shm_info->swap_attempts, shm_info->swap_successes);
                return;
        }
 
@@ -343,14 +359,16 @@ static void do_sem (char format)
        {
                struct ipc_limits lim;
 
-               printf (_("------ Semaphore Limits --------\n"));
-               if (ipc_sem_get_limits(&lim))
+               if (ipc_sem_get_limits(&lim)) {
+                       printf (_("unable to fetch semaphore limits\n"));
                        return;
+               }
+               printf (_("------ Semaphore Limits --------\n"));
                printf (_("max number of arrays = %d\n"), lim.semmni);
                printf (_("max semaphores per array = %d\n"), lim.semmsl);
                printf (_("max semaphores system wide = %d\n"), lim.semmns);
                printf (_("max ops per semop call = %d\n"), lim.semopm);
-               printf (_("semaphore max value = %d\n"), lim.semvmx);
+               printf (_("semaphore max value = %u\n"), lim.semvmx);
                return;
        }
        case STATUS:
@@ -443,8 +461,10 @@ static void do_msg (char format, int unit)
        {
                struct ipc_limits lim;
 
-               if (ipc_msg_get_limits(&lim))
+               if (ipc_msg_get_limits(&lim)) {
+                       printf (_("unable to fetch message limits\n"));
                        return;
+               }
                printf (_("------ Messages Limits --------\n"));
                printf (_("max queues system wide = %d\n"), lim.msgmni);
                ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
@@ -461,8 +481,10 @@ static void do_msg (char format, int unit)
                        return;
                }
                printf (_("------ Messages Status --------\n"));
+#ifndef __FreeBSD_kernel__
                printf (_("allocated queues = %d\n"), msginfo.msgpool);
                printf (_("used headers = %d\n"), msginfo.msgmap);
+#endif
                ipc_print_size(unit, _("used space"), msginfo.msgtql,
                               unit == IPC_UNIT_DEFAULT ? _(" bytes\n") : "\n", 0);
                return;
@@ -561,7 +583,7 @@ static void print_shm(int shmid, int unit)
 
        printf(_("\nShared memory Segment shmid=%d\n"), shmid);
        printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
-              shmdata->shm_perm.uid, shmdata->shm_perm.uid,
+              shmdata->shm_perm.uid, shmdata->shm_perm.gid,
               shmdata->shm_perm.cuid, shmdata->shm_perm.cgid);
        printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata->shm_perm.mode,
               shmdata->shm_perm.mode & 0777);
@@ -580,7 +602,7 @@ static void print_shm(int shmid, int unit)
        ipc_shm_free_info(shmdata);
 }
 
-void print_msg(int msgid, int unit)
+static void print_msg(int msgid, int unit)
 {
        struct msg_data *msgdata;
 
@@ -591,7 +613,7 @@ void print_msg(int msgid, int unit)
 
        printf(_("\nMessage Queue msqid=%d\n"), msgid);
        printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
-              msgdata->msg_perm.uid, msgdata->msg_perm.uid,
+              msgdata->msg_perm.uid, msgdata->msg_perm.gid,
               msgdata->msg_perm.cuid, msgdata->msg_perm.cgid,
               msgdata->msg_perm.mode);
        ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("csize=") : _("cbytes="),
@@ -624,7 +646,7 @@ static void print_sem(int semid)
 
        printf(_("\nSemaphore Array semid=%d\n"), semid);
        printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
-              semdata->sem_perm.uid, semdata->sem_perm.uid,
+              semdata->sem_perm.uid, semdata->sem_perm.gid,
               semdata->sem_perm.cuid, semdata->sem_perm.cgid);
        printf(_("mode=%#o, access_perms=%#o\n"),
               semdata->sem_perm.mode, semdata->sem_perm.mode & 0777);
@@ -638,7 +660,7 @@ static void print_sem(int semid)
 
        for (i = 0; i < semdata->sem_nsems; i++) {
                struct sem_elem *e = &semdata->elements[i];
-               printf("%-10zd %-10d %-10d %-10d %-10d\n",
+               printf("%-10zu %-10d %-10d %-10d %-10d\n",
                       i, e->semval, e->ncount, e->zcount, e->pid);
        }
        printf("\n");