eBPF-based security platform
paths: join a directory and a name in one helper
John Fastabendaec63383232a
diff --git a/builtin-upatch.c b/builtin-upatch.cindex b879b4ce..1f85102b 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1313,23 +1313,17 @@ static void count_attach_progs(const struct prog_spec *specs,*out = n;}-/* entry in dir: 0 or -ENAMETOOLONG, as upatch_link_pin() returns. */-static int path_in(char *out, size_t sz, const char *dir, const char *entry)-{- return strsprintf(out, sz, "%s/%s", dir, entry) < 0 ? -ENAMETOOLONG : 0;-}-static int prog_pin_path(char *pin, size_t sz, const char *dir,const char *serializers_dir,const struct prog_spec *spec,__u32 attach_count, __u32 attach_idx){if (spec->role == UPATCH_PROG_ROLE_SERIALIZER)- return path_in(pin, sz, serializers_dir, spec->map);+ return path_join(pin, sz, serializers_dir, spec->map);if (spec->role == UPATCH_PROG_ROLE_TEST_RUN)- return path_in(pin, sz, dir, "test");+ return path_join(pin, sz, dir, "test");if (attach_count == 1)- return path_in(pin, sz, dir, "link");+ return path_join(pin, sz, dir, "link");return upatch_link_pin(pin, sz, dir, attach_idx);}diff --git a/testing/unit/paths.c b/testing/unit/paths.cindex cf107974..e59d93af 100644--- a/testing/unit/paths.c+++ b/testing/unit/paths.c@@ -33,6 +33,16 @@ static void pin_path_too_long_is_empty(void)EXPECT_STREQ(out, "");}+static void join_is_dir_slash_name(void)+{+ char out[sizeof("dir/name")];++ ASSERT_EQ(path_join(out, sizeof(out), "dir", "name"), 0);+ EXPECT_STREQ(out, "dir/name");+ EXPECT_EQ(path_join(out, sizeof(out), "dir", "names"), -ENAMETOOLONG);+ EXPECT_STREQ(out, "");+}+static void bpffs_name_round_trips(void){char bpffs[64], back[32];@@ -49,6 +59,7 @@ static void bpffs_name_round_trips(void)static const struct unit_case cases[] = {UNIT_CASE(pin_path_is_the_bpffs_name_and_its_parts),UNIT_CASE(pin_path_too_long_is_empty),+ UNIT_CASE(join_is_dir_slash_name),UNIT_CASE(bpffs_name_round_trips),{}};diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 420a6530..87f4a7d6 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -1110,11 +1110,6 @@ static int build_validate(struct bpm_build_spec *spec,return ret;}-static int build_join(char *out, size_t cap, const char *dir, const char *name)-{- return strsprintf(out, cap, "%s/%s", dir, name) < 0 ? -ENAMETOOLONG : 0;-}-static int build_entry_name(char *out, size_t cap, const char *top,const char *entry){@@ -1556,7 +1551,8 @@ static int build_archive(const struct bpm_build_spec *spec, const char *version,char readme[PATH_MAX];struct stat st;- ret = build_join(readme, sizeof(readme), spec->scope, "README.md");+ ret = path_join(readme, sizeof(readme), spec->scope,+ "README.md");if (!ret) {if (!stat(readme, &st)) {if (!build_within(spec->scope, readme))@@ -1731,8 +1727,8 @@ static int __build_bpm_build(int argc, char **argv,if (file_arg) {if (strscpy(build_file, file_arg, sizeof(build_file)) < 0)return -ENAMETOOLONG;- } else if (build_join(build_file, sizeof(build_file), context,- "Beefile")) {+ } else if (path_join(build_file, sizeof(build_file), context,+ "Beefile")) {return -ENAMETOOLONG;}if (!realpath(build_file, resolved_file))@@ -2109,8 +2105,8 @@ static int __build_bpm_config_update(int argc, char **argv,ret = -EINVAL;break;}- if (build_join(output, sizeof(output), spec->scope,- spec->files[i].source)) {+ if (path_join(output, sizeof(output), spec->scope,+ spec->files[i].source)) {ret = -ENAMETOOLONG;break;}@@ -2121,8 +2117,8 @@ static int __build_bpm_config_update(int argc, char **argv,if (!ret) {char readme[PATH_MAX];- if (build_join(readme, sizeof(readme), spec->scope,- "README.md")) {+ if (path_join(readme, sizeof(readme), spec->scope,+ "README.md")) {ret = -ENAMETOOLONG;} else {ret = build_copy_package_member(base_package,diff --git a/util/paths.c b/util/paths.cindex 22bcbd70..5e54054c 100644--- a/util/paths.c+++ b/util/paths.c@@ -90,6 +90,15 @@ too_long:return -ENAMETOOLONG;}+int path_join(char *out, size_t cap, const char *dir, const char *name)+{+ if (strsprintf(out, cap, "%s/%s", dir, name) >= 0)+ return 0;+ if (cap)+ out[0] = '\0';+ return -ENAMETOOLONG;+}+bool path_exists(const char *path){struct stat st;diff --git a/util/paths.h b/util/paths.hindex d445f717..660cced5 100644--- a/util/paths.h+++ b/util/paths.h@@ -215,6 +215,9 @@ static inline int upatch_name_from_bpffs(char *out, size_t out_sz,*/int upatch_pin_path(char *out, size_t cap, const char *name, ...) __sentinel;+/* dir, a slash and name: 0, or -ENAMETOOLONG with out empty. */+int path_join(char *out, size_t cap, const char *dir, const char *name);+bool path_exists(const char *path);#endif /* SHIRUDO_PATHS_H */