#include <glib/ghash.h>
#include <glib/gstring.h>
#include <glib/gmem.h>
+#include <glib/gstrfuncs.h>
+#include <glib/gutils.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
-#include <glib/gstrfuncs.h>
#include <string.h>
+#include <stdlib.h>
#include "configuration.h" /* self */
+#include "pathname.h"
#include "network.h"
#include "main.h"
-/* OK, GConf would be nice.
+/* OK, FIXME: GConf would be nice.
* Unfortunately the fully-static build does not support GConf process spawn at
- * all. Also /etc/sysconfig directory is standard for the daemon services.
+ * all.
*/
/* Config: */
-#define CONFIGURATION_FILE "/etc/sysconfig/udpgate"
+#define LOCATION_LINK "/proc/self/exe" /* for Linux kernel */
+static G_CONST_RETURN gchar *configuration_pathname(void)
+{
+static const gchar *static_pathname;
+
+ return pathname_find(&static_pathname,
+ G_STRINGIFY(SYSCONFDIR) "/sysconfig",PACKAGE,
+ G_STRINGIFY(SYSCONFDIR) "/default",PACKAGE,
+ G_STRINGIFY(SYSCONFDIR) "",PACKAGE,
+ /* g_get_home_dir() may return NULL and terminate the list prematurely. */
+ g_get_home_dir(),"." PACKAGE "rc",
+ NULL);
+}
+
static GHashTable *configuration_hash_new(void)
{
return g_hash_table_new_full(g_str_hash,g_str_equal,
- (GDestroyNotify)g_free, /* key_equal_func; of g_strdup() strings */
- (GDestroyNotify)g_free); /* value_equal_func; of g_strdup() strings */
+ (GDestroyNotify)g_free, /* key_destroy_func; of g_strdup() strings */
+ (GDestroyNotify)g_free); /* value_destroy_func; of g_strdup() strings */
}
static GHashTable *configuration_comments_hash_new(void)
if (!hash) {
hash=g_hash_table_new(g_str_hash,g_str_equal);
g_hash_table_insert(hash,"PORT",_("Local UDP port"));
+ g_hash_table_insert(hash,"LOCATION",_("Binary location pathname"));
}
return hash;
}
static gboolean configuration_file_write(const gchar *file_content)
{
FILE *f;
+const gchar *pathname;
g_return_val_if_fail(file_content!=NULL,FALSE);
- if (!(f=fopen(CONFIGURATION_FILE,("w")))) {
- g_warning(_("Error write opening configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ if (!(pathname=configuration_pathname()))
+ return FALSE;
+ if (!(f=fopen(pathname,"w"))) {
+ g_warning(_("Error write opening configuration file \"%s\": %m"),pathname);
return FALSE;
}
if (fputs(file_content,f)<0) {
- g_warning(_("Error writing configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error writing configuration file \"%s\": %m"),pathname);
fclose(f); /* errors ignored */
return FALSE;
}
if (fclose(f))
- g_warning(_("Error closing configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error closing configuration file \"%s\": %m"),pathname);
return TRUE;
}
GString *gstring;
gboolean modified=FALSE; /* 'gstring' contains modified value */
gboolean already_written=FALSE;
+const gchar *pathname;
+ if (!(pathname=configuration_pathname()))
+ return NULL;
open_retry:
- if (!(f=fopen(CONFIGURATION_FILE,(!hash_flush ? "r" : "rw")))) {
+ if (!(f=fopen(pathname,(!hash_flush ? "r" : "rw")))) {
if (errno!=ENOENT)
- g_warning(_("Error r/o opening configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error r/o opening configuration file \"%s\": %m"),pathname);
if (!hash_flush || already_written)
return NULL;
else {
while (errno=0,fgets(line,sizeof(line),f)) {
char *s;
char *varname_start,*varname_stop,varname_stop_orig;
-char *varcontent_start,*varcontent_stop,varcontent_stop_orig;
+char *varcontent_start,*varcontent_stop,varcontent_stop_orig=0 /* shut up GCC */;
const gchar *value;
lineno++;
*varname_stop=varname_stop_orig;
if (varcontent_stop)
*varcontent_stop=varcontent_stop_orig;
- g_warning(_("Error parsing line %d of the configuration file \"%s\": %s"),lineno,CONFIGURATION_FILE,line);
+ g_warning(_("Error parsing line %d of the configuration file \"%s\": %s"),lineno,pathname,line);
goto err_append;
}
while (*s && isspace(*s)) s++;
goto err_append;
}
if (errno) {
- g_warning(_("Error reading line from the configuration file \"%s\": %s"),CONFIGURATION_FILE,strerror(errno));
+ g_warning(_("Error reading line from the configuration file \"%s\": %s"),pathname,strerror(errno));
if (hash_fill)
g_hash_table_destroy(hash_fill);
fclose(f); /* errors ignored */
return NULL;
}
if (fclose(f))
- g_warning(_("Error closing configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error closing configuration file \"%s\": %m"),pathname);
if (hash_flushed) {
struct configuration_hash_readwrite_hash_flush_foreach_param param;
g_return_if_fail(key!=NULL);
g_return_if_fail(value!=NULL);
- if (!strcmp(key,"PORT"))
+ /**/ if (!strcmp(key,"PORT"))
optarg_port_set_string(value);
+ else if (!strcmp(key,"LOCATION"))
+ /* nop */;
else
g_warning(_("Unknown configuration key \"%s\" with value \"%s\" found in the file \"%s\""),
- key,value,CONFIGURATION_FILE);
+ key,value,configuration_pathname());
}
gboolean configuration_read(void)
return TRUE;
}
+static void location_insert(GHashTable *hash)
+{
+gchar buf[LINE_MAX];
+int got;
+
+ g_return_if_fail(hash!=NULL);
+
+ /* FIXME: Support also argv[0] as a fallback. */
+ got=readlink(LOCATION_LINK,buf,sizeof(buf)-1);
+ if (got<=0 || got>=(int)(sizeof(buf)-1))
+ return;
+ buf[got]='\0';
+ g_hash_table_insert(hash,"LOCATION",g_strdup(buf));
+}
+
gboolean configuration_write(void)
{
GHashTable *hash;
- hash=g_hash_table_new(g_str_hash,g_str_equal);
- g_hash_table_insert(hash,"PORT",(/* de-const */ gpointer)udpgate_printf_alloca("%d",(int)optarg_port));
+ hash=g_hash_table_new_full(g_str_hash,g_str_equal,
+ (GDestroyNotify)NULL, /* key_destroy_func */
+ (GDestroyNotify)g_free); /* value_destroy_func; of g_strdup() strings */
+ g_hash_table_insert(hash,"PORT",g_strdup_printf("%d",(int)optarg_port));
+ location_insert(hash);
configuration_hash_readwrite(hash); /* FIXME: errors ignored */
g_hash_table_destroy(hash);
return TRUE;