From: Djalal Harouni Date: Mon, 18 Aug 2014 14:38:03 +0000 (+0100) Subject: test: add helpers for user namespace mapping X-Git-Tag: upstream/0.20140911.160207utc~29 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=52f8384912ac5a3d79e2599be3de4914483aa9f6;p=platform%2Fcore%2Fsystem%2Fkdbus-bus.git test: add helpers for user namespace mapping Will be used by other tests. Signed-off-by: Djalal Harouni --- diff --git a/test/kdbus-util.c b/test/kdbus-util.c index a979c99..e59254d 100644 --- a/test/kdbus-util.c +++ b/test/kdbus-util.c @@ -795,3 +795,53 @@ int drop_privileges(uid_t uid, gid_t gid) return ret; } + +static int do_userns_map_id(pid_t pid, + const char *map_file, + const char *map_id) +{ + int ret; + int fd; + + fd = open(map_file, O_RDWR); + if (fd < 0) { + ret = -errno; + fprintf(stderr, "error open %s: %d (%m)\n", + map_file, ret); + return ret; + } + + ret = write(fd, map_id, strlen(map_id)); + if (ret < 0) { + ret = -errno; + fprintf(stderr, "error write to %s: %d (%m)\n", + map_file, ret); + goto out; + } + + ret = 0; + +out: + close(fd); + return ret; +} + +int userns_map_uid_gid(pid_t pid, + const char *map_uid, + const char *map_gid) +{ + int ret; + char file_id[128] = {'\0'}; + + snprintf(file_id, sizeof(file_id), "/proc/%ld/uid_map", + (long) pid); + + ret = do_userns_map_id(pid, file_id, map_uid); + if (ret < 0) + return ret; + + snprintf(file_id, sizeof(file_id), "/proc/%ld/gid_map", + (long) pid); + + return do_userns_map_id(pid, file_id, map_gid); +} diff --git a/test/kdbus-util.h b/test/kdbus-util.h index 5ecab2f..765f6af 100644 --- a/test/kdbus-util.h +++ b/test/kdbus-util.h @@ -29,6 +29,9 @@ (uint8_t *)(item) < (uint8_t *)(head) + (head)->size; \ item = KDBUS_ITEM_NEXT(item)) +/* Dump as user of process, useful for user namespace testing */ +#define SUID_DUMP_USER 1 + struct conn { int fd; uint64_t id; @@ -67,3 +70,7 @@ int conn_update_policy(struct conn *conn, const char *name, void add_match_empty(int fd); int drop_privileges(uid_t uid, gid_t gid); + +int userns_map_uid_gid(pid_t pid, + const char *map_uid, + const char *map_gid);