1
########################################################################
2
## generic include for XXX. Do not use directly.
3
## $Id: mfd-persistence.m2i,v 1.4.2.1 2006/01/05 23:06:28 rstory Exp $
4
########################################################################
5
@if $m2c_mark_boundary == 1@
6
/** START code generated by $RCSfile: mfd-persistence.m2i,v $ $Revision: 1.4.2.1 $ */
8
########################################################################
9
@if $m2c_processing_type eq 'h'@
10
/* *********************************************************************
11
* Persistent declarations
16
#define LINE_TERM_CHAR '$'
18
void ${context}_container_init_persistence( netsnmp_container * container );
19
int ${context}_container_should_save(${context}_rowreq_ctx * rowreq_ctx);
21
@end@ // m2c_processing_type eq 'h'
22
######################################################################
23
######################################################################
24
######################################################################
25
@if $m2c_processing_type eq 'c'@
26
/************************************************************
27
* the *_should_save routine is called to determine if a row
28
* should be stored persistently.
30
* Note that this is not a 'dirty' check (i.e. if a row has changed),
31
* but a check for volatile rows that should not be saved between
34
* return 1 if the row should be stored
35
* return 0 if the row should not be stored
38
${context}_container_should_save(${context}_rowreq_ctx * rowreq_ctx)
40
@ foreach $node column@
41
@ if "$node.syntax" eq "StorageType"@
42
@ include m2c_setup_node.m2i@
43
if (SNMP_STORAGE_VOLATILE == $m2c_ctx_rh )
48
return 1; /* save the row */
51
@end@ // m2c_processing_type eq 'h'
52
######################################################################
53
######################################################################
54
######################################################################
55
@if $m2c_processing_type eq 'i'@
56
/***********************************************************************
60
***********************************************************************/
62
static int _${context}_container_save_rows(int majorID, int minorID, void *serverarg, void *clientarg);
63
static void _${context}_container_row_restore(const char *token, char *buf);
64
static int _${context}_container_row_save(
65
${context}_rowreq_ctx *rowreq_ctx,
67
static char * _${context}_container_col_restore(
68
${context}_rowreq_ctx *rowreq_ctx,
69
u_int col, char* buf);
70
static char * _${context}_container_col_save(
71
${context}_rowreq_ctx *rowreq_ctx,
72
u_int col, char* buf);
74
static char row_token[] = "${context}";
76
/************************************************************
77
* *_init_persistence should be called from the main table
80
* If your table depends on rows in another table,
81
* you should register your callback after the other table,
82
* which should ensure the rows on which you depend are saved
83
* (and re-created) before the dependent rows.
86
${context}_container_init_persistence( netsnmp_container * container )
90
register_config_handler(NULL, row_token,
91
_${context}_container_row_restore, NULL, NULL);
92
rc = snmp_register_callback( SNMP_CALLBACK_LIBRARY,
93
SNMP_CALLBACK_STORE_DATA,
94
_${context}_container_save_rows,
97
if( rc != SNMP_ERR_NOERROR )
98
snmp_log(LOG_ERR, "error registering for STORE_DATA callback "
99
"in _${context}_container_init_persistence\n");
103
_${context}_container_save_rows(int majorID, int minorID, void *serverarg, void *clientarg)
106
"##############################################################";
109
"# $context persistent data\n"
111
char *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
112
NETSNMP_DS_LIB_APPTYPE);
114
read_config_store((char*)type, sep);
115
read_config_store((char*)type, buf);
120
CONTAINER_FOR_EACH((netsnmp_container*)clientarg,
121
(netsnmp_container_obj_func*)_${context}_container_row_save,
124
read_config_store((char*)type, sep);
125
read_config_store((char*)type, "\n");
130
return SNMPERR_SUCCESS;
135
/************************************************************
136
* _${context}_container_row_save
139
_${context}_container_row_save(
140
${context}_rowreq_ctx *rowreq_ctx,
144
* Allocate space for a line with all data for a row. An
145
* attempt is made to come up with a default maximum size, but
146
* there is no guarantee it will be enough. It probably will be,
147
* unless you are dealing with large values or you have external
150
* 1) allocate space for each column. Comment out columns you don't
151
* intend to save. You may also need to add room for any non-
152
* column data you want to store. Remeber, data will be stored in
153
* ASCII form, so you need to allow for that. Here are some
154
* general guidelines:
156
* Object ID : 12 * len [ASCII len of max int + 1 for .]
157
* Octet String: (2 * len) + 2 [2 ASCII chars per byte + "0x"]
158
* Integers : 12 [ASCII len for smallest negative number]
160
* 2) You also need to allocate space for the row index. This will
161
* be stored as an OID, which means that Octet Strings need to
162
* be treated a little differently. Specifically, you will need
163
* (4 * len) + 4 [3 ASCII chars per byte + 1 for ., + 4 for len].
165
* 3) Also, remeber to add space for the identifier and seperator
166
* characters (for example, each column is prefixed by the
167
* column number and a semicolon. To allow for the maximum
168
* column values, 12 bytes [11 for oid + 1 for ':'] per
171
/** xxx: add storage for external index(s)! */
172
#define MAX_ROW_SIZE (sizeof(row_token) + 1 + \
173
@ if $ext_index != 0@
175
@ foreach $node nonindex@
176
@ include m2c_setup_node.m2i@
177
@ if ($node.settable == 1)@
178
@ if "$node.type" eq "ASN_OBJECT_ID"@
179
( ( 12 * sizeof(${m2c_ctx_rh}) ) + 3 ) + /* $node.type */ \
180
@ elsif "$node.type" eq "ASN_OCTET_STR"@
181
( ( 2 * sizeof(${m2c_ctx_rh}) ) + 3 ) + /* $node.type */ \
183
( 12 ) + /* $node.type $node */ \
187
( $table.uc_MAX_COL * 12 ) + /* column num prefix + : */ \
188
2 /* LINE_TERM_CHAR + \n */ )
190
char buf[MAX_ROW_SIZE], *pos = buf, *max = &buf[MAX_ROW_SIZE-1];
194
if (${context}_container_should_save(rowreq_ctx) == 0) {
195
return SNMP_ERR_NOERROR;
201
pos += sprintf(pos, "%s ", row_token);
202
pos = read_config_save_objid(pos, rowreq_ctx->oid_idx.oids,
203
rowreq_ctx->oid_idx.len);
205
snmp_log(LOG_ERR,"error saving ${context} row "
206
"to persistent file\n");
207
return SNMP_ERR_GENERR;
211
snmp_log(LOG_ERR,"error saving ${context} row "
212
"to persistent file (too long)\n");
213
return SNMP_ERR_GENERR;
219
for(i = $table.uc_MIN_COL; i <= $table.uc_MAX_COL; ++i ) {
221
if ((0x1 << (i-1)) & ~$context.uc_SETTABLE_COLS)
225
pos = _${context}_container_col_save(rowreq_ctx, i, pos);
231
snmp_log(LOG_ERR,"error saving ${context} row "
232
"to persistent file (too long)\n");
233
return SNMP_ERR_GENERR;
238
* if you have non-column data, add it here
245
pos += sprintf(pos, "%c", LINE_TERM_CHAR);
247
snmp_log(LOG_ERR,"error saving ${context} row "
248
"to persistent file (too long)\n");
249
return SNMP_ERR_GENERR;
251
read_config_store((char*)type, buf);
253
DEBUGMSGTL(("internal:${context}:_${context}_container_row_save",
254
"saving line '%s'\n", buf));
256
return SNMP_ERR_NOERROR;
260
_${context}_container_row_restore(const char *token, char *buf)
262
${context}_rowreq_ctx * rowreq_ctx;
264
oid tmp_oid[ MAX_${context}_IDX_LEN];
265
u_int col = 0, found = 0;
268
if (strncmp(token, row_token, sizeof(row_token)) != 0) {
269
snmp_log(LOG_ERR, "unknown token in _${context}_container_row_restore\n");
273
DEBUGMSGTL(("internal:${context}:_${context}_container_row_restore",
274
"parsing line '%s'\n", buf));
277
* pull out index and create default row
279
index.oids = tmp_oid;
280
index.len = OID_LENGTH(tmp_oid);
281
buf = read_config_read_objid(buf, &index.oids,
284
snmp_log(LOG_ERR, "error reading row index in "
285
"_${context}_container_row_restore\n");
288
rowreq_ctx = _mfd_${context}_rowreq_from_index( &index, NULL );
289
if (NULL == rowreq_ctx) {
290
snmp_log(LOG_ERR, "error creating row index in "
291
"_${context}_container_row_restore\n");
296
* loop through and get each column
298
buf = skip_white(buf);
299
while ( (NULL != buf) && isdigit(*buf) ) {
301
* extract column, skip ':'
303
col = (u_int)strtol(buf, &buf, 10);
315
DEBUGMSGTL(("_${context}_container_row_restore",
316
"parsing column %d\n", col));
317
buf = _${context}_container_col_restore( rowreq_ctx, col, buf );
321
snmp_log(LOG_ERR, "error parsing ${context} row; no columns found\n");
322
${context}_release_rowreq_ctx( rowreq_ctx );
327
* if you added any non-column data, this is where
328
* you should handle it.
332
* if the pointer is NULL and we didn't reach the
333
* end of the line, something went wrong. Log message,
334
* delete the row and bail.
336
if ((buf == NULL) || (*buf != LINE_TERM_CHAR)) {
337
snmp_log(LOG_ERR, "error parsing ${context} row around column %d\n",
339
${context}_release_rowreq_ctx( rowreq_ctx );
343
DEBUGMSGTL(("internal:${context}:_${context}_container_row_restore",
347
* copy oid index and insert row
349
rowreq_ctx->oid_idx.len = index.len;
350
memcpy(rowreq_ctx->oid_idx.oids, index.oids, index.len * sizeof(oid));
352
CONTAINER_INSERT(${context}_if_ctx.container, rowreq_ctx);
355
/************************************************************
356
* _${context}_container_col_save
359
_${context}_container_col_save(
360
${context}_rowreq_ctx *rowreq_ctx,
361
u_int col, char* buf)
363
if( ( NULL == rowreq_ctx ) || ( NULL == buf )) {
364
snmp_log(LOG_ERR, "bad parameter in "
365
"_${context}_container_col_save\n");
369
DEBUGMSGTL(("internal:${context}:_${context}_container_col_save",
370
"processing column %d\n", col));
373
* prefix with column number, so we don't ever depend on
376
buf += sprintf(buf, "%u:", col);
379
* save data for the column
383
@ foreach $node nonindex@
384
@ include m2c_setup_node.m2i@
385
case COLUMN_$node.uc: /** $node.syntax = $node.type */
386
@ if $m2c_node_needlength == 1@
387
@ if "$node.type" eq "ASN_OBJECT_ID"@
388
buf = read_config_save_objid(buf, ${m2c_ctx_rh},
390
@ else@ # "$node.type" eq "ASN_OCTET_STR"@
391
buf = read_config_save_octet_string(buf, ${m2c_ctx_rh},
394
@ elsif "$node.type" eq "ASN_INTEGER"@
395
buf += sprintf(buf,"%ld",${m2c_ctx_rh});
397
buf += sprintf(buf,"%lu",${m2c_ctx_rh});
402
default: /** We shouldn't get here */
403
snmp_log(LOG_ERR, "unknown column %d in "
404
"_${context}_container_col_save\n", col);
411
/************************************************************
412
* _${context}_container_col_restore
415
_${context}_container_col_restore(
416
${context}_rowreq_ctx *rowreq_ctx,
417
u_int col, char* buf)
420
if( ( NULL == rowreq_ctx ) || ( NULL == buf )) {
421
snmp_log(LOG_ERR, "bad parameter in "
422
"_${context}_container_col_restore\n");
426
DEBUGMSGTL(("verbose:${context}:_${context}_container_col_restore",
427
"processing column %d\n", col));
430
* restore data for the column
434
@ foreach $node nonindex@
435
@ include m2c_setup_node.m2i@
436
case COLUMN_$node.uc: /** $node.syntax = $node.type */
437
@ if $m2c_node_needlength == 1@
438
${m2c_ctx_rhs} = sizeof(${m2c_ctx_rh});
439
buf = read_config_read_memory($node.type,buf,
440
(char*)&${m2c_ctx_rh},
441
(size_t*)&${m2c_ctx_rhs} );
442
@ if "$node.type" eq "ASN_OBJECT_ID"@
443
${m2c_ctx_rhs} /= sizeof(oid);
446
len = sizeof(${m2c_ctx_rh});
447
@ if "$node.type" eq "ASN_OCTET_STR"@ # BITS
448
@ eval $m2c_tmp = "ASN_INTEGER"@
450
@ eval $m2c_tmp = $node.type@
452
buf = read_config_read_memory($m2c_tmp, buf,
453
(char*)&${m2c_ctx_rh},
456
@ if $m2c_table_sparse == 1@
458
rowreq_ctx->column_exists_flags |= COLUMN_$node.uc_FLAG;
459
@ end@ # table sparse
463
default: /** We shouldn't get here */
464
snmp_log(LOG_ERR, "unknown column %d in "
465
"_${context}_container_col_restore\n", col);
473
@end@ // $m2c_processing_type eq 'i'
474
########################################################################
475
@if $m2c_mark_boundary == 1@
476
/** END code generated by $RCSfile: mfd-persistence.m2i,v $ $Revision: 1.4.2.1 $ */