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