modinfo: make it select PLATFORM_LINUX. Closes 4411
[oweals/busybox.git] / util-linux / ipcs.c
index 393f9483272a43c2f94881f083b927adce3da61a..ee7df5e3367d033754a1bdc90f2f44847c62095e 100644 (file)
@@ -5,14 +5,24 @@
  * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
  * Adapted for busybox from util-linux-2.12a.
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
-#include "libbb.h"
-#include <errno.h>
-#include <time.h>
-#include <pwd.h>
-#include <grp.h>
+//usage:#define ipcs_trivial_usage
+//usage:       "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
+//usage:#define ipcs_full_usage "\n\n"
+//usage:       "       -i      Show specific resource"
+//usage:     "\nResource specification:"
+//usage:     "\n       -m      Shared memory segments"
+//usage:     "\n       -q      Message queues"
+//usage:     "\n       -s      Semaphore arrays"
+//usage:     "\n       -a      All (default)"
+//usage:     "\nOutput format:"
+//usage:     "\n       -t      Time"
+//usage:     "\n       -c      Creator"
+//usage:     "\n       -p      Pid"
+//usage:     "\n       -l      Limits"
+//usage:     "\n       -u      Summary"
 
 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
@@ -23,7 +33,7 @@
 #include <sys/msg.h>
 #include <sys/shm.h>
 
-
+#include "libbb.h"
 
 /*-------------------------------------------------------------------*/
 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
 #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;
+       unsigned long shm_tot;          /* total allocated shm */
+       unsigned long shm_rss;          /* total resident shm */
+       unsigned long shm_swp;          /* total swapped shm */
+       unsigned long swap_attempts;
+       unsigned long swap_successes;
 };
 #endif
 
@@ -74,7 +84,7 @@ struct shm_info {
 union semun {
        int val;
        struct semid_ds *buf;
-       unsigned short int *array;
+       unsigned short *array;
        struct seminfo *__buf;
 };
 #endif
@@ -105,27 +115,23 @@ static void print_perms(int id, struct ipc_perm *ipcp)
 
        printf("%-10d %-10o", id, ipcp->mode & 0777);
 
-       if ((pw = getpwuid(ipcp->cuid)))
-               printf(" %-10s", pw->pw_name);
-       else
-               printf(" %-10d", ipcp->cuid);
-       if ((gr = getgrgid(ipcp->cgid)))
-               printf(" %-10s", gr->gr_name);
-       else
-               printf(" %-10d", ipcp->cgid);
-
-       if ((pw = getpwuid(ipcp->uid)))
-               printf(" %-10s", pw->pw_name);
-       else
-               printf(" %-10d", ipcp->uid);
-       if ((gr = getgrgid(ipcp->gid)))
-               printf(" %-10s\n", gr->gr_name);
-       else
-               printf(" %-10d\n", ipcp->gid);
+       pw = getpwuid(ipcp->cuid);
+       if (pw) printf(" %-10s", pw->pw_name);
+       else    printf(" %-10d", ipcp->cuid);
+       gr = getgrgid(ipcp->cgid);
+       if (gr) printf(" %-10s", gr->gr_name);
+       else    printf(" %-10d", ipcp->cgid);
+
+       pw = getpwuid(ipcp->uid);
+       if (pw) printf(" %-10s", pw->pw_name);
+       else    printf(" %-10d", ipcp->uid);
+       gr = getgrgid(ipcp->gid);
+       if (gr) printf(" %-10s\n", gr->gr_name);
+       else    printf(" %-10d\n", ipcp->gid);
 }
 
 
-static void do_shm(void)
+static NOINLINE void do_shm(void)
 {
        int maxid, shmid, id;
        struct shmid_ds shmseg;
@@ -252,7 +258,7 @@ static void do_shm(void)
 }
 
 
-static void do_sem(void)
+static NOINLINE void do_sem(void)
 {
        int maxid, semid, id;
        struct semid_ds semary;
@@ -261,7 +267,7 @@ static void do_sem(void)
        struct passwd *pw;
        union semun arg;
 
-       arg.array = (ushort *) (void *) &seminfo;
+       arg.array = (unsigned short *) (void *) &seminfo;
        maxid = semctl(0, 0, SEM_INFO, arg);
        if (maxid < 0) {
                printf("kernel not configured for %s\n", "semaphores");
@@ -271,7 +277,7 @@ static void do_sem(void)
        switch (format) {
        case LIMITS:
                printf("------ Semaphore %s --------\n", "Limits");
-               arg.array = (ushort *) (void *) &seminfo;       /* damn union */
+               arg.array = (unsigned short *) (void *) &seminfo;       /* damn union */
                if ((semctl(0, 0, IPC_INFO, arg)) < 0)
                        return;
                printf("max number of arrays = %d\n"
@@ -358,7 +364,7 @@ static void do_sem(void)
 }
 
 
-static void do_msg(void)
+static NOINLINE void do_msg(void)
 {
        int maxid, msqid, id;
        struct msqid_ds msgque;
@@ -565,11 +571,11 @@ static void print_sem(int semid)
                }
                printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
        }
-       puts("");
+       bb_putchar('\n');
 }
 
-int ipcs_main(int argc, char **argv);
-int ipcs_main(int argc, char **argv)
+int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int ipcs_main(int argc UNUSED_PARAM, char **argv)
 {
        int id = 0;
        unsigned flags = 0;
@@ -580,7 +586,7 @@ int ipcs_main(int argc, char **argv)
 #define flag_sem       (1<<2)
 #define flag_shm       (1<<3)
 
-       opt = getopt32(argc, argv, "i:aqsmtcplu", &opt_i);
+       opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
        if (opt & 0x1) { // -i
                id = xatoi(opt_i);
                flags |= flag_print;
@@ -598,34 +604,34 @@ int ipcs_main(int argc, char **argv)
        if (flags & flag_print) {
                if (flags & flag_shm) {
                        print_shm(id);
-                       fflush_stdout_and_exit(0);
+                       fflush_stdout_and_exit(EXIT_SUCCESS);
                }
                if (flags & flag_sem) {
                        print_sem(id);
-                       fflush_stdout_and_exit(0);
+                       fflush_stdout_and_exit(EXIT_SUCCESS);
                }
                if (flags & flag_msg) {
                        print_msg(id);
-                       fflush_stdout_and_exit(0);
+                       fflush_stdout_and_exit(EXIT_SUCCESS);
                }
                bb_show_usage();
        }
 
        if (!(flags & (flag_shm | flag_msg | flag_sem)))
                flags |= flag_msg | flag_shm | flag_sem;
-       puts("");
+       bb_putchar('\n');
 
        if (flags & flag_shm) {
                do_shm();
-               puts("");
+               bb_putchar('\n');
        }
        if (flags & flag_sem) {
                do_sem();
-               puts("");
+               bb_putchar('\n');
        }
        if (flags & flag_msg) {
                do_msg();
-               puts("");
+               bb_putchar('\n');
        }
-       fflush_stdout_and_exit(0);
+       fflush_stdout_and_exit(EXIT_SUCCESS);
 }