diff --git a/meson.build b/meson.build index b4093b130f..c2d48f0770 100644 --- a/meson.build +++ b/meson.build @@ -1388,6 +1388,8 @@ includes = include_directories('src/basic', add_project_arguments('-include', 'config.h', language : 'c') +generate_gperfs = find_program('tools/generate-gperfs.py') + subdir('po') subdir('catalog') subdir('src/systemd') diff --git a/src/basic/af-list.c b/src/basic/af-list.c index 866a1d4317..cf03dfc6d3 100644 --- a/src/basic/af-list.c +++ b/src/basic/af-list.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include #include #include @@ -29,7 +30,7 @@ int af_from_name(const char *name) { sc = lookup_af(name, strlen(name)); if (!sc) - return AF_UNSPEC; + return -EINVAL; return sc->id; } diff --git a/src/basic/arphrd-list.c b/src/basic/arphrd-list.c index a7ae4b8162..a6b8fa9d84 100644 --- a/src/basic/arphrd-list.c +++ b/src/basic/arphrd-list.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include #include #include @@ -29,7 +30,7 @@ int arphrd_from_name(const char *name) { sc = lookup_arphrd(name, strlen(name)); if (!sc) - return 0; + return -EINVAL; return sc->id; } diff --git a/src/basic/meson.build b/src/basic/meson.build index 0b27ffda7d..b13897f334 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -179,8 +179,6 @@ basic_sources = files(''' missing_h = files('missing.h') -generate_gperfs = find_program('generate-gperfs.py') - generate_af_list = find_program('generate-af-list.sh') af_list_txt = custom_target( 'af-list.txt', @@ -209,19 +207,11 @@ errno_list_txt = custom_target( command : [generate_errno_list, cpp], capture : true) -generate_socket_protocol_list = find_program('generate-socket-protocol-list.sh') -socket_protocol_list_txt = custom_target( - 'socket-protocol-list.txt', - output : 'socket-protocol-list.txt', - command : [generate_socket_protocol_list, cpp], - capture : true) - generated_gperf_headers = [] foreach item : [['af', af_list_txt, 'af', ''], ['arphrd', arphrd_list_txt, 'arphrd', 'ARPHRD_'], ['cap', cap_list_txt, 'capability', ''], - ['errno', errno_list_txt, 'errno', ''], - ['socket-protocol', socket_protocol_list_txt, 'socket_protocol', 'IPPROTO_']] + ['errno', errno_list_txt, 'errno', '']] fname = '@0@-from-name.gperf'.format(item[0]) gperf_file = custom_target( diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 39823c8675..31f9099b98 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -1517,8 +1517,8 @@ int bus_exec_context_set_transient_property( int af; af = af_from_name(*s); - if (af <= 0) - return -EINVAL; + if (af < 0) + return af; if (!invert == c->address_families_whitelist) { r = set_put(c->address_families, INT_TO_PTR(af)); diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index ce222e4023..8b36365dd6 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -65,7 +65,7 @@ static int supported_socket_protocol_from_string(const char *s) { r = socket_protocol_from_name(s); if (r < 0) - return -EINVAL; + return r; if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP)) return -EPROTONOSUPPORT; @@ -2871,8 +2871,8 @@ int config_parse_address_families( } af = af_from_name(word); - if (af <= 0) { - log_syntax(unit, LOG_ERR, filename, line, 0, + if (af < 0) { + log_syntax(unit, LOG_ERR, filename, line, af, "Failed to parse address family, ignoring: %s", word); continue; } diff --git a/src/nspawn/nspawn-settings.c b/src/nspawn/nspawn-settings.c index 264ec61ad9..505e8cac40 100644 --- a/src/nspawn/nspawn-settings.c +++ b/src/nspawn/nspawn-settings.c @@ -177,7 +177,6 @@ int config_parse_capability( for (;;) { _cleanup_free_ char *word = NULL; - int cap; r = extract_first_word(&rvalue, &word, NULL, 0); if (r < 0) { @@ -187,13 +186,13 @@ int config_parse_capability( if (r == 0) break; - cap = capability_from_name(word); - if (cap < 0) { - log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse capability, ignoring: %s", word); + r = capability_from_name(word); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse capability, ignoring: %s", word); continue; } - u |= UINT64_C(1) << cap; + u |= UINT64_C(1) << r; } if (u == 0) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index c581514b29..6eddae49d9 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -774,17 +774,14 @@ static int parse_argv(int argc, char *argv[]) { else minus = (uint64_t) -1; } else { - int cap; - - cap = capability_from_name(t); - if (cap < 0) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Failed to parse capability %s.", t); + r = capability_from_name(t); + if (r < 0) + return log_error_errno(r, "Failed to parse capability %s.", t); if (c == ARG_CAPABILITY) - plus |= 1ULL << (uint64_t) cap; + plus |= 1ULL << r; else - minus |= 1ULL << (uint64_t) cap; + minus |= 1ULL << r; } } diff --git a/src/basic/generate-socket-protocol-list.sh b/src/shared/generate-socket-protocol-list.sh similarity index 100% rename from src/basic/generate-socket-protocol-list.sh rename to src/shared/generate-socket-protocol-list.sh diff --git a/src/shared/meson.build b/src/shared/meson.build index f7c2c7f560..b7676b6eb9 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -186,6 +186,45 @@ if conf.get('HAVE_KMOD') == 1 shared_sources += files('module-util.c') endif +generate_socket_protocol_list = find_program('generate-socket-protocol-list.sh') +socket_protocol_list_txt = custom_target( + 'socket-protocol-list.txt', + output : 'socket-protocol-list.txt', + command : [generate_socket_protocol_list, cpp], + capture : true) + +fname = 'socket-protocol-from-name.gperf' +gperf_file = custom_target( + fname, + input : socket_protocol_list_txt, + output : fname, + command : [generate_gperfs, 'socket_protocol', 'IPPROTO_', '@INPUT@'], + capture : true) + +fname = 'socket-protocol-from-name.h' +target1 = custom_target( + fname, + input : gperf_file, + output : fname, + command : [gperf, + '-L', 'ANSI-C', '-t', '--ignore-case', + '-N', 'lookup_socket_protocol', + '-H', 'hash_socket_protocol_name', + '-p', '-C', + '@INPUT@'], + capture : true) + +fname = 'socket-protocol-to-name.h' +awkscript = 'socket-protocol-to-name.awk' +target2 = custom_target( + fname, + input : [awkscript, socket_protocol_list_txt], + output : fname, + command : [awk, '-f', '@INPUT0@', '@INPUT1@'], + capture : true) + +shared_sources += [target1, target2] + libshared_name = 'systemd-shared-@0@'.format(meson.project_version()) libshared_deps = [threads, diff --git a/src/shared/socket-protocol-list.c b/src/shared/socket-protocol-list.c index 8041b84958..797f1e9507 100644 --- a/src/shared/socket-protocol-list.c +++ b/src/shared/socket-protocol-list.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include #include #include @@ -29,7 +30,7 @@ int socket_protocol_from_name(const char *name) { sc = lookup_socket_protocol(name, strlen(name)); if (!sc) - return 0; + return -EINVAL; return sc->id; } diff --git a/src/basic/socket-protocol-to-name.awk b/src/shared/socket-protocol-to-name.awk similarity index 100% rename from src/basic/socket-protocol-to-name.awk rename to src/shared/socket-protocol-to-name.awk diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c index e9fd66b1e6..c8ef3295b5 100644 --- a/src/test/test-af-list.c +++ b/src/test/test-af-list.c @@ -27,8 +27,8 @@ int main(int argc, const char *argv[]) { assert_se(af_to_name(af_max()) == NULL); assert_se(af_to_name(-1) == NULL); - assert_se(af_from_name("huddlduddl") == AF_UNSPEC); - assert_se(af_from_name("") == AF_UNSPEC); + assert_se(af_from_name("huddlduddl") == -EINVAL); + assert_se(af_from_name("") == -EINVAL); return 0; } diff --git a/src/test/test-arphrd-list.c b/src/test/test-arphrd-list.c index fd4cd62623..9803c53f21 100644 --- a/src/test/test-arphrd-list.c +++ b/src/test/test-arphrd-list.c @@ -27,8 +27,8 @@ int main(int argc, const char *argv[]) { assert_se(arphrd_to_name(arphrd_max()) == NULL); assert_se(arphrd_to_name(0) == NULL); - assert_se(arphrd_from_name("huddlduddl") == 0); - assert_se(arphrd_from_name("") == 0); + assert_se(arphrd_from_name("huddlduddl") == -EINVAL); + assert_se(arphrd_from_name("") == -EINVAL); return 0; } diff --git a/src/basic/generate-gperfs.py b/tools/generate-gperfs.py similarity index 100% rename from src/basic/generate-gperfs.py rename to tools/generate-gperfs.py