1 // SPDX-License-Identifier: GPL-2.0
10 #include <sys/ioctl.h>
11 #include <sys/mount.h>
13 #include <sys/types.h>
15 #include <linux/android/binder.h>
16 #include <linux/android/binderfs.h>
18 int main(int argc, char *argv[])
20 int fd, ret, saved_errno;
21 struct binderfs_device device = { 0 };
23 ret = unshare(CLONE_NEWNS);
25 fprintf(stderr, "%s - Failed to unshare mount namespace\n",
30 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
32 fprintf(stderr, "%s - Failed to mount / as private\n",
37 ret = mkdir("/dev/binderfs", 0755);
38 if (ret < 0 && errno != EEXIST) {
39 fprintf(stderr, "%s - Failed to create binderfs mountpoint\n",
44 ret = mount(NULL, "/dev/binderfs", "binder", 0, 0);
46 fprintf(stderr, "%s - Failed to mount binderfs\n",
51 memcpy(device.name, "my-binder", strlen("my-binder"));
53 fd = open("/dev/binderfs/binder-control", O_RDONLY | O_CLOEXEC);
55 fprintf(stderr, "%s - Failed to open binder-control device\n",
60 ret = ioctl(fd, BINDER_CTL_ADD, &device);
65 fprintf(stderr, "%s - Failed to allocate new binder device\n",
70 printf("Allocated new binder device with major %d, minor %d, and name %s\n",
71 device.major, device.minor, device.name);
73 ret = unlink("/dev/binderfs/my-binder");
75 fprintf(stderr, "%s - Failed to delete binder device\n",
80 /* Cleanup happens when the mount namespace dies. */