merge lib/config_args with lib/posix

Merging the config_args library with the POSIX library supplies
'argc' and 'argv' arguments to components using a 'main' entry.

Fix #2218
Ref #1987
This commit is contained in:
Emery Hemingway 2017-01-03 15:28:09 +01:00 committed by Norman Feske
parent 7948a7261b
commit 18504b4801
12 changed files with 50 additions and 86 deletions

View File

@ -3,7 +3,7 @@ include $(REP_DIR)/lib/import/import-av.inc
TARGET = avplay
SRC_C = avplay.c cmdutils.c libc_dummies.c
LIBS += avfilter avformat avcodec avutil avresample swscale
LIBS += sdl posix config_args
LIBS += sdl posix
CC_WARN += -Wno-parentheses -Wno-switch -Wno-uninitialized \
-Wno-format-zero-length -Wno-pointer-sign

View File

@ -15,7 +15,8 @@
#include <libc/component.h>
/* libc includes */
#include <stdlib.h> /* for 'exit' */
#include <stdlib.h> /* 'malloc' */
#include <stdlib.h> /* 'exit' */
extern char **genode_argv;
extern int genode_argc;
@ -28,5 +29,45 @@ extern "C" int main(int argc, char ** argv, char **envp);
void Libc::Component::construct(Libc::Env &env)
{
using Genode::Xml_node;
using Genode::Xml_attribute;
env.config([&] (Xml_node const &node) {
int argc = 0;
static char **argv;
/* count the number of arguments */
node.for_each_sub_node("arg", [&] (Xml_node const &arg_node) {
/* check if the 'value' attribute exists */
if (arg_node.has_attribute("value"))
++argc;
});
if (argc == 0)
return;
argv = (char**)malloc((argc + 1) * sizeof(char*));
/* read the arguments */
int i = 0;
node.for_each_sub_node("arg", [&] (Xml_node const &arg_node) {
try {
Xml_attribute attr = arg_node.attribute("value");
Genode::size_t const arg_len = attr.value_size()+1;
argv[i] = (char*)malloc(arg_len);
attr.value(argv[i], arg_len);
++i;
} catch (Xml_node::Nonexistent_sub_node) { }
});
argv[i] = nullptr;
/* register command-line arguments at Genode's startup code */
genode_argc = argc;
genode_argv = argv;
});
/* TODO: environment variables, see #2236 */
exit(main(genode_argc, genode_argv, genode_envp));
}

View File

@ -1,5 +1,5 @@
TARGET = test-ping_client_libc_lwip
LIBS = posix lwip libc_lwip_nic_dhcp config_args
LIBS = posix lwip libc_lwip_nic_dhcp
SRC_CC = main.cc pingpong.cc
vpath main.cc $(PRG_DIR)/..

View File

@ -1,5 +1,5 @@
TARGET = test-ping_client_lwip
LIBS = posix lwip config_args
LIBS = posix lwip
SRC_CC = main.cc pingpong.cc
INC_DIR += $(REP_DIR)/src/lib/lwip/include

View File

@ -1,5 +1,5 @@
TARGET = test-ping_server_lwip
LIBS = posix lwip config_args
LIBS = posix lwip
SRC_CC = main.cc pingpong.cc
CC_OPT += -DLWIP_NATIVE

View File

@ -1,5 +0,0 @@
SRC_CC = config_args.cc
LIBS += config
vpath %.cc $(REP_DIR)/src/lib/config_args

View File

@ -31,7 +31,7 @@ install_config {
</config>
}
build_boot_image "core ld.lib.so init timer test-config_args"
build_boot_image "core ld.lib.so init timer test-config_args libc.lib.so libm.lib.so"
append qemu_args "-nographic -m 64"

View File

@ -1,70 +0,0 @@
/*
* \brief Read program arguments from the config file
* \date 2012-04-19
* \author Christian Prochaska
*/
/*
* Copyright (C) 2012-2013 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#include <os/config.h>
#include <base/log.h>
using namespace Genode;
/* external symbols provided by Genode's startup code */
extern char **genode_argv;
extern int genode_argc;
__attribute__((constructor))
void init_config_args(void)
{
int argc = 0;
static char **argv;
/* count the number of arguments */
try {
Xml_node arg_node = config()->xml_node().sub_node("arg");
for (;;) {
/* check if the 'value' attribute exists */
arg_node.attribute("value");
argc++;
arg_node = arg_node.next("arg");
}
}
catch (Xml_node::Nonexistent_sub_node) { }
catch (Xml_node::Nonexistent_attribute)
{
error("<arg> node has no 'value' attribute, ignoring further <arg> nodes");
}
if (argc == 0)
return;
argv = (char**)env()->heap()->alloc((argc + 1) * sizeof(char*));
/* read the arguments */
Xml_node arg_node = config()->xml_node().sub_node("arg");
try {
for (int i = 0; i < argc; i++) {
static char buf[512];
arg_node.attribute("value").value(buf, sizeof(buf));
size_t arg_size = strlen(buf) + 1;
argv[i] = (char*)env()->heap()->alloc(arg_size);
strncpy(argv[i], buf, arg_size);
arg_node = arg_node.next("arg");
}
} catch (Xml_node::Nonexistent_sub_node) { }
argv[argc] = 0;
/* register command-line arguments at Genode's startup code */
genode_argc = argc;
genode_argv = argv;
}

View File

@ -1,3 +1,3 @@
TARGET = test-config_args
SRC_CC = main.cc
LIBS = base config_args
LIBS = posix

View File

@ -54,4 +54,4 @@ CC_WARN += -Wno-unused-variable -Wno-unused-function -Wno-switch -Wno-unused-val
-Wno-parentheses
LIBS += posix libpng sdl sdl_net stdcxx zlib
LIBS += libc_lwip_nic_dhcp config_args
LIBS += libc_lwip_nic_dhcp

View File

@ -30,4 +30,4 @@ INC_DIR += $(PRG_DIR)
INC_DIR += $(LIGHTTPD_DIR)/src
LIBS += posix
LIBS += zlib config_args
LIBS += zlib

View File

@ -2,8 +2,6 @@
NETPERF_DIR := $(call select_from_ports,netperf)/src/app/netperf
LIBS += posix libc-resolv libc-net libc-nameser libc-isc
# plug-in to libc
LIBS += config_args
SRC_C = netserver.c netlib.c netsh.c nettest_bsd.c dscp.c
# omni test