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