Exemple #1
0
CTEST(txn, begin_with_no_parent)
{
	int r;
	struct txn *tx1;
	struct txn *tx2;
	struct txn *tx3;
	struct txnmgr *tm = txnmgr_new();
	LOGGER *logger = logger_new(NULL, tm);

	/* transaction 1 */
	r = txn_begin(NULL, logger, TXN_ISO_REPEATABLE, &tx1);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(0, tx1->txnid);
	ASSERT_EQUAL(1, tm->live_root_txnids->used);
	ASSERT_EQUAL(tx1->txnid, tm->live_root_txnids->txnids[0]);

	/* transaction 2 */
	r = txn_begin(NULL, logger, TXN_ISO_REPEATABLE, &tx2);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(1, tx2->txnid);
	ASSERT_EQUAL(tx1->txnid, tx2->txnid_clone->txnids[0]);
	ASSERT_EQUAL(tx2->txnid, tx2->txnid_clone->txnids[1]);

	/* transaction 3 */
	r = txn_begin(NULL, logger, TXN_ISO_SERIALIZABLE, &tx3);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(2, tx3->txnid);
	ASSERT_NULL(tx3->txnid_clone);

	logger_free(logger);
	txnmgr_free(tm);
}
Exemple #2
0
int
main () {
	logger_t *test_logger = logger_new("test");
	assert(test_logger);
	assert(test_logger->name == "test");
	test_logger->log("log");
	test_logger->info("info");
	test_logger->debug("debug");
	test_logger->error("error");
	logger_free(test_logger);
}
Exemple #3
0
/*
 * Log pre-init: open all log files but don't start any threads, since we may
 * fork() after pre-initialization.
 * Return -1 on errors, 0 otherwise.
 */
int
log_preinit(opts_t *opts)
{
	if (opts->contentlog) {
		if (!opts->contentlogdir) {
			if (log_content_open_singlefile(opts->contentlog)
			    == -1)
				goto out;
		} else {
			if (log_content_open_logdir(opts->contentlog) == -1)
				goto out;
		}
		if (!(content_log = logger_new(log_content_writecb))) {
			log_content_close_singlefile();
			goto out;
		}
	}
	if (opts->connectlog) {
		if (log_connect_open(opts->connectlog) == -1)
			goto out;
		if (!(connect_log = logger_new(log_connect_writecb))) {
			log_connect_close();
			goto out;
		}
	}
	if (!(err_log = logger_new(log_err_writecb)))
		goto out;
	return 0;

out:
	if (content_log) {
		log_content_close_singlefile();
		logger_free(content_log);
	}
	if (connect_log) {
		log_connect_close();
		logger_free(connect_log);
	}
	return -1;
}
Exemple #4
0
CTEST(txn, begin_with_parent)
{
	int r;
	struct txn *tx1;
	struct txn *tx2;
	struct txn *tx3;
	struct txn *tx4;
	struct txnmgr *tm = txnmgr_new();
	LOGGER *logger = logger_new(NULL, tm);

	/* tx1: snapshot */
	r = txn_begin(NULL, logger, TXN_ISO_REPEATABLE, &tx1);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(0, tx1->txnid);
	ASSERT_EQUAL(1, tm->live_root_txnids->used);
	ASSERT_EQUAL(1, tx1->txnid_clone->used);
	ASSERT_EQUAL(tx1->txnid, tm->live_root_txnids->txnids[0]);

	/* tx2: snapshot */
	r = txn_begin(NULL, logger, TXN_ISO_REPEATABLE, &tx2);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(1, tx2->txnid);
	ASSERT_EQUAL(2, tx2->txnid_clone->used);
	ASSERT_EQUAL(tx1->txnid, tx2->txnid_clone->txnids[0]);
	ASSERT_EQUAL(tx2->txnid, tx2->txnid_clone->txnids[1]);

	/* tx3: not snapshot */
	r = txn_begin(NULL, logger, TXN_ISO_SERIALIZABLE, &tx3);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(2, tx3->txnid);
	ASSERT_NULL(tx3->txnid_clone);

	/* tx4: not snapshot with parent tx2 */
	r = txn_begin(tx2, logger, TXN_ISO_REPEATABLE, &tx4);
	ASSERT_EQUAL(1, r);
	ASSERT_EQUAL(3, tx4->txnid);
	ASSERT_EQUAL(2, tx4->txnid_clone->used);
	ASSERT_EQUAL(tx1->txnid, tx4->txnid_clone->txnids[0]);
	ASSERT_EQUAL(tx2->txnid, tx4->txnid_clone->txnids[1]);

	logger_free(logger);
	txnmgr_free(tm);
}
Exemple #5
0
void
samplecat_init()
{
	samplecat.logger = logger_new();

	samplecat.model = samplecat_model_new();

	samplecat_model_add_filter (samplecat.model, samplecat.model->filters.search   = samplecat_filter_new("search"));
	samplecat_model_add_filter (samplecat.model, samplecat.model->filters.dir      = samplecat_filter_new("directory"));
	samplecat_model_add_filter (samplecat.model, samplecat.model->filters.category = samplecat_filter_new("category"));

	samplecat.store = (GtkListStore*)samplecat_list_store_new();

#ifdef USE_MYSQL
	samplecat_model_add_backend("mysql");
#endif
#ifdef USE_SQLITE
	samplecat_model_add_backend("sqlite");
#endif
#ifdef USE_TRACKER
	samplecat_model_add_backend("tracker");
#endif
}
Exemple #6
0
gint main_runShadow(gint argc, gchar* argv[]) {
    /* check the compiled GLib version */
    if (!GLIB_CHECK_VERSION(2, 32, 0)) {
        g_printerr("** GLib version 2.32.0 or above is required but Shadow was compiled against version %u.%u.%u\n",
            (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION);
        return EXIT_FAILURE;
    }

    if(GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION == 40) {
        g_printerr("** You compiled against GLib version %u.%u.%u, which has bugs known to break Shadow. Please update to a newer version of GLib.\n",
                    (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION);
        return EXIT_FAILURE;
    }

    /* check the that run-time GLib matches the compiled version */
    const gchar* mismatch = glib_check_version(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
    if(mismatch) {
        g_printerr("** The version of the run-time GLib library (%u.%u.%u) is not compatible with the version against which Shadow was compiled (%u.%u.%u). GLib message: '%s'\n",
        glib_major_version, glib_minor_version, glib_micro_version,
        (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION,
        mismatch);
        return EXIT_FAILURE;
    }

    /* parse the options from the command line */
    gchar* cmds = g_strjoinv(" ", argv);
    gchar** cmdv = g_strsplit(cmds, " ", 0);
    g_free(cmds);
    Options* options = options_new(argc, cmdv);
    g_strfreev(cmdv);
    if(!options) {
        return EXIT_FAILURE;
    }

    /* if they just want the shadow version, print it and exit */
    if(options_doRunPrintVersion(options)) {
        g_printerr("%s running GLib v%u.%u.%u and IGraph v%s\n%s\n",
                SHADOW_VERSION_STRING,
                (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION,
#if defined(IGRAPH_VERSION)
                IGRAPH_VERSION,
#else
                "(n/a)",
#endif
                SHADOW_INFO_STRING);
        options_free(options);
        return EXIT_SUCCESS;
    }

    /* start up the logging subsystem to handle all future messages */
    Logger* shadowLogger = logger_new(options_getLogLevel(options));
    logger_setDefault(shadowLogger);

    /* disable buffering during startup so that we see every message immediately in the terminal */
    logger_setEnableBuffering(shadowLogger, FALSE);

    gint returnCode = _main_helper(options);

    options_free(options);
    Logger* logger = logger_getDefault();
    if(logger) {
        logger_setDefault(NULL);
        logger_unref(logger);
    }

    g_printerr("** Stopping Shadow, returning code %i (%s)\n", returnCode, (returnCode == 0) ? "success" : "error");
    return returnCode;
}
Exemple #7
0
int main(int argc, char *argv[])
{
    if (argc == 2 && !tox_strncasecmp(argv[1], "-h", 3)) {
        printf("Usage (connected)  : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
        printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
        exit(0);
    }

    /* let user override default by cmdline */
    bool ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
    int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);

    if (argvoffset < 0) {
        exit(1);
    }

    /* Initialize networking -
       Bind to ip 0.0.0.0 / [::] : PORT */
    IP ip;
    ip_init(&ip, ipv6enabled);

    Logger *logger = logger_new();

    if (MIN_LOGGER_LEVEL == LOGGER_LEVEL_TRACE || MIN_LOGGER_LEVEL == LOGGER_LEVEL_DEBUG) {
        logger_callback_log(logger, print_log, nullptr, nullptr);
    }

    Mono_Time *mono_time = mono_time_new();
    DHT *dht = new_dht(logger, mono_time, new_networking(logger, ip, PORT), true);
    Onion *onion = new_onion(mono_time, dht);
    Onion_Announce *onion_a = new_onion_announce(mono_time, dht);

#ifdef DHT_NODE_EXTRA_PACKETS
    bootstrap_set_callbacks(dht_get_net(dht), DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
#endif

    if (!(onion && onion_a)) {
        printf("Something failed to initialize.\n");
        exit(1);
    }

    perror("Initialization");

    manage_keys(dht);
    printf("Public key: ");
    uint32_t i;

#ifdef TCP_RELAY_ENABLED
#define NUM_PORTS 3
    uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
    TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion);

    if (tcp_s == nullptr) {
        printf("TCP server failed to initialize.\n");
        exit(1);
    }

#endif

    const char *const public_id_filename = "PUBLIC_ID.txt";
    FILE *file = fopen(public_id_filename, "w");

    if (file == nullptr) {
        printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename);
        exit(1);
    }

    for (i = 0; i < 32; i++) {
        const uint8_t *const self_public_key = dht_get_self_public_key(dht);
        printf("%02X", self_public_key[i]);
        fprintf(file, "%02X", self_public_key[i]);
    }

    fclose(file);

    printf("\n");
    printf("Port: %u\n", net_ntohs(net_port(dht_get_net(dht))));

    if (argc > argvoffset + 3) {
        printf("Trying to bootstrap into the network...\n");
        uint16_t port = net_htons(atoi(argv[argvoffset + 2]));
        uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
        int res = dht_bootstrap_from_address(dht, argv[argvoffset + 1],
                                             ipv6enabled, port, bootstrap_key);
        free(bootstrap_key);

        if (!res) {
            printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
            exit(1);
        }
    }

    int is_waiting_for_dht_connection = 1;

    uint64_t last_LANdiscovery = 0;
    lan_discovery_init(dht);

    while (1) {
        mono_time_update(mono_time);

        if (is_waiting_for_dht_connection && dht_isconnected(dht)) {
            printf("Connected to other bootstrap node successfully.\n");
            is_waiting_for_dht_connection = 0;
        }

        do_dht(dht);

        if (mono_time_is_timeout(mono_time, last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
            lan_discovery_send(net_htons(PORT), dht);
            last_LANdiscovery = mono_time_get(mono_time);
        }

#ifdef TCP_RELAY_ENABLED
        do_TCP_server(tcp_s, mono_time);
#endif
        networking_poll(dht_get_net(dht), nullptr);

        c_sleep(1);
    }
}
Exemple #8
0
//======================================================
// create all the objects that are needed by the process
void prepare(params_t *params) {


    // === initialize RNG ===
    // init default generator
    params->rng = gsl_rng_alloc(gsl_rng_default);
    // seed the generator
    if (params->seed == 0)
	// draw a seed from /dev/urandom
	params->seed = rand();
    printf("Seed = %d\n", params->seed);
    // set the seed
    gsl_rng_set(params->rng, params->seed);

    // initialize the mutation wheel
    int var;
    params->mwheel = g_array_new(FALSE, FALSE, sizeof(int));
    if (params->m_beta) {
	var = M_BETA;
	g_array_append_val(params->mwheel, var);
    }
    if (params->m_kappa) {
	var = M_KAPPA;
	g_array_append_val(params->mwheel, var);
    }
    if (params->m_alpha) {
	var = M_ALPHA;
	g_array_append_val(params->mwheel, var);
    }

    // compute the optimal copy number
    params->cn_hat = sqrt(params->phi * params->lambda / params->gamma) 
	- params->lambda;

    printf("Optimal CN is %.2f\n", params->cn_hat);


    // initialize the population
    params->cells = g_ptr_array_new();
    // initialize array of daughter cells
    params->dcells = g_ptr_array_new();
    // initialize the profile pool
    params->pool = pool_new();


    // load the population from a file??
    if (params->load_from)
	load_population_from_file(params);
    else
	initialize_population(params);

    // do we have a competition??
    if (params->compete) {

	// set the number of contenders based on the pool's contents
	params->contenders = params->pool->size;

	printf("COMPETITION MODE : %d contenders found\n", params->contenders);

	// OK, we do -- now cancel all mutations
	g_array_set_size(params->mwheel, 0);
	
    }

    // adjust steps so that it is a multiple of fparts
    int steps = params->steps;
    while (params->steps % params->fparts > 0)
	++ params->steps;
    if (steps != params->steps)
	printf("Adjusted simulation steps so that it is a multiple of FPARTS (old=%d, new=%d)\n",
	       steps, params->steps);

    // initialize logger
    params->logger = logger_new(params);


}
Exemple #9
0
/*
 * Log pre-init: open all log files but don't start any threads, since we may
 * fork() after pre-initialization.
 * Return -1 on errors, 0 otherwise.
 */
int
log_preinit(opts_t *opts)
{
	logger_reopen_func_t reopencb;
	logger_open_func_t opencb;
	logger_close_func_t closecb;
	logger_write_func_t writecb;
	logger_prep_func_t prepcb;

	if (opts->contentlog) {
		if (opts->contentlog_isdir) {
			reopencb = NULL;
			opencb = log_content_dir_opencb;
			closecb = log_content_dir_closecb;
			writecb = log_content_dir_writecb;
			prepcb = NULL;
		} else if (opts->contentlog_isspec) {
			reopencb = NULL;
			opencb = log_content_spec_opencb;
			closecb = log_content_spec_closecb;
			writecb = log_content_spec_writecb;
			prepcb = NULL;
		} else {
			if (log_content_file_preinit(opts->contentlog) == -1)
				goto out;
			reopencb = log_content_file_reopencb;
			opencb = NULL;
			closecb = log_content_file_closecb;
			writecb = log_content_file_writecb;
			prepcb = log_content_file_prepcb;
		}
		if (!(content_log = logger_new(reopencb, opencb, closecb,
		                               writecb, prepcb,
		                               log_exceptcb))) {
			log_content_file_fini();
			goto out;
		}
	}
	if (opts->connectlog) {
		if (log_connect_preinit(opts->connectlog) == -1)
			goto out;
		if (!(connect_log = logger_new(log_connect_reopencb,
		                               NULL, NULL,
		                               log_connect_writecb, NULL,
		                               log_exceptcb))) {
			log_connect_fini();
			goto out;
		}
	}
	if (opts->certgendir) {
		if (!(cert_log = logger_new(NULL, NULL, NULL, log_cert_writecb,
		                            NULL, log_exceptcb)))
			goto out;
	}
	if (!(err_log = logger_new(NULL, NULL, NULL, log_err_writecb, NULL,
	                           log_exceptcb)))
		goto out;
	return 0;

out:
	if (content_log) {
		log_content_file_fini();
		logger_free(content_log);
	}
	if (connect_log) {
		log_connect_fini();
		logger_free(connect_log);
	}
	if (cert_log) {
		logger_free(cert_log);
	}
	return -1;
}