int main(int argc1, char *argv[]) { route_init(NULL, 0); route_register(&rt_filea_method); route_register(&rt_fileov_method); route_register(&rt_stdin_method); route_register(&rt_stdout_method); route_register(&rt_stderr_method); route_register(&rt_rs_method); if ( ! elog_init(1, "cascade test", NULL)) elog_die(FATAL, "didn't initialise elog\n"); out = route_open("stdout", NULL, NULL, 0); err = route_open("stderr", NULL, NULL, 0); rs_init(); /* run cascade with all the possible modes */ test_cascade(CASCADE_AVG, "avg", TAB_SING, TAB_SINGINFO, TAB_SINGINFOKEY, TAB_MULT, TAB_MULTINFO, TAB_MULTINFOKEY, RES_AVGSING, RES_AVGSINGKEY, RES_AVGMULT, RES_AVGMULTKEY); test_cascade(CASCADE_MIN, "min", TAB_SING, TAB_SINGINFO, TAB_SINGINFOKEY, TAB_MULT, TAB_MULTINFO, TAB_MULTINFOKEY, RES_MINSING, RES_MINSINGKEY, RES_MINMULT, RES_MINMULTKEY); test_cascade(CASCADE_MAX, "max", TAB_SING, TAB_SINGINFO, TAB_SINGINFOKEY, TAB_MULT, TAB_MULTINFO, TAB_MULTINFOKEY, RES_MAXSING, RES_MAXSINGKEY, RES_MAXMULT, RES_MAXMULTKEY); test_cascade(CASCADE_SUM, "sum", TAB_SING, TAB_SINGINFO, TAB_SINGINFOKEY, TAB_MULT, TAB_MULTINFO, TAB_MULTINFOKEY, RES_SUMSING, RES_SUMSINGKEY, RES_SUMMULT, RES_SUMMULTKEY); test_cascade(CASCADE_FIRST, "first", TAB_SING, TAB_SINGINFO, TAB_SINGINFOKEY, TAB_MULT, TAB_MULTINFO, TAB_MULTINFOKEY, RES_FIRSTSING, RES_FIRSTSINGKEY, RES_FIRSTMULT, RES_FIRSTMULTKEY); test_cascade(CASCADE_LAST, "last", TAB_SING, TAB_SINGINFO, TAB_SINGINFOKEY, TAB_MULT, TAB_MULTINFO, TAB_MULTINFOKEY, RES_LASTSING, RES_LASTSINGKEY, RES_LASTMULT, RES_LASTMULTKEY); rs_fini(); elog_fini(); route_close(err); route_close(out); route_fini(); printf("tests finished successfully\n"); exit(0); }
in_addr_t anubis_default_route(void) { route_t *handle = route_open(); struct route_entry route_entry = {0}; in_addr_t addr = 0; if(!handle) { anubis_perror("route_open()"); return addr; }//end if int ret = route_loop(handle, route_default_route_callback, (void *)&route_entry); if(ret == 1) { struct sockaddr_in sa = {0}; addr_ntos(&route_entry.route_gw, (struct sockaddr *)&sa); addr = sa.sin_addr.s_addr; anubis_verbose("Default route: %s\n", anubis_ip_ntoa(addr)); }//end if else { anubis_err("anubis_default_route(): Default gateway is not found\n"); }//end else route_close(handle); return addr; }//end anubis_default_route
/* * Sample the tablestore ring set up by cascade_init and described in CASCADE. * See the description above for how the thing works. * The computed table is sent to the output route and errors are sent to * the error route. Returns 0 for success or -1 for failure. */ int cascade_sample(CASCADE *session, /* cascade reference */ ROUTE output, /* output route */ ROUTE error /* error route */ ) { ROUTE rt; int r, seq, size, ret=0; time_t modt; TABLE dataset, result; /* attempt to read from the current point to the last */ rt = route_open(session->purl, NULL, NULL, 0); if ( ! rt ) { elog_printf(ERROR, "route does not exist %s", session->purl); return -1; /* monitored route does not exist (yet). * so return successfully so we can try again * next time */ } dataset = route_seektread(rt, session->seq, -1); r = route_tell(rt, &seq, &size, &modt); route_close(rt); if ( ! dataset ) { /* NULL dataset is not an error here, just an indicaton that we * don't have any data to return and thus process */ elog_printf(DIAG, "no data to cascade sample in %s", session->purl); return 0; /* success */ } if ( table_nrows(dataset) == 0 ) { /* Empty table means the same as above */ elog_printf(DIAG, "no rows to cascade sample in %s", session->purl); table_destroy(dataset); return 0; /* success */ } /* update session sequence for next time */ session->seq = seq+1; /* now carry out the aggregation on the table */ result = cascade_aggregate(session->fn, dataset); /* save the results */ if ( result ) { if ( ! route_twrite(output, result) ) { elog_printf(ERROR, "unable to write result"); ret = -1; } table_destroy(result); } else { elog_printf(ERROR, "no results produced from cascade"); ret = -1; } if (dataset) table_destroy(dataset); return ret; /* success */ }
/* finalise and deconstruct the eventlog */ void elog_fini() { int i, j; elog_checkinit(); /* close any open purls, that is severities for which elog_ opened * a route. Routes present in elog_opendest[] without purl values * were suplied externally and it is their responsibility to clear up! */ for (i=0; i < ELOG_NSEVERITIES; i++) if (elog_opendest[i].purl) { /* is there another severity with the same purl? * If so, clear that severity */ for (j=i+1; j < ELOG_NSEVERITIES; j++) { if ( elog_opendest[j].purl && strcmp(elog_opendest[j].purl, elog_opendest[i].purl) == 0 ) { nfree(elog_opendest[j].purl); elog_opendest[j].purl = NULL; elog_opendest[j].route = NULL; } } /* close old route opened and owned by us */ route_close(elog_opendest[i].route); nfree(elog_opendest[i].purl); elog_opendest[i].purl = NULL; elog_opendest[i].route = NULL; } /* free any format strings */ for (i=0; i < ELOG_NSEVERITIES; i++) if (elog_opendest[i].format) { nfree(elog_opendest[i].format); elog_opendest[i].format = NULL; } tree_clearoutandfree(elog_override); tree_destroy(elog_override); if (elog_origin) nfree(elog_origin); route_close(elog_errors); }
route_t * route_open(void) { route_t *r; if ((r = calloc(1, sizeof(*r))) != NULL) { r->fd = -1; #ifdef HAVE_STREAMS_MIB2 if ((r->ip_fd = open(IP_DEV_NAME, O_RDWR)) < 0) return (route_close(r)); #endif #ifdef HAVE_STREAMS_ROUTE if ((r->fd = open("/dev/route", O_RDWR, 0)) < 0) #else if ((r->fd = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) #endif return (route_close(r)); } return (r); }
tun_t * tun_open(struct addr *src, struct addr *dst, int mtu) { struct intf_entry ifent; tun_t *tun; char dev[128]; int i; if (src->addr_type != ADDR_TYPE_IP || dst->addr_type != ADDR_TYPE_IP || src->addr_bits != IP_ADDR_BITS || dst->addr_bits != IP_ADDR_BITS) { errno = EINVAL; return (NULL); } if ((tun = calloc(1, sizeof(*tun))) == NULL) return (NULL); if ((tun->intf = intf_open()) == NULL) return (tun_close(tun)); memset(&ifent, 0, sizeof(ifent)); ifent.intf_len = sizeof(ifent); for (i = 0; i < MAX_DEVS; i++) { snprintf(dev, sizeof(dev), "/dev/tun%d", i); strlcpy(ifent.intf_name, dev + 5, sizeof(ifent.intf_name)); tun->save = ifent; if ((tun->fd = open(dev, O_RDWR, 0)) != -1 && intf_get(tun->intf, &tun->save) == 0) { route_t *r; struct route_entry entry; ifent.intf_flags = INTF_FLAG_UP|INTF_FLAG_POINTOPOINT; ifent.intf_addr = *src; ifent.intf_dst_addr = *dst; ifent.intf_mtu = mtu; if (intf_set(tun->intf, &ifent) < 0) tun = tun_close(tun); /* XXX - try to ensure our route got set */ if ((r = route_open()) != NULL) { entry.route_dst = *dst; entry.route_gw = *src; route_add(r, &entry); route_close(r); } break; } } if (i == MAX_DEVS) tun = tun_close(tun); return (tun); }
static int fragroute_close(void) { if (ctx.tun != NULL) tun_close(ctx.tun); if (ctx.route != NULL) route_close(ctx.route); if (ctx.intf != NULL) intf_close(ctx.intf); if (ctx.eth != NULL) eth_close(ctx.eth); if (ctx.arp != NULL) arp_close(ctx.arp); if (ctx.dfile != NULL) pcap_dump_close(ctx.dfile); #ifdef WIN32 WSACleanup(); #endif return (-1); }
route_t * route_open(void) { struct sockaddr_nl snl; route_t *r; if ((r = calloc(1, sizeof(*r))) != NULL) { r->fd = r->nlfd = -1; if ((r->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return (route_close(r)); if ((r->nlfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) return (route_close(r)); memset(&snl, 0, sizeof(snl)); snl.nl_family = AF_NETLINK; if (bind(r->nlfd, (struct sockaddr *)&snl, sizeof(snl)) < 0) return (route_close(r)); } return (r); }
int main(void) { route_t *r; r=route_open(); if ( r == NULL ) { perror("Route open error"); exit(-1); } if ( route_loop(r, print_route_entry, NULL) == -1 ) { fprintf(stderr, "Error in route table print\n"); exit(-1); } route_close(r); exit(0); }
int main(int argc, char* argv[]) { if (getuid() != 0) { printf("You need to be root\n"); return 1; } route_t* router = route_open(); struct route_entry entry; addr_pton("8.8.8.8", &entry.route_dst); route_get(router, &entry); printf("gateway is %s\n", addr_ntoa(&entry.route_gw)); route_close(router); return 0; }
/* Set a route for a given severity to be an already opened ROUTE supplied * by the caller. * If a route already exists for that severity, it will NOT be closed * before being overwritten, unless elog_ had opened the route from a purl * (with elog_setsevpurl()) and it is not in use for any other severity. * If that is the case, then the route will be closed. * Returns 1 for success, 0 for failure */ int elog_setsevroute(enum elog_severity severity, /* severity level */ ROUTE route /* opened route */ ) { int i; elog_checkinit(); /* severity in range? */ if (severity >= ELOG_NSEVERITIES || severity < 0) return 0; /* Do we have to bother to clear up? */ if (elog_opendest[severity].purl != NULL) { /* elog_ opened this routine with setsevpurl(); * is there another severity with the same route? */ for (i=0; i < ELOG_NSEVERITIES; i++) { if (i == severity) continue; /* don't count ourselves */ if (elog_opendest[severity].route == elog_opendest[i].route) { nfree(elog_opendest[severity].purl); goto noclose; /* route is used elsewhere */ } } /* we have to clearup the old route as we allocated it but * no other severity is using it */ route_close(elog_opendest[severity].route); nfree(elog_opendest[severity].purl); } noclose: elog_opendest[severity].purl = NULL; elog_opendest[severity].route = route; return 1; }
/* run though a series of tests for the current cascade configuration */ void test_cascade(enum cascade_fn mode, char *mode_label, char *tab_sing, char *tab_singinfo, char *tab_singinfokey, char *tab_mult, char *tab_multinfo, char *tab_multinfokey, char *result_sing, char *result_singkey, char *result_mult, char *result_multkey) { int r, resseq, resoff; ROUTE resrt, samprt; CASCADE *cas; char *buf1, *resbuf1, *wantbuf1; TABLE tab1, restab1, wanttab1; time_t modt; /* [1] run cascade aggregation on empty tables */ tab1 = table_create(); restab1 = cascade_aggregate(mode, tab1); if (restab1) elog_die(FATAL, "[1] should return NULL when aggregating an " "empty table"); /* [2] aggregate a single sample table, no info, no key */ buf1 = xnstrdup(tab_sing); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); wanttab1 = table_create(); wantbuf1 = xnstrdup(result_sing); table_scan(wanttab1, wantbuf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(wanttab1, wantbuf1); table_rminfo(wanttab1, "info"); /* remove info line */ restab1 = cascade_aggregate(mode, tab1); if ( ! restab1) elog_die(FATAL, "[2a] can't aggregate table: " "mode %s, single sample, no info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); wantbuf1 = table_outtable(wanttab1); if (strcmp(wantbuf1, resbuf1)) elog_die(FATAL, "[2b] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, wantbuf1); nfree(buf1); nfree(resbuf1); nfree(wantbuf1); table_destroy(tab1); table_destroy(restab1); table_destroy(wanttab1); /* [3] aggregate a single sample table, with info but no key */ tab1 = table_create(); buf1 = xnstrdup(tab_singinfo); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); restab1 = cascade_aggregate(mode, tab1); if ( ! restab1) elog_die(FATAL, "[3a] can't aggregate table: " "mode %s, single sample, with info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_sing, resbuf1)) elog_die(FATAL, "[3b] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_sing); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* [4] aggregate a single sample table, with info and key */ tab1 = table_create(); buf1 = xnstrdup(tab_singinfokey); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); restab1 = cascade_aggregate(mode, tab1); if ( ! restab1) elog_die(FATAL, "[4a] can't aggregate table: " "mode %s, single sample, with info and key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_singkey, resbuf1)) elog_die(FATAL, "[4b] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_singkey); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* [5] aggregate a multi sample table, no info, no key */ tab1 = table_create(); buf1 = xnstrdup(tab_mult); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); wanttab1 = table_create(); wantbuf1 = xnstrdup(result_mult); table_scan(wanttab1, wantbuf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(wanttab1, wantbuf1); table_rminfo(wanttab1, "info"); /* remove info line */ restab1 = cascade_aggregate(mode, tab1); if ( ! restab1) elog_die(FATAL, "[5a] can't aggregate table: " "mode %s, multi sample, no info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); wantbuf1 = table_outtable(wanttab1); if (strcmp(wantbuf1, resbuf1)) elog_die(FATAL, "[5b] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, wantbuf1); nfree(buf1); nfree(resbuf1); nfree(wantbuf1); table_destroy(tab1); table_destroy(restab1); table_destroy(wanttab1); /* [6] aggregate a multi sample table, with info but no key */ tab1 = table_create(); buf1 = xnstrdup(tab_multinfo); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); restab1 = cascade_aggregate(mode, tab1); if ( ! restab1) elog_die(FATAL, "[6a] can't aggregate table: " "mode %s, multi sample, with info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_mult, resbuf1)) elog_die(FATAL, "[6b] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_mult); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* [7] aggregate a multi sample table, with info and key */ tab1 = table_create(); buf1 = xnstrdup(tab_multinfokey); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); restab1 = cascade_aggregate(mode, tab1); if ( ! restab1) elog_die(FATAL, "[7a] can't aggregate table: " "mode %s, multi sample, with info and key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_multkey, resbuf1)) elog_die(FATAL, "[7b] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_multkey); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* * And now the route based methods. * We run through the same tests using routes to store the samples * also the results */ /* unlink previous storage */ unlink(RS_SAMPFILE); unlink(RS_RESFILE); /* [8] create sample and result routes */ resrt = route_open(RS_RESPURL, "Output of testing results", NULL, 20); if ( ! resrt ) elog_die(FATAL, "[8a] Can't open result route"); samprt = route_open(RS_SAMPPURL, "Samples under test", NULL, 20); if ( ! samprt ) elog_die(FATAL, "[8b] Can't open result route"); /* [9] run cascade on an empty ring and sample several times * where there is no change */ cas = cascade_init(mode, RS_SAMPPURL); if ( ! cas) elog_die(FATAL, "[9a] can't start cascade"); r = cascade_sample(cas, out, err); if ( r ) elog_die(FATAL, "[9b] cascade sample failed"); r = cascade_sample(cas, out, err); if ( r ) elog_die(FATAL, "[9c] cascade sample failed"); /* [10] add tab_sing to table and sample */ tab1 = table_create(); buf1 = xnstrdup(tab_sing); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); wanttab1 = table_create(); wantbuf1 = xnstrdup(result_sing); table_scan(wanttab1, wantbuf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(wanttab1, wantbuf1); table_rminfo(wanttab1, "info"); /* remove info line */ r = route_twrite(samprt, tab1); if ( r < 0 ) elog_die(FATAL, "[10a] add table failed"); r = cascade_sample(cas, resrt, err); if ( r ) elog_die(FATAL, "[10b] cascade sample failed, mode %s", mode_label); r = route_tell(resrt, &resseq, &resoff, &modt); if ( !r ) elog_die(FATAL, "[10c] can't route_tell(), mode %s", mode_label); restab1 = route_seektread(resrt, resseq, 0); if ( ! restab1) elog_die(FATAL, "[10d] can't read result ring: " "mode %s, single sample, no info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); wantbuf1 = table_outtable(wanttab1); if (strcmp(wantbuf1, resbuf1)) elog_die(FATAL, "[10e] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, wantbuf1); nfree(buf1); nfree(resbuf1); nfree(wantbuf1); table_destroy(tab1); table_destroy(restab1); table_destroy(wanttab1); /* [11] add tab_singinfo to table and sample */ tab1 = table_create(); buf1 = xnstrdup(tab_singinfo); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); r = route_twrite(samprt, tab1); if ( r < 0 ) elog_die(FATAL, "[11a] add table failed"); r = cascade_sample(cas, resrt, err); if ( r ) elog_die(FATAL, "[11b] cascade sample failed, mode %s", mode_label); r = route_tell(resrt, &resseq, &resoff, &modt); if ( !r ) elog_die(FATAL, "[11c] can't route_tell(), mode %s", mode_label); restab1 = route_seektread(resrt, resseq, 0); if ( ! restab1) elog_die(FATAL, "[11d] can't read result ring: " "mode %s, single sample, with info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_sing, resbuf1)) elog_die(FATAL, "[11e] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_sing); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* [12] add tab_singinfokey to table and sample */ tab1 = table_create(); buf1 = xnstrdup(tab_singinfokey); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); r = route_twrite(samprt, tab1); if ( r < 0 ) elog_die(FATAL, "[12a] add table failed"); r = cascade_sample(cas, resrt, err); if ( r ) elog_die(FATAL, "[12b] cascade sample failed, mode %s", mode_label); r = route_tell(resrt, &resseq, &resoff, &modt); if ( !r ) elog_die(FATAL, "[12c] can't route_tell(), mode %s", mode_label); restab1 = route_seektread(resrt, resseq, 0); if ( ! restab1) elog_die(FATAL, "[12d] can't read result ring: " "mode %s, single sample, with info and key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_singkey, resbuf1)) elog_die(FATAL, "[12e] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_sing); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* [13] add tab_mult to table and sample */ tab1 = table_create(); buf1 = xnstrdup(tab_mult); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); wanttab1 = table_create(); wantbuf1 = xnstrdup(result_mult); table_scan(wanttab1, wantbuf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(wanttab1, wantbuf1); table_rminfo(wanttab1, "info"); /* remove info line */ r = route_twrite(samprt, tab1); if ( r < 0 ) elog_die(FATAL, "[13a] add table failed"); r = cascade_sample(cas, resrt, err); if ( r ) elog_die(FATAL, "[13b] cascade sample failed, mode %s", mode_label); r = route_tell(resrt, &resseq, &resoff, &modt); if ( !r ) elog_die(FATAL, "[13c] can't route_tell(), mode %s", mode_label); restab1 = route_seektread(resrt, resseq, 0); if ( ! restab1) elog_die(FATAL, "[13d] can't read result ring: " "mode %s, multi sample, no info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); wantbuf1 = table_outtable(wanttab1); if (strcmp(wantbuf1, resbuf1)) elog_die(FATAL, "[13e] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, wantbuf1); nfree(buf1); nfree(resbuf1); nfree(wantbuf1); table_destroy(tab1); table_destroy(restab1); table_destroy(wanttab1); /* [14] add tab_multinfo to table and sample */ tab1 = table_create(); buf1 = xnstrdup(tab_multinfo); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); r = route_twrite(samprt, tab1); if ( r < 0 ) elog_die(FATAL, "[14a] add table failed"); r = cascade_sample(cas, resrt, err); if ( r ) elog_die(FATAL, "[14b] cascade sample failed, mode %s", mode_label); r = route_tell(resrt, &resseq, &resoff, &modt); if ( !r ) elog_die(FATAL, "[14c] can't route_tell(), mode %s", mode_label); restab1 = route_seektread(resrt, resseq, 0); if ( ! restab1) elog_die(FATAL, "[14d] can't read result ring: " "mode %s, multi sample, with info, no key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_mult, resbuf1)) elog_die(FATAL, "[14e] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_mult); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* [15] add tab_multinfokey to table and sample */ tab1 = table_create(); buf1 = xnstrdup(tab_multinfokey); table_scan(tab1, buf1, "\t", TABLE_SINGLESEP, TABLE_HASCOLNAMES, TABLE_HASRULER); table_freeondestroy(tab1, buf1); r = route_twrite(samprt, tab1); if ( r < 0 ) elog_die(FATAL, "[15a] add table failed"); r = cascade_sample(cas, resrt, err); if ( r ) elog_die(FATAL, "[15b] cascade sample failed, mode %s", mode_label); r = route_tell(resrt, &resseq, &resoff, &modt); if ( !r ) elog_die(FATAL, "[15c] can't route_tell(), mode %s", mode_label); restab1 = route_seektread(resrt, resseq, 0); if ( ! restab1) elog_die(FATAL, "[15d] can't read result ring: " "mode %s, multi sample, with info and key", mode_label); table_rmcol(restab1, "_dur"); table_rmcol(restab1, "_seq"); table_replaceinfocell(restab1, "type", "_time", "i32"); table_replaceinfocell(restab1, "key", "_time", "-"); buf1 = table_outtable(tab1); resbuf1 = table_outtable(restab1); if (strcmp(result_multkey, resbuf1)) elog_die(FATAL, "[15e] aggregation failed, mode %s:-\n" "--- in ---\n%s\n--- out ---\n%s\n--- want ---\n%s", mode_label, buf1, resbuf1, result_multkey); nfree(buf1); nfree(resbuf1); table_destroy(tab1); table_destroy(restab1); /* shutdown */ cascade_fini(cas); route_close(resrt); route_close(samprt); }
/* set a route for a severity, as _setsevroute(). But open a purl to * obtain the route. If the route is already known, reuse it. * return 0 for failure or 1 for success. */ int elog_setsevpurl(enum elog_severity severity, /* severity level */ char *purl /* purl of route */ ) { ROUTE lroute=NULL; int i; elog_checkinit(); /* severity in range? */ if (severity >= ELOG_NSEVERITIES || severity < 0) return 0; /* do we already have this purl open? */ if ( elog_opendest[severity].purl && strcmp(purl, elog_opendest[severity].purl) == 0 ) return 1; /* scan already open routes to reuse them */ for (i=0; i < ELOG_NSEVERITIES; i++) { if (elog_opendest[i].purl && strcmp(purl, elog_opendest[i].purl) == 0 ) { /* a match is found, reuse it */ lroute = elog_opendest[i].route; break; } } /* if no existing open route: attempt to open a new one */ if ( ! lroute ) lroute = route_open(purl, "Event log", NULL, ELOG_KEEPDEF); if ( ! lroute) { elog_printf(ERROR, "unable to open %s to log errors\n", purl); return 0; } /* now close down the existing route for this severity */ if (elog_opendest[severity].purl) { /* is there another severity with the same purl? */ for (i=0; i < ELOG_NSEVERITIES; i++) { if (i == severity) continue; /* don't count ourselves */ if (elog_opendest[i].purl && strcmp(elog_opendest[severity].purl, elog_opendest[i].purl) == 0) { nfree(elog_opendest[severity].purl); goto noclose2; } } /* we have to clearup the old route as we allocated it but * no other severity is using it */ route_close(elog_opendest[severity].route); nfree(elog_opendest[severity].purl); } noclose2: /* comit the allocation */ elog_opendest[severity].purl = xnstrdup(purl); elog_opendest[severity].route = lroute; return 1; }
int main(int argc, char **argv) { ROUTE err, saveroute; int i; TABLE tab; char *str; route_init(NULL, 0); route_register(&rt_filea_method); route_register(&rt_fileov_method); route_register(&rt_stdin_method); route_register(&rt_stdout_method); route_register(&rt_stderr_method); route_register(&rt_rs_method); rs_init(); if ( ! elog_init(1, argv[0], NULL)) elog_die(FATAL, "Didn't initialise elog\n"); err = route_open("stderr", NULL, NULL, 0); /* first lot of messages sent to the default places */ elog_send(INFO, "This is an eventlog test"); elog_send(INFO, NULL); elog_send(INFO, ""); elog_send(INFO, "Event!!"); elog_send(DEBUG, "Event!!"); elog_send(WARNING, "Event!!"); elog_send(ERROR, "Event!!"); elog_send(INFO, "Event!!"); /* change origin */ elog_setorigin("etest"); elog_send(INFO, "test of set origin"); /* set one new purl route */ elog_setsevpurl(DEBUG, FILE1); elog_send(INFO, "on screen"); elog_send(DEBUG, "in file"); elog_send(WARNING, "on screen"); /* set second identical purl route to reuse the previous one */ elog_setsevpurl(ERROR, FILE1); if (elog_opendest[DEBUG].route != elog_opendest[ERROR].route) route_die(err, "[13] didnt reuse already open DEBUG route\n"); elog_send(ERROR, "in file"); /* set identical below purl route */ if ( !elog_setbelowpurl(INFO, FILE1)) route_die(err, "[14] unable to setbelowpurl() file\n"); if (elog_opendest[DEBUG].route != elog_opendest[ERROR].route || elog_opendest[INFO].route != elog_opendest[ERROR].route) route_die(err, "[14] didnt reuse already open ERROR route\n"); elog_send(DEBUG, "in file"); elog_send(INFO, "in file"); elog_send(WARNING, "on screen"); elog_send(ERROR, "in file"); elog_send(FATAL, "on screen"); /* set identical above purl route */ if ( !elog_setabovepurl(ERROR, FILE1)) route_die(err, "[19] unable to setabovepurl() file\n"); if (elog_opendest[ERROR].route != elog_opendest[INFO].route || elog_opendest[FATAL].route != elog_opendest[INFO].route) route_die(err, "[19] didnt reuse already open INFO route\n"); elog_send(DEBUG, "in file"); elog_send(INFO, "in file"); elog_send(WARNING, "on screen"); elog_send(ERROR, "in file"); elog_send(FATAL, "in file"); /* set identical all purl route */ saveroute = elog_opendest[DEBUG].route; if ( !elog_setallpurl(FILE1)) route_die(err, "[24] unable to setallpurl() file\n"); for (i=0; i < ELOG_NSEVERITIES; i++) if (elog_opendest[i].route != saveroute) route_die(err, "[24] didnt reuse already open %s route\n", elog_sevstring[i]); elog_send(DEBUG, "in file"); elog_send(INFO, "in file"); elog_send(WARNING, "in file"); elog_send(ERROR, "in file"); elog_send(FATAL, "in file"); /* set one different purl - timestore that we currently have to set up * ourselves */ saveroute = route_open(RS1, "event log test", NULL, 10); if ( ! saveroute) route_die(err, "[29] unable to create/open timestore\n"); route_close(saveroute); if ( ! elog_setsevpurl(INFO, RS1)) route_die(err, "[29] unable to setsevpurl() timestore\n"); if (elog_opendest[INFO].route == elog_opendest[WARNING].route) route_die(err, "[29] different route same as WARNING\n"); elog_send(DEBUG, "in file"); elog_send(INFO, "in timestore"); elog_send(WARNING, "in file"); elog_send(ERROR, "in file"); elog_send(FATAL, "in file"); /* set one different route */ #if 0 elog_send(INFO, ""); elog_send(INFO, "Event!!"); elog_send(DEBUG, "Event!!"); elog_send(WARNING, "Event!!"); elog_send(ERROR, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); elog_send(INFO, "Event!!"); #endif /* change format */ elog_setsevroute(WARNING, err); elog_setformat(WARNING, "%s %s"); elog_send(WARNING, "still works??"); /* safe logging */ elog_safeprintf(INFO, "This is an eventlog test 35"); elog_safeprintf(INFO, NULL); elog_safeprintf(INFO, ""); elog_safeprintf(INFO, "Event!! 38"); elog_safeprintf(DEBUG, "Event!! 39"); elog_safeprintf(WARNING, "Event!! 40"); elog_safeprintf(ERROR, "Event!! 41"); elog_safeprintf(INFO, "Event!!"); /* print the status out */ tab = elog_getstatus(); str = table_printcols_a(tab, elog_colnames); printf("%s\n", str); nfree(str); table_destroy(tab); rs_fini(); elog_fini(); route_close(err); route_fini(); exit(0); }