1*7c3d14c8STreehugger Robot /*
2*7c3d14c8STreehugger Robot * Copyright (C) 2017 The Android Open Source Project
3*7c3d14c8STreehugger Robot * All rights reserved.
4*7c3d14c8STreehugger Robot *
5*7c3d14c8STreehugger Robot * Redistribution and use in source and binary forms, with or without
6*7c3d14c8STreehugger Robot * modification, are permitted provided that the following conditions
7*7c3d14c8STreehugger Robot * are met:
8*7c3d14c8STreehugger Robot * * Redistributions of source code must retain the above copyright
9*7c3d14c8STreehugger Robot * notice, this list of conditions and the following disclaimer.
10*7c3d14c8STreehugger Robot * * Redistributions in binary form must reproduce the above copyright
11*7c3d14c8STreehugger Robot * notice, this list of conditions and the following disclaimer in
12*7c3d14c8STreehugger Robot * the documentation and/or other materials provided with the
13*7c3d14c8STreehugger Robot * distribution.
14*7c3d14c8STreehugger Robot *
15*7c3d14c8STreehugger Robot * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*7c3d14c8STreehugger Robot * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*7c3d14c8STreehugger Robot * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*7c3d14c8STreehugger Robot * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*7c3d14c8STreehugger Robot * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*7c3d14c8STreehugger Robot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*7c3d14c8STreehugger Robot * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*7c3d14c8STreehugger Robot * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*7c3d14c8STreehugger Robot * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*7c3d14c8STreehugger Robot * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*7c3d14c8STreehugger Robot * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*7c3d14c8STreehugger Robot * SUCH DAMAGE.
27*7c3d14c8STreehugger Robot */
28*7c3d14c8STreehugger Robot
29*7c3d14c8STreehugger Robot #include <elf.h>
30*7c3d14c8STreehugger Robot #include <fcntl.h>
31*7c3d14c8STreehugger Robot #include <stdio.h>
32*7c3d14c8STreehugger Robot #include <stdlib.h>
33*7c3d14c8STreehugger Robot #include <sys/stat.h>
34*7c3d14c8STreehugger Robot #include <sys/types.h>
35*7c3d14c8STreehugger Robot #include <unistd.h>
36*7c3d14c8STreehugger Robot
37*7c3d14c8STreehugger Robot #include <string>
38*7c3d14c8STreehugger Robot
usage(const char * me)39*7c3d14c8STreehugger Robot void usage(const char* me) {
40*7c3d14c8STreehugger Robot static const char* usage_s = "Usage:\n"
41*7c3d14c8STreehugger Robot " %s /system/bin/app_process <args>\n"
42*7c3d14c8STreehugger Robot "or, better:\n"
43*7c3d14c8STreehugger Robot " setprop wrap.<nicename> %s\n";
44*7c3d14c8STreehugger Robot fprintf(stderr, usage_s, me, me);
45*7c3d14c8STreehugger Robot exit(1);
46*7c3d14c8STreehugger Robot }
47*7c3d14c8STreehugger Robot
main(int argc,char ** argv)48*7c3d14c8STreehugger Robot int main(int argc, char** argv) {
49*7c3d14c8STreehugger Robot if (argc < 2) {
50*7c3d14c8STreehugger Robot usage(argv[0]);
51*7c3d14c8STreehugger Robot }
52*7c3d14c8STreehugger Robot char** args = new char*[argc];
53*7c3d14c8STreehugger Robot // If we are wrapping app_process, replace it with app_process_asan.
54*7c3d14c8STreehugger Robot // TODO(eugenis): rewrite to <dirname>/asan/<basename>, if exists?
55*7c3d14c8STreehugger Robot if (strcmp(argv[1], "/system/bin/app_process") == 0) {
56*7c3d14c8STreehugger Robot args[0] = (char*)"/system/bin/asan/app_process";
57*7c3d14c8STreehugger Robot } else if (strcmp(argv[1], "/system/bin/app_process32") == 0) {
58*7c3d14c8STreehugger Robot args[0] = (char*)"/system/bin/asan/app_process32";
59*7c3d14c8STreehugger Robot } else if (strcmp(argv[1], "/system/bin/app_process64") == 0) {
60*7c3d14c8STreehugger Robot args[0] = (char*)"/system/bin/asan/app_process64";
61*7c3d14c8STreehugger Robot } else {
62*7c3d14c8STreehugger Robot args[0] = argv[1];
63*7c3d14c8STreehugger Robot }
64*7c3d14c8STreehugger Robot
65*7c3d14c8STreehugger Robot for (int i = 1; i < argc - 1; ++i)
66*7c3d14c8STreehugger Robot args[i] = argv[i + 1];
67*7c3d14c8STreehugger Robot args[argc - 1] = 0;
68*7c3d14c8STreehugger Robot
69*7c3d14c8STreehugger Robot for (int i = 0; i < argc - 1; ++i)
70*7c3d14c8STreehugger Robot printf("argv[%d] = %s\n", i, args[i]);
71*7c3d14c8STreehugger Robot
72*7c3d14c8STreehugger Robot execv(args[0], args);
73*7c3d14c8STreehugger Robot }
74