]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ipc/ipcs.c
Add ipcs, ipcrm, ipck progs from cygutils
[cygwin-apps/cygutils.git] / src / ipc / ipcs.c
1 /* $Id$ */
2
3 /*
4 * ipcs
5 *
6 * Copyright (C) 1997 Philippe CHAPUY
7 * Copyright (C) 1998 Ludovic LANGE
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /*
25 * HISTORY:
26 * --------
27 *
28 * 13/05/1998 : Version 1.00 released
29 * First public release
30 * adress any comments to llange@capgemini.fr
31 * 2003/06/13 : (C. Wilson) Ported to the cygutils package from
32 * the cygipc package.
33 *
34 */
35
36
37 /************************************************************************/
38 /* Philippe Chapuy, le 19/05/97 */
39 /************************************************************************/
40
41 #if HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #include "common.h"
46
47 #if HAVE_SYS_IPC_H
48 # include <sys/ipc.h>
49 # if HAVE_SYS_SEM_H
50 # include <sys/sem.h>
51 # endif
52 # if HAVE_SYS_SHM_H
53 # include <sys/shm.h>
54 # endif
55 # if HAVE_SYS_MSG_H
56 # include <sys/msg.h>
57 # endif
58 #endif
59
60 /* These includes moved to common.h
61 #include <unistd.h>
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <errno.h>
65 #include <getopt.h>
66 */
67
68 /* arg for semctl system calls. */
69 union semun {
70 int val; /* value for SETVAL */
71 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
72 ushort *array; /* array for GETALL & SETALL */
73 struct seminfo *__buf; /* buffer for IPC_INFO */
74 void *__pad;
75 };
76
77 #define LIMITS 1
78 #define STATUS 2
79 #define CREATOR 3
80 #define TIME 4
81 #define PID 5
82
83 void do_sem (char format);
84 void do_shm (char format);
85 void do_msg (char format);
86
87 static char *progname;
88
89 void usage(void)
90 {
91 printf ("usage : %s -asmq -tclup \n", progname);
92 printf ("\t%s [-s -m -q] -i id\n", progname);
93 printf ("\t%s -h for help.\n", progname);
94 return;
95 }
96
97 void help (void)
98 {
99 printf ("%s provides information on ipc facilities for", progname);
100 printf (" which you have read access.\n");
101 printf ("Resource Specification:\n\t-m : shared_mem\n\t-q : messages\n");
102 printf ("\t-s : semaphores\n\t-a : all (default)\n");
103 printf ("Output Format:\n\t-t : time\n\t-p : pid\n\t-c : creator\n");
104 printf ("\t-l : limits\n\t-u : summary\n");
105 printf ("-i id [-s -q -m] : details on resource identified by id\n");
106 usage();
107 return;
108 }
109
110 int main (int argc, char **argv)
111 {
112 int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
113 char format = 0;
114 char options[] = "atcluphsmqi:";
115
116 progname = argv[0];
117 while ((opt = getopt (argc, argv, options)) != EOF) {
118 switch (opt) {
119 case 'i':
120 id = atoi (argv[2]);
121 print = 1;
122 break;
123 case 'a':
124 msg = shm = sem = 1;
125 break;
126 case 'q':
127 msg = 1;
128 break;
129 case 's':
130 sem = 1;
131 break;
132 case 'm':
133 shm = 1;
134 break;
135 case 't':
136 format = TIME;
137 break;
138 case 'c':
139 format = CREATOR;
140 break;
141 case 'p':
142 format = PID;
143 break;
144 case 'l':
145 format = LIMITS;
146 break;
147 case 'u':
148 format = STATUS;
149 break;
150 case 'h':
151 help();
152 exit (0);
153 case '?':
154 usage();
155 exit (0);
156 }
157 }
158
159 if ( !shm && !msg && !sem)
160 msg = sem = shm = 1;
161 printf ("\n");
162 if (shm) {
163 do_shm (0);
164 printf ("\n");
165 }
166 if (sem) {
167 do_sem (0);
168 printf ("\n");
169 }
170 if (msg) {
171 do_msg (0);
172 printf ("\n");
173 }
174 return 0;
175 }
176
177 void do_shm (char format)
178 {
179 int maxid, shmid, id;
180 struct shmid_ds shmseg;
181 struct shm_info shm_info;
182 struct ipc_perm *ipcp = &shmseg.shm_perm;
183
184 maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) &shm_info);
185 if (maxid < 0)
186 {
187 printf ("kernel not configured for shared memory\n");
188 return;
189 }
190
191 printf ("---------- Shared Memory Segments --------\n");
192 printf (" %-10s%-10s%-10s%-10s%-12s\n", "shmid", "key",
193 "bytes","nattch","status");
194
195 for (id = 0; id <= maxid; id++)
196 {
197 shmid = shmctl (id, SHM_STAT, &shmseg);
198 if (shmid < 0)
199 continue;
200
201 printf ("_shm %-10d", shmid);
202 printf ("%-10d%-10d%-10d%-6s%-6s\n",
203 (int)shmseg.shm_perm.key, shmseg.shm_segsz,
204 shmseg.shm_nattch,
205 ipcp->mode & SHM_DEST ? "dest" : " ",
206 ipcp->mode & SHM_LOCKED ? "locked" : " ");
207 }
208 return;
209 }
210
211 void do_sem (char format)
212 {
213 int maxid, semid, id;
214 struct semid_ds semary;
215 struct seminfo seminfo;
216 union semun arg;
217 int nb_curr,i;
218
219 arg.array = (ushort *) &seminfo;
220 maxid = semctl (0, 0, SEM_INFO, arg);
221 if (maxid < 0) {
222 printf ("kernel not configured for semaphore\n");
223 return;
224 }
225
226 printf ("---------- Semaphore Arrays --------\n");
227 printf (" %-10s%-10s%-10s\n",
228 "semid","nsems","key");
229
230 for (id = 0; id <= maxid; id++) {
231 arg.buf = (struct semid_ds *) &semary;
232 semid = semctl (id, 0, SEM_STAT, arg);
233 if (semid < 0)
234 continue;
235 printf ("_sem %-10d", semid);
236 printf ("%-10d%-10d currents: ",
237 semary.sem_nsems, (int) semary.sem_perm.key);
238
239 for (i = 0; i < semary.sem_nsems; i++) {
240 nb_curr = semctl (semid, i, GETVAL, arg);
241 if( nb_curr >= 0 )
242 {
243 printf ("%-2d ", nb_curr);
244 }
245 else
246 {
247 printf ("%-2d,errno=%d ", nb_curr, errno);
248 }
249 }
250 printf ("\n");
251 }
252 return;
253 }
254
255 void do_msg (char format)
256 {
257 int maxid, msqid, id;
258 struct msqid_ds msgque;
259 struct msginfo msginfo;
260
261 maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) &msginfo);
262 if (maxid < 0) {
263 printf ("kernel not configured for message queue\n");
264 return;
265 }
266
267 printf ("---------- Message Queues --------\n");
268 printf (" %-10s%-12s%-12s\n", "msqid",
269 "used-bytes", "messages");
270
271 for (id = 0; id <= maxid; id++) {
272 msqid = msgctl (id, MSG_STAT, &msgque);
273 if (msqid < 0)
274 continue;
275 printf ("_msg %-10d", msqid);
276 printf ("%-12d%-12d\n",
277 msgque.msg_cbytes,
278 msgque.msg_qnum);
279 }
280 return;
281 }
This page took 0.047424 seconds and 5 git commands to generate.