~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/binderfs/binderfs_example.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #define _GNU_SOURCE
  4 #include <errno.h>
  5 #include <fcntl.h>
  6 #include <sched.h>
  7 #include <stdio.h>
  8 #include <stdlib.h>
  9 #include <string.h>
 10 #include <sys/ioctl.h>
 11 #include <sys/mount.h>
 12 #include <sys/stat.h>
 13 #include <sys/types.h>
 14 #include <unistd.h>
 15 #include <linux/android/binder.h>
 16 #include <linux/android/binderfs.h>
 17 
 18 int main(int argc, char *argv[])
 19 {
 20         int fd, ret, saved_errno;
 21         struct binderfs_device device = { 0 };
 22 
 23         ret = unshare(CLONE_NEWNS);
 24         if (ret < 0) {
 25                 fprintf(stderr, "%s - Failed to unshare mount namespace\n",
 26                         strerror(errno));
 27                 exit(EXIT_FAILURE);
 28         }
 29 
 30         ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
 31         if (ret < 0) {
 32                 fprintf(stderr, "%s - Failed to mount / as private\n",
 33                         strerror(errno));
 34                 exit(EXIT_FAILURE);
 35         }
 36 
 37         ret = mkdir("/dev/binderfs", 0755);
 38         if (ret < 0 && errno != EEXIST) {
 39                 fprintf(stderr, "%s - Failed to create binderfs mountpoint\n",
 40                         strerror(errno));
 41                 exit(EXIT_FAILURE);
 42         }
 43 
 44         ret = mount(NULL, "/dev/binderfs", "binder", 0, 0);
 45         if (ret < 0) {
 46                 fprintf(stderr, "%s - Failed to mount binderfs\n",
 47                         strerror(errno));
 48                 exit(EXIT_FAILURE);
 49         }
 50 
 51         memcpy(device.name, "my-binder", strlen("my-binder"));
 52 
 53         fd = open("/dev/binderfs/binder-control", O_RDONLY | O_CLOEXEC);
 54         if (fd < 0) {
 55                 fprintf(stderr, "%s - Failed to open binder-control device\n",
 56                         strerror(errno));
 57                 exit(EXIT_FAILURE);
 58         }
 59 
 60         ret = ioctl(fd, BINDER_CTL_ADD, &device);
 61         saved_errno = errno;
 62         close(fd);
 63         errno = saved_errno;
 64         if (ret < 0) {
 65                 fprintf(stderr, "%s - Failed to allocate new binder device\n",
 66                         strerror(errno));
 67                 exit(EXIT_FAILURE);
 68         }
 69 
 70         printf("Allocated new binder device with major %d, minor %d, and name %s\n",
 71                device.major, device.minor, device.name);
 72 
 73         ret = unlink("/dev/binderfs/my-binder");
 74         if (ret < 0) {
 75                 fprintf(stderr, "%s - Failed to delete binder device\n",
 76                         strerror(errno));
 77                 exit(EXIT_FAILURE);
 78         }
 79 
 80         /* Cleanup happens when the mount namespace dies. */
 81         exit(EXIT_SUCCESS);
 82 }
 83 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php