pull-prototypes-to-head-20020821
[openafs.git] / src / venus / cmdebug.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15
16 #include <sys/types.h>
17 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <netdb.h>
20 #include <stdio.h>
21 #ifdef HAVE_STRING_H
22 #include <string.h>
23 #else
24 #ifdef HAVE_STRINGS_H
25 #include <strings.h>
26 #endif
27 #endif
28 #ifdef  AFS_AIX32_ENV
29 #include <signal.h>
30 #endif
31 #include <afs/afscbint.h>
32 #include <afs/cmd.h>
33 #include <rx/rx.h>
34 #include <lock.h>
35 #include <afs/afs_args.h>
36
37 extern struct hostent *hostutil_GetHostByName();
38
39 static PrintCacheConfig(aconn)
40     struct rx_connection *aconn;
41 {
42     struct cacheConfig c;
43     afs_uint32 srv_ver, conflen;
44     int code;
45
46     c.cacheConfig_len = 0;
47     c.cacheConfig_val = NULL;
48     code = RXAFSCB_GetCacheConfig(aconn, 1, &srv_ver, &conflen, &c);
49     if (code) {
50         printf("cmdebug: error checking cache config: %s\n",
51                error_message(code));
52         return 0;
53     }
54
55     if (srv_ver == AFS_CLIENT_RETRIEVAL_FIRST_EDITION) {
56         struct cm_initparams_v1 *c1;
57
58         if (c.cacheConfig_len != sizeof(*c1) / sizeof(afs_uint32)) {
59             printf("cmdebug: configuration data size mismatch (%d != %d)\n",
60                    c.cacheConfig_len, sizeof(*c1) / sizeof(afs_uint32));
61             return 0;
62         }
63
64         c1 = (struct cm_initparams_v1 *) c.cacheConfig_val;
65         printf("Chunk files:   %d\n", c1->nChunkFiles);
66         printf("Stat caches:   %d\n", c1->nStatCaches);
67         printf("Data caches:   %d\n", c1->nDataCaches);
68         printf("Volume caches: %d\n", c1->nVolumeCaches);
69         printf("Chunk size:    %d", c1->otherChunkSize);
70         if (c1->firstChunkSize != c1->otherChunkSize)
71             printf(" (first: %d)", c1->firstChunkSize);
72         printf("\n");
73         printf("Cache size:    %d kB\n", c1->cacheSize);
74         printf("Set time:      %s\n", c1->setTime ? "yes" : "no");
75         printf("Cache type:    %s\n", c1->memCache ? "memory" : "disk");
76     } else {
77         printf("cmdebug: unsupported server version %d\n", srv_ver);
78     }
79 }
80
81 static PrintInterfaces(aconn)
82     struct rx_connection *aconn;
83 {
84     struct interfaceAddr addr;
85     int i, code;
86
87     code = RXAFSCB_WhoAreYou(aconn, &addr);
88     if (code) {
89         printf("cmdebug: error checking interfaces: %s\n", error_message(code));
90         return 0;
91     }
92
93     printf("Host interfaces:\n");
94     for (i=0; i<addr.numberOfInterfaces; i++) {
95         printf("%s", inet_ntoa(&addr.addr_in[i]));
96         if (addr.subnetmask[i])
97             printf(", netmask %s", inet_ntoa(&addr.subnetmask[i]));
98         if (addr.mtu[i])
99             printf(", MTU %d", addr.mtu[i]);
100         printf("\n");
101     }
102
103     return 0;
104 }
105
106 static IsLocked(alock)
107 register struct AFSDBLockDesc *alock; {
108     if (alock->waitStates || alock->exclLocked
109         || alock->numWaiting || alock->readersReading)
110         return 1;
111     return 0;
112 }
113
114 static PrintLock(alock)
115 register struct AFSDBLockDesc *alock; {
116     printf("(");
117     if (alock->waitStates) {
118         if (alock->waitStates & READ_LOCK)
119             printf("reader_waiting");
120         if (alock->waitStates & WRITE_LOCK)
121             printf("writer_waiting");
122         if (alock->waitStates & SHARED_LOCK)
123             printf("upgrade_waiting");
124     }
125     else
126         printf("none_waiting");
127     if (alock->exclLocked) {
128         if (alock->exclLocked & WRITE_LOCK)
129             printf(", write_locked");
130         if (alock->exclLocked & SHARED_LOCK)
131             printf(", upgrade_locked");
132         printf("(pid:%d at:%d)", alock->pid_writer, alock->src_indicator);
133     }
134     if (alock->readersReading)
135         printf(", %d read_locks(pid:%d)", alock->readersReading,alock->pid_last_reader);
136     if (alock->numWaiting) printf(", %d waiters", alock->numWaiting);
137     printf(")");
138     return 0;
139 }
140
141 static PrintLocks(aconn, aint32)
142 int aint32;
143 register struct rx_connection *aconn; {
144     register int i;
145     struct AFSDBLock lock;
146     afs_int32 code;
147
148     for(i=0;i<1000;i++) {
149         code = RXAFSCB_GetLock(aconn, i, &lock);
150         if (code) {
151             if (code == 1) break;
152             /* otherwise we have an unrecognized error */
153             printf("cmdebug: error checking locks: %s\n", error_message(code));
154             return code;
155         }
156         /* here we have the lock information, so display it, perhaps */
157         if (aint32 || IsLocked(&lock.lock)) {
158             printf("Lock %s status: ", lock.name);
159             PrintLock(&lock.lock);
160             printf("\n");
161         }
162     }
163     return 0;
164 }
165
166 static PrintCacheEntries(aconn, aint32)
167 int aint32;
168 register struct rx_connection *aconn; {
169     register int i;
170     register afs_int32 code;
171     struct AFSDBCacheEntry centry;
172
173     for(i=0;i<10000;i++) {
174         code = RXAFSCB_GetCE(aconn, i, &centry);
175         if (code) {
176             if (code == 1) break;
177             printf("cmdebug: failed to get cache entry %d (%s)\n", i,
178                    error_message(code));
179             return code;
180         }
181
182         if (centry.addr == 0) {
183             /* PS output */
184             printf("Proc %4d sleeping at %08x, pri %3d\n",
185                    centry.netFid.Vnode, centry.netFid.Volume, centry.netFid.Unique-25);
186             continue;
187         }
188
189         if (!aint32 && !IsLocked(&centry.lock)) continue;
190
191         /* otherwise print this entry */
192         printf("** Cache entry @ 0x%08x for %d.%d.%d.%d\n", centry.addr, centry.cell,
193                centry.netFid.Volume, centry.netFid.Vnode, centry.netFid.Unique);
194         if (IsLocked(&centry.lock)) {
195             printf("    locks: ");
196             PrintLock(&centry.lock);
197             printf("\n");
198         }
199         printf("    %d bytes\tDV %d refcnt %d\n", centry.Length, centry.DataVersion, centry.refCount);
200         printf("    callback %08x\texpires %u\n", centry.callback, centry.cbExpires);
201         printf("    %d opens\t%d writers\n", centry.opens, centry.writers);
202
203         /* now display states */
204         printf("    ");
205         if (centry.mvstat == 0) printf("normal file");
206         else if (centry.mvstat == 1) printf("mount point");
207         else if (centry.mvstat == 2) printf("volume root");
208         else printf("bogus mvstat %d", centry.mvstat);
209         printf("\n    states (0x%x)", centry.states);
210         if (centry.states & 1) printf(", stat'd");
211         if (centry.states & 2) printf(", backup");
212         if (centry.states & 4) printf(", read-only");
213         if (centry.states & 8) printf(", mt pt valid");
214         if (centry.states & 0x10) printf(", pending core");
215         if (centry.states & 0x40) printf(", wait-for-store");
216         if (centry.states & 0x80) printf(", mapped");
217         printf("\n");
218     }
219     return 0;
220 }
221
222 static CommandProc(as)
223 struct cmd_syndesc *as; {
224     struct rx_connection *conn;
225     register char *hostName;
226     register struct hostent *thp;
227     afs_int32 port;
228     struct rx_securityClass *secobj;
229     int int32p;
230     afs_int32 addr;
231
232     hostName = as->parms[0].items->data;
233     if (as->parms[1].items)
234         port = atoi(as->parms[1].items->data);
235     else
236         port = 7001;
237     thp = hostutil_GetHostByName(hostName);
238     if (!thp) {
239         printf("cmdebug: can't resolve address for host %s.\n", hostName);
240         exit(1);
241     }
242     memcpy(&addr, thp->h_addr, sizeof(afs_int32));
243     secobj = rxnull_NewServerSecurityObject();
244     conn = rx_NewConnection(addr, htons(port), 1, secobj, 0);
245     if (!conn) {
246         printf("cmdebug: failed to create connection for host %s\n", hostName);
247         exit(1);
248     }
249     if (as->parms[3].items) {
250         /* -addrs */
251         PrintInterfaces(conn);
252         return 0;
253     }
254     if (as->parms[4].items) {
255         /* -cache */
256         PrintCacheConfig(conn);
257         return 0;
258     }
259     if (as->parms[2].items) int32p = 1;
260     else int32p = 0;
261     PrintLocks(conn, int32p);
262     PrintCacheEntries(conn, int32p);
263     return 0;
264 }
265
266 #include "AFS_component_version_number.c"
267
268 main(argc, argv)
269 int argc;
270 char **argv; {
271     register struct cmd_syndesc *ts;
272
273 #ifdef  AFS_AIX32_ENV
274     /*
275      * The following signal action for AIX is necessary so that in case of a 
276      * crash (i.e. core is generated) we can include the user's data section 
277      * in the core dump. Unfortunately, by default, only a partial core is
278      * generated which, in many cases, isn't too useful.
279      */
280     struct sigaction nsa;
281     
282     sigemptyset(&nsa.sa_mask);
283     nsa.sa_handler = SIG_DFL;
284     nsa.sa_flags = SA_FULLDUMP;
285     sigaction(SIGSEGV, &nsa, NULL);
286 #endif
287     rx_Init(0);
288
289     ts = cmd_CreateSyntax(NULL, CommandProc, 0, "probe unik server");
290     cmd_AddParm(ts, "-servers", CMD_SINGLE, CMD_REQUIRED, "server machine");
291     cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "IP port");
292     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "print all info");
293     cmd_AddParm(ts, "-addrs", CMD_FLAG, CMD_OPTIONAL, "print only host interfaces");
294     cmd_AddParm(ts, "-cache", CMD_FLAG, CMD_OPTIONAL, "print only cache configuration");
295
296     cmd_Dispatch(argc, argv);
297     exit(0);
298 }