From patchwork Wed Jan 16 22:27:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 10767033 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B427013A4 for ; Wed, 16 Jan 2019 22:28:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A36652E745 for ; Wed, 16 Jan 2019 22:28:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96C052F68E; Wed, 16 Jan 2019 22:28:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD7452E745 for ; Wed, 16 Jan 2019 22:28:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733186AbfAPW2F (ORCPT ); Wed, 16 Jan 2019 17:28:05 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:33839 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729544AbfAPW2F (ORCPT ); Wed, 16 Jan 2019 17:28:05 -0500 Received: by mail-wr1-f65.google.com with SMTP id j2so8875298wrw.1 for ; Wed, 16 Jan 2019 14:28:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=A4DHCkv2lu1CPlpTJrlHIOhL8OGBKrQypMFbimNXXGE=; b=F4vXE/WU9IfPz2F6f/BEMChmW7sAluWtkpCXpO7EmcmzNcfL2IwYRVExLFJ+ioULEI bJnqJNQPOPm7Oc7zEA1IaW5p6i4P5iUqiH7R+NBtSNvf1+9r2MDTGaexWKHoH2GO+Fqm RZqGxyKj+IdzLkpvOKsso4wvF/FaDEo6IcHDza8HIhkM+Okn73HmZR+KKr/21W1X71mi gob79aCJUQIiCRgTfaj1nCSAn0W0nySUIqGhKUTlWiKITvn+TSSCVh+ebGHVNqFn+E4C BHvP2gcwuUTOQIg7p6IrLQ5UGGijMnP8Qi+9HS62JCe0FQUgk8T5V6CZHO+MRqyKDjaE CNpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=A4DHCkv2lu1CPlpTJrlHIOhL8OGBKrQypMFbimNXXGE=; b=d16K0KBXhVbwAWdET8/pwZ/1w6jwc5a7vE3JhHUh9QkzF948EePlhzbG4SWkl8t0nK WY1nHYNj7BY4pq/eLWFnxId+GblQoVUA2pMeOPVXeI38EUEOoNa4r80XAvy5xGXnTrqB xy7SkoGlAYnfBJHAnmyleeyTFxzzevDT88458LN6MURJs/d6C4wVcO0A9ymj6BnBj2sU eZ1/I4f6OfJOU+vcNxfcuZraWNjJcKHlfQE8OS3HUeM2sFO0TDYsCGPVEnaRSXjgCM12 vIHmn/9Td/o7MeqjPVVdQMKmHn6e0gCSX5/gM0tUfW+Gq2LaAKJmujgInx7ZKbVgrQdG mgWA== X-Gm-Message-State: AJcUukd79ZSlhOb7INmj7Xyezz4RTbmduvvyNHPdm6z9yqfh5+5rlBvZ Vsjfu2kMRa58EWr5XzL575yy1g== X-Google-Smtp-Source: ALg8bN7YU3GdBt9L/LNm88Tib8RnYGxdHmF3XXufcMjlFgiNgzrvZczt17HKWdwQ0OA5/p674LrrMw== X-Received: by 2002:a5d:42ce:: with SMTP id t14mr9461741wrr.51.1547677682753; Wed, 16 Jan 2019 14:28:02 -0800 (PST) Received: from localhost.localdomain ([154.119.55.246]) by smtp.gmail.com with ESMTPSA id a18sm98332964wrp.13.2019.01.16.14.27.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Jan 2019 14:28:02 -0800 (PST) From: Christian Brauner To: gregkh@linuxfoundation.org, tkjos@android.com, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: arve@android.com, maco@android.com, joel@joelfernandes.org, tkjos@google.com, shuah@kernel.org, Christian Brauner Subject: [PATCH v1] selftests: add binderfs selftests Date: Wed, 16 Jan 2019 23:27:45 +0100 Message-Id: <20190116222745.26217-1-christian@brauner.io> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-Patchwork-Bot: notify Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This adds the promised selftest for binderfs. It will verify the following things: - binderfs mounting works - binder device allocation works - performing a binder ioctl() request through a binderfs device works - binder device removal works - binder-control removal fails - binderfs unmounting works Cc: Todd Kjos Signed-off-by: Christian Brauner --- /* Changelog */ v1: - check for ENODEV on mount failure to detect whether binderfs is available If it is not, skip the test and exit with success. --- tools/testing/selftests/Makefile | 1 + .../selftests/filesystems/binderfs/.gitignore | 1 + .../selftests/filesystems/binderfs/Makefile | 6 + .../filesystems/binderfs/binderfs_test.c | 126 ++++++++++++++++++ .../selftests/filesystems/binderfs/config | 3 + 5 files changed, 137 insertions(+) create mode 100644 tools/testing/selftests/filesystems/binderfs/.gitignore create mode 100644 tools/testing/selftests/filesystems/binderfs/Makefile create mode 100644 tools/testing/selftests/filesystems/binderfs/binderfs_test.c create mode 100644 tools/testing/selftests/filesystems/binderfs/config diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 1a2bd15c5b6e..400ee81a3043 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -10,6 +10,7 @@ TARGETS += drivers/dma-buf TARGETS += efivarfs TARGETS += exec TARGETS += filesystems +TARGETS += filesystems/binderfs TARGETS += firmware TARGETS += ftrace TARGETS += futex diff --git a/tools/testing/selftests/filesystems/binderfs/.gitignore b/tools/testing/selftests/filesystems/binderfs/.gitignore new file mode 100644 index 000000000000..8a5d9bf63dd4 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/.gitignore @@ -0,0 +1 @@ +binderfs_test diff --git a/tools/testing/selftests/filesystems/binderfs/Makefile b/tools/testing/selftests/filesystems/binderfs/Makefile new file mode 100644 index 000000000000..58cb659b56b4 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 + +CFLAGS += -I../../../../../usr/include/ +TEST_GEN_PROGS := binderfs_test + +include ../../lib.mk diff --git a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file mode 100644 index 000000000000..34361efcb9c8 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../kselftest.h" + +int main(int argc, char *argv[]) +{ + int fd, ret, saved_errno; + size_t len; + struct binderfs_device device = { 0 }; + struct binder_version version = { 0 }; + + ret = unshare(CLONE_NEWNS); + if (ret < 0) + ksft_exit_fail_msg("%s - Failed to unshare mount namespace\n", + strerror(errno)); + + ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0); + if (ret < 0) + ksft_exit_fail_msg("%s - Failed to mount / as private\n", + strerror(errno)); + + ret = mkdir("/dev/binderfs", 0755); + if (ret < 0 && errno != EEXIST) + ksft_exit_fail_msg( + "%s - Failed to create binderfs mountpoint\n", + strerror(errno)); + + ret = mount(NULL, "/dev/binderfs", "binder", 0, 0); + if (ret < 0) { + if (errno != ENODEV) + ksft_exit_fail_msg("%s - Failed to mount binderfs\n", + strerror(errno)); + + ksft_test_result_skip( + "The Android binderfs filesystem is not available\n"); + ksft_exit_pass(); + } + + /* binderfs mount test passed */ + ksft_inc_pass_cnt(); + + memcpy(device.name, "my-binder", strlen("my-binder")); + + fd = open("/dev/binderfs/binder-control", O_RDONLY | O_CLOEXEC); + if (fd < 0) + ksft_exit_fail_msg( + "%s - Failed to open binder-control device\n", + strerror(errno)); + + ret = ioctl(fd, BINDER_CTL_ADD, &device); + saved_errno = errno; + close(fd); + errno = saved_errno; + if (ret < 0) + ksft_exit_fail_msg( + "%s - Failed to allocate new binder device\n", + strerror(errno)); + + printf("Allocated new binder device with major %d, minor %d, and name %s\n", + device.major, device.minor, device.name); + + /* binder device allocation test passed */ + ksft_inc_pass_cnt(); + + fd = open("/dev/binderfs/my-binder", O_CLOEXEC | O_RDONLY); + if (fd < 0) + ksft_exit_fail_msg("%s - Failed to open my-binder device\n", + strerror(errno)); + + ret = ioctl(fd, BINDER_VERSION, &version); + saved_errno = errno; + close(fd); + errno = saved_errno; + if (ret < 0) + ksft_exit_fail_msg( + "%s - Failed to open perform BINDER_VERSION request\n", + strerror(errno)); + + printf("Detected binder version: %d\n", version.protocol_version); + + /* binder transaction with binderfs binder device passed */ + ksft_inc_pass_cnt(); + + ret = unlink("/dev/binderfs/my-binder"); + if (ret < 0) + ksft_exit_fail_msg("%s - Failed to delete binder device\n", + strerror(errno)); + + /* binder device removal passed */ + ksft_inc_pass_cnt(); + + ret = unlink("/dev/binderfs/binder-control"); + if (!ret) + ksft_exit_fail_msg("Managed to delete binder-control device\n"); + else if (errno != EPERM) + ksft_exit_fail_msg( + "%s - Failed to delete binder-control device but exited with unexpected error code\n", + strerror(errno)); + + /* binder-control device removal failed as expected */ + ksft_inc_xfail_cnt(); + + ret = umount2("/dev/binderfs", MNT_DETACH); + if (ret < 0) + ksft_exit_fail_msg("%s - Failed to unmount binderfs\n", + strerror(errno)); + + /* binderfs unmount test passed */ + ksft_inc_pass_cnt(); + + ksft_exit_pass(); +} diff --git a/tools/testing/selftests/filesystems/binderfs/config b/tools/testing/selftests/filesystems/binderfs/config new file mode 100644 index 000000000000..02dd6cc9cf99 --- /dev/null +++ b/tools/testing/selftests/filesystems/binderfs/config @@ -0,0 +1,3 @@ +CONFIG_ANDROID=y +CONFIG_ANDROID_BINDERFS=y +CONFIG_ANDROID_BINDER_IPC=y