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