~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to local/mib2c-conf.d/mfd-persistence.m2i

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-10 22:20:23 UTC
  • mto: (1.4.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20070510222023-3fr07xb9i17xvq32
Tags: upstream-5.3.1
ImportĀ upstreamĀ versionĀ 5.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 $ */
 
7
@end@
 
8
########################################################################
 
9
@if $m2c_processing_type eq 'h'@
 
10
/* *********************************************************************
 
11
 * Persistent declarations
 
12
 */
 
13
/*
 
14
 * persistence
 
15
 */
 
16
#define LINE_TERM_CHAR '$'
 
17
 
 
18
void ${context}_container_init_persistence( netsnmp_container * container );
 
19
int ${context}_container_should_save(${context}_rowreq_ctx * rowreq_ctx);
 
20
 
 
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.
 
29
 *
 
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
 
32
 * restarts.
 
33
 *
 
34
 * return 1 if the row should be stored
 
35
 * return 0 if the row should not be stored
 
36
 */
 
37
int
 
38
${context}_container_should_save(${context}_rowreq_ctx * rowreq_ctx)
 
39
{
 
40
@   foreach $node column@
 
41
@      if "$node.syntax" eq "StorageType"@
 
42
@         include m2c_setup_node.m2i@
 
43
    if (SNMP_STORAGE_VOLATILE == $m2c_ctx_rh    )
 
44
        return 0;
 
45
@       end@
 
46
@   end@
 
47
    
 
48
    return 1; /* save the row */
 
49
}
 
50
 
 
51
@end@ // m2c_processing_type eq 'h'
 
52
######################################################################
 
53
######################################################################
 
54
######################################################################
 
55
@if $m2c_processing_type eq 'i'@
 
56
/***********************************************************************
 
57
 *
 
58
 * PERSISTENCE
 
59
 *
 
60
 ***********************************************************************/
 
61
 
 
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,
 
66
            void *type);
 
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);
 
73
 
 
74
static char row_token[] = "${context}";
 
75
 
 
76
/************************************************************
 
77
 * *_init_persistence should be called from the main table
 
78
 * init routine.
 
79
 *
 
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.
 
84
 */
 
85
void
 
86
${context}_container_init_persistence( netsnmp_container * container )
 
87
{
 
88
    int rc;
 
89
 
 
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,
 
95
                                     container);
 
96
 
 
97
    if( rc != SNMP_ERR_NOERROR )
 
98
        snmp_log(LOG_ERR, "error registering for STORE_DATA callback "
 
99
                 "in _${context}_container_init_persistence\n");
 
100
}
 
101
 
 
102
static int
 
103
_${context}_container_save_rows(int majorID, int minorID, void *serverarg, void *clientarg)
 
104
{
 
105
    char sep[] =
 
106
        "##############################################################";
 
107
    char buf[] =
 
108
        "#\n"
 
109
        "# $context persistent data\n"
 
110
        "#";
 
111
    char *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
 
112
                                       NETSNMP_DS_LIB_APPTYPE);
 
113
 
 
114
    read_config_store((char*)type, sep);
 
115
    read_config_store((char*)type, buf);
 
116
 
 
117
    /*
 
118
     * save all rows
 
119
     */
 
120
    CONTAINER_FOR_EACH((netsnmp_container*)clientarg,
 
121
                       (netsnmp_container_obj_func*)_${context}_container_row_save,
 
122
                       type);
 
123
 
 
124
    read_config_store((char*)type, sep);
 
125
    read_config_store((char*)type, "\n");
 
126
 
 
127
    /*
 
128
     * never fails 
 
129
     */
 
130
    return SNMPERR_SUCCESS;
 
131
}
 
132
 
 
133
 
 
134
 
 
135
/************************************************************
 
136
 * _${context}_container_row_save
 
137
 */
 
138
static int
 
139
_${context}_container_row_save(
 
140
            ${context}_rowreq_ctx *rowreq_ctx,
 
141
            void *type)
 
142
{
 
143
    /*
 
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
 
148
     * indexes.
 
149
     *
 
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:
 
155
     *
 
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]
 
159
     *
 
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].
 
164
     *
 
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
 
169
     * column are added).
 
170
     */
 
171
        /** xxx: add storage for external index(s)! */
 
172
#define MAX_ROW_SIZE (sizeof(row_token) + 1 +  \
 
173
@   if $ext_index != 0@
 
174
@   end@
 
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 */ \
 
182
@      else@
 
183
        ( 12 ) + /* $node.type $node */ \
 
184
@      end@
 
185
@    end@
 
186
@   end@
 
187
        ( $table.uc_MAX_COL * 12 ) + /* column num prefix + : */ \
 
188
    2 /* LINE_TERM_CHAR + \n */ )
 
189
 
 
190
    char buf[MAX_ROW_SIZE], *pos = buf, *max = &buf[MAX_ROW_SIZE-1];
 
191
    char *tmp;
 
192
    int i;
 
193
 
 
194
    if (${context}_container_should_save(rowreq_ctx) == 0) {
 
195
        return SNMP_ERR_NOERROR;
 
196
    }
 
197
 
 
198
    /*
 
199
     * build the line
 
200
     */
 
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);
 
204
    if(NULL == pos) {
 
205
        snmp_log(LOG_ERR,"error saving ${context} row "
 
206
                 "to persistent file\n");
 
207
        return SNMP_ERR_GENERR;
 
208
    }
 
209
    *pos++ = ' ';
 
210
    if(pos > max) {
 
211
        snmp_log(LOG_ERR,"error saving ${context} row "
 
212
                 "to persistent file (too long)\n");
 
213
        return SNMP_ERR_GENERR;
 
214
    }
 
215
 
 
216
    /*
 
217
     * add each column
 
218
     */
 
219
    for(i = $table.uc_MIN_COL; i <= $table.uc_MAX_COL; ++i ) {
 
220
 
 
221
        if ((0x1 << (i-1)) & ~$context.uc_SETTABLE_COLS)
 
222
           continue;
 
223
 
 
224
        tmp = pos;
 
225
        pos = _${context}_container_col_save(rowreq_ctx, i, pos);
 
226
        if(NULL == pos)
 
227
            pos = tmp;
 
228
        else
 
229
            *pos++ = ' ';        
 
230
        if(pos > max) {
 
231
            snmp_log(LOG_ERR,"error saving ${context} row "
 
232
                     "to persistent file (too long)\n");
 
233
            return SNMP_ERR_GENERR;
 
234
        }
 
235
    }
 
236
 
 
237
    /*
 
238
     * if you have non-column data, add it here
 
239
     */
 
240
 
 
241
 
 
242
    /*
 
243
     * store the line
 
244
     */
 
245
    pos += sprintf(pos, "%c", LINE_TERM_CHAR);
 
246
    if(pos > max) {
 
247
        snmp_log(LOG_ERR,"error saving ${context} row "
 
248
                 "to persistent file (too long)\n");
 
249
        return SNMP_ERR_GENERR;
 
250
    }
 
251
    read_config_store((char*)type, buf);
 
252
 
 
253
    DEBUGMSGTL(("internal:${context}:_${context}_container_row_save",
 
254
                "saving line '%s'\n", buf));
 
255
    
 
256
    return SNMP_ERR_NOERROR;
 
257
}
 
258
 
 
259
static void
 
260
_${context}_container_row_restore(const char *token, char *buf)
 
261
{
 
262
    ${context}_rowreq_ctx * rowreq_ctx;
 
263
    netsnmp_index index;
 
264
    oid tmp_oid[ MAX_${context}_IDX_LEN];
 
265
    u_int col = 0, found = 0;
 
266
 
 
267
    
 
268
    if (strncmp(token, row_token, sizeof(row_token)) != 0) {
 
269
        snmp_log(LOG_ERR, "unknown token in _${context}_container_row_restore\n");
 
270
        return;
 
271
    }
 
272
 
 
273
    DEBUGMSGTL(("internal:${context}:_${context}_container_row_restore",
 
274
                "parsing line '%s'\n", buf));
 
275
 
 
276
    /*
 
277
     * pull out index and create default row
 
278
     */
 
279
    index.oids = tmp_oid;
 
280
    index.len = OID_LENGTH(tmp_oid);
 
281
    buf = read_config_read_objid(buf, &index.oids,
 
282
                                 &index.len);
 
283
    if (NULL == buf) {
 
284
        snmp_log(LOG_ERR, "error reading row index in "
 
285
                 "_${context}_container_row_restore\n");
 
286
        return;
 
287
    }
 
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");
 
292
        return;
 
293
    }
 
294
    
 
295
    /*
 
296
     * loop through and get each column
 
297
     */
 
298
    buf = skip_white(buf);
 
299
    while ( (NULL != buf) && isdigit(*buf) ) {
 
300
        /*
 
301
         * extract column, skip ':'
 
302
         */
 
303
        col = (u_int)strtol(buf, &buf, 10);
 
304
        if (NULL == buf)
 
305
            break;
 
306
        if (*buf != ':') {
 
307
            buf = NULL;
 
308
            break;
 
309
        }
 
310
        ++buf; /* skip : */
 
311
 
 
312
        /*
 
313
         * parse value
 
314
         */
 
315
        DEBUGMSGTL(("_${context}_container_row_restore",
 
316
                    "parsing column %d\n", col));
 
317
        buf = _${context}_container_col_restore( rowreq_ctx, col, buf );
 
318
        ++found;
 
319
    }
 
320
    if (0 == found) {
 
321
        snmp_log(LOG_ERR, "error parsing ${context} row; no columns found\n");
 
322
        ${context}_release_rowreq_ctx( rowreq_ctx );
 
323
        return;
 
324
    }
 
325
 
 
326
    /*
 
327
     * if you added any non-column data, this is where
 
328
     * you should handle it.
 
329
     */
 
330
    
 
331
    /*
 
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.
 
335
     */
 
336
    if ((buf == NULL) || (*buf != LINE_TERM_CHAR)) {
 
337
        snmp_log(LOG_ERR, "error parsing ${context} row around column %d\n",
 
338
                 col);
 
339
        ${context}_release_rowreq_ctx( rowreq_ctx );
 
340
        return;
 
341
    }
 
342
 
 
343
    DEBUGMSGTL(("internal:${context}:_${context}_container_row_restore",
 
344
                "inserting row\n"));
 
345
 
 
346
    /*
 
347
     * copy oid index and insert row
 
348
     */
 
349
    rowreq_ctx->oid_idx.len = index.len;
 
350
    memcpy(rowreq_ctx->oid_idx.oids, index.oids, index.len * sizeof(oid));
 
351
 
 
352
    CONTAINER_INSERT(${context}_if_ctx.container, rowreq_ctx);
 
353
}
 
354
 
 
355
/************************************************************
 
356
 * _${context}_container_col_save
 
357
 */
 
358
static char *
 
359
_${context}_container_col_save(
 
360
            ${context}_rowreq_ctx *rowreq_ctx,
 
361
            u_int col, char* buf)
 
362
{
 
363
    if( ( NULL == rowreq_ctx ) || ( NULL == buf )) {
 
364
        snmp_log(LOG_ERR, "bad parameter in "
 
365
                 "_${context}_container_col_save\n");
 
366
        return NULL;
 
367
    }
 
368
 
 
369
    DEBUGMSGTL(("internal:${context}:_${context}_container_col_save",
 
370
                "processing column %d\n", col));
 
371
 
 
372
    /*
 
373
     * prefix with column number, so we don't ever depend on
 
374
     * order saved.
 
375
     */
 
376
    buf += sprintf(buf, "%u:", col);
 
377
 
 
378
    /*
 
379
     * save data for the column
 
380
     */
 
381
    switch(col) {
 
382
 
 
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},
 
389
                                         ${m2c_ctx_rhs} );
 
390
@         else@ # "$node.type" eq "ASN_OCTET_STR"@
 
391
            buf = read_config_save_octet_string(buf, ${m2c_ctx_rh},
 
392
                                                ${m2c_ctx_rhs} );
 
393
@         end@
 
394
@      elsif "$node.type" eq "ASN_INTEGER"@
 
395
            buf += sprintf(buf,"%ld",${m2c_ctx_rh});
 
396
@      else@
 
397
            buf += sprintf(buf,"%lu",${m2c_ctx_rh});
 
398
@      end@
 
399
        break;
 
400
    
 
401
@   end@ # for each
 
402
    default: /** We shouldn't get here */
 
403
        snmp_log(LOG_ERR, "unknown column %d in "
 
404
                 "_${context}_container_col_save\n", col);
 
405
        return NULL;
 
406
    }
 
407
 
 
408
    return buf;
 
409
}
 
410
 
 
411
/************************************************************
 
412
 * _${context}_container_col_restore
 
413
 */
 
414
static char *
 
415
_${context}_container_col_restore(
 
416
            ${context}_rowreq_ctx *rowreq_ctx,
 
417
            u_int col, char* buf)
 
418
{
 
419
    size_t len;
 
420
    if( ( NULL == rowreq_ctx ) || ( NULL == buf )) {
 
421
        snmp_log(LOG_ERR, "bad parameter in "
 
422
                 "_${context}_container_col_restore\n");
 
423
        return NULL;
 
424
    }
 
425
 
 
426
    DEBUGMSGTL(("verbose:${context}:_${context}_container_col_restore",
 
427
                "processing column %d\n", col));
 
428
 
 
429
    /*
 
430
     * restore data for the column
 
431
     */
 
432
    switch(col) {
 
433
 
 
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);
 
444
@         end@
 
445
@      else@
 
446
            len = sizeof(${m2c_ctx_rh});
 
447
@         if "$node.type" eq "ASN_OCTET_STR"@ # BITS
 
448
@            eval $m2c_tmp = "ASN_INTEGER"@
 
449
@         else@
 
450
@            eval $m2c_tmp = $node.type@
 
451
@         end@
 
452
            buf = read_config_read_memory($m2c_tmp, buf,
 
453
                                          (char*)&${m2c_ctx_rh},
 
454
                                          &len);
 
455
@      end@
 
456
@      if $m2c_table_sparse == 1@
 
457
            if (NULL != buf)
 
458
                rowreq_ctx->column_exists_flags |= COLUMN_$node.uc_FLAG;
 
459
@      end@ # table sparse
 
460
        break;
 
461
    
 
462
@   end@ # foreach col
 
463
    default: /** We shouldn't get here */
 
464
        snmp_log(LOG_ERR, "unknown column %d in "
 
465
                 "_${context}_container_col_restore\n", col);
 
466
        return NULL;
 
467
    }
 
468
 
 
469
    return buf;
 
470
}
 
471
 
 
472
##
 
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 $ */
 
477
@end@