aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/ipcs.c
blob: b5986af7eeff22f9a84192b507aa449ce1669dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* ipcs.c - provide information on ipc facilities
 *
 * Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com>
 *
 * see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ipcs.html

USE_IPCS(NEWTOY(ipcs, "acptulsqmi#", TOYFLAG_USR|TOYFLAG_BIN))

config IPCS
  bool "ipcs"
  default n
  help
    usage: ipcs [[-smq] -i shmid] | [[-asmq] [-tcplu]]

    -i Show specific resource
    Resource specification:
    -a All (default)
    -m Shared memory segments
    -q Message queues
    -s Semaphore arrays
    Output format:
    -c Creator
    -l Limits
    -p Pid
    -t Time
    -u Summary
*/

#define FOR_ipcs
#include "toys.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/msg.h>

GLOBALS(
  int id;
)

//used many times, so good to paste it
#define flag(x) (toys.optflags & FLAG_ ## x)

union semun { //man says declare it yourself
  int              val;
  struct semid_ds *buf;
  unsigned short  *array;
  struct seminfo  *__buf;
};

static void show_msg_id(void)
{
  struct msqid_ds buf;
  int ret;

  if ((ret = msgctl(TT.id, IPC_STAT, &buf)) < 0) {
    perror_msg("msgctl");
    return;
  }

#define ipcperm buf.msg_perm

  printf("\nMessage Queue msqid=%d\n"
      "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n",
      TT.id, ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,ipcperm.mode);
  printf ("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
      (long) buf.msg_cbytes, (long) buf.msg_qbytes,
      (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);

  printf("send_time=%-26.24s\nrcv_time=%-26.24s\nchange_time=%-26.24s\n\n",
      buf.msg_stime ? ctime(&buf.msg_stime) : "Not set",
      buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set",
      buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
#undef ipcperm
}

static void show_sem_id(void)
{
  struct semid_ds buf;
  union semun n;
  int ret, i;

  n.buf = &buf;
  if ((ret = semctl(TT.id, 0, IPC_STAT, n)) < 0) {
    perror_msg("semctl");
    return;
  }

#define ipcperm buf.sem_perm
  printf("\nSemaphore Array semid=%d\n"
      "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
      "mode=%#o, access_perms=%#o\n"
      "nsems = %ld\n"
      "otime = %-26.24s\n",
      TT.id,
      ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,
      ipcperm.mode, ipcperm.mode & 0777,
      (long) buf.sem_nsems,
      buf.sem_otime ? ctime(&buf.sem_otime) : "Not set");
  printf("ctime = %-26.24s\n"
      "%-10s %-10s %-10s %-10s %-10s\n",
      ctime(&buf.sem_ctime),
      "semnum", "value", "ncount", "zcount", "pid");
#undef ipcperm

  for (i = 0; i < buf.sem_nsems; i++) {
    int val, nc, zc, pid;
    val = semctl(TT.id, i, GETVAL, n);
    nc = semctl(TT.id, i, GETNCNT, n);
    zc = semctl(TT.id, i, GETZCNT, n);
    pid = semctl(TT.id, i, GETPID, n);
    if (val < 0 || nc < 0 || zc < 0 || pid < 0)
      perror_exit("semctl");
    printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, nc, zc, pid);
  }
  xputc('\n');
}

static void show_shm_id(void)
{
  struct shmid_ds buf;
  int ret;

  if ((ret = shmctl(TT.id, IPC_STAT, &buf)) < 0) {
    perror_msg("shmctl");
    return;
  }

#define ipcperm buf.shm_perm

  printf("\nShared memory Segment shmid=%d\n"
      "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
      "mode=%#o\taccess_perms=%#o\n"
      "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
      TT.id,
      ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,
      ipcperm.mode, (ipcperm.mode & 0777),
      (long) buf.shm_segsz, buf.shm_lpid, buf.shm_cpid,
      (long) buf.shm_nattch);
  printf("att_time=%-26.24s\n",
      buf.shm_atime ? ctime(&buf.shm_atime) : "Not set");
  printf("det_time=%-26.24s\n",
      buf.shm_dtime ? ctime(&buf.shm_dtime) : "Not set");
  printf("change_time=%-26.24s\n\n", ctime(&buf.shm_ctime));
#undef ipcperm
}

static void shm_array(void)
{
  struct shm_info shm_buf;
  struct shminfo ipc_buf;
  struct shmid_ds buf;
  int max_nr, i, shmid;
  struct passwd *pw;
  struct group *gr;

  if ((max_nr = shmctl(0, SHM_INFO, (struct shmid_ds*)&shm_buf)) < 0) {
    perror_msg("kernel not configured for shared memory");
    return;
  }

  if (flag(u)) {
    printf("------ Shared Memory Status --------\n");
    printf("segments allocated %d\n"
        "pages allocated %ld\n"
        "pages resident  %ld\n"
        "pages swapped   %ld\n"
        "Swap performance: %ld attempts\t%ld successes\n",
        shm_buf.used_ids,
        shm_buf.shm_tot,
        shm_buf.shm_rss,
        shm_buf.shm_swp,
        shm_buf.swap_attempts, shm_buf.swap_successes);
    return;
  }
  if (flag(l)) {
    if ((shmctl(0, 3, (struct shmid_ds*)&ipc_buf)) < 0) return; //IPC_INFO
    printf("------ Shared Memory Limits --------\n");
    printf("max number of segments = %lu\n"
        "max seg size (kbytes) = %lu\n"
        "max total shared memory (pages) = %lu\n"
        "min seg size (bytes) = %lu\n",
        (unsigned long) ipc_buf.shmmni,
        (unsigned long) (ipc_buf.shmmax >> 10),
        (unsigned long) ipc_buf.shmall,
        (unsigned long) ipc_buf.shmmin);
    return;
  }

  if (flag(t)) {
    printf("------ Shared Memory Attach/Detach/Change Times --------\n");
    printf("%-10s %-10s %-20s %-20s %-20s\n",
        "shmid", "owner", "attached", "detached", "changed");
  } else if (flag(p)) {
    printf("------ Shared Memory Creator/Last-op --------\n");
    printf("%-10s %-10s %-10s %-10s\n",
        "shmid", "owner", "cpid", "lpid");
  } else if (flag(c)) {
    printf("------ Shared Memory Segment Creators/Owners --------\n");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
        "shmid", "perms", "cuid", "cgid", "uid", "gid");
  } else {
    printf("------ Shared Memory Segments --------\n");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
        "key", "shmid", "owner", "perms", "bytes", "nattch",
        "status");
  }

  for (i = 0; i <= max_nr; i++) {
    if ((shmid = shmctl(i, SHM_STAT, &buf)) < 0 ) continue;
    if (flag(t)) {
      if ((pw = getpwuid(buf.shm_perm.uid)))
        printf("%-10d %-10.10s", shmid, pw->pw_name);
      else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
      printf(" %-20.16s", buf.shm_atime
          ? ctime(&buf.shm_atime) + 4 : "Not set");
      printf(" %-20.16s", buf.shm_dtime
          ? ctime(&buf.shm_dtime) + 4 : "Not set");
      printf(" %-20.16s\n", buf.shm_ctime
          ? ctime(&buf.shm_ctime) + 4 : "Not set");
    } else if (flag(p)) {
      if ((pw = getpwuid(buf.shm_perm.uid)))
        printf("%-10d %-10.10s", shmid, pw->pw_name);
      else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
      printf(" %-10d %-10d\n", buf.shm_cpid, buf.shm_lpid);
    } else if (flag(c)) {
      printf("%-10d %-10o", shmid, buf.shm_perm.mode & 0777);
      if ((pw = getpwuid(buf.shm_perm.cuid))) printf(" %-10s", pw->pw_name);
      else printf(" %-10d", buf.shm_perm.cuid);
      if ((gr = getgrgid(buf.shm_perm.cgid))) printf(" %-10s", gr->gr_name);
      else printf(" %-10d", buf.shm_perm.cgid);
      if ((pw = getpwuid(buf.shm_perm.uid))) printf(" %-10s", pw->pw_name);
      else printf(" %-10d", buf.shm_perm.uid);
      if ((gr = getgrgid(buf.shm_perm.gid))) printf(" %-10s\n", gr->gr_name);
      else printf(" %-10d\n", buf.shm_perm.gid);
    } else {
      printf("0x%08x ", buf.shm_perm.__key);
      if ((pw = getpwuid(buf.shm_perm.uid)))
        printf("%-10d %-10.10s", shmid, pw->pw_name);
      else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
      printf(" %-10o %-10lu %-10ld %-6s %-6s\n", buf.shm_perm.mode & 0777,
          (unsigned long) buf.shm_segsz,
          (long) buf.shm_nattch,
          buf.shm_perm.mode & SHM_DEST ? "dest" : " ",
          buf.shm_perm.mode & SHM_LOCKED ? "locked" : " ");
    }
  }
}

static void sem_array(void)
{
  struct seminfo info_buf;
  struct semid_ds buf;
  union semun u;
  int max_nr, i,semid;
  struct passwd *pw;
  struct group *gr;

  u.array = (unsigned short *)&info_buf;
  if ((max_nr = semctl(0, 0, SEM_INFO, u)) < 0) {
    perror_msg("kernel is not configured for semaphores");
    return;
  }


  if (flag(u)) {
    printf("------ Semaphore Status --------\n");
    printf("used arrays = %d\n"
        "allocated semaphores = %d\n",
        info_buf.semusz, info_buf.semaem);
    return;
  } 
  if (flag(l)) {
    printf("------ Semaphore Limits --------\n");
    u.array = (unsigned short *)&info_buf;
    if ((semctl(0, 0, 3, u)) < 0) //IPC_INFO
      return;
    printf("max number of arrays = %d\n"
        "max semaphores per array = %d\n"
        "max semaphores system wide = %d\n"
        "max ops per semop call = %d\n"
        "semaphore max value = %d\n",
        info_buf.semmni,
        info_buf.semmsl,
        info_buf.semmns, info_buf.semopm, info_buf.semvmx);
    return;
  }

  if (flag(t)) {
    printf("------ Semaphore Operation/Change Times --------\n");
    printf("%-8s %-10s %-26.24s %-26.24s\n",
        "shmid", "owner", "last-op", "last-changed");
  } else if (flag(c)) {
    printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
        "semid", "perms", "cuid", "cgid", "uid", "gid");

  } else if (flag(p)){
    return;
  } else {
    printf("------ Semaphore %s --------\n", "Arrays");
    printf("%-10s %-10s %-10s %-10s %-10s\n",
        "key", "semid", "owner", "perms", "nsems");
  }

  for (i = 0; i <= max_nr; i++) {
    u.buf = &buf;
    if ((semid = semctl(i, 0, SEM_STAT, u)) < 0) continue;
    pw = getpwuid(buf.sem_perm.uid);
    if (flag(t)) {
      if (pw) printf("%-8d %-10.10s", semid, pw->pw_name);
      else printf("%-8d %-10d", semid, buf.sem_perm.uid);

      printf("  %-26.24s", buf.sem_otime
          ? ctime(&buf.sem_otime) : "Not set");
      printf(" %-26.24s\n", buf.sem_ctime
          ? ctime(&buf.sem_ctime) : "Not set");
    } else if (flag(c)) {
      printf("%-10d %-10o", semid, buf.sem_perm.mode & 0777);
      if ((pw = getpwuid(buf.sem_perm.cuid))) printf(" %-10s", pw->pw_name);
      else printf(" %-10d", buf.sem_perm.cuid);
      if ((gr = getgrgid(buf.sem_perm.cgid))) printf(" %-10s", gr->gr_name);
      else printf(" %-10d", buf.sem_perm.cgid);
      if ((pw = getpwuid(buf.sem_perm.uid))) printf(" %-10s", pw->pw_name);
      else printf(" %-10d", buf.sem_perm.uid);
      if ((gr = getgrgid(buf.sem_perm.gid))) printf(" %-10s\n", gr->gr_name);
      else printf(" %-10d\n", buf.sem_perm.gid);
    } else {
      printf("0x%08x ", buf.sem_perm.__key);
      if (pw) printf("%-10d %-10.9s", semid, pw->pw_name);
      else printf("%-10d %-9d", semid, buf.sem_perm.uid);
      printf(" %-10o %-10ld\n", buf.sem_perm.mode & 0777,
          (long) buf.sem_nsems);
    }
  }
}

static void msg_array(void)
{
  struct msginfo info_buf;
  struct msqid_ds buf;
  int max_nr, i, msqid;
  struct passwd *pw;
  struct group *gr;

  if ((max_nr = msgctl(0, MSG_INFO, (struct msqid_ds*)&info_buf)) < 0) {
    perror_msg("kernel not configured for message queue");
    return;
  }

  if (flag(u)) {
    printf("------ Message%s --------\n", "s: Status");
    printf("allocated queues = %d\n"
        "used headers = %d\n"
        "used space = %d bytes\n",
        info_buf.msgpool, info_buf.msgmap, info_buf.msgtql);
    return;
  }
  if (flag(l)) {
    if ((msgctl(0, 3, (struct msqid_ds*)&info_buf)) < 0) return; //IPC_INFO
    printf("------ Messages: Limits --------\n");
    printf("max queues system wide = %d\n"
        "max size of message (bytes) = %d\n"
        "default max size of queue (bytes) = %d\n",
        info_buf.msgmni, info_buf.msgmax, info_buf.msgmnb);
    return;
  }

  if (flag(t)) {
    printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
    printf("%-8s %-10s %-20s %-20s %-20s\n",
        "msqid", "owner", "send", "recv", "change");
  } else if (flag(p)) {
    printf("------ Message%s --------\n", " Queues PIDs");
    printf("%-10s %-10s %-10s %-10s\n",
        "msqid", "owner", "lspid", "lrpid");
  } else if (flag(c)) {
    printf("------ Message%s --------\n", " Queues: Creators/Owners");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
        "msqid", "perms", "cuid", "cgid", "uid", "gid");
  } else {
    printf("------ Message%s --------\n", " Queues");
    printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
        "key", "msqid", "owner", "perms", "used-bytes", "messages");
  }

  for (i = 0; i <= max_nr; i++) {
    if ((msqid = msgctl(i, MSG_STAT, &buf)) < 0 ) continue;
    pw = getpwuid(buf.msg_perm.uid);
    if (flag(t)) {
      if (pw) printf("%-8d %-10.10s", msqid, pw->pw_name);
      else printf("%-8d %-10d", msqid, buf.msg_perm.uid);
      printf(" %-20.16s", buf.msg_stime
          ? ctime(&buf.msg_stime) + 4 : "Not set");
      printf(" %-20.16s", buf.msg_rtime
          ? ctime(&buf.msg_rtime) + 4 : "Not set");
      printf(" %-20.16s\n", buf.msg_ctime
          ? ctime(&buf.msg_ctime) + 4 : "Not set");
    } else if (flag(p)) {
      if (pw) printf("%-8d %-10.10s", msqid, pw->pw_name);
      else printf("%-8d %-10d", msqid, buf.msg_perm.uid);
      printf("  %5d     %5d\n", buf.msg_lspid, buf.msg_lrpid);
    } else if (flag(c)) {
      printf("%-10d %-10o", msqid, buf.msg_perm.mode & 0777);
      if ((pw = getpwuid(buf.msg_perm.cuid))) printf(" %-10s", pw->pw_name);
      else printf(" %-10d", buf.msg_perm.cuid);
      if ((gr = getgrgid(buf.msg_perm.cgid))) printf(" %-10s", gr->gr_name);
      else printf(" %-10d", buf.msg_perm.cgid);
      if ((pw = getpwuid(buf.msg_perm.uid))) printf(" %-10s", pw->pw_name);
      else printf(" %-10d", buf.msg_perm.uid);
      if ((gr = getgrgid(buf.msg_perm.gid))) printf(" %-10s\n", gr->gr_name);
      else printf(" %-10d\n", buf.msg_perm.gid);
    } else {
      printf("0x%08x ", buf.msg_perm.__key);
      if (pw) printf("%-10d %-10.10s", msqid, pw->pw_name);
      else printf("%-10d %-10d", msqid, buf.msg_perm.uid);
      printf(" %-10o %-12ld %-12ld\n", buf.msg_perm.mode & 0777,
          (long) buf.msg_cbytes, (long) buf.msg_qnum);
    }
  }
}

void ipcs_main(void)
{
  if (flag(i)) {
    if (flag(m)) show_shm_id();
    else if (flag(s)) show_sem_id();
    else if (flag(q)) show_msg_id();
    else help_exit(0);
    return;
  }

  if (!(flag(m) || flag(s) || flag(q)) || flag(a)) toys.optflags |= (FLAG_m|FLAG_s|FLAG_q);

  xputc('\n');
  if (flag(m)) {
    shm_array();
    xputc('\n');
  }
  if (flag(s)) {
    sem_array();
    xputc('\n');
  }
  if (flag(q)) {
    msg_array();
    xputc('\n');
  }
}