/**
 * initialize the basic components of the chassis
 */
int chassis_frontend_init_glib() {
	const gchar *check_str = NULL;
#if 0
	g_mem_set_vtable(glib_mem_profiler_table);
#endif

	if (!GLIB_CHECK_VERSION(2, 6, 0)) {
		g_critical("the glib header are too old, need at least 2.6.0, got: %d.%d.%d", 
				GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);

		return -1;
	}

	check_str = glib_check_version(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);

	if (check_str) {
		g_critical("%s, got: lib=%d.%d.%d, headers=%d.%d.%d", 
			check_str,
			glib_major_version, glib_minor_version, glib_micro_version,
			GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);

		return -1;
	}

	if (!g_module_supported()) {
		g_critical("loading modules is not supported on this platform");
		return -1;
	}

	g_thread_init(NULL);

	return 0;
}
Esempio n. 2
0
/* Note the only way to reliably test readability on Windows is to actually 
    try to open the file. Here we are basically checking for existence. */
gboolean
file_is_readable (const gchar *fileName)
{
  if (GLIB_CHECK_VERSION(2,8,0))
    return(!g_access(fileName, R_OK));
  return !access(fileName, R_OK);
}
Esempio n. 3
0
gint shadow_main(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 -1;
    }

    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 -1;
    }

    /* 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 -1;
    }

    /* setup configuration - this fails and aborts if invalid */
    gchar* cmds = g_strjoinv(" ", argv);
    gchar** cmdv = g_strsplit(cmds, " ", 0);
    Configuration* config = configuration_new(argc, cmdv);
    g_free(cmds);
    g_strfreev(cmdv);
    if(!config) {
        /* incorrect options given */
        return -1;
    } else if(config->printSoftwareVersion) {
        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);
        configuration_free(config);
        return 0;
    }

    /* check environment for LD_PRELOAD */
    gchar** envlist = g_get_environ();
    gboolean preloadSuccess = _main_checkPreloadEnvironment(envlist);
    g_strfreev(envlist);
    gboolean respawned = g_getenv("SHADOW_SPAWNED") != NULL ? TRUE : FALSE;

    if(respawned) {
        /* if shadow already respawned once and LD_PRELOAD still isnt correct,
         * then the user will need to provide the correct path */
        if(!preloadSuccess) {
            g_printerr("** Environment Check Failed: LD_PRELOAD does not contain an absolute path to "INTERPOSELIBSTR"\n");
            return -1;
        }
        /* NOTE: we ignore valgrind and preload options during the respawn */
    } else {
        /* if preload is not set, or the user added a preload library,
         * or we are going to run valgrind, we need to respawn */
        if(config->preloads || config->runValgrind || !preloadSuccess) {
            gchar** envlist = _main_getSpawnEnviroment(config->preloads, config->runValgrind);
            gchar* cmds = g_strjoinv(" ", argv);
            gchar** cmdv = g_strsplit(cmds, " ", 0);
            GError* error = NULL;
            gint exitStatus = 0;

            gboolean spawnSuccess = config->runValgrind ?
                    _main_spawnShadowWithValgrind(cmdv, envlist, &exitStatus, &error) :
                    _main_spawnShadow(cmdv, envlist, &exitStatus, &error);

            g_free(cmds);
            g_strfreev(cmdv);
            g_strfreev(envlist);

            if(!spawnSuccess) {
                g_printerr("** Error %i while re-spawning shadow process: %s\n", error->code, error->message);
                return -1;
            }

            /* child was run */
            return (exitStatus == 0) ? 0 : -1;
        }
    }

    utility_assert(preloadSuccess);

    /* tell the preaload lib we are ready for action */
    extern void interposer_setShadowIsLoaded();
    interposer_setShadowIsLoaded();

    /* allocate and initialize our main simulation driver */
    gint returnCode = 0;
    shadowMaster = master_new(config);
    if(shadowMaster) {
        /* run the simulation */
        returnCode = master_run(shadowMaster);
        /* cleanup */
        master_free(shadowMaster);
        shadowMaster = NULL;
    }

    configuration_free(config);
    g_printerr("** shadow returning code %i (%s)\n", returnCode, (returnCode == 0) ? "success" : "error");
    return returnCode;
}
Esempio n. 4
0
int main(int argc, char **argv) {
	network_mysqld_table *table;

	/* read the command-line options */
	GOptionContext *option_ctx;
	GOptionGroup *option_grp;
	GError *gerr = NULL;
	int i;
	int exit_code = 0;
	int print_version = 0;
	int daemon_mode = 0;
	int start_proxy = 1;
	const gchar *check_str = NULL;

	gchar *partition_info_host = NULL;
	gchar *partition_info_db = NULL;
	gchar *db_user = NULL;
	gchar *db_passwd = NULL;
	gchar *config_file = NULL;
	gchar *log_file = NULL;
	gchar *max_conn_pool = NULL;
	int log_all_queries = 0;
	int log_debug_msgs = 0;
	int log_raw_data = 0;

	GOptionEntry admin_entries[] = {
		{ "admin-address", 'a', 0, G_OPTION_ARG_STRING, NULL, "listening address:port of internal admin-server (default: :4041)", "<host:port>"},

		{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
	};

	GOptionEntry proxy_entries[] = {
		{ "proxy-address", 'p', 0, G_OPTION_ARG_STRING, NULL, "listening address:port of the proxy-server (default: :4040)", "<host:port>"},

		{ "partition-info-host", 'h', 0, G_OPTION_ARG_STRING, NULL, "host name of table partition info (default: not set)", "<host>"},
		{ "partition-info-database", 'd', 0, G_OPTION_ARG_STRING, NULL, "database name of table partition info (default: not set)", "<database>"},
		{ "db-user", 'u', 0, G_OPTION_ARG_STRING, NULL, "db user for connecting to the proxy and database partititons (default: not set)", "<userid>"},
		{ "db-user-password", 'P', 0, G_OPTION_ARG_STRING, NULL, "db user password for connecting to the proxy and database partititons (default: not set)", "password"},

		{ "proxy-skip-profiling", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, NULL, "disables profiling of queries (default: enabled)", NULL},

		{ "proxy-fix-bug-25371", 0, 0, G_OPTION_ARG_NONE, NULL, "fix bug #25371 (mysqld > 5.1.12) for older libmysql versions", NULL},
		{ "proxy-lua-script", 0, 0, G_OPTION_ARG_STRING, NULL, "filename of the lua script (default: not set)", "<file>"},


		{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
	};

	GOptionEntry main_entries[] = {
		{ "version", 'V', 0, G_OPTION_ARG_NONE, NULL, "Show version", NULL},
		{ "daemon", 'D', 0, G_OPTION_ARG_NONE, NULL, "Start in daemon-mode", NULL},
		{ "pid-file", 0, 0, G_OPTION_ARG_STRING, NULL, "PID file in case we are started as daemon", "<file>"},
		{ "config-file", 'c', 0, G_OPTION_ARG_STRING, NULL, "configuration file (default is ./xxfs_dbproxy.conf)", "<file>"},
		{ "log-file", 'l', 0, G_OPTION_ARG_STRING, NULL, "log file (default is not set, using syslog)", "<file>"},
		{ "max-conn-pool-size", 'm', 0, G_OPTION_ARG_STRING, NULL, "max backend connections for each shard(default: 50)", "<number>"},
		{ "log-all-queries", 'L', 0, G_OPTION_ARG_NONE, NULL, "Log all queries(default: disabled)", NULL},
		{ "log-debug-messages", 'g', 0, G_OPTION_ARG_NONE, NULL, "Log debug messages(default: disabled)", NULL},
		{ "log-raw-data", 'r', 0, G_OPTION_ARG_NONE, NULL, "Log raw data sent/recved(default: disabled)", NULL},

		{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
	};


	if (!GLIB_CHECK_VERSION(2, 6, 0)) {
		log_error("the glib header are too old, need at least 2.6.0, got: %d.%d.%d",
				GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
	}

	check_str = glib_check_version(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);

	if (check_str) {
		log_error("%s, got: lib=%d.%d.%d, headers=%d.%d.%d",
				check_str,
				glib_major_version, glib_minor_version, glib_micro_version,
				GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
	}

	srv = network_mysqld_init();
	srv->config.network_type = NETWORK_TYPE_PROXY; /* doesn't matter anymore */
	srv->config.proxy.fix_bug_25371 = 0; /** double ERR packet on AUTH failures */
	srv->config.proxy.profiling = 1;
	srv->config.proxy.log_debug_messages = 1;
	set_debug_msg_logging(true);


	i = 0;
	admin_entries[i++].arg_data = &(srv->config.admin.address);

	i = 0;
	proxy_entries[i++].arg_data = &(srv->config.proxy.address);
	proxy_entries[i++].arg_data = &partition_info_host;
	proxy_entries[i++].arg_data = &partition_info_db;
	proxy_entries[i++].arg_data = &db_user;
	proxy_entries[i++].arg_data = &db_passwd;

	proxy_entries[i++].arg_data = &(srv->config.proxy.profiling);

	proxy_entries[i++].arg_data = &(srv->config.proxy.fix_bug_25371);
	proxy_entries[i++].arg_data = &(srv->config.proxy.lua_script);

	i = 0;
	main_entries[i++].arg_data = &(print_version);
	main_entries[i++].arg_data = &(daemon_mode);
	main_entries[i++].arg_data = &(srv->config.pid_file);
	main_entries[i++].arg_data = &config_file;
	main_entries[i++].arg_data = &log_file;
	main_entries[i++].arg_data = &max_conn_pool;
	main_entries[i++].arg_data = &log_all_queries;
	main_entries[i++].arg_data = &log_debug_msgs;
	main_entries[i++].arg_data = &log_raw_data;

	option_ctx = g_option_context_new("- SpockProxy");
	g_option_context_add_main_entries(option_ctx, main_entries, GETTEXT_PACKAGE);

	option_grp = g_option_group_new("admin", "admin module", "Show options for the admin-module", NULL, NULL);
	g_option_group_add_entries(option_grp, admin_entries);
	g_option_context_add_group(option_ctx, option_grp);

	option_grp = g_option_group_new("proxy", "proxy-module", "Show options for the proxy-module", NULL, NULL);
	g_option_group_add_entries(option_grp, proxy_entries);
	g_option_context_add_group(option_ctx, option_grp);

	if (FALSE == g_option_context_parse(option_ctx, &argc, &argv, &gerr)) {
		log_error("%s", gerr->message);

		g_error_free(gerr);

		return -1;
	}

	g_option_context_free(option_ctx);

	if (config_file && config_file[0] != '\0')
		load_config_file(config_file);
	else
		load_config_file("xxfs_dbproxy.conf");

	add_config_string("PARTITION_INFO_HOST", partition_info_host);
	add_config_string("PARTITION_INFO_DB", partition_info_db);
	add_config_string("DB_USER", db_user);
	add_config_string("DB_PASSWD", db_passwd);
	add_config_string("LOGFILE", log_file);
	add_config_string("MAX_CONN_POOL_SIZE", max_conn_pool);

	add_config_string("LOG_ALL_QUERIES", log_all_queries ? "1" : "0");
	add_config_string("LOG_DEBUG_MSG", log_debug_msgs ? "1" : "0");
	add_config_string("LOG_RAW_DATA", log_raw_data ? "1" : "0");

	if (srv->config.admin.address == NULL)
		srv->config.admin.address = g_strdup(get_config_string("ADMIN_ADDRESS"));
	if (srv->config.proxy.address == NULL)
		srv->config.proxy.address = g_strdup(get_config_string("PROXY_ADDRESS"));

	srv->config.proxy.profiling = get_config_int("SKIP_PROFILING", srv->config.proxy.profiling);
	srv->config.proxy.fix_bug_25371 = get_config_int("FIX_BUG_25371", srv->config.proxy.fix_bug_25371);
	if (srv->config.proxy.lua_script == NULL)
		srv->config.proxy.lua_script = get_config_string("LUA_SCRIPT");
	srv->config.proxy.log_debug_messages = get_config_int("LOG_DEBUG_MSG", srv->config.proxy.log_debug_messages);
	set_debug_msg_logging(srv->config.proxy.log_debug_messages);

	print_version = get_config_int("PRINT_PROXY_VERSION", print_version);
	daemon_mode = get_config_int("DAEMON_MODE", daemon_mode);
	if (srv->config.pid_file == NULL)
		srv->config.pid_file = get_config_string("PID_FILE");

#if defined(HAVE_LUA_H) && defined(LIBDIR)
	/**
	 * if the LUA_PATH is not set, set a good default 
	 */
	if (!g_getenv("LUA_PATH")) {
		g_setenv("LUA_PATH", LUA_PATHSEP LUA_PATHSEP LIBDIR "/?.lua", 1);
	}
#endif

	if (print_version) {
		printf("%s, build time: %s %s\r\n", PACKAGE_STRING, __DATE__, __TIME__);
		return 0;
	}

	log_info("%s, build time: %s %s\r\n", PACKAGE_STRING, __DATE__, __TIME__);

	if(database_lookup_load() <0) exit(0);

	if (start_proxy) {
		if (!srv->config.proxy.address) srv->config.proxy.address = g_strdup(":4040");
		if (!srv->config.proxy.backend_addresses) {
			srv->config.proxy.backend_addresses = g_new0(char *, 2);
			srv->config.proxy.backend_addresses[0] = g_strdup("127.0.0.1:3306");
		}
	}
Esempio n. 5
0
int main(int argc, char **argv, char **env)
{
  int argc_copy;
  char **argv_copy;
  char *perlout, *perlerr;
  const owl_style *s;
  const char *dir;
  owl_options opts;
  GSource *source;

  if (!GLIB_CHECK_VERSION (2, 12, 0))
    g_error ("GLib version 2.12.0 or above is needed.");

  argc_copy = argc;
  argv_copy = g_strdupv(argv);

  setlocale(LC_ALL, "");

  memset(&opts, 0, sizeof opts);
  opts.load_initial_subs = 1;
  owl_parse_options(argc, argv, &opts);
  g.load_initial_subs = opts.load_initial_subs;

  owl_start_curses();

  /* owl global init */
  owl_global_init(&g);
  if (opts.debug) owl_global_set_debug_on(&g);
  if (opts.confdir) owl_global_set_confdir(&g, opts.confdir);
  owl_function_debugmsg("startup: first available debugging message");
  owl_global_set_startupargs(&g, argc_copy, argv_copy);
  g_strfreev(argv_copy);
  owl_global_set_haveaim(&g);

  owl_register_signal_handlers();

  /* register STDIN dispatch; throw away return, we won't need it */
  owl_select_add_io_dispatch(STDIN_FILENO, OWL_IO_READ, &owl_process_input, NULL, NULL);
  owl_zephyr_initialize();

#if OWL_STDERR_REDIR
  /* Do this only after we've started curses up... */
  owl_function_debugmsg("startup: doing stderr redirection");
  owl_select_add_io_dispatch(stderr_replace(), OWL_IO_READ, &stderr_redirect_handler, NULL, NULL);
#endif

  /* create the owl directory, in case it does not exist */
  owl_function_debugmsg("startup: creating owl directory, if not present");
  dir=owl_global_get_confdir(&g);
  mkdir(dir, S_IRWXU);

  /* set the tty, either from the command line, or by figuring it out */
  owl_function_debugmsg("startup: setting tty name");
  if (opts.tty) {
    owl_global_set_tty(&g, opts.tty);
  } else {
    char *tty = owl_util_get_default_tty();
    owl_global_set_tty(&g, tty);
    g_free(tty);
  }

  /* Initialize perl */
  owl_function_debugmsg("startup: processing config file");

  owl_global_pop_context(&g);
  owl_global_push_context(&g, OWL_CTX_READCONFIG, NULL, NULL, NULL);

  perlerr=owl_perlconfig_initperl(opts.configfile, &argc, &argv, &env);
  if (perlerr) {
    endwin();
    fprintf(stderr, "Internal perl error: %s\n", perlerr);
    fflush(stderr);
    printf("Internal perl error: %s\n", perlerr);
    fflush(stdout);
    exit(1);
  }

  owl_global_complete_setup(&g);

  owl_global_setup_default_filters(&g);

  /* set the current view */
  owl_function_debugmsg("startup: setting the current view");
  owl_view_create(owl_global_get_current_view(&g), "main",
                  owl_global_get_filter(&g, "all"),
                  owl_global_get_style_by_name(&g, "default"));

  /* AIM init */
  owl_function_debugmsg("startup: doing AIM initialization");
  owl_aim_init();

  /* execute the startup function in the configfile */
  owl_function_debugmsg("startup: executing perl startup, if applicable");
  perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();");
  g_free(perlout);

  /* welcome message */
  owl_function_debugmsg("startup: creating splash message");
  owl_function_adminmsg("",
    "-----------------------------------------------------------------------\n"
    "Welcome to barnowl version " OWL_VERSION_STRING ".\n"
    "To see a quick introduction, type ':show quickstart'.                  \n"
    "Press 'h' for on-line help.                                            \n"
    "                                                                       \n"
    "BarnOwl is free software. Type ':show license' for more                \n"
    "information.                                                     ^ ^   \n"
    "                                                                 OvO   \n"
    "Please report any bugs or suggestions to [email protected]    (   )  \n"
    "-----------------------------------------------------------------m-m---\n"
  );

  /* process the startup file */
  owl_function_debugmsg("startup: processing startup file");
  owl_function_source(NULL);

  owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g));
  s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g));
  if(s)
      owl_view_set_style(owl_global_get_current_view(&g), s);
  else
      owl_function_error("No such style: %s", owl_global_get_default_style(&g));

  owl_function_debugmsg("startup: setting context interactive");

  owl_global_pop_context(&g);
  owl_global_push_context(&g, OWL_CTX_INTERACTIVE|OWL_CTX_RECV, NULL, "recv", NULL);

  source = owl_window_redraw_source_new();
  g_source_attach(source, NULL);
  g_source_unref(source);

  source = g_source_new(&owl_process_messages_funcs, sizeof(GSource));
  g_source_attach(source, NULL);
  g_source_unref(source);

  owl_log_init();

  owl_function_debugmsg("startup: entering main loop");
  owl_select_run_loop();

  /* Shut down everything. */
  owl_zephyr_shutdown();
  owl_signal_shutdown();
  owl_shutdown_curses();
  owl_log_shutdown();
  return 0;
}
Esempio n. 6
0
gint main(gint argc, gchar **argv) {
	if (!GLIB_CHECK_VERSION (2, 2, 0))
		g_error ("GLib version 2.2.0 or above is needed");
	g_print("%s\n", "Hello World!");
	return 0;
}
Esempio n. 7
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;
}