forked from maoali/freeipmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipmi_monitoring_sdr_cache.c
389 lines (339 loc) · 12.3 KB
/
ipmi_monitoring_sdr_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*****************************************************************************\
* $Id: ipmi_monitoring_sdr_cache.c,v 1.28 2010-03-19 22:07:58 chu11 Exp $
*****************************************************************************
* Copyright (C) 2007-2015 Lawrence Livermore National Security, LLC.
* Copyright (C) 2006-2007 The Regents of the University of California.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* Written by Albert Chu <[email protected]>
* UCRL-CODE-222073
*
* This file is part of Ipmimonitoring, an IPMI sensor monitoring
* library. For details, see http://www.llnl.gov/linux/.
*
* Ipmimonitoring is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* Ipmimonitoring is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Ipmimonitoring. If not, see <http://www.gnu.org/licenses/>.
\*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#if STDC_HEADERS
#include <string.h>
#endif /* STDC_HEADERS */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <assert.h>
#include <errno.h>
#include "ipmi_monitoring.h"
#include "ipmi_monitoring_debug.h"
#include "ipmi_monitoring_defs.h"
#include "ipmi_monitoring_ipmi_communication.h"
#include "ipmi_monitoring_sdr_cache.h"
#include "freeipmi-portability.h"
#define IPMI_MONITORING_SDR_CACHE_DIRECTORY IPMI_MONITORING_SDR_CACHE_DIR
#define IPMI_MONITORING_SDR_CACHE_FILENAME "ipmimonitoringsdrcache"
#define IPMI_MONITORING_SDR_CACHE_INBAND "localhost"
extern uint32_t _ipmi_monitoring_flags;
static int
_ipmi_monitoring_sdr_ctx_init (ipmi_monitoring_ctx_t c, const char *hostname)
{
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
if (!(c->sdr_ctx = ipmi_sdr_ctx_create ()))
{
IPMI_MONITORING_DEBUG (("ipmi_sdr_cache_create: %s", strerror (errno)));
if (errno == EPERM || errno == EACCES)
c->errnum = IPMI_MONITORING_ERR_PERMISSION;
else
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
return (-1);
}
if (_ipmi_monitoring_flags & IPMI_MONITORING_FLAGS_DEBUG_IPMI_PACKETS)
{
/* Don't error out, if this fails we can still continue */
if (ipmi_sdr_ctx_set_flags (c->sdr_ctx, IPMI_SDR_FLAGS_DEBUG_DUMP) < 0)
IPMI_MONITORING_DEBUG (("ipmi_sdr_ctx_set_flags: %s", ipmi_sdr_ctx_errormsg (c->sdr_ctx)));
if (hostname)
{
if (ipmi_sdr_ctx_set_debug_prefix (c->sdr_ctx, hostname) < 0)
IPMI_MONITORING_DEBUG (("ipmi_sdr_ctx_set_debug_prefix: %s",
ipmi_sdr_ctx_errormsg (c->sdr_ctx)));
}
}
return (0);
}
static int
_ipmi_monitoring_sdr_cache_filename (ipmi_monitoring_ctx_t c,
const char *hostname,
char *buf,
unsigned int buflen)
{
char sdr_cache_filename[MAXPATHLEN+1];
char *dir;
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
assert (buf);
assert (buflen);
if (c->sdr_cache_directory_set)
dir = c->sdr_cache_directory;
else
dir = IPMI_MONITORING_SDR_CACHE_DIRECTORY;
if (!hostname)
hostname = IPMI_MONITORING_SDR_CACHE_INBAND;
memset (sdr_cache_filename, '\0', MAXPATHLEN+1);
if (c->sdr_cache_filename_format_set)
{
int index = 0;
int percent = 0;
char *str;
str = c->sdr_cache_filename_format;
while (str && *str && index < MAXPATHLEN)
{
if (percent)
{
percent = 0;
if (*str == '%')
{
sdr_cache_filename[index] = *str;
index++;
}
else if (*str == 'L')
{
char local_hostname[MAXHOSTNAMELEN+1];
char *ptr;
memset (local_hostname, '\0', MAXHOSTNAMELEN+1);
if (gethostname (local_hostname, MAXHOSTNAMELEN) < 0)
{
IPMI_MONITORING_DEBUG (("gethostname: %s", strerror (errno)));
c->errnum = IPMI_MONITORING_ERR_SYSTEM_ERROR;
return (-1);
}
/* shorten hostname if necessary */
if ((ptr = strchr (local_hostname, '.')))
*ptr = '\0';
if ((index + strlen (local_hostname)) >= MAXPATHLEN)
{
IPMI_MONITORING_DEBUG (("_ipmi_monitoring_sdr_cache_filename: overflow"));
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
return (-1);
}
strcat (&sdr_cache_filename[index], local_hostname);
index += strlen (local_hostname);
}
else if (*str == 'H')
{
if ((index + strlen (hostname)) >= MAXPATHLEN)
{
IPMI_MONITORING_DEBUG (("_ipmi_monitoring_sdr_cache_filename: overflow"));
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
return (-1);
}
strcat (&sdr_cache_filename[index], hostname);
index += strlen (hostname);
}
else
{
sdr_cache_filename[index] = '%';
index++;
sdr_cache_filename[index] = *str;
index++;
}
}
else if (*str == '%')
percent = 1;
else
{
sdr_cache_filename[index] = *str;
index++;
}
str++;
}
}
else
snprintf (sdr_cache_filename,
MAXPATHLEN,
"%s.%s",
IPMI_MONITORING_SDR_CACHE_FILENAME,
hostname);
snprintf (buf,
buflen - 1,
"%s/%s",
dir,
sdr_cache_filename);
return (0);
}
static int
_ipmi_monitoring_sdr_cache_retrieve (ipmi_monitoring_ctx_t c,
const char *hostname,
char *filename,
unsigned int sdr_create_flags)
{
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
assert (c->sdr_ctx);
assert (c->ipmi_ctx);
assert (filename && strlen (filename));
if (ipmi_sdr_cache_create (c->sdr_ctx,
c->ipmi_ctx,
filename,
sdr_create_flags,
NULL,
NULL) < 0)
{
IPMI_MONITORING_DEBUG (("ipmi_sdr_cache_create: %s", ipmi_sdr_ctx_errormsg (c->sdr_ctx)));
if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_FILESYSTEM)
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_FILESYSTEM;
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_PERMISSION)
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_PERMISSION;
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_IPMI_ERROR)
ipmi_monitoring_ipmi_ctx_error_convert (c);
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_SYSTEM_ERROR)
c->errnum = IPMI_MONITORING_ERR_SYSTEM_ERROR;
else
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
return (-1);
}
return (0);
}
static int
_ipmi_monitoring_sdr_cache_delete (ipmi_monitoring_ctx_t c,
const char *hostname,
char *filename)
{
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
assert (c->sdr_ctx);
if (ipmi_sdr_cache_delete (c->sdr_ctx, filename) < 0)
{
if (ipmi_sdr_ctx_errnum (c->sdr_ctx) != IPMI_SDR_ERR_FILENAME_INVALID)
{
IPMI_MONITORING_DEBUG (("ipmi_sdr_cache_delete: %s", ipmi_sdr_ctx_errormsg (c->sdr_ctx)));
if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_PERMISSION)
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_PERMISSION;
else
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
return (-1);
}
}
return (0);
}
int
ipmi_monitoring_sdr_cache_load (ipmi_monitoring_ctx_t c,
const char *hostname,
unsigned int sdr_create_flags)
{
char filename[MAXPATHLEN+1];
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
assert (c->ipmi_ctx);
memset (filename, '\0', MAXPATHLEN + 1);
if (_ipmi_monitoring_sdr_cache_filename (c, hostname, filename, MAXPATHLEN + 1) < 0)
goto cleanup;
if (_ipmi_monitoring_sdr_ctx_init (c, hostname) < 0)
goto cleanup;
if (ipmi_sdr_cache_open (c->sdr_ctx,
c->ipmi_ctx,
filename) < 0)
{
if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_CACHE_READ_CACHE_DOES_NOT_EXIST)
{
if (_ipmi_monitoring_sdr_cache_retrieve (c, hostname, filename, sdr_create_flags) < 0)
goto cleanup;
}
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_CACHE_INVALID
|| ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_CACHE_OUT_OF_DATE)
{
if (_ipmi_monitoring_sdr_cache_delete (c, hostname, filename) < 0)
goto cleanup;
if (_ipmi_monitoring_sdr_cache_retrieve (c, hostname, filename, sdr_create_flags) < 0)
goto cleanup;
}
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_FILESYSTEM)
{
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_FILESYSTEM;
goto cleanup;
}
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_PERMISSION)
{
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_PERMISSION;
goto cleanup;
}
else
{
IPMI_MONITORING_DEBUG (("ipmi_sdr_cache_open: %s", ipmi_sdr_ctx_errormsg (c->sdr_ctx)));
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
goto cleanup;
}
/* 2nd try after the sdr was retrieved*/
if (ipmi_sdr_cache_open (c->sdr_ctx,
c->ipmi_ctx,
filename) < 0)
{
if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_FILESYSTEM)
{
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_FILESYSTEM;
goto cleanup;
}
else if (ipmi_sdr_ctx_errnum (c->sdr_ctx) == IPMI_SDR_ERR_PERMISSION)
{
c->errnum = IPMI_MONITORING_ERR_SDR_CACHE_PERMISSION;
goto cleanup;
}
else
{
IPMI_MONITORING_DEBUG (("ipmi_sdr_cache_open: %s", ipmi_sdr_ctx_errormsg (c->sdr_ctx)));
c->errnum = IPMI_MONITORING_ERR_INTERNAL_ERROR;
goto cleanup;
}
}
}
return (0);
cleanup:
if (strlen (filename))
ipmi_sdr_cache_delete (c->sdr_ctx, filename);
ipmi_sdr_ctx_destroy (c->sdr_ctx);
c->sdr_ctx = NULL;
return (-1);
}
int
ipmi_monitoring_sdr_cache_unload (ipmi_monitoring_ctx_t c)
{
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
ipmi_sdr_cache_close (c->sdr_ctx);
ipmi_sdr_ctx_destroy (c->sdr_ctx);
c->sdr_ctx = NULL;
return (0);
}
int
ipmi_monitoring_sdr_cache_flush (ipmi_monitoring_ctx_t c,
const char *hostname)
{
char filename[MAXPATHLEN+1];
assert (c);
assert (c->magic == IPMI_MONITORING_MAGIC);
memset (filename, '\0', MAXPATHLEN + 1);
if (_ipmi_monitoring_sdr_cache_filename (c, hostname, filename, MAXPATHLEN + 1) < 0)
goto cleanup;
if (_ipmi_monitoring_sdr_ctx_init (c, hostname) < 0)
goto cleanup;
if (_ipmi_monitoring_sdr_cache_delete (c, hostname, filename) < 0)
goto cleanup;
return (0);
cleanup:
ipmi_sdr_ctx_destroy (c->sdr_ctx);
c->sdr_ctx = NULL;
return (-1);
}