bluetoothd-wrapper.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdbool.h>
  11. #include <cutils/properties.h>
  12. #include "hal-utils.h"
  13. #define VALGRIND_BIN "/system/bin/valgrind"
  14. #define BLUETOOTHD_BIN "/system/bin/bluetoothd-main"
  15. static void run_valgrind(int debug, int mgmt_dbg)
  16. {
  17. char *prg_argv[7];
  18. char *prg_envp[3];
  19. prg_argv[0] = VALGRIND_BIN;
  20. prg_argv[1] = "--leak-check=full";
  21. prg_argv[2] = "--track-origins=yes";
  22. prg_argv[3] = BLUETOOTHD_BIN;
  23. prg_argv[4] = debug ? "-d" : NULL;
  24. prg_argv[5] = mgmt_dbg ? "--mgmt-debug" : NULL;
  25. prg_argv[6] = NULL;
  26. prg_envp[0] = "G_SLICE=always-malloc";
  27. prg_envp[1] = "G_DEBUG=gc-friendly";
  28. prg_envp[2] = NULL;
  29. execve(prg_argv[0], prg_argv, prg_envp);
  30. }
  31. static void run_bluetoothd(int debug, int mgmt_dbg)
  32. {
  33. char *prg_argv[4];
  34. char *prg_envp[1];
  35. prg_argv[0] = BLUETOOTHD_BIN;
  36. prg_argv[1] = debug ? "-d" : NULL;
  37. prg_argv[2] = mgmt_dbg ? "--mgmt-debug" : NULL;
  38. prg_argv[3] = NULL;
  39. prg_envp[0] = NULL;
  40. execve(prg_argv[0], prg_argv, prg_envp);
  41. }
  42. int main(int argc, char *argv[])
  43. {
  44. char value[PROPERTY_VALUE_MAX];
  45. int debug = 0;
  46. int mgmt_dbg = 0;
  47. if (get_config("debug", value, NULL) > 0 &&
  48. (!strcasecmp(value, "true") || atoi(value) > 0))
  49. debug = 1;
  50. if (get_config("mgmtdbg", value, NULL) > 0 &&
  51. (!strcasecmp(value, "true") || atoi(value) > 0)) {
  52. debug = 1;
  53. mgmt_dbg = 1;
  54. }
  55. if (get_config("valgrind", value, NULL) > 0 &&
  56. (!strcasecmp(value, "true") || atoi(value) > 0))
  57. run_valgrind(debug, mgmt_dbg);
  58. /*
  59. * In case we failed to execute Valgrind, try to run bluetoothd
  60. * without it
  61. */
  62. run_bluetoothd(debug, mgmt_dbg);
  63. return 0;
  64. }