-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaudio_hal_installer.cpp
571 lines (488 loc) · 15.9 KB
/
audio_hal_installer.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include "audio_hal_installer.h"
bool cmdMatch(int pid, const char *name) {
char cmdline[1024];
int fd, r;
sprintf(cmdline, "/proc/%d/cmdline", pid);
fd = open(cmdline, O_RDONLY);
if (fd == 0)
return false;
r = read(fd, cmdline, 1023);
close(fd);
if (r <= 0) {
return false;
}
cmdline[r] = 0;
if (!strncmp(cmdline, name, r))
return true;
return false;
}
int getProcessPid(const char *name) {
DIR *d;
struct dirent *de;
d = opendir("/proc");
if(d == 0)
return -1;
while((de = readdir(d)) != 0){
if (!isdigit(de->d_name[0]))
continue;
int pid = atoi(de->d_name);
if (pid != 0 && cmdMatch(pid, name)) {
closedir(d);
return pid;
}
}
closedir(d);
return -1;
}
bool waitForProcessStop(int pid, int waitTime, int maxWait) {
int totalWait = 0;
while (totalWait < maxWait) {
if (kill(pid, 0) == -1) {
return true;
}
usleep(waitTime);
totalWait += waitTime;
}
return false;
}
int getMediaServerPid() {
return getProcessPid("/system/bin/mediaserver");
}
bool moveFile(const char* src, const char* dst) {
if (!rename(src, dst)) {
ALOGV("\t%s => %s", src, dst);
return true;
}
ALOGE("\t%s => %s error: %s", src, dst, strerror(errno));
return false;
}
bool fileExists(const char* path) {
if (!access(path, R_OK))
return true;
if (errno != ENOENT) {
ALOGE("error accessing a file %s %s", path, strerror(errno));
}
return false;
}
bool copyFile(const char* src, const char* dst) {
char buf[BUFSIZ];
ssize_t size;
bool success = true;
int srcFd = open(src, O_RDONLY, 0);
if (srcFd < 0) {
ALOGE("Can't open source file %s error: %s", src, strerror(errno));
return false;
}
int dstFd = open(dst, O_WRONLY | O_CREAT, 0644);
if (dstFd < 0) {
ALOGE("Can't open destination file %s error: %s", dst, strerror(errno));
close(srcFd);
return false;
}
while ((size = read(srcFd, buf, BUFSIZ)) > 0) {
write(dstFd, buf, size);
}
if (size == -1) {
ALOGE("Error copying %s to %s error: %s", src, dst, strerror(errno));
success = false;
} else {
ALOGV("\tcopy %s to %s", src, dst);
}
close(srcFd);
close(dstFd);
chmod(dst, 0644);
return success;
}
bool removeFile(const char* path) {
if (unlink(path)) {
ALOGE("error deleting file %s error: %s", path, strerror(errno));
return false;
}
return true;
}
bool symlinkRwFiles(const char *baseDir) {
char confPath [1024];
char logPath [1024];
sprintf(confPath, "%.950s/scr_audio.conf", baseDir);
sprintf(logPath, "%.950s/scr_audio.log", baseDir);
if (fileExists("/system/lib/hw/scr_audio.conf")) {
removeFile("/system/lib/hw/scr_audio.conf");
}
if (fileExists("/system/lib/hw/scr_audio.log")) {
removeFile("/system/lib/hw/scr_audio.log");
}
ALOGV("Creating rw files symlinks");
if (symlink(confPath, "/system/lib/hw/scr_audio.conf") || symlink(logPath, "/system/lib/hw/scr_audio.log")) {
ALOGE("Error creating symlink %s", strerror(errno));
return false;
}
return true;
}
bool moveOriginalModules() {
DIR *d;
struct dirent *de;
char src[512];
char dst[512];
ALOGV("Moving original audio drivers");
d = opendir("/system/lib/hw/");
if(d == 0)
return -1;
while((de = readdir(d)) != 0){
if (strncmp(de->d_name, "audio.primary.", 13))
continue;
sprintf(src, "/system/lib/hw/audio.primary.%s", de->d_name + 14);
sprintf(dst, "/system/lib/hw/audio.original_primary.%s", de->d_name + 14);
if (fileExists(dst)) {
ALOGV("%s file already exists. Skipping.", dst);
} else {
if (!moveFile(src, dst)) {
closedir(d);
return false;
}
}
}
closedir(d);
return true;
}
bool restoreOriginalModules() {
DIR *d;
struct dirent *de;
char src[512];
char dst[512];
bool success = true;
ALOGV("Restoring original audio drivers");
d = opendir("/system/lib/hw/");
if(d == 0)
return -1;
while((de = readdir(d)) != 0){
if (strncmp(de->d_name, "audio.original_primary.", 22))
continue;
sprintf(src, "/system/lib/hw/audio.original_primary.%s", de->d_name + 23);
sprintf(dst, "/system/lib/hw/audio.primary.%s", de->d_name + 23);
if (fileExists(dst)) {
removeFile(dst);
chmod(dst, 0644);
}
success = moveFile(src, dst) && success;
chmod(dst, 0644);
}
closedir(d);
return success;
}
int waitForMediaServerPid() {
int retryCount = 0;
int pid = -1;
while (pid == -1 && retryCount++ < 100) {
usleep(100000);
pid = getMediaServerPid();
}
return pid;
}
void backupConfigFile(const char* src, const char* dst) {
if (fileExists(src) && !fileExists(dst)) {
ALOGV("Moving original config file %s to %s", src, dst);
moveFile(src, dst);
}
}
void restoreConfigFile(const char* src, const char* dst) {
if (!fileExists(src)) {
return;
}
if (fileExists(dst)) {
removeFile(dst);
}
ALOGV("Restoring original config file %s to %s", src, dst);
moveFile(src, dst);
chmod(dst, 0644);
}
void stopMediaServer() {
ALOGV("Restarting ms");
int pid = getMediaServerPid();
if (pid == -1) {
ALOGV("No ms running!");
} else {
kill(pid, SIGTERM);
if (!waitForProcessStop(pid, 500000, 5000000)) {
kill(pid, SIGKILL);
waitForProcessStop(pid, 100000, 1000000);
}
}
}
bool isProcessWriting(int pid, dev_t device, const char *fd) {
char procFd[32];
sprintf(procFd, "/proc/%d/fd/%s", pid, fd);
struct stat st, lst;
if (stat(procFd, &st) != 0) {
ALOGE("Can't access %s : %s", procFd, strerror(errno));
return false;
}
if (lstat(procFd, &lst) != 0) {
ALOGE("Can't access link %s : %s", procFd, strerror(errno));
return false;
}
if (st.st_dev == device && (lst.st_mode & S_IWUSR)) {
char *path = realpath(procFd, NULL);
if (path == NULL) {
ALOGE("Error resolving path %s %s", procFd, strerror(errno));
} else {
ALOGW("%d writting to %s", pid, path);
free(path);
}
return true;
}
return false;
}
bool isProcessWriting(int pid, dev_t device) {
DIR *d;
struct dirent *de;
char procFd[32];
sprintf(procFd, "/proc/%d/fd", pid);
d = opendir(procFd);
if (d == 0) {
ALOGW("Error opening %s %s", procFd, strerror(errno));
return false;
}
while ((de = readdir(d)) != 0) {
if (isProcessWriting(pid, device, de->d_name)) {
closedir(d);
return true;
}
}
closedir(d);
return false;
}
int killWritingProcesses(const char *path) {
DIR *d;
struct dirent *de;
struct stat st;
if (stat(path, &st) != 0) {
ALOGE("Can't access %s : %s", path, strerror(errno));
return -1;
}
dev_t device = st.st_dev;
d = opendir("/proc");
if (d == 0)
return -1;
while ((de = readdir(d)) != 0) {
if (!isdigit(de->d_name[0]))
continue;
int pid = atoi(de->d_name);
if (isProcessWriting(pid, device)) {
if (pid == getpid()) {
ALOGE("We're still writing!");
} else {
char cmdline[1024];
int fd, r = 0;
sprintf(cmdline, "/proc/%d/cmdline", pid);
fd = open(cmdline, O_RDONLY);
if (fd != 0) {
r = read(fd, cmdline, 1023);
close(fd);
}
cmdline[r > 0 ? r : 0] = '\0';
ALOGW("Killing %d %s", pid, cmdline);
kill(pid, SIGKILL);
}
}
}
closedir(d);
return -1;
}
bool remountReadOnly() {
ALOGV("Flushing filesystem changes");
sync();
ALOGV("Mounting system partition in read-only mode");
if (mount(NULL, "/system", NULL, MS_REMOUNT | MS_RDONLY, 0)) {
bool busy = (errno == EBUSY);
ALOGW("Error mounting read-only /system filesystem. error: %s", strerror(errno));
if (busy) {
killWritingProcesses("/system/");
if (mount(NULL, "/system", NULL, MS_REMOUNT | MS_RDONLY, 0)) {
ALOGE("Fatal error mounting read-only /system filesystem. error: %s", strerror(errno));
return false;
}
}
}
return true;
}
int installAudioHAL(const char *baseDir) {
ALOGV("Installing SCR audio HAL");
char modulePath [1024];
char policyPath [1024];
char mediaProfilesPath [1024];
char uninstallPath [1024];
sprintf(modulePath, "%.950s/audio.scr_primary.default.so", baseDir);
sprintf(policyPath, "%.950s/scr_audio_policy.conf", baseDir);
sprintf(mediaProfilesPath, "%.950s/scr_media_profiles.xml", baseDir);
sprintf(uninstallPath, "%.950s/deinstaller.sh", baseDir);
ALOGV("Mounting system partition in read-write mode");
if (mount(NULL, "/system", NULL, MS_REMOUNT | MS_NOATIME, 0)) {
ALOGE("Error mounting /system filesystem. error: %s", strerror(errno));
return 167;
}
ALOGV("Copying uninstall script");
if (!copyFile(uninstallPath, "/system/lib/hw/uninstall_scr.sh")){
remountReadOnly();
return 174;
}
chmod("/system/lib/hw/uninstall_scr.sh", 0655);
if (!symlinkRwFiles(baseDir)) {
remountReadOnly();
return 173;
}
if (!moveOriginalModules()) {
restoreOriginalModules();
remountReadOnly();
return 168;
}
ALOGV("Copying SCR audio driver");
if (!copyFile(modulePath, "/system/lib/hw/audio.primary.default.so")){
restoreOriginalModules();
remountReadOnly();
return 169;
}
if (fileExists(policyPath)) {
ALOGV("Installing audio policy file");
backupConfigFile("/system/etc/audio_policy.conf", "/system/etc/audio_policy.conf.back");
backupConfigFile("/vendor/etc/audio_policy.conf", "/vendor/etc/audio_policy.conf.back");
if (!copyFile(policyPath, "/system/etc/audio_policy.conf")) {
restoreConfigFile("/system/etc/audio_policy.conf.back", "/system/etc/audio_policy.conf");
restoreConfigFile("/vendor/etc/audio_policy.conf.back", "/vendor/etc/audio_policy.conf");
remountReadOnly();
return 170;
}
}
if (fileExists(mediaProfilesPath)) {
ALOGV("Installing media profiles file");
backupConfigFile("/system/etc/media_profiles.xml", "/system/etc/media_profiles.xml.back");
if (!copyFile(mediaProfilesPath, "/system/etc/media_profiles.xml")) {
restoreConfigFile("/system/etc/audio_policy.conf.back", "/system/etc/audio_policy.conf");
restoreConfigFile("/vendor/etc/audio_policy.conf.back", "/vendor/etc/audio_policy.conf");
restoreConfigFile("/system/etc/media_profiles.xml.back", "/system/etc/media_profiles.xml");
remountReadOnly();
return 177;
}
}
bool readOnly = remountReadOnly();
stopMediaServer();
int pid = waitForMediaServerPid();
if (pid == -1) {
ALOGE("ms process not showing up!");
uninstallAudioHAL();
return 171;
}
ALOGV("Waiting 3s to see if %d won't die", pid);
if (waitForProcessStop(pid, 250000, 3000000)) {
ALOGE("ms process died!");
uninstallAudioHAL();
return 172;
}
ALOGV("Installed!");
return readOnly ? 0 : 202;
}
int uninstallAudioHAL() {
ALOGV("Uninstalling SCR audio HAL");
ALOGV("Mounting system partition in read-write mode");
if (mount(NULL, "/system", NULL, MS_REMOUNT | MS_NOATIME, 0)) {
ALOGE("Error mounting /system filesystem");
return 167;
}
restoreOriginalModules();
restoreConfigFile("/system/etc/audio_policy.conf.back", "/system/etc/audio_policy.conf");
restoreConfigFile("/vendor/etc/audio_policy.conf.back", "/vendor/etc/audio_policy.conf");
restoreConfigFile("/system/etc/media_profiles.xml.back", "/system/etc/media_profiles.xml");
removeFile("/system/lib/hw/scr_audio.log");
removeFile("/system/lib/hw/scr_audio.conf");
removeFile("/system/lib/hw/uninstall_scr.sh");
stopMediaServer();
bool readOnly = remountReadOnly();
ALOGV("Uninstalled!");
return readOnly ? 0 : 202;
}
int mountAudioHAL(const char *baseDir) {
ALOGV("Soft-Installing SCR audio HAL");
char vendorPolicyPath [1024];
char systemPolicyPath [1024];
char mediaProfilesPath [1024];
sprintf(vendorPolicyPath, "%.950s/vendor_audio_policy.conf", baseDir);
sprintf(systemPolicyPath, "%.950s/system_audio_policy.conf", baseDir);
sprintf(mediaProfilesPath, "%.950s/media_profiles.xml", baseDir);
ALOGV("Linking modules dir");
if (mount(baseDir, "/system/lib/hw", NULL, MS_BIND, 0)) {
ALOGE("Error linking modules directory. error: %s", strerror(errno));
return 174;
}
if (fileExists("/vendor/etc/audio_policy.conf") && fileExists(vendorPolicyPath)) {
ALOGV("Linking vendor audio policy file");
if (mount(vendorPolicyPath, "/vendor/etc/audio_policy.conf", NULL, MS_BIND, 0)) {
ALOGE("Error linking policy file. error: %s", strerror(errno));
return 175;
}
}
if (fileExists("/system/etc/audio_policy.conf") && fileExists(systemPolicyPath)) {
ALOGV("Linking system audio policy file");
if (mount(systemPolicyPath, "/system/etc/audio_policy.conf", NULL, MS_BIND, 0)) {
ALOGE("Error linking policy file. error: %s", strerror(errno));
return 176;
}
}
if (fileExists("/system/etc/media_profiles.xml") && fileExists(mediaProfilesPath)) {
ALOGV("Linking media profiles file");
if (mount(mediaProfilesPath, "/system/etc/media_profiles.xml", NULL, MS_BIND, 0)) {
ALOGE("Error linking media profiles file. error: %s", strerror(errno));
return 178;
}
}
ALOGV("Soft-Installed!");
return 0;
}
void forceUnmount(const char *path) {
if (umount2(path, MNT_DETACH)) {
ALOGE("Can't uninstall %s error: %s", path, strerror(errno));
}
}
int unmountAudioHAL() {
forceUnmount("/system/lib/hw");
forceUnmount("/system/etc/audio_policy.conf");
if (fileExists("/vendor/etc/audio_policy.conf")) {
forceUnmount("/vendor/etc/audio_policy.conf");
}
forceUnmount("/system/etc/media_profiles.xml");
return 0;
}
bool crashUnmount(const char *path) {
if (umount2(path, MNT_DETACH) == 0) {
ALOGV("Uninstalled: %s", path);
return true;
}
return false;
}
void forkUmountProcess(const char* executablePath) {
ALOGV("Emergency uninstall");
char umountCommand[1024];
sprintf(umountCommand, "%s umount", executablePath);
pid_t pid = fork();
int status = -1;
if (pid == -1) {
ALOGW("Error forking process: %s ", strerror(errno));
} else if (pid == 0) {
if (execlp("su", "su", "--mount-master", "-c", umountCommand, NULL) == -1) {
ALOGW("Error executing command: %s ", strerror(errno));
}
} else {
waitpid(pid, &status, 0);
ALOGV("Uninstall process returned %d", status);
}
}
int crashUnmountAudioHAL(const char* executablePath) {
bool uninstalled = false;
uninstalled |= crashUnmount("/system/lib/hw");
uninstalled |= crashUnmount("/system/etc/audio_policy.conf");
if (fileExists("/vendor/etc/audio_policy.conf")) {
uninstalled |= crashUnmount("/vendor/etc/audio_policy.conf");
}
if (uninstalled && executablePath != NULL) {
forkUmountProcess(executablePath);
}
return 0;
}