Avoid use of deprecated Xml_node methods

Issue #3755
This commit is contained in:
Norman Feske 2020-05-06 19:06:25 +02:00
parent d22b95ded3
commit be65c4acd2
16 changed files with 196 additions and 212 deletions

View File

@ -865,16 +865,16 @@ class Rump_factory : public Vfs::File_system_factory
rump_io_backend_init();
/* limit RAM consumption */
try {
Genode::Number_of_bytes memlimit;
config.attribute("ram").value(&memlimit);
rump_set_memlimit(memlimit);
} catch (...) {
if (!config.has_attribute("ram")) {
Genode::error("mandatory 'ram' attribute missing");
throw Genode::Exception();
}
Genode::Number_of_bytes const memlimit =
config.attribute_value("ram", Genode::Number_of_bytes(0));
rump_set_memlimit(memlimit);
/* start rump kernel */
try { rump_init(); }
catch (...) { throw Genode::Exception(); }

View File

@ -381,14 +381,11 @@ struct Vfs_trace::Local_factory : File_system_factory
size_t _config_session_ram(Xml_node config)
{
try {
Genode::Number_of_bytes ram;
config.attribute("ram").value(&ram);
return ram;
} catch (...) {
if (!config.has_attribute("ram")) {
Genode::error("mandatory 'ram' attribute missing");
throw Genode::Exception();
}
return config.attribute_value("ram", Number_of_bytes(0));
}
Local_factory(Vfs::Env &env, Xml_node config)

View File

@ -930,14 +930,14 @@ struct Usb_devices : List<Usb_host_device>
Dev_info const dev_info(bus, dev, vendor, product);
String<128> label;
try {
node.attribute("label").value(&label);
} catch (Xml_attribute::Nonexistent_attribute) {
if (!node.has_attribute("label")) {
error("no label found for device ", dev_info);
return;
}
typedef String<128> Label;
Label const label = node.attribute_value("label", Label());
/* ignore if already created */
bool exists = false;
for_each([&] (Usb_host_device &device) {

View File

@ -34,8 +34,7 @@
namespace Gpio {
class Invalid_gpio_number : Genode::Exception {};
class Invalid_mode : Genode::Exception {};
class Invalid_mode : Genode::Exception {};
static void process_config(Genode::Xml_node const &config, Gpio::Driver &driver);
}
@ -43,41 +42,37 @@ namespace Gpio {
void Gpio::process_config(Genode::Xml_node const &config, Gpio::Driver &driver)
{
try {
Genode::Xml_node gpio_node = config.sub_node("gpio");
for (;; gpio_node = gpio_node.next("gpio")) {
unsigned num = 0;
char mode[2] = {0};
unsigned value = 0;
try {
gpio_node.attribute("num").value(&num);
if (!driver.gpio_valid(num)) throw Invalid_gpio_number();
gpio_node.attribute("mode").value(mode, sizeof(mode));
if (mode[0] == 'O' || mode[0] == 'o') {
gpio_node.attribute("value").value(&value);
driver.write(num, value);
driver.direction(num, false);
} else if (mode[0] == 'I' || mode[0] == 'i') {
driver.direction(num, true);
} else throw Invalid_mode();
Genode::log("gpio ", num, " "
"mode ", Genode::Cstring(mode), " "
"value=", value);
} catch(Genode::Xml_node::Nonexistent_attribute) {
Genode::warning("missing attribute. Ignore node.");
} catch(Gpio::Invalid_gpio_number) {
Genode::warning("invalid GPIO number ", num, ", ignore node");
}
if (gpio_node.last("gpio")) break;
}
}
catch (Genode::Xml_node::Nonexistent_sub_node) {
if (!config.has_sub_node("gpio"))
Genode::warning("no GPIO config");
}
config.for_each_sub_node("gpio", [&] (Genode::Xml_node const &gpio_node) {
unsigned const num = gpio_node.attribute_value("num", 0U);
if (!driver.gpio_valid(num)) {
Genode::warning("invalid GPIO number ", num, ", ignore node");
return;
}
typedef Genode::String<2> Mode;
Mode const mode = gpio_node.attribute_value("mode", Mode());
unsigned value = 0;
if (mode == "O" || mode == "o") {
value = gpio_node.attribute_value("value", value);
driver.write(num, value);
driver.direction(num, false);
}
else if (mode == "I" || mode == "i") {
driver.direction(num, true);
}
else {
Genode::error("gpio ", num, " has invalid mode, must be I or O");
throw Invalid_mode();
}
Genode::log("gpio ", num, " mode ", mode, " value=", value);
});
}
#endif /* _INCLUDE__GPIO__CONFIG_H_ */

View File

@ -276,16 +276,14 @@ struct Audio_out::Main
Main(Genode::Env &env) : env(env)
{
char dev[32] = { 'h', 'w', 0 };
try {
config.xml().attribute("alsa_device").value(dev, sizeof(dev));
} catch (...) { }
typedef Genode::String<32> Dev;
Dev const dev = config.xml().attribute_value("alsa_device", Dev("hw"));
/* init ALSA */
int err = audio_drv_init(dev);
int err = audio_drv_init(dev.string());
if (err) {
if (err == -1) {
Genode::error("could not open ALSA device ", Genode::Cstring(dev));
Genode::error("could not open ALSA device ", dev);
} else {
Genode::error("could not initialize driver error ", err);
}

View File

@ -59,49 +59,40 @@ struct Main
using namespace Genode;
log("--- Raspberry Pi GPIO driver ---");
Xml_node const config = config_rom.xml();
/*
* Check configuration for async events detect
*/
unsigned int async = 0;
try {
config_rom.xml().attribute("async_events").value(&async);
} catch (...) { }
unsigned int async = config.attribute_value("async_events", 0U);
driver.set_async_events(async>0);
/*
* Check for common GPIO configuration
*/
Gpio::process_config(config_rom.xml(), driver);
Gpio::process_config(config, driver);
/*
* Check configuration for specific function
*/
try {
Xml_node gpio_node = config_rom.xml().sub_node("gpio");
if (!config.has_sub_node("gpio"))
warning("no GPIO config");
for (;; gpio_node = gpio_node.next("gpio")) {
unsigned num = 0;
unsigned function = 0;
config_rom.xml().for_each_sub_node("gpio", [&] (Xml_node const &gpio_node) {
try {
gpio_node.attribute("num").value(&num);
gpio_node.attribute("function").value(&function);
unsigned const num = gpio_node.attribute_value("num", 0U);
unsigned const function = gpio_node.attribute_value("function", 0U);
switch(function){
case 0: driver.set_func(num, Gpio::Reg::FSEL_ALT0); break;
case 1: driver.set_func(num, Gpio::Reg::FSEL_ALT1); break;
case 2: driver.set_func(num, Gpio::Reg::FSEL_ALT2); break;
case 3: driver.set_func(num, Gpio::Reg::FSEL_ALT3); break;
case 4: driver.set_func(num, Gpio::Reg::FSEL_ALT4); break;
case 5: driver.set_func(num, Gpio::Reg::FSEL_ALT5); break;
default: warning("wrong pin function, ignore node");
}
} catch(Xml_node::Nonexistent_attribute) {
warning("missing attribute, ignore node");
}
if (gpio_node.last("gpio")) break;
switch(function){
case 0: driver.set_func(num, Gpio::Reg::FSEL_ALT0); break;
case 1: driver.set_func(num, Gpio::Reg::FSEL_ALT1); break;
case 2: driver.set_func(num, Gpio::Reg::FSEL_ALT2); break;
case 3: driver.set_func(num, Gpio::Reg::FSEL_ALT3); break;
case 4: driver.set_func(num, Gpio::Reg::FSEL_ALT4); break;
case 5: driver.set_func(num, Gpio::Reg::FSEL_ALT5); break;
default: warning("wrong pin function, ignore node");
}
} catch (Xml_node::Nonexistent_sub_node) { warning("no GPIO config"); }
});
/*
* Announce service

View File

@ -113,7 +113,11 @@ class Linux_session_component : public Nic::Session_component
/* get tap device from config */
try {
Genode::Xml_node nic_node = _config_rom.xml().sub_node("nic");
nic_node.attribute("tap").value(ifr.ifr_name, sizeof(ifr.ifr_name));
nic_node.attribute("tap").with_raw_value([&] (char const *ptr, size_t len) {
len = Genode::min(len, sizeof(ifr.ifr_name) - 1);
Genode::memcpy(ifr.ifr_name, ptr, len);
ifr.ifr_name[len] = 0;
});
Genode::log("using tap device \"", Genode::Cstring(ifr.ifr_name), "\"");
} catch (...) {
/* use tap0 if no config has been provided */
@ -212,20 +216,20 @@ class Linux_session_component : public Nic::Session_component
_config_rom(env, "config"),
_tap_fd(_setup_tap_fd()), _rx_thread(env, _tap_fd, _packet_stream_dispatcher)
{
/* fall back to fake MAC address (unicast, locally managed) */
_mac_addr.addr[0] = 0x02;
_mac_addr.addr[1] = 0x00;
_mac_addr.addr[2] = 0x00;
_mac_addr.addr[3] = 0x00;
_mac_addr.addr[4] = 0x00;
_mac_addr.addr[5] = 0x01;
/* try using configured MAC address */
try {
Genode::Xml_node nic_config = _config_rom.xml().sub_node("nic");
nic_config.attribute("mac").value(&_mac_addr);
_mac_addr = nic_config.attribute_value("mac", _mac_addr);
Genode::log("Using configured MAC address ", _mac_addr);
} catch (...) {
/* fall back to fake MAC address (unicast, locally managed) */
_mac_addr.addr[0] = 0x02;
_mac_addr.addr[1] = 0x00;
_mac_addr.addr[2] = 0x00;
_mac_addr.addr[3] = 0x00;
_mac_addr.addr[4] = 0x00;
_mac_addr.addr[5] = 0x01;
}
} catch (...) { }
_rx_thread.start();
}

View File

@ -55,21 +55,20 @@ class Server::Gem_session_component : public Cadence_gem
{
Nic::Mac_address mac_addr;
/* fall back to fake MAC address (unicast, locally managed) */
mac_addr.addr[0] = 0x02;
mac_addr.addr[1] = 0x00;
mac_addr.addr[2] = 0x00;
mac_addr.addr[3] = 0x00;
mac_addr.addr[4] = 0x00;
mac_addr.addr[5] = 0x01;
/* try using configured MAC address */
try {
Genode::Xml_node nic_config = _config_rom.xml().sub_node("nic");
nic_config.attribute("mac").value(&mac_addr);
} catch (...) {
/* fall back to fake MAC address (unicast, locally managed) */
mac_addr.addr[0] = 0x02;
mac_addr.addr[1] = 0x00;
mac_addr.addr[2] = 0x00;
mac_addr.addr[3] = 0x00;
mac_addr.addr[4] = 0x00;
mac_addr.addr[5] = 0x01;
}
Genode::log("Using MAC address ", mac_addr);
mac_addr = nic_config.attribute_value("mac", mac_addr);
Genode::log("Using configured MAC address ", mac_addr);
} catch (...) { }
/* set mac address */
mac_address(mac_addr);

View File

@ -413,53 +413,43 @@ class Lx_fs::Root : public Root_component<Session_component>
char const *root_dir = ".";
bool writeable = false;
enum { ROOT_MAX_LEN = 256 };
char root[ROOT_MAX_LEN];
root[0] = 0;
Session_label const label = label_from_args(args);
Session_policy const policy(label, _config.xml());
Session_label const label = label_from_args(args);
try {
Session_policy policy(label, _config.xml());
/*
* Determine directory that is used as root directory of
* the session.
*/
try {
policy.attribute("root").value(root, sizeof(root));
/*
* Make sure the root path is specified with a
* leading path delimiter. For performing the
* lookup, we remove all leading slashes.
*/
if (root[0] != '/') {
Genode::error("Root directory must start with / but is \"",
Genode::Cstring(root), "\"");
throw Service_denied();
}
for (root_dir = root; *root_dir == '/'; ++root_dir) ;
/* sanitize possibly empty root_dir to current directory */
if (*root_dir == 0)
root_dir = ".";
} catch (Xml_node::Nonexistent_attribute) {
Genode::error("missing \"root\" attribute in policy definition");
throw Service_denied();
}
/*
* Determine if write access is permitted for the session.
*/
writeable = policy.attribute_value("writeable", false) &&
writeable_from_args(args);
if (!policy.has_attribute("root")) {
Genode::error("missing \"root\" attribute in policy definition");
throw Service_denied();
}
catch (Session_policy::No_policy_defined) {
Genode::error("invalid session request, no matching policy");
throw Genode::Service_denied();
/*
* Determine directory that is used as root directory of
* the session.
*/
typedef String<256> Root;
Root const root = policy.attribute_value("root", Root());
/*
* Make sure the root path is specified with a
* leading path delimiter. For performing the
* lookup, we remove all leading slashes.
*/
if (root.string()[0] != '/') {
Genode::error("Root directory must start with / but is \"", root, "\"");
throw Service_denied();
}
for (root_dir = root.string(); *root_dir == '/'; ++root_dir) ;
/* sanitize possibly empty root_dir to current directory */
if (*root_dir == 0)
root_dir = ".";
/*
* Determine if write access is permitted for the session.
*/
writeable = policy.attribute_value("writeable", false) &&
writeable_from_args(args);
size_t ram_quota =
Arg_string::find_arg(args, "ram_quota" ).ulong_value(0);
size_t tx_buf_size =
@ -486,7 +476,7 @@ class Lx_fs::Root : public Root_component<Session_component>
Session_component(tx_buf_size, _env, root_dir, writeable, *md_alloc());
}
catch (Lookup_failed) {
Genode::error("session root directory \"", Genode::Cstring(root), "\" "
Genode::error("session root directory \"", root, "\" "
"does not exist");
throw Service_denied();
}

View File

@ -68,10 +68,8 @@ struct Main
Genode::Attached_rom_dataspace config { env, "config" };
verbose = config.xml().attribute_value("verbose", false);
config.xml().attribute("expect").value(line, sizeof(line));
expect = Line(line);
config.xml().attribute("send").value(line, sizeof(line));
send = Line(line);
expect = config.xml().attribute_value("expect", Line());
send = config.xml().attribute_value("send", Line());
} catch (...) { warning("No config data available"); }
}
};

View File

@ -38,6 +38,8 @@ class Boot_module_provider
enum { MODULE_NAME_MAX_LEN = 48 };
typedef Genode::String<MODULE_NAME_MAX_LEN> Name;
public:
/**
@ -83,17 +85,13 @@ class Boot_module_provider
* attribute of the 'rom' node. If no 'label' argument is
* provided, use the 'name' attribute as file name.
*/
char name[MODULE_NAME_MAX_LEN];
try {
mod_node.attribute("label").value(name, sizeof(name));
} catch (Xml_node::Nonexistent_attribute) {
mod_node.attribute("name").value(name, sizeof(name));
}
Name const label = mod_node.has_attribute("label")
? mod_node.attribute_value("label", Name())
: mod_node.attribute_value("name", Name());
/*
* Open ROM session
*/
Rom_connection rom(env, name);
Rom_connection rom(env, label.string());
Dataspace_capability ds = rom.dataspace();
Genode::size_t const src_len = Dataspace_client(ds).size();
@ -117,18 +115,14 @@ class Boot_module_provider
env.rm().detach(src);
return src_len;
} else if (mod_node.has_type("inline")) {
/*
* Determine ROM file name, which is specified as 'name'
* attribute of the 'rom' node.
*/
char name[MODULE_NAME_MAX_LEN];
mod_node.attribute("name").value(name, sizeof(name));
/*
* Copy inline content directly to destination buffer
*/
Genode::memcpy(dst, mod_node.content_addr(), mod_node.content_size());
mod_node.with_raw_content([&] (char const *ptr, size_t size) {
Genode::memcpy(dst, ptr, size); });
return mod_node.content_size();
}
@ -177,10 +171,9 @@ class Boot_module_provider
Genode::size_t cmd_len = 0;
char name[MODULE_NAME_MAX_LEN];
mod_node.attribute("name").value(name, sizeof(name));
Name const name = mod_node.attribute_value("name", Name());
Genode::size_t const name_len = Genode::strlen(name);
Genode::size_t const name_len = Genode::strlen(name.string());
/*
* Check if destination buffer can hold the name including
@ -190,7 +183,7 @@ class Boot_module_provider
return 0;
/* copy name to command line */
strncpy(&dst[cmd_len], name, name_len + 1);
strncpy(&dst[cmd_len], name.string(), name_len + 1);
cmd_len += name_len;
/* check if name fills entire destination buffer */
@ -199,8 +192,10 @@ class Boot_module_provider
return cmd_len;
}
try {
Xml_node::Attribute cmdline_attr = mod_node.attribute("cmdline");
if (mod_node.has_attribute("cmdline")) {
typedef String<256> Cmdline;
Cmdline const cmdline = mod_node.attribute_value("cmdline", Cmdline());
/* add single space between name and arguments */
dst[cmd_len++] = ' ';
@ -210,7 +205,7 @@ class Boot_module_provider
}
/* copy 'cmdline' attribute to destination buffer */
cmdline_attr.value(&dst[cmd_len], dst_len - cmd_len);
Genode::strncpy(&dst[cmd_len], cmdline.string(), dst_len - cmd_len);
/*
* The string returned by the 'value' function is
@ -219,7 +214,7 @@ class Boot_module_provider
*/
return Genode::strlen(dst);
} catch (Xml_node::Nonexistent_attribute) { }
}
return cmd_len;
}

View File

@ -1242,18 +1242,18 @@ class Machine : public StaticReceiver<Machine>
Xml_node node = machine_node.sub_node();
for (;; node = node.next()) {
enum { MODEL_NAME_MAX_LEN = 32 };
char name[MODEL_NAME_MAX_LEN];
node.type_name(name, sizeof(name));
typedef String<32> Model_name;
Model_name const name = node.type();
if (verbose)
Genode::log("device: ", (char const *)name);
Genode::log("device: ", name);
Device_model_info *dmi = device_model_registry()->lookup(name);
Device_model_info *dmi = device_model_registry()->lookup(name.string());
if (!dmi) {
Genode::error("configuration error: device model '",
(char const *)name, "' does not exist");
name, "' does not exist");
throw Config_error();
}
@ -1267,15 +1267,11 @@ class Machine : public StaticReceiver<Machine>
argv[i] = ~0UL;
for (int i = 0; dmi->arg_names[i] && (i < MAX_ARGS); i++) {
try {
Xml_node::Attribute arg = node.attribute(dmi->arg_names[i]);
arg.value(&argv[i]);
if (node.has_attribute(dmi->arg_names[i])) {
argv[i] = node.attribute_value(dmi->arg_names[i], ~0UL);
if (verbose)
Genode::log(" arg[", i, "]: ", Genode::Hex(argv[i]));
}
catch (Xml_node::Nonexistent_attribute) { }
}
/*

View File

@ -120,6 +120,16 @@ static void inline cpuid(unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *
asm volatile ("cpuid" : "+a" (*eax), "+d" (*edx), "+b" (*ebx), "+c"(*ecx) :: "memory");
}
static unsigned cpu_attribute_value(Genode::Xml_node const &cpu, char const *attribute)
{
if (!cpu.has_attribute(attribute)) {
Genode::error("missing cpu attribute ", attribute);
throw Genode::Exception();
}
return cpu.attribute_value(attribute, 0U);
}
void Component::construct(Genode::Env &env)
{
using namespace Genode;
@ -141,15 +151,13 @@ void Component::construct(Genode::Env &env)
try {
Xml_node const cpus = platform_info.xml().sub_node("hardware").sub_node("cpus");
cpus.for_each_sub_node("cpu", [&] (Xml_node cpu) {
uint8_t family = 0, model = 0, stepping = 0, platform = 0;
unsigned id = 0, patch = 0;
cpu.attribute("id").value(&id);
cpu.attribute("family").value(&family);
cpu.attribute("model").value(&model);
cpu.attribute("stepping").value(&stepping);
cpu.attribute("platform").value(&platform);
cpu.attribute("patch").value(&patch);
unsigned const id = cpu_attribute_value(cpu, "id");
uint8_t const family = cpu_attribute_value(cpu, "family");
uint8_t const model = cpu_attribute_value(cpu, "model");
uint8_t const stepping = cpu_attribute_value(cpu, "stepping");
uint8_t const platform = cpu_attribute_value(cpu, "platform");
unsigned const patch = cpu_attribute_value(cpu, "patch");
String<9> name(Hex(family, Hex::OMIT_PREFIX, Hex::PAD), "-",
Hex(model, Hex::OMIT_PREFIX, Hex::PAD), "-",

View File

@ -262,18 +262,25 @@ void Libc::Component::construct(Libc::Env &env)
/* make Genode environment accessible via the global 'genode_env()' */
genode_env_ptr = &env;
try {
{
using namespace Genode;
Attached_rom_dataspace config(env, "config");
Xml_node::Attribute vbox_file = config.xml().attribute("vbox_file");
vbox_file.value(c_vbox_file, sizeof(c_vbox_file));
Xml_node::Attribute vm_name = config.xml().attribute("vm_name");
vm_name.value(c_vbox_vmname, sizeof(c_vbox_vmname));
} catch (...) {
Genode::error("missing attributes in configuration, minimum requirements: ");
Genode::error(" <config vbox_file=\"...\" vm_name=\"...\">" );
throw;
Attached_rom_dataspace config_ds(env, "config");
Xml_node const config = config_ds.xml();
if (!config.has_attribute("vbox_file") || !config.has_attribute("vm_name")) {
error("missing attributes in configuration, minimum requirements: ");
error(" <config vbox_file=\"...\" vm_name=\"...\">" );
throw Exception();
}
typedef String<128> Name;
Name const vbox_file = config.attribute_value("vbox_file", Name());
Genode::strncpy(c_vbox_file, vbox_file.string(), sizeof(c_vbox_file));
Name const vm_name = config.attribute_value("vm_name", Name());
Genode::strncpy(c_vbox_vmname, vm_name.string(), sizeof(c_vbox_vmname));
}
/* enable stdout/stderr for VBox Log infrastructure */

View File

@ -721,9 +721,12 @@ uint64_t genode_cpu_hz()
if (!cpu_freq) {
try {
platform_rom().sub_node("tsc").attribute("freq_khz").value(&cpu_freq);
platform_rom().with_sub_node("tsc", [&] (Genode::Xml_node const &tsc) {
cpu_freq = tsc.attribute_value("freq_khz", cpu_freq); });
cpu_freq *= 1000ULL;
} catch (...) {
} catch (...) { }
if (cpu_freq == 0) {
Genode::error("could not read out CPU frequency");
Genode::Lock lock;
lock.lock();

View File

@ -693,9 +693,12 @@ uint64_t genode_cpu_hz()
if (!cpu_freq) {
try {
platform_rom().sub_node("tsc").attribute("freq_khz").value(&cpu_freq);
platform_rom().with_sub_node("tsc", [&] (Genode::Xml_node const &tsc) {
cpu_freq = tsc.attribute_value("freq_khz", cpu_freq); });
cpu_freq *= 1000ULL;
} catch (...) {
} catch (...) { }
if (cpu_freq == 0) {
Genode::error("could not read out CPU frequency");
Genode::Lock lock;
lock.lock();