Skip to content

Commit

Permalink
read network parameters from kernel cmdline
Browse files Browse the repository at this point in the history
Ip, gateway and mode can be set in the kernel command line. The value in
the kernel command line have precedence.
  • Loading branch information
Reto Zingg committed May 17, 2013
1 parent a747ef0 commit cc4e4fe
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/usb_moded-config.c
Expand Up @@ -24,6 +24,7 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>

#include <sys/stat.h>
#include <sys/types.h>
Expand All @@ -40,6 +41,7 @@

static int get_conf_int(const gchar *entry, const gchar *key);
static const char * get_conf_string(const gchar *entry, const gchar *key);
static const char * get_kcmdline_string(const char *entry);

const char *find_mounts(void)
{
Expand Down Expand Up @@ -119,6 +121,10 @@ const char * get_trigger_value(void)

const char * get_network_ip(void)
{
const char * ip = get_kcmdline_string(NETWORK_IP_KEY);
if (ip != NULL)
return(ip);

return(get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY));
}

Expand All @@ -129,6 +135,10 @@ const char * get_network_interface(void)

const char * get_network_gateway(void)
{
const char * gw = get_kcmdline_string(NETWORK_GATEWAY_KEY);
if (gw != NULL)
return(gw);

return(get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY));
}

Expand Down Expand Up @@ -218,9 +228,56 @@ static const char * get_conf_string(const gchar *entry, const gchar *key)

}

static const char * get_kcmdline_string(const char *entry)
{
int fd;
char cmdLine[1024];
const char *ret = NULL;
int len;
gint argc = 0;
gchar **argv = NULL;
GError *optErr = NULL;
int i;

if ((fd = open("/proc/cmdline", O_RDONLY)) < 0){
log_debug("could not read /proc/cmdline");
return(ret);
}

len = read(fd, cmdLine, sizeof(cmdLine) - 1);
close(fd);

if (len <= 0){
log_debug("kernel command line was empty");
return(ret);
}

cmdLine[len] = '\0';

if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr)) {
g_error_free(optErr);
return(ret);
}

for (i=0; i < argc; i++) {
gchar ** arg_tokens;
arg_tokens = g_strsplit(argv[i], "=", 2);
if (!g_ascii_strcasecmp(arg_tokens[0], entry)){
log_debug("use '%s' for '%s' from kernel command line", arg_tokens[1], entry);
return(arg_tokens[1]);
}
}

return(ret);
}

#ifndef GCONF
const char * get_mode_setting(void)
{
const char * mode = get_kcmdline_string(MODE_SETTING_KEY);
if (mode != NULL)
return(mode);

return(get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
}

Expand Down

0 comments on commit cc4e4fe

Please sign in to comment.