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
;
22 struct binderfs_device device
= { 0 };
24 ret
= unshare(CLONE_NEWNS
);
26 fprintf(stderr
, "%s - Failed to unshare mount namespace\n",
31 ret
= mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0);
33 fprintf(stderr
, "%s - Failed to mount / as private\n",
38 ret
= mkdir("/dev/binderfs", 0755);
39 if (ret
< 0 && errno
!= EEXIST
) {
40 fprintf(stderr
, "%s - Failed to create binderfs mountpoint\n",
45 ret
= mount(NULL
, "/dev/binderfs", "binder", 0, 0);
47 fprintf(stderr
, "%s - Failed to mount binderfs\n",
52 memcpy(device
.name
, "my-binder", strlen("my-binder"));
54 fd
= open("/dev/binderfs/binder-control", O_RDONLY
| O_CLOEXEC
);
56 fprintf(stderr
, "%s - Failed to open binder-control device\n",
61 ret
= ioctl(fd
, BINDER_CTL_ADD
, &device
);
66 fprintf(stderr
, "%s - Failed to allocate new binder device\n",
71 printf("Allocated new binder device with major %d, minor %d, and name %s\n",
72 device
.major
, device
.minor
, device
.name
);
74 ret
= unlink("/dev/binderfs/my-binder");
76 fprintf(stderr
, "%s - Failed to delete binder device\n",
81 /* Cleanup happens when the mount namespace dies. */