~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to server/statedebug.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* statedebug.c - debugging tool for writing support programs
2
 
 
3
 
   Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
4
 
 
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.
9
 
 
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.
14
 
 
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
18
 
*/
19
 
 
20
 
#define STANDALONE
21
 
#include "upsd.h"
22
 
#include "shared-tables.h"
23
 
 
24
 
const char *progname = NULL;
25
 
 
26
 
static  time_t  lastmtime = 0;
27
 
 
28
 
static const char *fname(int cmdnum)
29
 
{
30
 
        int     i;
31
 
 
32
 
        for (i = 0; netvars[i].name != NULL; i++)
33
 
                if (netvars[i].type == cmdnum)
34
 
                        return(netvars[i].name);
35
 
 
36
 
        /* handle some special cases */
37
 
        switch(cmdnum) {
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");
45
 
        }
46
 
 
47
 
        return("UNKNOWN");
48
 
}
49
 
 
50
 
/* return a name for an instant command given its identifier (from shared.h) */
51
 
const char *instcmdname(int cmdnum)
52
 
{
53
 
        int     i;
54
 
 
55
 
        for (i = 0; instcmds[i].name != NULL; i++)
56
 
                if (instcmds[i].cmd == cmdnum)
57
 
                        return(instcmds[i].name);
58
 
 
59
 
        return("*** Unknown INSTCMD");
60
 
}
61
 
 
62
 
void printinfo(int pos, itype *info)
63
 
{
64
 
        if (pos == 0)
65
 
                printf("pos| type                  | aux   | value\n");
66
 
 
67
 
        printf("%3i: %15s (%04x) [%04x] = ", pos,
68
 
               fname(info->type), info->type, 
69
 
               info->auxdata);
70
 
 
71
 
        switch(info->type) {
72
 
                case INFO_INSTCMD:
73
 
                        printf("%s\n", instcmdname(info->auxdata));
74
 
                        break;
75
 
                case INFO_ENUM:
76
 
                        printf("%s: %s\n", 
77
 
                              fname(info->auxdata), info->value);
78
 
                        break;
79
 
                default:
80
 
                        printf("%s\n", info->value);
81
 
        }
82
 
}
83
 
 
84
 
#ifdef HAVE_SHMAT
85
 
void displayshm(int shmid)
86
 
{
87
 
        itype   *info;
88
 
        int     i, num;
89
 
        struct  shmid_ds shmbuf;
90
 
 
91
 
        info = (itype *) shmat(shmid, 0, 0);
92
 
        if (info == (itype *)(-1))
93
 
                fatal("Can't attach to shared memory");
94
 
 
95
 
        if (info[0].type != INFO_MEMBERS) {
96
 
                shmdt(info);
97
 
                fatalx("First entry in info isn't INFO_MEMBERS, giving up...");
98
 
        }
99
 
 
100
 
        if (shmctl(shmid, IPC_STAT, &shmbuf) != 0)
101
 
                fatal("shmctl STAT");
102
 
 
103
 
        if (shmbuf.shm_ctime != lastmtime) {
104
 
                lastmtime = shmbuf.shm_ctime;
105
 
 
106
 
                num = atoi(info[0].value);
107
 
 
108
 
                printf("Shared memory ID: %d\n", shmid);
109
 
                for (i = 0; i < num; i++) {
110
 
                        printinfo(i, &info[i]);
111
 
                }
112
 
        }
113
 
 
114
 
        shmdt((char *)info);
115
 
}
116
 
#else
117
 
void displayshm(int shmid)
118
 
{
119
 
        printf("This state file uses shared memory and you didn't compile that in!\n");
120
 
        exit(1);
121
 
}
122
 
#endif  /* HAVE_SHMAT */
123
 
 
124
 
static void display(const char *fn)
125
 
{
126
 
        itype   info;
127
 
        int     ifd, ret, i, count;
128
 
 
129
 
        ifd = open(fn, O_RDONLY);
130
 
        if (ifd < 0)
131
 
                fatal("open %s", fn);
132
 
 
133
 
        ret = read(ifd, &info, sizeof(itype));
134
 
        if (ret < 1)
135
 
                fatal("read");
136
 
 
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...");
141
 
        } else {
142
 
                struct stat st;
143
 
 
144
 
                if (fstat(ifd, &st))
145
 
                        fatal("fstat");
146
 
 
147
 
                if (st.st_mtime != lastmtime) {
148
 
                        lastmtime = st.st_mtime;
149
 
 
150
 
                        count = atoi(info.value);
151
 
                        printinfo(0, &info);
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);
155
 
                                printinfo(i, &info);
156
 
                        }
157
 
                }
158
 
        }
159
 
 
160
 
        close(ifd);
161
 
        return;
162
 
}
163
 
 
164
 
static void help(void)
165
 
{
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");
173
 
        exit(1);
174
 
}
175
 
 
176
 
int main (int argc, char **argv)
177
 
{
178
 
        int continuous = 0;
179
 
        char *file;
180
 
        int c;
181
 
        unsigned int delay = 30;
182
 
 
183
 
        progname = argv[0];
184
 
 
185
 
        while ((c=getopt(argc, argv, "cw:h")) != EOF) {
186
 
                switch (c) {
187
 
                        case 'c':
188
 
                                continuous = 1;
189
 
                                break;
190
 
                        case 'w':
191
 
                                delay = atoi(optarg);
192
 
                                break;
193
 
                        case 'h':
194
 
                                help();
195
 
                                break;
196
 
                        default:
197
 
                                help();
198
 
                                break;
199
 
                }
200
 
        }
201
 
 
202
 
        argc -= optind;
203
 
        argv += optind;
204
 
 
205
 
        if (argc != 1)
206
 
                help();
207
 
 
208
 
        file = argv[0];
209
 
 
210
 
        do {
211
 
                display(file);
212
 
 
213
 
                if (continuous)
214
 
                        sleep(delay);
215
 
        } while (continuous);
216
 
 
217
 
        return 0;
218
 
}