Make binary 'LOCATION' autodetected and configurable.
authorshort <>
Wed, 9 Jun 2004 06:50:32 +0000 (06:50 +0000)
committershort <>
Wed, 9 Jun 2004 06:50:32 +0000 (06:50 +0000)
init.d/udpgate.init
src/configuration.c

index 01f92bf..4f526a6 100755 (executable)
@@ -2,31 +2,33 @@
 # Startup script for udpgate
 #
 # chkconfig: 2345 98 02
-# description: UDP packats gateway
+# description: UDP packets gateway
 
 # Source function library.
 . /etc/rc.d/init.d/functions
 
-# Where we are?
-# [ -f /usr/bin/udpgate ] || exit 0
+# Source our configuration file for these variables.
+PORT=9201
+LOCATION=/usr/bin/udpgate
+if [ -f /etc/sysconfig/udpgate ] ; then
+       . /etc/sysconfig/udpgate
+fi
 
 prog="udpgate"
 
 start() {
        echo -n $"Starting $prog: " 
-       daemon udpgate --start
+       daemon $LOCATION --start --port=$PORT
        RETVAL=$?
        echo
        return $RETVAL
 }
 
 stop() {
-       if test "x`pidof udpgate`" != x; then
-               echo -n $"Stopping $prog: "
-               killproc udpgate
-               echo
-       fi
+       echo -n $"Stopping $prog: "
+       $LOCATION --stop
        RETVAL=$?
+       echo
        return $RETVAL
 }
 
index a06feab..3dee3cb 100644 (file)
 
 /* Config: */
 #define CONFIGURATION_FILE "/etc/sysconfig/udpgate"
+#define LOCATION_LINK "/proc/self/exe" /* for Linux kernel */
 
 
 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)
@@ -58,6 +59,7 @@ static GHashTable *hash;
        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;
 }
@@ -261,8 +263,10 @@ static void configuration_read_hash_foreach(const gchar *key,const gchar *value,
        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);
@@ -279,12 +283,30 @@ GHashTable *hash;
        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;