DEVEL15-openafs-string-header-cleanup-20071030
[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 #include <string.h>
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #else
22 #include <sys/file.h>
23 #include <sys/param.h>
24 #include <netinet/in.h>
25 #include <netdb.h>
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <signal.h>
31 #include <afs/afsutil.h>
32 #include <time.h>
33
34 #include <lock.h>
35 #include <rx/xdr.h>
36 #include <rx/rx.h>
37 #include <afs/cmd.h>
38
39 #define UBIK_INTERNALS
40 #include "ubik.h"
41 #include "ubik_int.h"
42
43 /* needed by Irix. Include a header to get it, or leave it alone. */
44 extern struct hostent *hostutil_GetHostByName();
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, char *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, &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, diff);
176     if (abs(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(htonl(udebug.lastYesHost)), diff,
196                ((udebug.lastYesState) ? "" : "not "));
197
198         diff = udebug.now - udebug.lastYesClaim;
199         newtime = now - diff;
200         times = ctime(&newtime);
201         times[24] = 0;
202         printf("Last vote started %d secs ago (at %s)\n", diff, times);
203     }
204
205     printf("Local db version is %d.%d\n", udebug.localVersion.epoch,
206            udebug.localVersion.counter);
207
208     if (udebug.amSyncSite) {
209         if (udebug.syncSiteUntil == 0x7fffffff) {
210             printf("I am sync site forever (%d server%s)\n", udebug.nServers,
211                    ((udebug.nServers > 1) ? "s" : ""));
212         } else {
213             diff = udebug.syncSiteUntil - udebug.now;
214             newtime = now + diff;
215             times = ctime(&newtime);
216             times[24] = 0;
217             printf
218                 ("I am sync site until %d secs from now (at %s) (%d server%s)\n",
219                  diff, times, udebug.nServers,
220                  ((udebug.nServers > 1) ? "s" : ""));
221         }
222         printf("Recovery state %x\n", udebug.recoveryState);
223         if (udebug.activeWrite) {
224             printf("I am currently managing write trans %d.%d\n",
225                    udebug.epochTime, udebug.tidCounter);
226         }
227     } else {
228         if (isClone)
229             printf("I am a clone and never can become sync site\n");
230         else
231             printf("I am not sync site\n");
232         diff = udebug.now - udebug.lowestTime;
233         printf("Lowest host %s was set %d secs ago\n",
234                afs_inet_ntoa(htonl(udebug.lowestHost)),
235                diff);
236
237         diff = udebug.now - udebug.syncTime;
238         printf("Sync host %s was set %d secs ago\n",
239                afs_inet_ntoa(htonl(udebug.syncHost)),
240                diff);
241     }
242
243     printf("Sync site's db version is %d.%d\n", udebug.syncVersion.epoch,
244            udebug.syncVersion.counter);
245     printf("%d locked pages, %d of them for write\n", udebug.lockedPages,
246            udebug.writeLockedPages);
247
248     if (udebug.anyReadLocks)
249         printf("There are read locks held\n");
250     if (udebug.anyWriteLocks)
251         printf("There are write locks held\n");
252
253     if (udebug.currentTrans) {
254         if (udebug.writeTrans)
255             printf("There is an active write transaction\n");
256         else
257             printf("There is at least one active read transaction\n");
258         printf("Transaction tid is %d.%d\n", udebug.syncTid.epoch,
259                udebug.syncTid.counter);
260     }
261     if (udebug.epochTime) {
262         diff = udebug.now - udebug.epochTime;
263         newtime = now - diff;
264         times = ctime(&newtime);
265         times[24] = 0;
266         printf
267             ("Last time a new db version was labelled was:\n\t %d secs ago (at %s)\n",
268              diff, times);
269     }
270
271     if (int32p || udebug.amSyncSite) {
272         /* now do the subcalls */
273         for (i = 0;; i++) {
274             isClone = 0;
275             code = VOTE_XSDebug(tconn, i, &usdebug, &isClone);
276             if (code < 0) {
277                 if (oldServer) {        /* pre 3.5 server */
278                     memset(&usdebug, 0, sizeof(usdebug));
279                     code = VOTE_SDebugOld(tconn, i, &usdebug);
280                 } else
281                     code = VOTE_SDebug(tconn, i, &usdebug);
282             }
283             if (code > 0)
284                 break;          /* done */
285             if (code < 0) {
286                 printf("error code %d from VOTE_SDebug\n", code);
287                 break;
288             }
289             /* otherwise print the structure */
290             printf("\nServer (%s", afs_inet_ntoa(htonl(usdebug.addr)));
291             for (j = 0;
292                  ((usdebug.altAddr[j]) && (j < UBIK_MAX_INTERFACE_ADDR - 1));
293                  j++)
294                 printf(" %s", afs_inet_ntoa(htonl(usdebug.altAddr[j])));
295             printf("): (db %d.%d)", usdebug.remoteVersion.epoch,
296                    usdebug.remoteVersion.counter);
297             if (isClone)
298                 printf("    is only a clone!");
299             printf("\n");
300
301             if (usdebug.lastVoteTime == 0) {
302                 printf("    last vote never rcvd \n");
303             } else {
304                 diff = udebug.now - usdebug.lastVoteTime;
305                 newtime = now - diff;
306                 times = ctime(&newtime);
307                 times[24] = 0;
308                 printf("    last vote rcvd %d secs ago (at %s),\n", diff,
309                        times);
310             }
311
312             if (usdebug.lastBeaconSent == 0) {
313                 printf("    last beacon never sent \n");
314             } else {
315                 diff = udebug.now - usdebug.lastBeaconSent;
316                 newtime = now - diff;
317                 times = ctime(&newtime);
318                 times[24] = 0;
319                 printf
320                     ("    last beacon sent %d secs ago (at %s), last vote was %s\n",
321                      diff, times, ((usdebug.lastVote) ? "yes" : "no"));
322             }
323
324             printf("    dbcurrent=%d, up=%d beaconSince=%d\n",
325                    usdebug.currentDB, usdebug.up, usdebug.beaconSinceDown);
326         }
327     }
328     return (0);
329 }
330
331 #include "AFS_component_version_number.c"
332
333 int
334 main(int argc, char **argv)
335 {
336     struct cmd_syndesc *ts;
337
338 #ifdef  AFS_AIX32_ENV
339     /*
340      * The following signal action for AIX is necessary so that in case of a 
341      * crash (i.e. core is generated) we can include the user's data section 
342      * in the core dump. Unfortunately, by default, only a partial core is
343      * generated which, in many cases, isn't too useful.
344      */
345     struct sigaction nsa;
346
347     sigemptyset(&nsa.sa_mask);
348     nsa.sa_handler = SIG_DFL;
349     nsa.sa_flags = SA_FULLDUMP;
350     sigaction(SIGSEGV, &nsa, NULL);
351 #endif
352     ts = cmd_CreateSyntax(NULL, CommandProc, 0, "probe ubik server");
353     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "server machine");
354     cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "IP port");
355     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "print all info");
356
357     cmd_Dispatch(argc, argv);
358     exit(0);
359 }