10f66f451Sopenharmony_ci/* ipcs.c - provide information on ipc facilities
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com>
40f66f451Sopenharmony_ci *
50f66f451Sopenharmony_ci * see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ipcs.html
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciUSE_IPCS(NEWTOY(ipcs, "acptulsqmi#", TOYFLAG_USR|TOYFLAG_BIN))
80f66f451Sopenharmony_ci
90f66f451Sopenharmony_ciconfig IPCS
100f66f451Sopenharmony_ci  bool "ipcs"
110f66f451Sopenharmony_ci  default n
120f66f451Sopenharmony_ci  help
130f66f451Sopenharmony_ci    usage: ipcs [[-smq] -i shmid] | [[-asmq] [-tcplu]]
140f66f451Sopenharmony_ci
150f66f451Sopenharmony_ci    -i Show specific resource
160f66f451Sopenharmony_ci    Resource specification:
170f66f451Sopenharmony_ci    -a All (default)
180f66f451Sopenharmony_ci    -m Shared memory segments
190f66f451Sopenharmony_ci    -q Message queues
200f66f451Sopenharmony_ci    -s Semaphore arrays
210f66f451Sopenharmony_ci    Output format:
220f66f451Sopenharmony_ci    -c Creator
230f66f451Sopenharmony_ci    -l Limits
240f66f451Sopenharmony_ci    -p Pid
250f66f451Sopenharmony_ci    -t Time
260f66f451Sopenharmony_ci    -u Summary
270f66f451Sopenharmony_ci*/
280f66f451Sopenharmony_ci
290f66f451Sopenharmony_ci#define FOR_ipcs
300f66f451Sopenharmony_ci#include "toys.h"
310f66f451Sopenharmony_ci#include <sys/ipc.h>
320f66f451Sopenharmony_ci#include <sys/shm.h>
330f66f451Sopenharmony_ci#include <sys/sem.h>
340f66f451Sopenharmony_ci#include <sys/msg.h>
350f66f451Sopenharmony_ci
360f66f451Sopenharmony_ciGLOBALS(
370f66f451Sopenharmony_ci  int id;
380f66f451Sopenharmony_ci)
390f66f451Sopenharmony_ci
400f66f451Sopenharmony_ci//used many times, so good to paste it
410f66f451Sopenharmony_ci#define flag(x) (toys.optflags & FLAG_ ## x)
420f66f451Sopenharmony_ci
430f66f451Sopenharmony_ciunion semun { //man says declare it yourself
440f66f451Sopenharmony_ci  int              val;
450f66f451Sopenharmony_ci  struct semid_ds *buf;
460f66f451Sopenharmony_ci  unsigned short  *array;
470f66f451Sopenharmony_ci  struct seminfo  *__buf;
480f66f451Sopenharmony_ci};
490f66f451Sopenharmony_ci
500f66f451Sopenharmony_cistatic void show_msg_id(void)
510f66f451Sopenharmony_ci{
520f66f451Sopenharmony_ci  struct msqid_ds buf;
530f66f451Sopenharmony_ci  int ret;
540f66f451Sopenharmony_ci
550f66f451Sopenharmony_ci  if ((ret = msgctl(TT.id, IPC_STAT, &buf)) < 0) {
560f66f451Sopenharmony_ci    perror_msg("msgctl");
570f66f451Sopenharmony_ci    return;
580f66f451Sopenharmony_ci  }
590f66f451Sopenharmony_ci
600f66f451Sopenharmony_ci#define ipcperm buf.msg_perm
610f66f451Sopenharmony_ci
620f66f451Sopenharmony_ci  printf("\nMessage Queue msqid=%d\n"
630f66f451Sopenharmony_ci      "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n",
640f66f451Sopenharmony_ci      TT.id, ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,ipcperm.mode);
650f66f451Sopenharmony_ci  printf ("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
660f66f451Sopenharmony_ci      (long) buf.msg_cbytes, (long) buf.msg_qbytes,
670f66f451Sopenharmony_ci      (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
680f66f451Sopenharmony_ci
690f66f451Sopenharmony_ci  printf("send_time=%-26.24s\nrcv_time=%-26.24s\nchange_time=%-26.24s\n\n",
700f66f451Sopenharmony_ci      buf.msg_stime ? ctime(&buf.msg_stime) : "Not set",
710f66f451Sopenharmony_ci      buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set",
720f66f451Sopenharmony_ci      buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
730f66f451Sopenharmony_ci#undef ipcperm
740f66f451Sopenharmony_ci}
750f66f451Sopenharmony_ci
760f66f451Sopenharmony_cistatic void show_sem_id(void)
770f66f451Sopenharmony_ci{
780f66f451Sopenharmony_ci  struct semid_ds buf;
790f66f451Sopenharmony_ci  union semun n;
800f66f451Sopenharmony_ci  int ret, i;
810f66f451Sopenharmony_ci
820f66f451Sopenharmony_ci  n.buf = &buf;
830f66f451Sopenharmony_ci  if ((ret = semctl(TT.id, 0, IPC_STAT, n)) < 0) {
840f66f451Sopenharmony_ci    perror_msg("semctl");
850f66f451Sopenharmony_ci    return;
860f66f451Sopenharmony_ci  }
870f66f451Sopenharmony_ci
880f66f451Sopenharmony_ci#define ipcperm buf.sem_perm
890f66f451Sopenharmony_ci  printf("\nSemaphore Array semid=%d\n"
900f66f451Sopenharmony_ci      "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
910f66f451Sopenharmony_ci      "mode=%#o, access_perms=%#o\n"
920f66f451Sopenharmony_ci      "nsems = %ld\n"
930f66f451Sopenharmony_ci      "otime = %-26.24s\n",
940f66f451Sopenharmony_ci      TT.id,
950f66f451Sopenharmony_ci      ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,
960f66f451Sopenharmony_ci      ipcperm.mode, ipcperm.mode & 0777,
970f66f451Sopenharmony_ci      (long) buf.sem_nsems,
980f66f451Sopenharmony_ci      buf.sem_otime ? ctime(&buf.sem_otime) : "Not set");
990f66f451Sopenharmony_ci  printf("ctime = %-26.24s\n"
1000f66f451Sopenharmony_ci      "%-10s %-10s %-10s %-10s %-10s\n",
1010f66f451Sopenharmony_ci      ctime(&buf.sem_ctime),
1020f66f451Sopenharmony_ci      "semnum", "value", "ncount", "zcount", "pid");
1030f66f451Sopenharmony_ci#undef ipcperm
1040f66f451Sopenharmony_ci
1050f66f451Sopenharmony_ci  for (i = 0; i < buf.sem_nsems; i++) {
1060f66f451Sopenharmony_ci    int val, nc, zc, pid;
1070f66f451Sopenharmony_ci    val = semctl(TT.id, i, GETVAL, n);
1080f66f451Sopenharmony_ci    nc = semctl(TT.id, i, GETNCNT, n);
1090f66f451Sopenharmony_ci    zc = semctl(TT.id, i, GETZCNT, n);
1100f66f451Sopenharmony_ci    pid = semctl(TT.id, i, GETPID, n);
1110f66f451Sopenharmony_ci    if (val < 0 || nc < 0 || zc < 0 || pid < 0)
1120f66f451Sopenharmony_ci      perror_exit("semctl");
1130f66f451Sopenharmony_ci    printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, nc, zc, pid);
1140f66f451Sopenharmony_ci  }
1150f66f451Sopenharmony_ci  xputc('\n');
1160f66f451Sopenharmony_ci}
1170f66f451Sopenharmony_ci
1180f66f451Sopenharmony_cistatic void show_shm_id(void)
1190f66f451Sopenharmony_ci{
1200f66f451Sopenharmony_ci  struct shmid_ds buf;
1210f66f451Sopenharmony_ci  int ret;
1220f66f451Sopenharmony_ci
1230f66f451Sopenharmony_ci  if ((ret = shmctl(TT.id, IPC_STAT, &buf)) < 0) {
1240f66f451Sopenharmony_ci    perror_msg("shmctl");
1250f66f451Sopenharmony_ci    return;
1260f66f451Sopenharmony_ci  }
1270f66f451Sopenharmony_ci
1280f66f451Sopenharmony_ci#define ipcperm buf.shm_perm
1290f66f451Sopenharmony_ci
1300f66f451Sopenharmony_ci  printf("\nShared memory Segment shmid=%d\n"
1310f66f451Sopenharmony_ci      "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
1320f66f451Sopenharmony_ci      "mode=%#o\taccess_perms=%#o\n"
1330f66f451Sopenharmony_ci      "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
1340f66f451Sopenharmony_ci      TT.id,
1350f66f451Sopenharmony_ci      ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,
1360f66f451Sopenharmony_ci      ipcperm.mode, (ipcperm.mode & 0777),
1370f66f451Sopenharmony_ci      (long) buf.shm_segsz, buf.shm_lpid, buf.shm_cpid,
1380f66f451Sopenharmony_ci      (long) buf.shm_nattch);
1390f66f451Sopenharmony_ci  printf("att_time=%-26.24s\n",
1400f66f451Sopenharmony_ci      buf.shm_atime ? ctime(&buf.shm_atime) : "Not set");
1410f66f451Sopenharmony_ci  printf("det_time=%-26.24s\n",
1420f66f451Sopenharmony_ci      buf.shm_dtime ? ctime(&buf.shm_dtime) : "Not set");
1430f66f451Sopenharmony_ci  printf("change_time=%-26.24s\n\n", ctime(&buf.shm_ctime));
1440f66f451Sopenharmony_ci#undef ipcperm
1450f66f451Sopenharmony_ci}
1460f66f451Sopenharmony_ci
1470f66f451Sopenharmony_cistatic void shm_array(void)
1480f66f451Sopenharmony_ci{
1490f66f451Sopenharmony_ci  struct shm_info shm_buf;
1500f66f451Sopenharmony_ci  struct shminfo ipc_buf;
1510f66f451Sopenharmony_ci  struct shmid_ds buf;
1520f66f451Sopenharmony_ci  int max_nr, i, shmid;
1530f66f451Sopenharmony_ci  struct passwd *pw;
1540f66f451Sopenharmony_ci  struct group *gr;
1550f66f451Sopenharmony_ci
1560f66f451Sopenharmony_ci  if ((max_nr = shmctl(0, SHM_INFO, (struct shmid_ds*)&shm_buf)) < 0) {
1570f66f451Sopenharmony_ci    perror_msg("kernel not configured for shared memory");
1580f66f451Sopenharmony_ci    return;
1590f66f451Sopenharmony_ci  }
1600f66f451Sopenharmony_ci
1610f66f451Sopenharmony_ci  if (flag(u)) {
1620f66f451Sopenharmony_ci    printf("------ Shared Memory Status --------\n");
1630f66f451Sopenharmony_ci    printf("segments allocated %d\n"
1640f66f451Sopenharmony_ci        "pages allocated %ld\n"
1650f66f451Sopenharmony_ci        "pages resident  %ld\n"
1660f66f451Sopenharmony_ci        "pages swapped   %ld\n"
1670f66f451Sopenharmony_ci        "Swap performance: %ld attempts\t%ld successes\n",
1680f66f451Sopenharmony_ci        shm_buf.used_ids,
1690f66f451Sopenharmony_ci        shm_buf.shm_tot,
1700f66f451Sopenharmony_ci        shm_buf.shm_rss,
1710f66f451Sopenharmony_ci        shm_buf.shm_swp,
1720f66f451Sopenharmony_ci        shm_buf.swap_attempts, shm_buf.swap_successes);
1730f66f451Sopenharmony_ci    return;
1740f66f451Sopenharmony_ci  }
1750f66f451Sopenharmony_ci  if (flag(l)) {
1760f66f451Sopenharmony_ci    if ((shmctl(0, 3, (struct shmid_ds*)&ipc_buf)) < 0) return; //IPC_INFO
1770f66f451Sopenharmony_ci    printf("------ Shared Memory Limits --------\n");
1780f66f451Sopenharmony_ci    printf("max number of segments = %lu\n"
1790f66f451Sopenharmony_ci        "max seg size (kbytes) = %lu\n"
1800f66f451Sopenharmony_ci        "max total shared memory (pages) = %lu\n"
1810f66f451Sopenharmony_ci        "min seg size (bytes) = %lu\n",
1820f66f451Sopenharmony_ci        (unsigned long) ipc_buf.shmmni,
1830f66f451Sopenharmony_ci        (unsigned long) (ipc_buf.shmmax >> 10),
1840f66f451Sopenharmony_ci        (unsigned long) ipc_buf.shmall,
1850f66f451Sopenharmony_ci        (unsigned long) ipc_buf.shmmin);
1860f66f451Sopenharmony_ci    return;
1870f66f451Sopenharmony_ci  }
1880f66f451Sopenharmony_ci
1890f66f451Sopenharmony_ci  if (flag(t)) {
1900f66f451Sopenharmony_ci    printf("------ Shared Memory Attach/Detach/Change Times --------\n");
1910f66f451Sopenharmony_ci    printf("%-10s %-10s %-20s %-20s %-20s\n",
1920f66f451Sopenharmony_ci        "shmid", "owner", "attached", "detached", "changed");
1930f66f451Sopenharmony_ci  } else if (flag(p)) {
1940f66f451Sopenharmony_ci    printf("------ Shared Memory Creator/Last-op --------\n");
1950f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s\n",
1960f66f451Sopenharmony_ci        "shmid", "owner", "cpid", "lpid");
1970f66f451Sopenharmony_ci  } else if (flag(c)) {
1980f66f451Sopenharmony_ci    printf("------ Shared Memory Segment Creators/Owners --------\n");
1990f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
2000f66f451Sopenharmony_ci        "shmid", "perms", "cuid", "cgid", "uid", "gid");
2010f66f451Sopenharmony_ci  } else {
2020f66f451Sopenharmony_ci    printf("------ Shared Memory Segments --------\n");
2030f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
2040f66f451Sopenharmony_ci        "key", "shmid", "owner", "perms", "bytes", "nattch",
2050f66f451Sopenharmony_ci        "status");
2060f66f451Sopenharmony_ci  }
2070f66f451Sopenharmony_ci
2080f66f451Sopenharmony_ci  for (i = 0; i <= max_nr; i++) {
2090f66f451Sopenharmony_ci    if ((shmid = shmctl(i, SHM_STAT, &buf)) < 0 ) continue;
2100f66f451Sopenharmony_ci    if (flag(t)) {
2110f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.shm_perm.uid)))
2120f66f451Sopenharmony_ci        printf("%-10d %-10.10s", shmid, pw->pw_name);
2130f66f451Sopenharmony_ci      else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
2140f66f451Sopenharmony_ci      printf(" %-20.16s", buf.shm_atime
2150f66f451Sopenharmony_ci          ? ctime(&buf.shm_atime) + 4 : "Not set");
2160f66f451Sopenharmony_ci      printf(" %-20.16s", buf.shm_dtime
2170f66f451Sopenharmony_ci          ? ctime(&buf.shm_dtime) + 4 : "Not set");
2180f66f451Sopenharmony_ci      printf(" %-20.16s\n", buf.shm_ctime
2190f66f451Sopenharmony_ci          ? ctime(&buf.shm_ctime) + 4 : "Not set");
2200f66f451Sopenharmony_ci    } else if (flag(p)) {
2210f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.shm_perm.uid)))
2220f66f451Sopenharmony_ci        printf("%-10d %-10.10s", shmid, pw->pw_name);
2230f66f451Sopenharmony_ci      else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
2240f66f451Sopenharmony_ci      printf(" %-10d %-10d\n", buf.shm_cpid, buf.shm_lpid);
2250f66f451Sopenharmony_ci    } else if (flag(c)) {
2260f66f451Sopenharmony_ci      printf("%-10d %-10o", shmid, buf.shm_perm.mode & 0777);
2270f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.shm_perm.cuid))) printf(" %-10s", pw->pw_name);
2280f66f451Sopenharmony_ci      else printf(" %-10d", buf.shm_perm.cuid);
2290f66f451Sopenharmony_ci      if ((gr = getgrgid(buf.shm_perm.cgid))) printf(" %-10s", gr->gr_name);
2300f66f451Sopenharmony_ci      else printf(" %-10d", buf.shm_perm.cgid);
2310f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.shm_perm.uid))) printf(" %-10s", pw->pw_name);
2320f66f451Sopenharmony_ci      else printf(" %-10d", buf.shm_perm.uid);
2330f66f451Sopenharmony_ci      if ((gr = getgrgid(buf.shm_perm.gid))) printf(" %-10s\n", gr->gr_name);
2340f66f451Sopenharmony_ci      else printf(" %-10d\n", buf.shm_perm.gid);
2350f66f451Sopenharmony_ci    } else {
2360f66f451Sopenharmony_ci      printf("0x%08x ", buf.shm_perm.__key);
2370f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.shm_perm.uid)))
2380f66f451Sopenharmony_ci        printf("%-10d %-10.10s", shmid, pw->pw_name);
2390f66f451Sopenharmony_ci      else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
2400f66f451Sopenharmony_ci      printf(" %-10o %-10lu %-10ld %-6s %-6s\n", buf.shm_perm.mode & 0777,
2410f66f451Sopenharmony_ci          (unsigned long) buf.shm_segsz,
2420f66f451Sopenharmony_ci          (long) buf.shm_nattch,
2430f66f451Sopenharmony_ci          buf.shm_perm.mode & SHM_DEST ? "dest" : " ",
2440f66f451Sopenharmony_ci          buf.shm_perm.mode & SHM_LOCKED ? "locked" : " ");
2450f66f451Sopenharmony_ci    }
2460f66f451Sopenharmony_ci  }
2470f66f451Sopenharmony_ci}
2480f66f451Sopenharmony_ci
2490f66f451Sopenharmony_cistatic void sem_array(void)
2500f66f451Sopenharmony_ci{
2510f66f451Sopenharmony_ci  struct seminfo info_buf;
2520f66f451Sopenharmony_ci  struct semid_ds buf;
2530f66f451Sopenharmony_ci  union semun u;
2540f66f451Sopenharmony_ci  int max_nr, i,semid;
2550f66f451Sopenharmony_ci  struct passwd *pw;
2560f66f451Sopenharmony_ci  struct group *gr;
2570f66f451Sopenharmony_ci
2580f66f451Sopenharmony_ci  u.array = (unsigned short *)&info_buf;
2590f66f451Sopenharmony_ci  if ((max_nr = semctl(0, 0, SEM_INFO, u)) < 0) {
2600f66f451Sopenharmony_ci    perror_msg("kernel is not configured for semaphores");
2610f66f451Sopenharmony_ci    return;
2620f66f451Sopenharmony_ci  }
2630f66f451Sopenharmony_ci
2640f66f451Sopenharmony_ci
2650f66f451Sopenharmony_ci  if (flag(u)) {
2660f66f451Sopenharmony_ci    printf("------ Semaphore Status --------\n");
2670f66f451Sopenharmony_ci    printf("used arrays = %d\n"
2680f66f451Sopenharmony_ci        "allocated semaphores = %d\n",
2690f66f451Sopenharmony_ci        info_buf.semusz, info_buf.semaem);
2700f66f451Sopenharmony_ci    return;
2710f66f451Sopenharmony_ci  }
2720f66f451Sopenharmony_ci  if (flag(l)) {
2730f66f451Sopenharmony_ci    printf("------ Semaphore Limits --------\n");
2740f66f451Sopenharmony_ci    u.array = (unsigned short *)&info_buf;
2750f66f451Sopenharmony_ci    if ((semctl(0, 0, 3, u)) < 0) //IPC_INFO
2760f66f451Sopenharmony_ci      return;
2770f66f451Sopenharmony_ci    printf("max number of arrays = %d\n"
2780f66f451Sopenharmony_ci        "max semaphores per array = %d\n"
2790f66f451Sopenharmony_ci        "max semaphores system wide = %d\n"
2800f66f451Sopenharmony_ci        "max ops per semop call = %d\n"
2810f66f451Sopenharmony_ci        "semaphore max value = %d\n",
2820f66f451Sopenharmony_ci        info_buf.semmni,
2830f66f451Sopenharmony_ci        info_buf.semmsl,
2840f66f451Sopenharmony_ci        info_buf.semmns, info_buf.semopm, info_buf.semvmx);
2850f66f451Sopenharmony_ci    return;
2860f66f451Sopenharmony_ci  }
2870f66f451Sopenharmony_ci
2880f66f451Sopenharmony_ci  if (flag(t)) {
2890f66f451Sopenharmony_ci    printf("------ Semaphore Operation/Change Times --------\n");
2900f66f451Sopenharmony_ci    printf("%-8s %-10s %-26.24s %-26.24s\n",
2910f66f451Sopenharmony_ci        "shmid", "owner", "last-op", "last-changed");
2920f66f451Sopenharmony_ci  } else if (flag(c)) {
2930f66f451Sopenharmony_ci    printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
2940f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
2950f66f451Sopenharmony_ci        "semid", "perms", "cuid", "cgid", "uid", "gid");
2960f66f451Sopenharmony_ci
2970f66f451Sopenharmony_ci  } else if (flag(p)){
2980f66f451Sopenharmony_ci    return;
2990f66f451Sopenharmony_ci  } else {
3000f66f451Sopenharmony_ci    printf("------ Semaphore %s --------\n", "Arrays");
3010f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s %-10s\n",
3020f66f451Sopenharmony_ci        "key", "semid", "owner", "perms", "nsems");
3030f66f451Sopenharmony_ci  }
3040f66f451Sopenharmony_ci
3050f66f451Sopenharmony_ci  for (i = 0; i <= max_nr; i++) {
3060f66f451Sopenharmony_ci    u.buf = &buf;
3070f66f451Sopenharmony_ci    if ((semid = semctl(i, 0, SEM_STAT, u)) < 0) continue;
3080f66f451Sopenharmony_ci    pw = getpwuid(buf.sem_perm.uid);
3090f66f451Sopenharmony_ci    if (flag(t)) {
3100f66f451Sopenharmony_ci      if (pw) printf("%-8d %-10.10s", semid, pw->pw_name);
3110f66f451Sopenharmony_ci      else printf("%-8d %-10d", semid, buf.sem_perm.uid);
3120f66f451Sopenharmony_ci
3130f66f451Sopenharmony_ci      printf("  %-26.24s", buf.sem_otime
3140f66f451Sopenharmony_ci          ? ctime(&buf.sem_otime) : "Not set");
3150f66f451Sopenharmony_ci      printf(" %-26.24s\n", buf.sem_ctime
3160f66f451Sopenharmony_ci          ? ctime(&buf.sem_ctime) : "Not set");
3170f66f451Sopenharmony_ci    } else if (flag(c)) {
3180f66f451Sopenharmony_ci      printf("%-10d %-10o", semid, buf.sem_perm.mode & 0777);
3190f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.sem_perm.cuid))) printf(" %-10s", pw->pw_name);
3200f66f451Sopenharmony_ci      else printf(" %-10d", buf.sem_perm.cuid);
3210f66f451Sopenharmony_ci      if ((gr = getgrgid(buf.sem_perm.cgid))) printf(" %-10s", gr->gr_name);
3220f66f451Sopenharmony_ci      else printf(" %-10d", buf.sem_perm.cgid);
3230f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.sem_perm.uid))) printf(" %-10s", pw->pw_name);
3240f66f451Sopenharmony_ci      else printf(" %-10d", buf.sem_perm.uid);
3250f66f451Sopenharmony_ci      if ((gr = getgrgid(buf.sem_perm.gid))) printf(" %-10s\n", gr->gr_name);
3260f66f451Sopenharmony_ci      else printf(" %-10d\n", buf.sem_perm.gid);
3270f66f451Sopenharmony_ci    } else {
3280f66f451Sopenharmony_ci      printf("0x%08x ", buf.sem_perm.__key);
3290f66f451Sopenharmony_ci      if (pw) printf("%-10d %-10.9s", semid, pw->pw_name);
3300f66f451Sopenharmony_ci      else printf("%-10d %-9d", semid, buf.sem_perm.uid);
3310f66f451Sopenharmony_ci      printf(" %-10o %-10ld\n", buf.sem_perm.mode & 0777,
3320f66f451Sopenharmony_ci          (long) buf.sem_nsems);
3330f66f451Sopenharmony_ci    }
3340f66f451Sopenharmony_ci  }
3350f66f451Sopenharmony_ci}
3360f66f451Sopenharmony_ci
3370f66f451Sopenharmony_cistatic void msg_array(void)
3380f66f451Sopenharmony_ci{
3390f66f451Sopenharmony_ci  struct msginfo info_buf;
3400f66f451Sopenharmony_ci  struct msqid_ds buf;
3410f66f451Sopenharmony_ci  int max_nr, i, msqid;
3420f66f451Sopenharmony_ci  struct passwd *pw;
3430f66f451Sopenharmony_ci  struct group *gr;
3440f66f451Sopenharmony_ci
3450f66f451Sopenharmony_ci  if ((max_nr = msgctl(0, MSG_INFO, (struct msqid_ds*)&info_buf)) < 0) {
3460f66f451Sopenharmony_ci    perror_msg("kernel not configured for message queue");
3470f66f451Sopenharmony_ci    return;
3480f66f451Sopenharmony_ci  }
3490f66f451Sopenharmony_ci
3500f66f451Sopenharmony_ci  if (flag(u)) {
3510f66f451Sopenharmony_ci    printf("------ Message%s --------\n", "s: Status");
3520f66f451Sopenharmony_ci    printf("allocated queues = %d\n"
3530f66f451Sopenharmony_ci        "used headers = %d\n"
3540f66f451Sopenharmony_ci        "used space = %d bytes\n",
3550f66f451Sopenharmony_ci        info_buf.msgpool, info_buf.msgmap, info_buf.msgtql);
3560f66f451Sopenharmony_ci    return;
3570f66f451Sopenharmony_ci  }
3580f66f451Sopenharmony_ci  if (flag(l)) {
3590f66f451Sopenharmony_ci    if ((msgctl(0, 3, (struct msqid_ds*)&info_buf)) < 0) return; //IPC_INFO
3600f66f451Sopenharmony_ci    printf("------ Messages: Limits --------\n");
3610f66f451Sopenharmony_ci    printf("max queues system wide = %d\n"
3620f66f451Sopenharmony_ci        "max size of message (bytes) = %d\n"
3630f66f451Sopenharmony_ci        "default max size of queue (bytes) = %d\n",
3640f66f451Sopenharmony_ci        info_buf.msgmni, info_buf.msgmax, info_buf.msgmnb);
3650f66f451Sopenharmony_ci    return;
3660f66f451Sopenharmony_ci  }
3670f66f451Sopenharmony_ci
3680f66f451Sopenharmony_ci  if (flag(t)) {
3690f66f451Sopenharmony_ci    printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
3700f66f451Sopenharmony_ci    printf("%-8s %-10s %-20s %-20s %-20s\n",
3710f66f451Sopenharmony_ci        "msqid", "owner", "send", "recv", "change");
3720f66f451Sopenharmony_ci  } else if (flag(p)) {
3730f66f451Sopenharmony_ci    printf("------ Message%s --------\n", " Queues PIDs");
3740f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s\n",
3750f66f451Sopenharmony_ci        "msqid", "owner", "lspid", "lrpid");
3760f66f451Sopenharmony_ci  } else if (flag(c)) {
3770f66f451Sopenharmony_ci    printf("------ Message%s --------\n", " Queues: Creators/Owners");
3780f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
3790f66f451Sopenharmony_ci        "msqid", "perms", "cuid", "cgid", "uid", "gid");
3800f66f451Sopenharmony_ci  } else {
3810f66f451Sopenharmony_ci    printf("------ Message%s --------\n", " Queues");
3820f66f451Sopenharmony_ci    printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
3830f66f451Sopenharmony_ci        "key", "msqid", "owner", "perms", "used-bytes", "messages");
3840f66f451Sopenharmony_ci  }
3850f66f451Sopenharmony_ci
3860f66f451Sopenharmony_ci  for (i = 0; i <= max_nr; i++) {
3870f66f451Sopenharmony_ci    if ((msqid = msgctl(i, MSG_STAT, &buf)) < 0 ) continue;
3880f66f451Sopenharmony_ci    pw = getpwuid(buf.msg_perm.uid);
3890f66f451Sopenharmony_ci    if (flag(t)) {
3900f66f451Sopenharmony_ci      if (pw) printf("%-8d %-10.10s", msqid, pw->pw_name);
3910f66f451Sopenharmony_ci      else printf("%-8d %-10d", msqid, buf.msg_perm.uid);
3920f66f451Sopenharmony_ci      printf(" %-20.16s", buf.msg_stime
3930f66f451Sopenharmony_ci          ? ctime(&buf.msg_stime) + 4 : "Not set");
3940f66f451Sopenharmony_ci      printf(" %-20.16s", buf.msg_rtime
3950f66f451Sopenharmony_ci          ? ctime(&buf.msg_rtime) + 4 : "Not set");
3960f66f451Sopenharmony_ci      printf(" %-20.16s\n", buf.msg_ctime
3970f66f451Sopenharmony_ci          ? ctime(&buf.msg_ctime) + 4 : "Not set");
3980f66f451Sopenharmony_ci    } else if (flag(p)) {
3990f66f451Sopenharmony_ci      if (pw) printf("%-8d %-10.10s", msqid, pw->pw_name);
4000f66f451Sopenharmony_ci      else printf("%-8d %-10d", msqid, buf.msg_perm.uid);
4010f66f451Sopenharmony_ci      printf("  %5d     %5d\n", buf.msg_lspid, buf.msg_lrpid);
4020f66f451Sopenharmony_ci    } else if (flag(c)) {
4030f66f451Sopenharmony_ci      printf("%-10d %-10o", msqid, buf.msg_perm.mode & 0777);
4040f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.msg_perm.cuid))) printf(" %-10s", pw->pw_name);
4050f66f451Sopenharmony_ci      else printf(" %-10d", buf.msg_perm.cuid);
4060f66f451Sopenharmony_ci      if ((gr = getgrgid(buf.msg_perm.cgid))) printf(" %-10s", gr->gr_name);
4070f66f451Sopenharmony_ci      else printf(" %-10d", buf.msg_perm.cgid);
4080f66f451Sopenharmony_ci      if ((pw = getpwuid(buf.msg_perm.uid))) printf(" %-10s", pw->pw_name);
4090f66f451Sopenharmony_ci      else printf(" %-10d", buf.msg_perm.uid);
4100f66f451Sopenharmony_ci      if ((gr = getgrgid(buf.msg_perm.gid))) printf(" %-10s\n", gr->gr_name);
4110f66f451Sopenharmony_ci      else printf(" %-10d\n", buf.msg_perm.gid);
4120f66f451Sopenharmony_ci    } else {
4130f66f451Sopenharmony_ci      printf("0x%08x ", buf.msg_perm.__key);
4140f66f451Sopenharmony_ci      if (pw) printf("%-10d %-10.10s", msqid, pw->pw_name);
4150f66f451Sopenharmony_ci      else printf("%-10d %-10d", msqid, buf.msg_perm.uid);
4160f66f451Sopenharmony_ci      printf(" %-10o %-12ld %-12ld\n", buf.msg_perm.mode & 0777,
4170f66f451Sopenharmony_ci          (long) buf.msg_cbytes, (long) buf.msg_qnum);
4180f66f451Sopenharmony_ci    }
4190f66f451Sopenharmony_ci  }
4200f66f451Sopenharmony_ci}
4210f66f451Sopenharmony_ci
4220f66f451Sopenharmony_civoid ipcs_main(void)
4230f66f451Sopenharmony_ci{
4240f66f451Sopenharmony_ci  if (flag(i)) {
4250f66f451Sopenharmony_ci    if (flag(m)) show_shm_id();
4260f66f451Sopenharmony_ci    else if (flag(s)) show_sem_id();
4270f66f451Sopenharmony_ci    else if (flag(q)) show_msg_id();
4280f66f451Sopenharmony_ci    else help_exit(0);
4290f66f451Sopenharmony_ci    return;
4300f66f451Sopenharmony_ci  }
4310f66f451Sopenharmony_ci
4320f66f451Sopenharmony_ci  if (!(flag(m) || flag(s) || flag(q)) || flag(a)) toys.optflags |= (FLAG_m|FLAG_s|FLAG_q);
4330f66f451Sopenharmony_ci
4340f66f451Sopenharmony_ci  xputc('\n');
4350f66f451Sopenharmony_ci  if (flag(m)) {
4360f66f451Sopenharmony_ci    shm_array();
4370f66f451Sopenharmony_ci    xputc('\n');
4380f66f451Sopenharmony_ci  }
4390f66f451Sopenharmony_ci  if (flag(s)) {
4400f66f451Sopenharmony_ci    sem_array();
4410f66f451Sopenharmony_ci    xputc('\n');
4420f66f451Sopenharmony_ci  }
4430f66f451Sopenharmony_ci  if (flag(q)) {
4440f66f451Sopenharmony_ci    msg_array();
4450f66f451Sopenharmony_ci    xputc('\n');
4460f66f451Sopenharmony_ci  }
4470f66f451Sopenharmony_ci}
448