Пример #1
0
/*
 * load_config()
 *
 * Set default options and overwrite with values from provided configuration
 * file.
 *
 * Returns true if a configuration file could be parsed, otherwise false.
 *
 * Any configuration options changed in this function must also be changed in
 * reload_config()
 */
bool
load_config(const char *config_file, t_configuration_options *options, char *argv0)
{
	struct stat config;

	/* Sanity checks */

	/*
	 * If a configuration file was provided, check it exists, otherwise
	 * emit an error and terminate
	 */
	if (config_file[0])
	{
		strncpy(config_file_path, config_file, MAXPGPATH);
		canonicalize_path(config_file_path);

		if (stat(config_file_path, &config) != 0)
		{
			log_err(_("provided configuration file '%s' not found: %s\n"),
					config_file,
					strerror(errno)
				);
			exit(ERR_BAD_CONFIG);
		}

		config_file_provided = true;
	}

	/*
	 * If no configuration file was provided, attempt to find a default file
	 */
	if (config_file_provided == false)
	{
		char		my_exec_path[MAXPGPATH];
		char		etc_path[MAXPGPATH];

		/* First check if one is in the default sysconfdir */
		if (find_my_exec(argv0, my_exec_path) < 0)
		{
			fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
			exit(EXIT_FAILURE);
		}

		get_etc_path(my_exec_path, etc_path);

		snprintf(config_file_path, MAXPGPATH, "%s/repmgr.conf", etc_path);

		log_debug(_("Looking for configuration file in %s\n"), etc_path);

		if (stat(config_file_path, &config) != 0)
		{
			/* Not found - default to ./repmgr.conf */
			strncpy(config_file_path, DEFAULT_CONFIG_FILE, MAXPGPATH);
			canonicalize_path(config_file_path);
			log_debug(_("Looking for configuration file in %s\n"), config_file_path);
		}
	}

	return parse_config(options);
}
Пример #2
0
/*
 * Load .psqlrc file, if found.
 */
static void
process_psqlrc(char *argv0)
{
	char		home[MAXPGPATH];
	char		rc_file[MAXPGPATH];
	char		my_exec_path[MAXPGPATH];
	char		etc_path[MAXPGPATH];
	char       *envrc;

	find_my_exec(argv0, my_exec_path);
	get_etc_path(my_exec_path, etc_path);

	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
	process_psqlrc_file(rc_file);

	envrc = getenv("PSQLRC");
	
	if (envrc != NULL && strlen(envrc) > 0)
	{
		expand_tilde(&envrc);
		process_psqlrc_file(envrc);
	}
	else if (get_home_path(home))
	{
		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
		process_psqlrc_file(rc_file);
	}
}
Пример #3
0
/*
 * Load .psqlrc file, if found.
 */
static void
process_psqlrc(char *argv0)
{
    char		home[MAXPGPATH];
    char		rc_file[MAXPGPATH];
    char		my_exec_path[MAXPGPATH];
    char		etc_path[MAXPGPATH];
    char	   *envrc = getenv("PSQLRC");

    if (find_my_exec(argv0, my_exec_path) < 0)
    {
        fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
        exit(EXIT_FAILURE);
    }

    get_etc_path(my_exec_path, etc_path);

    snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
    process_psqlrc_file(rc_file);

    if (envrc != NULL && strlen(envrc) > 0)
    {
        /* might need to free() this */
        char	   *envrc_alloc = pstrdup(envrc);

        expand_tilde(&envrc_alloc);
        process_psqlrc_file(envrc_alloc);
    }
    else if (get_home_path(home))
    {
        snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
        process_psqlrc_file(rc_file);
    }
}
Пример #4
0
/*
 *	set_pglocale_pgservice
 *
 *	Set application-specific locale and service directory
 *
 *	This function takes the value of argv[0] rather than a full path.
 *
 * (You may be wondering why this is in exec.c.  It requires this module's
 * services and doesn't introduce any new dependencies, so this seems as
 * good as anyplace.)
 */
void
set_pglocale_pgservice(const char *argv0, const char *app)
{
	char		path[MAXPGPATH];
	char		my_exec_path[MAXPGPATH];
	char		env_path[MAXPGPATH + sizeof("PGSYSCONFDIR=")];	/* longer than
																 * PGLOCALEDIR */
	char	   *dup_path;

	/* don't set LC_ALL in the backend */
	if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
	{
		setlocale(LC_ALL, "");

		/*
		 * One could make a case for reproducing here PostmasterMain()'s test
		 * for whether the process is multithreaded.  Unlike the postmaster,
		 * no frontend program calls sigprocmask() or otherwise provides for
		 * mutual exclusion between signal handlers.  While frontends using
		 * fork(), if multithreaded, are formally exposed to undefined
		 * behavior, we have not witnessed a concrete bug.  Therefore,
		 * complaining about multithreading here may be mere pedantry.
		 */
	}

	if (find_my_exec(argv0, my_exec_path) < 0)
		return;

#ifdef ENABLE_NLS
	get_locale_path(my_exec_path, path);
	bindtextdomain(app, path);
	textdomain(app);

	if (getenv("PGLOCALEDIR") == NULL)
	{
		/* set for libpq to use */
		snprintf(env_path, sizeof(env_path), "PGLOCALEDIR=%s", path);
		canonicalize_path(env_path + 12);
		dup_path = strdup(env_path);
		if (dup_path)
			putenv(dup_path);
	}
#endif

	if (getenv("PGSYSCONFDIR") == NULL)
	{
		get_etc_path(my_exec_path, path);

		/* set for libpq to use */
		snprintf(env_path, sizeof(env_path), "PGSYSCONFDIR=%s", path);
		canonicalize_path(env_path + 13);
		dup_path = strdup(env_path);
		if (dup_path)
			putenv(dup_path);
	}
}
Пример #5
0
static void
show_sysconfdir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("SYSCONFDIR = ");
	get_etc_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}
Пример #6
0
char *InstallHelper_hello()
{
	char pathbuf[MAXPGPATH];
	Invocation ctx;
	jstring nativeVer;
	jstring user;
	jstring dbname;
	jstring clustername;
	jstring ddir;
	jstring ldir;
	jstring sdir;
	jstring edir;
	jstring greeting;
	char *greetingC;
	char const *clusternameC = pljavaClusterName();

	Invocation_pushBootContext(&ctx);
	nativeVer = String_createJavaStringFromNTS(SO_VERSION_STRING);
	user = String_createJavaStringFromNTS(MyProcPort->user_name);
	dbname = String_createJavaStringFromNTS(MyProcPort->database_name);
	if ( '\0' == *clusternameC )
		clustername = NULL;
	else
		clustername = String_createJavaStringFromNTS(clusternameC);

	ddir = String_createJavaStringFromNTS(DataDir);

	get_pkglib_path(my_exec_path, pathbuf);
	ldir = String_createJavaStringFromNTS(pathbuf);

	get_share_path(my_exec_path, pathbuf);
	sdir = String_createJavaStringFromNTS(pathbuf);

	get_etc_path(my_exec_path, pathbuf);
	edir = String_createJavaStringFromNTS(pathbuf);

	greeting = JNI_callStaticObjectMethod(
		s_InstallHelper_class, s_InstallHelper_hello,
		nativeVer, user, dbname, clustername, ddir, ldir, sdir, edir);

	JNI_deleteLocalRef(nativeVer);
	JNI_deleteLocalRef(user);
	JNI_deleteLocalRef(dbname);
	if ( NULL != clustername )
		JNI_deleteLocalRef(clustername);
	JNI_deleteLocalRef(ddir);
	JNI_deleteLocalRef(ldir);
	JNI_deleteLocalRef(sdir);
	JNI_deleteLocalRef(edir);
	greetingC = String_createNTS(greeting);
	JNI_deleteLocalRef(greeting);
	Invocation_popBootContext();
	return greetingC;
}
Пример #7
0
/*
 * Load .psqlrc file, if found.
 */
static void process_psqlrc(char *argv0)
{
	char home[MAX_PG_PATH];
	char rc_file[MAX_PG_PATH];
	char my_exec_path[MAX_PG_PATH];
	char etc_path[MAX_PG_PATH];

	find_my_exec(argv0, my_exec_path);
	get_etc_path(my_exec_path, etc_path);

	snprintf(rc_file, MAX_PG_PATH, "%s/%s", etc_path, SYSPSQLRC);
	process_psqlrc_file(rc_file);

	if (get_home_path(home)) {
		snprintf(rc_file, MAX_PG_PATH, "%s/%s", home, PSQLRC);
		process_psqlrc_file(rc_file);
	}
}
Пример #8
0
/*
 *	set_pglocale_pgservice
 *
 *	Set application-specific locale and service directory
 *
 *	This function takes an argv[0] rather than a full path.
 */
void
set_pglocale_pgservice(const char *argv0, const char *app)
{
	char		path[MAXPGPATH];
	char		my_exec_path[MAXPGPATH];
	char		env_path[MAXPGPATH + sizeof("PGSYSCONFDIR=")];	/* longer than
																 * PGLOCALEDIR */

	/* don't set LC_ALL in the backend */
	if (strcmp(app, "postgres") != 0)
		setlocale(LC_ALL, "");

	if (find_my_exec(argv0, my_exec_path) < 0)
		return;

#ifdef ENABLE_NLS
	get_locale_path(my_exec_path, path);
	bindtextdomain(app, path);
	textdomain(app);

	if (getenv("PGLOCALEDIR") == NULL)
	{
		/* set for libpq to use */
		snprintf(env_path, sizeof(env_path), "PGLOCALEDIR=%s", path);
		canonicalize_path(env_path + 12);
		putenv(strdup(env_path));
	}
#endif

	if (getenv("PGSYSCONFDIR") == NULL)
	{
		get_etc_path(my_exec_path, path);

		/* set for libpq to use */
		snprintf(env_path, sizeof(env_path), "PGSYSCONFDIR=%s", path);
		canonicalize_path(env_path + 13);
		putenv(strdup(env_path));
	}
}
Пример #9
0
/*
 * get_configdata(const char *my_exec_path, size_t *configdata_len)
 *
 * Get configure-time constants. The caller is responsible
 * for pfreeing the result.
 */
ConfigData *
get_configdata(const char *my_exec_path, size_t *configdata_len)
{
	ConfigData *configdata;
	char		path[MAXPGPATH];
	char	   *lastsep;
	int			i = 0;

	/* Adjust this to match the number of items filled below */
	*configdata_len = 23;
	configdata = (ConfigData *) palloc(*configdata_len * sizeof(ConfigData));

	configdata[i].name = pstrdup("BINDIR");
	strlcpy(path, my_exec_path, sizeof(path));
	lastsep = strrchr(path, '/');
	if (lastsep)
		*lastsep = '\0';
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("DOCDIR");
	get_doc_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("HTMLDIR");
	get_html_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("INCLUDEDIR");
	get_include_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("PKGINCLUDEDIR");
	get_pkginclude_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("INCLUDEDIR-SERVER");
	get_includeserver_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("LIBDIR");
	get_lib_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("PKGLIBDIR");
	get_pkglib_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("LOCALEDIR");
	get_locale_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("MANDIR");
	get_man_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("SHAREDIR");
	get_share_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("SYSCONFDIR");
	get_etc_path(my_exec_path, path);
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("PGXS");
	get_pkglib_path(my_exec_path, path);
	strlcat(path, "/pgxs/src/makefiles/pgxs.mk", sizeof(path));
	cleanup_path(path);
	configdata[i].setting = pstrdup(path);
	i++;

	configdata[i].name = pstrdup("CONFIGURE");
#ifdef VAL_CONFIGURE
	configdata[i].setting = pstrdup(VAL_CONFIGURE);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("CC");
#ifdef VAL_CC
	configdata[i].setting = pstrdup(VAL_CC);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("CPPFLAGS");
#ifdef VAL_CPPFLAGS
	configdata[i].setting = pstrdup(VAL_CPPFLAGS);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("CFLAGS");
#ifdef VAL_CFLAGS
	configdata[i].setting = pstrdup(VAL_CFLAGS);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("CFLAGS_SL");
#ifdef VAL_CFLAGS_SL
	configdata[i].setting = pstrdup(VAL_CFLAGS_SL);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("LDFLAGS");
#ifdef VAL_LDFLAGS
	configdata[i].setting = pstrdup(VAL_LDFLAGS);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("LDFLAGS_EX");
#ifdef VAL_LDFLAGS_EX
	configdata[i].setting = pstrdup(VAL_LDFLAGS_EX);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("LDFLAGS_SL");
#ifdef VAL_LDFLAGS_SL
	configdata[i].setting = pstrdup(VAL_LDFLAGS_SL);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("LIBS");
#ifdef VAL_LIBS
	configdata[i].setting = pstrdup(VAL_LIBS);
#else
	configdata[i].setting = pstrdup(_("not recorded"));
#endif
	i++;

	configdata[i].name = pstrdup("VERSION");
	configdata[i].setting = pstrdup("PostgreSQL " PG_VERSION);
	i++;

	Assert(i == *configdata_len);

	return configdata;
}
Пример #10
0
/*
 * load_config()
 *
 * Set default options and overwrite with values from provided configuration
 * file.
 *
 * Returns true if a configuration file could be parsed, otherwise false.
 *
 * Any *repmgrd-specific* configuration options added/changed in this function must also be
 * added/changed in reload_config()
 *
 * NOTE: this function is called before the logger is set up, so we need
 * to handle the verbose option ourselves; also the default log level is NOTICE,
 * so we can't use DEBUG.
 */
bool
load_config(const char *config_file, bool verbose, t_configuration_options *options, char *argv0)
{
	struct stat stat_config;

	/*
	 * If a configuration file was provided, check it exists, otherwise
	 * emit an error and terminate. We assume that if a user explicitly
	 * provides a configuration file, they'll want to make sure it's
	 * used and not fall back to any of the defaults.
	 */
	if (config_file[0])
	{
		strncpy(config_file_path, config_file, MAXPGPATH);
		canonicalize_path(config_file_path);

		if (stat(config_file_path, &stat_config) != 0)
		{
			log_err(_("provided configuration file \"%s\" not found: %s\n"),
					config_file,
					strerror(errno)
				);
			exit(ERR_BAD_CONFIG);
		}

		if (verbose == true)
		{
			log_notice(_("using configuration file \"%s\"\n"), config_file);
		}

		config_file_provided = true;
		config_file_found = true;
	}

	/*
	 * If no configuration file was provided, attempt to find a default file
	 * in this order:
	 *	- current directory
	 *	- /etc/repmgr.conf
	 *	- default sysconfdir
	 *
	 * here we just check for the existence of the file; parse_config()
	 * will handle read errors etc.
	 */
	if (config_file_provided == false)
	{
		char		my_exec_path[MAXPGPATH];
		char		sysconf_etc_path[MAXPGPATH];

		/* 1. "./repmgr.conf" */
		if (verbose == true)
		{
			log_notice(_("looking for configuration file in current directory\n"));
		}

		snprintf(config_file_path, MAXPGPATH, "./%s", CONFIG_FILE_NAME);
		canonicalize_path(config_file_path);

		if (stat(config_file_path, &stat_config) == 0)
		{
			config_file_found = true;
			goto end_search;
		}

		/* 2. "/etc/repmgr.conf" */
		if (verbose == true)
		{
			log_notice(_("looking for configuration file in /etc\n"));
		}

		snprintf(config_file_path, MAXPGPATH, "/etc/%s", CONFIG_FILE_NAME);
		if (stat(config_file_path, &stat_config) == 0)
		{
			config_file_found = true;
			goto end_search;
		}

		/* 3. default sysconfdir */
		if (find_my_exec(argv0, my_exec_path) < 0)
		{
			fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
			exit(EXIT_FAILURE);
		}

		get_etc_path(my_exec_path, sysconf_etc_path);

		if (verbose == true)
		{
			log_notice(_("looking for configuration file in %s\n"), sysconf_etc_path);
		}

		snprintf(config_file_path, MAXPGPATH, "%s/%s", sysconf_etc_path, CONFIG_FILE_NAME);
		if (stat(config_file_path, &stat_config) == 0)
		{
			config_file_found = true;
			goto end_search;
		}

	end_search:
		if (config_file_found == true)
		{
			if (verbose == true)
			{
				log_notice(_("configuration file found at: %s\n"), config_file_path);
			}
		}
		else
		{
			if (verbose == true)
			{
				log_notice(_("no configuration file provided or found\n"));
			}
		}
	}

	return parse_config(options);
}