0001
0002
0003 #define _GNU_SOURCE
0004 #include <errno.h>
0005 #include <fcntl.h>
0006 #include <sched.h>
0007 #include <stdio.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include <sys/ioctl.h>
0011 #include <sys/mount.h>
0012 #include <sys/stat.h>
0013 #include <sys/types.h>
0014 #include <unistd.h>
0015 #include <linux/android/binder.h>
0016 #include <linux/android/binderfs.h>
0017
0018 int main(int argc, char *argv[])
0019 {
0020 int fd, ret, saved_errno;
0021 struct binderfs_device device = { 0 };
0022
0023 ret = unshare(CLONE_NEWNS);
0024 if (ret < 0) {
0025 fprintf(stderr, "%s - Failed to unshare mount namespace\n",
0026 strerror(errno));
0027 exit(EXIT_FAILURE);
0028 }
0029
0030 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
0031 if (ret < 0) {
0032 fprintf(stderr, "%s - Failed to mount / as private\n",
0033 strerror(errno));
0034 exit(EXIT_FAILURE);
0035 }
0036
0037 ret = mkdir("/dev/binderfs", 0755);
0038 if (ret < 0 && errno != EEXIST) {
0039 fprintf(stderr, "%s - Failed to create binderfs mountpoint\n",
0040 strerror(errno));
0041 exit(EXIT_FAILURE);
0042 }
0043
0044 ret = mount(NULL, "/dev/binderfs", "binder", 0, 0);
0045 if (ret < 0) {
0046 fprintf(stderr, "%s - Failed to mount binderfs\n",
0047 strerror(errno));
0048 exit(EXIT_FAILURE);
0049 }
0050
0051 memcpy(device.name, "my-binder", strlen("my-binder"));
0052
0053 fd = open("/dev/binderfs/binder-control", O_RDONLY | O_CLOEXEC);
0054 if (fd < 0) {
0055 fprintf(stderr, "%s - Failed to open binder-control device\n",
0056 strerror(errno));
0057 exit(EXIT_FAILURE);
0058 }
0059
0060 ret = ioctl(fd, BINDER_CTL_ADD, &device);
0061 saved_errno = errno;
0062 close(fd);
0063 errno = saved_errno;
0064 if (ret < 0) {
0065 fprintf(stderr, "%s - Failed to allocate new binder device\n",
0066 strerror(errno));
0067 exit(EXIT_FAILURE);
0068 }
0069
0070 printf("Allocated new binder device with major %d, minor %d, and name %s\n",
0071 device.major, device.minor, device.name);
0072
0073 ret = unlink("/dev/binderfs/my-binder");
0074 if (ret < 0) {
0075 fprintf(stderr, "%s - Failed to delete binder device\n",
0076 strerror(errno));
0077 exit(EXIT_FAILURE);
0078 }
0079
0080
0081 exit(EXIT_SUCCESS);
0082 }