1 /* ipcs.c - provide information on ipc facilities
2  *
3  * Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com>
4  *
5  * see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ipcs.html
6 
7 USE_IPCS(NEWTOY(ipcs, "acptulsqmi#", TOYFLAG_USR|TOYFLAG_BIN))
8 
9 config IPCS
10   bool "ipcs"
11   default n
12   help
13     usage: ipcs [[-smq] -i shmid] | [[-asmq] [-tcplu]]
14 
15     -i Show specific resource
16     Resource specification:
17     -a All (default)
18     -m Shared memory segments
19     -q Message queues
20     -s Semaphore arrays
21     Output format:
22     -c Creator
23     -l Limits
24     -p Pid
25     -t Time
26     -u Summary
27 */
28 
29 #define FOR_ipcs
30 #include "toys.h"
31 #include <sys/ipc.h>
32 #include <sys/shm.h>
33 #include <sys/sem.h>
34 #include <sys/msg.h>
35 
36 GLOBALS(
37   int id;
38 )
39 
40 //used many times, so good to paste it
41 #define flag(x) (toys.optflags & FLAG_ ## x)
42 
43 union semun { //man says declare it yourself
44   int              val;
45   struct semid_ds *buf;
46   unsigned short  *array;
47   struct seminfo  *__buf;
48 };
49 
show_msg_id(void)50 static void show_msg_id(void)
51 {
52   struct msqid_ds buf;
53   int ret;
54 
55   if ((ret = msgctl(TT.id, IPC_STAT, &buf)) < 0) {
56     perror_msg("msgctl");
57     return;
58   }
59 
60 #define ipcperm buf.msg_perm
61 
62   printf("\nMessage Queue msqid=%d\n"
63       "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n",
64       TT.id, ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,ipcperm.mode);
65   printf ("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
66       (long) buf.msg_cbytes, (long) buf.msg_qbytes,
67       (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
68 
69   printf("send_time=%-26.24s\nrcv_time=%-26.24s\nchange_time=%-26.24s\n\n",
70       buf.msg_stime ? ctime(&buf.msg_stime) : "Not set",
71       buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set",
72       buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
73 #undef ipcperm
74 }
75 
show_sem_id(void)76 static void show_sem_id(void)
77 {
78   struct semid_ds buf;
79   union semun n;
80   int ret, i;
81 
82   n.buf = &buf;
83   if ((ret = semctl(TT.id, 0, IPC_STAT, n)) < 0) {
84     perror_msg("semctl");
85     return;
86   }
87 
88 #define ipcperm buf.sem_perm
89   printf("\nSemaphore Array semid=%d\n"
90       "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
91       "mode=%#o, access_perms=%#o\n"
92       "nsems = %ld\n"
93       "otime = %-26.24s\n",
94       TT.id,
95       ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,
96       ipcperm.mode, ipcperm.mode & 0777,
97       (long) buf.sem_nsems,
98       buf.sem_otime ? ctime(&buf.sem_otime) : "Not set");
99   printf("ctime = %-26.24s\n"
100       "%-10s %-10s %-10s %-10s %-10s\n",
101       ctime(&buf.sem_ctime),
102       "semnum", "value", "ncount", "zcount", "pid");
103 #undef ipcperm
104 
105   for (i = 0; i < buf.sem_nsems; i++) {
106     int val, nc, zc, pid;
107     val = semctl(TT.id, i, GETVAL, n);
108     nc = semctl(TT.id, i, GETNCNT, n);
109     zc = semctl(TT.id, i, GETZCNT, n);
110     pid = semctl(TT.id, i, GETPID, n);
111     if (val < 0 || nc < 0 || zc < 0 || pid < 0)
112       perror_exit("semctl");
113     printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, nc, zc, pid);
114   }
115   xputc('\n');
116 }
117 
show_shm_id(void)118 static void show_shm_id(void)
119 {
120   struct shmid_ds buf;
121   int ret;
122 
123   if ((ret = shmctl(TT.id, IPC_STAT, &buf)) < 0) {
124     perror_msg("shmctl");
125     return;
126   }
127 
128 #define ipcperm buf.shm_perm
129 
130   printf("\nShared memory Segment shmid=%d\n"
131       "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
132       "mode=%#o\taccess_perms=%#o\n"
133       "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
134       TT.id,
135       ipcperm.uid, ipcperm.gid, ipcperm.cuid, ipcperm.cgid,
136       ipcperm.mode, (ipcperm.mode & 0777),
137       (long) buf.shm_segsz, buf.shm_lpid, buf.shm_cpid,
138       (long) buf.shm_nattch);
139   printf("att_time=%-26.24s\n",
140       buf.shm_atime ? ctime(&buf.shm_atime) : "Not set");
141   printf("det_time=%-26.24s\n",
142       buf.shm_dtime ? ctime(&buf.shm_dtime) : "Not set");
143   printf("change_time=%-26.24s\n\n", ctime(&buf.shm_ctime));
144 #undef ipcperm
145 }
146 
shm_array(void)147 static void shm_array(void)
148 {
149   struct shm_info shm_buf;
150   struct shminfo ipc_buf;
151   struct shmid_ds buf;
152   int max_nr, i, shmid;
153   struct passwd *pw;
154   struct group *gr;
155 
156   if ((max_nr = shmctl(0, SHM_INFO, (struct shmid_ds*)&shm_buf)) < 0) {
157     perror_msg("kernel not configured for shared memory");
158     return;
159   }
160 
161   if (flag(u)) {
162     printf("------ Shared Memory Status --------\n");
163     printf("segments allocated %d\n"
164         "pages allocated %ld\n"
165         "pages resident  %ld\n"
166         "pages swapped   %ld\n"
167         "Swap performance: %ld attempts\t%ld successes\n",
168         shm_buf.used_ids,
169         shm_buf.shm_tot,
170         shm_buf.shm_rss,
171         shm_buf.shm_swp,
172         shm_buf.swap_attempts, shm_buf.swap_successes);
173     return;
174   }
175   if (flag(l)) {
176     if ((shmctl(0, 3, (struct shmid_ds*)&ipc_buf)) < 0) return; //IPC_INFO
177     printf("------ Shared Memory Limits --------\n");
178     printf("max number of segments = %lu\n"
179         "max seg size (kbytes) = %lu\n"
180         "max total shared memory (pages) = %lu\n"
181         "min seg size (bytes) = %lu\n",
182         (unsigned long) ipc_buf.shmmni,
183         (unsigned long) (ipc_buf.shmmax >> 10),
184         (unsigned long) ipc_buf.shmall,
185         (unsigned long) ipc_buf.shmmin);
186     return;
187   }
188 
189   if (flag(t)) {
190     printf("------ Shared Memory Attach/Detach/Change Times --------\n");
191     printf("%-10s %-10s %-20s %-20s %-20s\n",
192         "shmid", "owner", "attached", "detached", "changed");
193   } else if (flag(p)) {
194     printf("------ Shared Memory Creator/Last-op --------\n");
195     printf("%-10s %-10s %-10s %-10s\n",
196         "shmid", "owner", "cpid", "lpid");
197   } else if (flag(c)) {
198     printf("------ Shared Memory Segment Creators/Owners --------\n");
199     printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
200         "shmid", "perms", "cuid", "cgid", "uid", "gid");
201   } else {
202     printf("------ Shared Memory Segments --------\n");
203     printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
204         "key", "shmid", "owner", "perms", "bytes", "nattch",
205         "status");
206   }
207 
208   for (i = 0; i <= max_nr; i++) {
209     if ((shmid = shmctl(i, SHM_STAT, &buf)) < 0 ) continue;
210     if (flag(t)) {
211       if ((pw = getpwuid(buf.shm_perm.uid)))
212         printf("%-10d %-10.10s", shmid, pw->pw_name);
213       else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
214       printf(" %-20.16s", buf.shm_atime
215           ? ctime(&buf.shm_atime) + 4 : "Not set");
216       printf(" %-20.16s", buf.shm_dtime
217           ? ctime(&buf.shm_dtime) + 4 : "Not set");
218       printf(" %-20.16s\n", buf.shm_ctime
219           ? ctime(&buf.shm_ctime) + 4 : "Not set");
220     } else if (flag(p)) {
221       if ((pw = getpwuid(buf.shm_perm.uid)))
222         printf("%-10d %-10.10s", shmid, pw->pw_name);
223       else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
224       printf(" %-10d %-10d\n", buf.shm_cpid, buf.shm_lpid);
225     } else if (flag(c)) {
226       printf("%-10d %-10o", shmid, buf.shm_perm.mode & 0777);
227       if ((pw = getpwuid(buf.shm_perm.cuid))) printf(" %-10s", pw->pw_name);
228       else printf(" %-10d", buf.shm_perm.cuid);
229       if ((gr = getgrgid(buf.shm_perm.cgid))) printf(" %-10s", gr->gr_name);
230       else printf(" %-10d", buf.shm_perm.cgid);
231       if ((pw = getpwuid(buf.shm_perm.uid))) printf(" %-10s", pw->pw_name);
232       else printf(" %-10d", buf.shm_perm.uid);
233       if ((gr = getgrgid(buf.shm_perm.gid))) printf(" %-10s\n", gr->gr_name);
234       else printf(" %-10d\n", buf.shm_perm.gid);
235     } else {
236       printf("0x%08x ", buf.shm_perm.__key);
237       if ((pw = getpwuid(buf.shm_perm.uid)))
238         printf("%-10d %-10.10s", shmid, pw->pw_name);
239       else printf("%-10d %-10.10d", shmid, buf.shm_perm.uid);
240       printf(" %-10o %-10lu %-10ld %-6s %-6s\n", buf.shm_perm.mode & 0777,
241           (unsigned long) buf.shm_segsz,
242           (long) buf.shm_nattch,
243           buf.shm_perm.mode & SHM_DEST ? "dest" : " ",
244           buf.shm_perm.mode & SHM_LOCKED ? "locked" : " ");
245     }
246   }
247 }
248 
sem_array(void)249 static void sem_array(void)
250 {
251   struct seminfo info_buf;
252   struct semid_ds buf;
253   union semun u;
254   int max_nr, i,semid;
255   struct passwd *pw;
256   struct group *gr;
257 
258   u.array = (unsigned short *)&info_buf;
259   if ((max_nr = semctl(0, 0, SEM_INFO, u)) < 0) {
260     perror_msg("kernel is not configured for semaphores");
261     return;
262   }
263 
264 
265   if (flag(u)) {
266     printf("------ Semaphore Status --------\n");
267     printf("used arrays = %d\n"
268         "allocated semaphores = %d\n",
269         info_buf.semusz, info_buf.semaem);
270     return;
271   }
272   if (flag(l)) {
273     printf("------ Semaphore Limits --------\n");
274     u.array = (unsigned short *)&info_buf;
275     if ((semctl(0, 0, 3, u)) < 0) //IPC_INFO
276       return;
277     printf("max number of arrays = %d\n"
278         "max semaphores per array = %d\n"
279         "max semaphores system wide = %d\n"
280         "max ops per semop call = %d\n"
281         "semaphore max value = %d\n",
282         info_buf.semmni,
283         info_buf.semmsl,
284         info_buf.semmns, info_buf.semopm, info_buf.semvmx);
285     return;
286   }
287 
288   if (flag(t)) {
289     printf("------ Semaphore Operation/Change Times --------\n");
290     printf("%-8s %-10s %-26.24s %-26.24s\n",
291         "shmid", "owner", "last-op", "last-changed");
292   } else if (flag(c)) {
293     printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
294     printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
295         "semid", "perms", "cuid", "cgid", "uid", "gid");
296 
297   } else if (flag(p)){
298     return;
299   } else {
300     printf("------ Semaphore %s --------\n", "Arrays");
301     printf("%-10s %-10s %-10s %-10s %-10s\n",
302         "key", "semid", "owner", "perms", "nsems");
303   }
304 
305   for (i = 0; i <= max_nr; i++) {
306     u.buf = &buf;
307     if ((semid = semctl(i, 0, SEM_STAT, u)) < 0) continue;
308     pw = getpwuid(buf.sem_perm.uid);
309     if (flag(t)) {
310       if (pw) printf("%-8d %-10.10s", semid, pw->pw_name);
311       else printf("%-8d %-10d", semid, buf.sem_perm.uid);
312 
313       printf("  %-26.24s", buf.sem_otime
314           ? ctime(&buf.sem_otime) : "Not set");
315       printf(" %-26.24s\n", buf.sem_ctime
316           ? ctime(&buf.sem_ctime) : "Not set");
317     } else if (flag(c)) {
318       printf("%-10d %-10o", semid, buf.sem_perm.mode & 0777);
319       if ((pw = getpwuid(buf.sem_perm.cuid))) printf(" %-10s", pw->pw_name);
320       else printf(" %-10d", buf.sem_perm.cuid);
321       if ((gr = getgrgid(buf.sem_perm.cgid))) printf(" %-10s", gr->gr_name);
322       else printf(" %-10d", buf.sem_perm.cgid);
323       if ((pw = getpwuid(buf.sem_perm.uid))) printf(" %-10s", pw->pw_name);
324       else printf(" %-10d", buf.sem_perm.uid);
325       if ((gr = getgrgid(buf.sem_perm.gid))) printf(" %-10s\n", gr->gr_name);
326       else printf(" %-10d\n", buf.sem_perm.gid);
327     } else {
328       printf("0x%08x ", buf.sem_perm.__key);
329       if (pw) printf("%-10d %-10.9s", semid, pw->pw_name);
330       else printf("%-10d %-9d", semid, buf.sem_perm.uid);
331       printf(" %-10o %-10ld\n", buf.sem_perm.mode & 0777,
332           (long) buf.sem_nsems);
333     }
334   }
335 }
336 
msg_array(void)337 static void msg_array(void)
338 {
339   struct msginfo info_buf;
340   struct msqid_ds buf;
341   int max_nr, i, msqid;
342   struct passwd *pw;
343   struct group *gr;
344 
345   if ((max_nr = msgctl(0, MSG_INFO, (struct msqid_ds*)&info_buf)) < 0) {
346     perror_msg("kernel not configured for message queue");
347     return;
348   }
349 
350   if (flag(u)) {
351     printf("------ Message%s --------\n", "s: Status");
352     printf("allocated queues = %d\n"
353         "used headers = %d\n"
354         "used space = %d bytes\n",
355         info_buf.msgpool, info_buf.msgmap, info_buf.msgtql);
356     return;
357   }
358   if (flag(l)) {
359     if ((msgctl(0, 3, (struct msqid_ds*)&info_buf)) < 0) return; //IPC_INFO
360     printf("------ Messages: Limits --------\n");
361     printf("max queues system wide = %d\n"
362         "max size of message (bytes) = %d\n"
363         "default max size of queue (bytes) = %d\n",
364         info_buf.msgmni, info_buf.msgmax, info_buf.msgmnb);
365     return;
366   }
367 
368   if (flag(t)) {
369     printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
370     printf("%-8s %-10s %-20s %-20s %-20s\n",
371         "msqid", "owner", "send", "recv", "change");
372   } else if (flag(p)) {
373     printf("------ Message%s --------\n", " Queues PIDs");
374     printf("%-10s %-10s %-10s %-10s\n",
375         "msqid", "owner", "lspid", "lrpid");
376   } else if (flag(c)) {
377     printf("------ Message%s --------\n", " Queues: Creators/Owners");
378     printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
379         "msqid", "perms", "cuid", "cgid", "uid", "gid");
380   } else {
381     printf("------ Message%s --------\n", " Queues");
382     printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
383         "key", "msqid", "owner", "perms", "used-bytes", "messages");
384   }
385 
386   for (i = 0; i <= max_nr; i++) {
387     if ((msqid = msgctl(i, MSG_STAT, &buf)) < 0 ) continue;
388     pw = getpwuid(buf.msg_perm.uid);
389     if (flag(t)) {
390       if (pw) printf("%-8d %-10.10s", msqid, pw->pw_name);
391       else printf("%-8d %-10d", msqid, buf.msg_perm.uid);
392       printf(" %-20.16s", buf.msg_stime
393           ? ctime(&buf.msg_stime) + 4 : "Not set");
394       printf(" %-20.16s", buf.msg_rtime
395           ? ctime(&buf.msg_rtime) + 4 : "Not set");
396       printf(" %-20.16s\n", buf.msg_ctime
397           ? ctime(&buf.msg_ctime) + 4 : "Not set");
398     } else if (flag(p)) {
399       if (pw) printf("%-8d %-10.10s", msqid, pw->pw_name);
400       else printf("%-8d %-10d", msqid, buf.msg_perm.uid);
401       printf("  %5d     %5d\n", buf.msg_lspid, buf.msg_lrpid);
402     } else if (flag(c)) {
403       printf("%-10d %-10o", msqid, buf.msg_perm.mode & 0777);
404       if ((pw = getpwuid(buf.msg_perm.cuid))) printf(" %-10s", pw->pw_name);
405       else printf(" %-10d", buf.msg_perm.cuid);
406       if ((gr = getgrgid(buf.msg_perm.cgid))) printf(" %-10s", gr->gr_name);
407       else printf(" %-10d", buf.msg_perm.cgid);
408       if ((pw = getpwuid(buf.msg_perm.uid))) printf(" %-10s", pw->pw_name);
409       else printf(" %-10d", buf.msg_perm.uid);
410       if ((gr = getgrgid(buf.msg_perm.gid))) printf(" %-10s\n", gr->gr_name);
411       else printf(" %-10d\n", buf.msg_perm.gid);
412     } else {
413       printf("0x%08x ", buf.msg_perm.__key);
414       if (pw) printf("%-10d %-10.10s", msqid, pw->pw_name);
415       else printf("%-10d %-10d", msqid, buf.msg_perm.uid);
416       printf(" %-10o %-12ld %-12ld\n", buf.msg_perm.mode & 0777,
417           (long) buf.msg_cbytes, (long) buf.msg_qnum);
418     }
419   }
420 }
421 
ipcs_main(void)422 void ipcs_main(void)
423 {
424   if (flag(i)) {
425     if (flag(m)) show_shm_id();
426     else if (flag(s)) show_sem_id();
427     else if (flag(q)) show_msg_id();
428     else help_exit(0);
429     return;
430   }
431 
432   if (!(flag(m) || flag(s) || flag(q)) || flag(a)) toys.optflags |= (FLAG_m|FLAG_s|FLAG_q);
433 
434   xputc('\n');
435   if (flag(m)) {
436     shm_array();
437     xputc('\n');
438   }
439   if (flag(s)) {
440     sem_array();
441     xputc('\n');
442   }
443   if (flag(q)) {
444     msg_array();
445     xputc('\n');
446   }
447 }
448