1
/* statedebug.c - debugging tool for writing support programs
3
Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include "shared-tables.h"
24
const char *progname = NULL;
26
static time_t lastmtime = 0;
28
static const char *fname(int cmdnum)
32
for (i = 0; netvars[i].name != NULL; i++)
33
if (netvars[i].type == cmdnum)
34
return(netvars[i].name);
36
/* handle some special cases */
38
case INFO_MEMBERS: return("MEMBERS");
39
case INFO_MTIME: return("MTIME");
40
case INFO_INSTCMD: return("INSTCMD");
41
case INFO_ENUM: return("ENUM");
42
case INFO_MSGID: return("MSGID");
43
case INFO_SHMID: return("SHMID");
44
case INFO_UNUSED: return("UNUSED");
50
/* return a name for an instant command given its identifier (from shared.h) */
51
const char *instcmdname(int cmdnum)
55
for (i = 0; instcmds[i].name != NULL; i++)
56
if (instcmds[i].cmd == cmdnum)
57
return(instcmds[i].name);
59
return("*** Unknown INSTCMD");
62
void printinfo(int pos, itype *info)
65
printf("pos| type | aux | value\n");
67
printf("%3i: %15s (%04x) [%04x] = ", pos,
68
fname(info->type), info->type,
73
printf("%s\n", instcmdname(info->auxdata));
77
fname(info->auxdata), info->value);
80
printf("%s\n", info->value);
85
void displayshm(int shmid)
89
struct shmid_ds shmbuf;
91
info = (itype *) shmat(shmid, 0, 0);
92
if (info == (itype *)(-1))
93
fatal("Can't attach to shared memory");
95
if (info[0].type != INFO_MEMBERS) {
97
fatalx("First entry in info isn't INFO_MEMBERS, giving up...");
100
if (shmctl(shmid, IPC_STAT, &shmbuf) != 0)
101
fatal("shmctl STAT");
103
if (shmbuf.shm_ctime != lastmtime) {
104
lastmtime = shmbuf.shm_ctime;
106
num = atoi(info[0].value);
108
printf("Shared memory ID: %d\n", shmid);
109
for (i = 0; i < num; i++) {
110
printinfo(i, &info[i]);
117
void displayshm(int shmid)
119
printf("This state file uses shared memory and you didn't compile that in!\n");
122
#endif /* HAVE_SHMAT */
124
static void display(const char *fn)
127
int ifd, ret, i, count;
129
ifd = open(fn, O_RDONLY);
131
fatal("open %s", fn);
133
ret = read(ifd, &info, sizeof(itype));
137
if (info.type == INFO_SHMID) {
138
displayshm(atoi(info.value));
139
} else if (info.type != INFO_MEMBERS) {
140
fatalx("First entry in file isn't INFO_MEMBERS, giving up...");
147
if (st.st_mtime != lastmtime) {
148
lastmtime = st.st_mtime;
150
count = atoi(info.value);
152
for (i = 1; i < count; i++) {
153
if ((ret = read(ifd, &info, sizeof(itype))) != sizeof(itype))
154
fatal("could not read correct size for %d members", count);
164
static void help(void)
166
printf("Network UPS Tools statedebug %s\n\n", UPS_VERSION);
167
printf("usage: %s [-h]\n", progname);
168
printf(" %s [-c] [-w <num>] <state file>\n\n", progname);
169
printf("Debugging tool for UPS driver state files.\n\n");
170
printf(" -h show this help\n");
171
printf(" -c run continuously\n");
172
printf(" -w <num> delay <num> seconds in continuous mode (default 30s)\n");
176
int main (int argc, char **argv)
181
unsigned int delay = 30;
185
while ((c=getopt(argc, argv, "cw:h")) != EOF) {
191
delay = atoi(optarg);
215
} while (continuous);