summaryrefslogtreecommitdiff
path: root/common/hal/hidl_service/libc_wrappers.cc
blob: 45b260bc23e6e3c88a61af4a0e197b81e57780a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <dirent.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <unordered_map>

#define LOG_TAG "GCH_libc_wrappers"
#include <log/log.h>

//#define MONITOR_FILE_OP

#ifdef __ANDROID_APEX__

#ifdef MONITOR_FILE_OP

using mkfifo_function_type = int (*)(const char*, mode_t);
using access_function_type = int (*)(const char*, int);
using mkdir_function_type = int (*)(const char*, mode_t);
using rmdir_function_type = int (*)(const char*);
using chdir_function_type = int (*)(const char*);
using link_function_type = int (*)(const char*, const char*);
using unlink_function_type = int (*)(const char*);
using rename_function_type = int (*)(const char*, const char*);
using stat_function_type = int (*)(const char*, struct stat*);
using chmod_function_type = int (*)(const char*, mode_t);
using chown_function_type = int (*)(const char*, uid_t, gid_t);
using utime_function_type = int (*)(const char*, struct utimbuf*);
using opendir_function_type = DIR* (*)(const char*);
using execl_function_type = int (*)(const char*, const char*, ...);
using execle_function_type = int (*)(const char*, const char*, ...);
using execlp_function_type = int (*)(const char*, const char*, ...);
using execv_function_type = int (*)(const char*, char* const[]);
using execvp_function_type = int (*)(const char*, char* const[]);
using openat_function_type = int (*)(int, const char*, int, ...);

extern "C" int mkfifo(const char* pathname, mode_t mode) {
  static auto real_function =
      reinterpret_cast<mkfifo_function_type>(dlsym(RTLD_NEXT, "mkfifo"));
  int ret = real_function(pathname, mode);
  ALOGI("mkfifo (%s,%d) == %d", pathname, mode, ret);
  return ret;
}
extern "C" int access(const char* pathname, int mode) {
  static auto real_function =
      reinterpret_cast<access_function_type>(dlsym(RTLD_NEXT, "access"));
  int ret = real_function(pathname, mode);
  ALOGI("access (%s,%d) == %d", pathname, mode, ret);
  return ret;
}
extern "C" int mkdir(const char* pathname, mode_t mode) {
  static auto real_function =
      reinterpret_cast<mkdir_function_type>(dlsym(RTLD_NEXT, "mkdir"));
  int ret = real_function(pathname, mode);
  ALOGI("mkdir (%s,%d) == %d", pathname, mode, ret);
  return ret;
}
extern "C" int rmdir(const char* pathname) {
  static auto real_function =
      reinterpret_cast<rmdir_function_type>(dlsym(RTLD_NEXT, "rmdir"));
  int ret = real_function(pathname);
  ALOGI("rmdir (%s) == %d", pathname, ret);
  return ret;
}
extern "C" int chdir(const char* path) {
  static auto real_function =
      reinterpret_cast<chdir_function_type>(dlsym(RTLD_NEXT, "chdir"));
  int ret = real_function(path);
  ALOGI("chdir (%s) == %d", path, ret);
  return ret;
}
extern "C" int link(const char* oldpath, const char* newpath) {
  static auto real_function =
      reinterpret_cast<link_function_type>(dlsym(RTLD_NEXT, "link"));
  int ret = real_function(oldpath, newpath);
  ALOGI("link (%s,%s) == %d", oldpath, newpath, ret);
  return ret;
}
extern "C" int unlink(const char* pathname) {
  static auto real_function =
      reinterpret_cast<unlink_function_type>(dlsym(RTLD_NEXT, "unlink"));
  int ret = real_function(pathname);
  ALOGI("unlink (%s) == %d", pathname, ret);
  return ret;
}
extern "C" int rename(const char* oldpath, const char* newpath) {
  static auto real_function =
      reinterpret_cast<rename_function_type>(dlsym(RTLD_NEXT, "rename"));
  int ret = real_function(oldpath, newpath);
  ALOGI("rename (%s,%s) == %d", oldpath, newpath, ret);
  return ret;
}
extern "C" int stat(const char* file_name, struct stat* buf) {
  static auto real_function =
      reinterpret_cast<stat_function_type>(dlsym(RTLD_NEXT, "stat"));
  int ret = real_function(file_name, buf);
  ALOGI("stat (%s, %p) == %d", file_name, buf, ret);
  return ret;
}
extern "C" int chmod(const char* path, mode_t mode) {
  static auto real_function =
      reinterpret_cast<chmod_function_type>(dlsym(RTLD_NEXT, "chmod"));
  int ret = real_function(path, mode);
  ALOGI("chmod (%s) == %d", path, ret);
  return ret;
}
extern "C" int chown(const char* path, uid_t owner, gid_t group) {
  static auto real_function =
      reinterpret_cast<chown_function_type>(dlsym(RTLD_NEXT, "chown"));
  int ret = real_function(path, owner, group);
  ALOGI("chown (%s,%d,%d) == %d", path, owner, group, ret);
  return ret;
}
extern "C" int utime(const char* filename, struct utimbuf* buf) {
  static auto real_function =
      reinterpret_cast<utime_function_type>(dlsym(RTLD_NEXT, "utime"));
  int ret = real_function(filename, buf);
  ALOGI("utime (%s, %p) == %d", filename, buf, ret);
  return ret;
}
extern "C" DIR* opendir(const char* name) {
  static auto real_function =
      reinterpret_cast<opendir_function_type>(dlsym(RTLD_NEXT, "opendir"));
  DIR* ret = real_function(name);
  ALOGI("opendir (%s) == %p", name, ret);
  return ret;
}
extern "C" int execl(const char* path, const char* arg, ...) {
  static auto real_function =
      reinterpret_cast<execl_function_type>(dlsym(RTLD_NEXT, "execl"));
  int ret = real_function(path, arg);
  ALOGI("execl (%s, %s) == %d", path, arg, ret);
  return ret;
}
extern "C" int execle(const char* path, const char* arg, ...) {
  static auto real_function =
      reinterpret_cast<execle_function_type>(dlsym(RTLD_NEXT, "execle"));
  int ret = real_function(path, arg);
  ALOGI("execle (%s, %s) == %d", path, arg, ret);
  return ret;
}
extern "C" int execlp(const char* file, const char* arg, ...) {
  static auto real_function =
      reinterpret_cast<execlp_function_type>(dlsym(RTLD_NEXT, "execlp"));
  int ret = real_function(file, arg);
  ALOGI("execlp %s, %s) == %d", file, arg, ret);
  return ret;
}
extern "C" int execv(const char* path, char* const argv[]) {
  static auto real_function =
      reinterpret_cast<execv_function_type>(dlsym(RTLD_NEXT, "execv"));
  int ret = real_function(path, argv);
  ALOGI("execv (%s, %s, ...) == %d", path, argv[0], ret);
  return ret;
}
extern "C" int execvp(const char* file, char* const argv[]) {
  static auto real_function =
      reinterpret_cast<execvp_function_type>(dlsym(RTLD_NEXT, "execvp"));
  int ret = real_function(file, argv);
  ALOGI("execvp (%s, %s,...) == %d", file, argv[0], ret);
  return ret;
}

extern "C" int openat(int dirfd, const char* pathname, int flags, ...) {
  static auto real_function =
      reinterpret_cast<openat_function_type>(dlsym(RTLD_NEXT, "openat"));
  int ret = real_function(dirfd, pathname, flags);
  ALOGI("openat(%d, %s, 0x%x) == %d", dirfd, pathname, flags, ret);
  return ret;
}
#endif

using dlopen_function_type = void* (*)(const char*, int);

// This is a temporary workaround for prebuilts calling dlopen with absolute
// paths.
extern "C" void* dlopen(const char* filename, int flags) {
  static auto real_dlopen_function =
      reinterpret_cast<dlopen_function_type>(dlsym(RTLD_NEXT, "dlopen"));
  if (!real_dlopen_function) {
    ALOGE("Could not RTLD_NEXT dlopen, something very wrong.");
    std::abort();
  }
  void* ret = real_dlopen_function(filename, flags);
  if (!ret) {
    ALOGI("dlopen(%s) failed, seeing if we can fix it", filename);
    std::string original_filename(filename);

    if (original_filename.find("/vendor/") == 0) {
      std::string new_filename = "/apex/com.google.pixel.camera.hal/" +
                                 original_filename.substr(strlen("/vendor/"));
      ALOGI("Trying %s instead of %s\n", new_filename.c_str(), filename);
      ret = real_dlopen_function(new_filename.c_str(), flags);
      if (ret) {
        ALOGE(
            "ERROR: Update your code to not use absolute paths. dlopen(%s) "
            "failed but dlopen(%s) succeeded instead.",
            filename, new_filename.c_str());
      }
    }
  }
  return ret;
}
#endif