ticket-2618-patches-20031207
[openafs.git] / src / ubik / udebug.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
14     ("$Header$");
15
16 #include <sys/types.h>
17 #include <stdlib.h>
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #else
21 #ifdef HAVE_STRINGS_H
22 #include <strings.h>
23 #endif
24 #endif
25 #ifdef AFS_NT40_ENV
26 #include <winsock2.h>
27 #else
28 #include <sys/file.h>
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <signal.h>
37 #include <time.h>
38
39 #include <lock.h>
40 #include <rx/xdr.h>
41 #include <rx/rx.h>
42 #include <afs/cmd.h>
43
44 #define UBIK_INTERNALS
45 #include "ubik.h"
46 #include "ubik_int.h"
47
48 static short
49 PortNumber(register char *aport)
50 {
51     register int tc;
52     register afs_int32 total;
53
54     total = 0;
55     while ((tc = *aport++)) {
56         if (tc < '0' || tc > '9')
57             return -1;          /* bad port number */
58         total *= 10;
59         total += tc - (int)'0';
60     }
61     return (total);
62 }
63
64 static short
65 PortName(char *aname)
66 {
67     struct servent *ts;
68     int len;
69
70     ts = getservbyname(aname, NULL);
71
72     if (ts)
73         return ntohs(ts->s_port);       /* returns it in host byte order */
74
75     len = strlen(aname);
76     if (strncmp(aname, "vlserver", len) == 0) {
77         return 7003;
78     } else if (strncmp(aname, "ptserver", len) == 0) {
79         return 7002;
80     } else if (strncmp(aname, "kaserver", len) == 0) {
81         return 7004;
82     } else if (strncmp(aname, "buserver", len) == 0) {
83         return 7021;
84     }
85     return (-1);
86 }
87
88 static int
89 CommandProc(struct cmd_syndesc *as, char *arock)
90 {
91     char *hostName, *portName, *times;
92     afs_int32 hostAddr;
93     struct in_addr inhostAddr;
94     register afs_int32 i, j, code;
95     short port;
96     int int32p;
97     afs_int32 now, diff, newtime;
98     struct hostent *th;
99     struct rx_connection *tconn;
100     struct rx_securityClass *sc;
101     struct ubik_debug udebug;
102     struct ubik_sdebug usdebug;
103     int oldServer = 0;          /* are we talking to a pre 3.5 server? */
104     afs_int32 isClone = 0;
105
106     int32p = (as->parms[2].items ? 1 : 0);
107
108     if (as->parms[0].items)
109         hostName = as->parms[0].items->data;
110     else
111         hostName = NULL;
112
113     if (as->parms[1].items)
114         portName = as->parms[1].items->data;
115     else
116         portName = NULL;
117
118     /* lookup host */
119     if (hostName) {
120         th = hostutil_GetHostByName(hostName);
121         if (!th) {
122             printf("udebug: host %s not found in host table\n", hostName);
123             exit(1);
124         }
125         memcpy(&hostAddr, th->h_addr, sizeof(afs_int32));
126     } else
127         hostAddr = htonl(0x7f000001);   /* IP localhost */
128
129     if (!portName)
130         port = htons(3000);     /* default */
131     else {
132         port = PortNumber(portName);
133         if (port < 0)
134             port = PortName(portName);
135         if (port < 0) {
136             printf("udebug: can't resolve port name %s\n", portName);
137             exit(1);
138         }
139         port = htons(port);
140     }
141
142     rx_Init(0);
143     sc = rxnull_NewClientSecurityObject();
144     tconn = rx_NewConnection(hostAddr, port, VOTE_SERVICE_ID, sc, 0);
145
146     /* now do the main call */
147     code = VOTE_XDebug(tconn, &udebug, &isClone);
148     if (code)
149         code = VOTE_Debug(tconn, &udebug);
150     if (code == RXGEN_OPCODE) {
151         oldServer = 1;          /* talking to a pre 3.5 server */
152         memset(&udebug, 0, sizeof(udebug));
153         code = VOTE_DebugOld(tconn, &udebug);
154     }
155
156     if (code) {
157         printf("return code %d from VOTE_Debug\n", code);
158         exit(0);
159     }
160     now = time(0);
161
162     /* now print the main info */
163     times = ctime((time_t *) & udebug.now);
164     times[24] = 0;
165     inhostAddr.s_addr = hostAddr;
166     if (!oldServer) {
167         printf("Host's addresses are: ");
168         for (j = 0; udebug.interfaceAddr[j] && (j < UBIK_MAX_INTERFACE_ADDR);
169              j++)
170             printf("%s ", afs_inet_ntoa(htonl(udebug.interfaceAddr[j])));
171         printf("\n");
172     }
173     printf("Host's %s time is %s\n", inet_ntoa(inhostAddr), times);
174
175     times = ctime(&now);
176     times[24] = 0;
177     diff = now - udebug.now;
178     printf("Local time is %s (time differential %d secs)\n", times, diff);
179     if (abs(diff) >= MAXSKEW)
180         printf("****clock may be bad\n");
181
182     /* UBIK skips the voting if 1 server - so we fudge it here */
183     if (udebug.amSyncSite && (udebug.nServers == 1)) {
184         udebug.lastYesHost = hostAddr;
185         udebug.lastYesTime = udebug.now;
186         udebug.lastYesState = 1;
187         udebug.lastYesClaim = udebug.now;
188         udebug.syncVersion.epoch = udebug.localVersion.epoch;
189         udebug.syncVersion.counter = udebug.localVersion.counter;
190     }
191
192     /* sockaddr is always in net-order */
193     if (udebug.lastYesHost == 0xffffffff) {
194         printf("Last yes vote not cast yet \n");
195     } else {
196         inhostAddr.s_addr = htonl(udebug.lastYesHost);
197         diff = udebug.now - udebug.lastYesTime;
198         printf("Last yes vote for %s was %d secs ago (%ssync site); \n",
199                inet_ntoa(inhostAddr), diff,
200                ((udebug.lastYesState) ? "" : "not "));
201
202         diff = udebug.now - udebug.lastYesClaim;
203         newtime = now - diff;
204         times = ctime(&newtime);
205         times[24] = 0;
206         printf("Last vote started %d secs ago (at %s)\n", diff, times);
207     }
208
209     printf("Local db version is %d.%d\n", udebug.localVersion.epoch,
210            udebug.localVersion.counter);
211
212     if (udebug.amSyncSite) {
213         if (udebug.syncSiteUntil == 0x7fffffff) {
214             printf("I am sync site forever (%d server%s)\n", udebug.nServers,
215                    ((udebug.nServers > 1) ? "s" : ""));
216         } else {
217             diff = udebug.syncSiteUntil - udebug.now;
218             newtime = now + diff;
219             times = ctime(&newtime);
220             times[24] = 0;
221             printf
222                 ("I am sync site until %d secs from now (at %s) (%d server%s)\n",
223                  diff, times, udebug.nServers,
224                  ((udebug.nServers > 1) ? "s" : ""));
225         }
226         printf("Recovery state %x\n", udebug.recoveryState);
227         if (udebug.activeWrite) {
228             printf("I am currently managing write trans %d.%d\n",
229                    udebug.epochTime, udebug.tidCounter);
230         }
231     } else {
232         if (isClone)
233             printf("I am a clone and never can become sync site\n");
234         else
235             printf("I am not sync site\n");
236         inhostAddr.s_addr = htonl(udebug.lowestHost);
237         diff = udebug.now - udebug.lowestTime;
238         printf("Lowest host %s was set %d secs ago\n", inet_ntoa(inhostAddr),
239                diff);
240
241         inhostAddr.s_addr = htonl(udebug.syncHost);
242         diff = udebug.now - udebug.syncTime;
243         printf("Sync host %s was set %d secs ago\n", inet_ntoa(inhostAddr),
244                diff);
245     }
246
247     printf("Sync site's db version is %d.%d\n", udebug.syncVersion.epoch,
248            udebug.syncVersion.counter);
249     printf("%d locked pages, %d of them for write\n", udebug.lockedPages,
250            udebug.writeLockedPages);
251
252     if (udebug.anyReadLocks)
253         printf("There are read locks held\n");
254     if (udebug.anyWriteLocks)
255         printf("There are write locks held\n");
256
257     if (udebug.currentTrans) {
258         if (udebug.writeTrans)
259             printf("There is an active write transaction\n");
260         else
261             printf("There is at least one active read transaction\n");
262         printf("Transaction tid is %d.%d\n", udebug.syncTid.epoch,
263                udebug.syncTid.counter);
264     }
265     if (udebug.epochTime) {
266         diff = udebug.now - udebug.epochTime;
267         newtime = now - diff;
268         times = ctime(&newtime);
269         times[24] = 0;
270         printf
271             ("Last time a new db version was labelled was:\n\t %d secs ago (at %s)\n",
272              diff, times);
273     }
274
275     if (int32p || udebug.amSyncSite) {
276         /* now do the subcalls */
277         for (i = 0;; i++) {
278             isClone = 0;
279             code = VOTE_XSDebug(tconn, i, &usdebug, &isClone);
280             if (code < 0) {
281                 if (oldServer) {        /* pre 3.5 server */
282                     memset(&usdebug, 0, sizeof(usdebug));
283                     code = VOTE_SDebugOld(tconn, i, &usdebug);
284                 } else
285                     code = VOTE_SDebug(tconn, i, &usdebug);
286             }
287             if (code > 0)
288                 break;          /* done */
289             if (code < 0) {
290                 printf("error code %d from VOTE_SDebug\n", code);
291                 break;
292             }
293             inhostAddr.s_addr = htonl(usdebug.addr);
294             /* otherwise print the structure */
295             printf("\nServer (%s", afs_inet_ntoa(htonl(usdebug.addr)));
296             for (j = 0;
297                  ((usdebug.altAddr[j]) && (j < UBIK_MAX_INTERFACE_ADDR - 1));
298                  j++)
299                 printf(" %s", afs_inet_ntoa(htonl(usdebug.altAddr[j])));
300             printf("): (db %d.%d)", usdebug.remoteVersion.epoch,
301                    usdebug.remoteVersion.counter);
302             if (isClone)
303                 printf("    is only a clone!");
304             printf("\n");
305
306             if (usdebug.lastVoteTime == 0) {
307                 printf("    last vote never rcvd \n");
308             } else {
309                 diff = udebug.now - usdebug.lastVoteTime;
310                 newtime = now - diff;
311                 times = ctime(&newtime);
312                 times[24] = 0;
313                 printf("    last vote rcvd %d secs ago (at %s),\n", diff,
314                        times);
315             }
316
317             if (usdebug.lastBeaconSent == 0) {
318                 printf("    last beacon never sent \n");
319             } else {
320                 diff = udebug.now - usdebug.lastBeaconSent;
321                 newtime = now - diff;
322                 times = ctime(&newtime);
323                 times[24] = 0;
324                 printf
325                     ("    last beacon sent %d secs ago (at %s), last vote was %s\n",
326                      diff, times, ((usdebug.lastVote) ? "yes" : "no"));
327             }
328
329             printf("    dbcurrent=%d, up=%d beaconSince=%d\n",
330                    usdebug.currentDB, usdebug.up, usdebug.beaconSinceDown);
331         }
332     }
333     return (0);
334 }
335
336 #include "AFS_component_version_number.c"
337
338 int
339 main(int argc, char **argv)
340 {
341     struct cmd_syndesc *ts;
342
343 #ifdef  AFS_AIX32_ENV
344     /*
345      * The following signal action for AIX is necessary so that in case of a 
346      * crash (i.e. core is generated) we can include the user's data section 
347      * in the core dump. Unfortunately, by default, only a partial core is
348      * generated which, in many cases, isn't too useful.
349      */
350     struct sigaction nsa;
351
352     sigemptyset(&nsa.sa_mask);
353     nsa.sa_handler = SIG_DFL;
354     nsa.sa_flags = SA_FULLDUMP;
355     sigaction(SIGSEGV, &nsa, NULL);
356 #endif
357     ts = cmd_CreateSyntax(NULL, CommandProc, 0, "probe ubik server");
358     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "server machine");
359     cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "IP port");
360     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "print all info");
361
362     cmd_Dispatch(argc, argv);
363     exit(0);
364 }