Exemplo n.º 1
0
/*
 *  appTeardown()
 *
 *    Teardown all modules, close all files, and tidy up all
 *    application state.
 *
 *    This function is idempotent.
 */
static void
appTeardown(
    void)
{
    static int teardownFlag = 0;
    int rv;

    if (teardownFlag) {
        return;
    }
    teardownFlag = 1;

    /* close SiLK flow output file */
    if (silk_output) {
        rv = skStreamClose(silk_output);
        if (rv && rv != SKSTREAM_ERR_NOT_OPEN) {
            skStreamPrintLastErr(silk_output, rv, &skAppPrintErr);
        }
        skStreamDestroy(&silk_output);
    }

    skpcTeardown();

    /* set level to "warning" to avoid the "Stopped logging" message */
    sklogSetLevel("warning");
    sklogTeardown();

    skOptionsNotesTeardown();
    skOptionsCtxDestroy(&optctx);
    skAppUnregister();
}
Exemplo n.º 2
0
int main(int UNUSED(argc), char UNUSED(**argv))
{
    skDQErr_t err;
    pthread_t a, b, c;
    int rv;
    char *v;

    skAppRegister("skdeque-test");
    skthread_init("main");

    rv = sklogSetup(0);
    assert(rv == 0);
    rv = sklogSetDestination("stderr");
    assert(rv == 0);
    rv = sklogOpen();
    assert(rv == 0);
    rv = sklogSetLevel("debug");
    assert(rv == 0);
    rv = sklogEnableThreadedLogging();
    assert(rv == 0);
    rv = sklogSetStampFunction(&log_stamp);
    assert(rv == 0);


    da = skDequeCreate();
    db = skDequeCreate();
    dc = skDequeCreate();

    /*** Single threaded tests ***/

    /* check empty deque */
    CHECK_EMPTY(da);

    /* push first element onto 'da' and check */
    err = skDequePushFront(da, (void*)xa);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequeBack(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    err = skDequeFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    XASSERT(1 == skDequeSize(da));

    /* push first element onto 'db' and check */
    err = skDequePushBack(db, (void*)xb);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequeBack(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xb);
    err = skDequeFront(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xb);
    XASSERT(1 == skDequeSize(db));

    /* push first element onto 'dc' */
    err = skDequePushFront(dc, (void*)xc);
    XASSERT(err == SKDQ_SUCCESS);

    /* push second element onto front 'da' and check */
    err = skDequePushFront(da, (void*)xx);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequeBack(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    err = skDequeFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    XASSERT(2 == skDequeSize(da));

    /* push second element onto 'db' and 'dc' */
    err = skDequePushFront(db, (void*)xy);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequePushFront(dc, (void*)xz);
    XASSERT(err == SKDQ_SUCCESS);

    /* push third element onto back of 'da' and check */
    err = skDequePushBack(da, (void*)xa);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequeBack(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    err = skDequeFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    XASSERT(3 == skDequeSize(da));

    /* push third element onto back of 'db' and 'dc' */
    err = skDequePushBack(db, (void*)xb);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequePushBack(dc, (void*)xc);
    XASSERT(err == SKDQ_SUCCESS);

    /* push fourth element onto back of 'da' and check */
    err = skDequePushBack(da, (void*)xx);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequeBack(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    err = skDequeFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    XASSERT(4 == skDequeSize(da));

    /* push fourth element onto back of 'db' and 'dc' */
    err = skDequePushBack(db, (void*)xy);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequePushBack(dc, (void*)xz);
    XASSERT(err == SKDQ_SUCCESS);

    /* pop four elements from 'da', each from the front */
    err = skDequePopFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    XASSERT(3 == skDequeSize(da));
    err = skDequePopFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    XASSERT(2 == skDequeSize(da));
    err = skDequePopFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    XASSERT(1 == skDequeSize(da));
    err = skDequePopFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    XASSERT(0 == skDequeSize(da));

    /* verify 'da' is empty */
    CHECK_EMPTY(da);

    /* pop four elements from 'db', each from the back */
    err = skDequePopBack(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xy);
    XASSERT(3 == skDequeSize(db));
    err = skDequePopBack(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xb);
    XASSERT(2 == skDequeSize(db));
    err = skDequePopBack(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xb);
    XASSERT(1 == skDequeSize(db));
    err = skDequePopBack(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xy);
    XASSERT(0 == skDequeSize(db));

    /* verify 'db' is empty */
    CHECK_EMPTY(db);

    /* pop four elements from 'dc' */
    err = skDequePopFrontNB(dc, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xz);
    err = skDequePopBackNB(dc, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xz);
    err = skDequePopFrontNB(dc, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xc);
    err = skDequePopBackNB(dc, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xc);
    err = skDequePopBackNB(dc, (void**)&v);
    XASSERT(err == SKDQ_EMPTY);

    /* verify 'dc' is empty */
    CHECK_EMPTY(dc);

    /* create a merged queue from 'da' and 'db' */
    dd = skDequeCreateMerged(da, db);

    /* verify 'dd' is empty */
    CHECK_EMPTY(dd);

    /* push the first element onto each of 'da' and 'db' */
    err = skDequePushBack(da, (void*)xa);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequePushBack(db, (void*)xx);
    XASSERT(err == SKDQ_SUCCESS);

    /* check sizes */
    XASSERT(1 == skDequeSize(da));
    XASSERT(1 == skDequeSize(db));
    XASSERT(2 == skDequeSize(dd));

    /* peek at front and back of merged deck */
    err = skDequeBack(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    err = skDequeFront(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);

    /* push the second element onto each of 'da' and 'db' */
    err = skDequePushBack(da, (void*)xb);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequePushBack(db, (void*)xy);
    XASSERT(err == SKDQ_SUCCESS);

    /* peek at front and back of merged deck */
    err = skDequeBack(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xy);
    err = skDequeFront(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);

    /* push a third element onto front and back of merged deck */
    err = skDequePushFront(dd, (void*)xc);
    XASSERT(err == SKDQ_SUCCESS);
    err = skDequePushBack(db, (void*)xz);
    XASSERT(err == SKDQ_SUCCESS);

    /* check sizes */
    XASSERT(3 == skDequeSize(da));
    XASSERT(3 == skDequeSize(db));
    XASSERT(6 == skDequeSize(dd));

    /* peek at front and back of 'da' */
    err = skDequeBack(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xb);
    err = skDequeFront(da, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xc);

    /* peek at front and back of 'db' */
    err = skDequeBack(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xz);
    err = skDequeFront(db, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);

    /* pop three elements from front of 'dd' */
    XASSERT(6 == skDequeSize(dd));
    err = skDequePopFrontNB(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xc);
    err = skDequePopFrontNB(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xa);
    err = skDequePopFrontNB(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xb);

    /* verify 'da' is empty */
    CHECK_EMPTY(da);

    /* pop three elements from front of 'dd' */
    XASSERT(3 == skDequeSize(dd));
    XASSERT(3 == skDequeSize(db));
    err = skDequePopFrontNB(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xx);
    err = skDequePopFrontNB(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xy);
    err = skDequePopFrontNB(dd, (void**)&v);
    XASSERT(err == SKDQ_SUCCESS);
    XASSERT(v == xz);

    /* verify 'db' is empty */
    CHECK_EMPTY(db);

    /* verify 'dd' is empty */
    CHECK_EMPTY(dd);

    /* done with 'dd' */
    skDequeDestroy(dd);

    /* verify 'da' and 'db' are still empty (and valid) */
    CHECK_EMPTY(da);
    CHECK_EMPTY(db);


    /*** Multi-threaded tests ***/

    rv = skthread_create("a", &a, thread_a, NULL);
    XASSERT(rv == 0);
    rv = skthread_create("b", &b, thread_b, NULL);
    XASSERT(rv == 0);
    rv = skthread_create("c", &c, thread_c, NULL);
    XASSERT(rv == 0);

    rv = pthread_join(a, NULL);
    XASSERT(rv == 0);
    rv = pthread_join(b, NULL);
    XASSERT(rv == 0);
    rv = pthread_join(c, NULL);
    XASSERT(rv == 0);

    skDequeDestroy(dc);
    skDequeDestroy(db);
    skDequeDestroy(da);

    sklogClose();
    sklogTeardown();
    skthread_teardown();
    skAppUnregister();

    return 0;
}
Exemplo n.º 3
0
/*
 *  appSetup(argc, argv);
 *
 *    Perform all the setup for this application include setting up
 *    required modules, parsing options, etc.  This function should be
 *    passed the same arguments that were passed into main().
 *
 *    Returns to the caller if all setup succeeds.  If anything fails,
 *    this function will cause the application to exit with a FAILURE
 *    exit status.
 */
static void
appSetup(
    int                 argc,
    char              **argv)
{
    SILK_FEATURES_DEFINE_STRUCT(features);
    unsigned int optctx_flags;
    sk_file_header_t *silk_hdr;
    int logmask;
    int rv;

    /* verify same number of options and help strings */
    assert((sizeof(appHelp)/sizeof(char *)) ==
           (sizeof(appOptions)/sizeof(struct option)));

    /* register the application */
    skAppRegister(argv[0]);
    skAppVerifyFeatures(&features, NULL);
    skOptionsSetUsageCallback(&appUsageLong);

    optctx_flags = (SK_OPTIONS_CTX_INPUT_BINARY | SK_OPTIONS_CTX_XARGS);

    /* register the options */
    if (skOptionsCtxCreate(&optctx, optctx_flags)
        || skOptionsCtxOptionsRegister(optctx)
        || skOptionsRegister(appOptions, &appOptionsHandler, NULL)
        || skOptionsNotesRegister(NULL)
        || skCompMethodOptionsRegister(&comp_method))
    {
        skAppPrintErr("Unable to register options");
        exit(EXIT_FAILURE);
    }

    /* enable the logger */
    sklogSetup(0);
    sklogSetDestination("stderr");
    sklogSetStampFunction(&logprefix);

    /* register the teardown handler */
    if (atexit(appTeardown) < 0) {
        skAppPrintErr("Unable to register appTeardown() with atexit()");
        appTeardown();
        exit(EXIT_FAILURE);
    }

    /* parse the options */
    rv = skOptionsCtxOptionsParse(optctx, argc, argv);
    if (rv < 0) {
        skAppUsage();           /* never returns */
    }

    if ('\0' == log_destination[0]) {
        strncpy(log_destination, LOG_DESTINATION_DEFAULT,
                sizeof(log_destination));
    } else {
        sklogSetLevel("debug");
    }
    sklogSetDestination(log_destination);

    /* default output is "stdout" */
    if (!silk_output) {
        if ((rv =skStreamCreate(&silk_output,SK_IO_WRITE,SK_CONTENT_SILK_FLOW))
            || (rv = skStreamBind(silk_output, "-")))
        {
            skStreamPrintLastErr(silk_output, rv, &skAppPrintErr);
            exit(EXIT_FAILURE);
        }
    }

    /* get the header */
    silk_hdr = skStreamGetSilkHeader(silk_output);

    /* open the output */
    if ((rv = skHeaderSetCompressionMethod(silk_hdr, comp_method))
        || (rv = skOptionsNotesAddToStream(silk_output))
        || (rv = skHeaderAddInvocation(silk_hdr, 1, argc, argv))
        || (rv = skStreamOpen(silk_output))
        || (rv = skStreamWriteSilkHeader(silk_output)))
    {
        skStreamPrintLastErr(silk_output, rv, &skAppPrintErr);
        exit(EXIT_FAILURE);
    }

    if (skpcSetup()) {
        exit(EXIT_FAILURE);
    }
    if (skpcProbeCreate(&probe, PROBE_ENUM_NETFLOW_V5)) {
        exit(EXIT_FAILURE);
    }
    skpcProbeSetName(probe, skAppName());
    skpcProbeSetFileSource(probe, "/dev/null");
    if (parseLogFlags(log_flags)) {
        exit(EXIT_FAILURE);
    }
    if (skpcProbeVerify(probe, 0)) {
        exit(EXIT_FAILURE);
    }

    /* set level to "warning" to avoid the "Started logging" message */
    logmask = sklogGetMask();
    sklogSetLevel("warning");
    sklogOpen();
    sklogSetMask(logmask);

    return;                     /* OK */
}