main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017-2019 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <getopt.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <ctype.h>
  18. #include <signal.h>
  19. #include <sys/prctl.h>
  20. #include <sys/stat.h>
  21. #include <ell/ell.h>
  22. #include "lib/bluetooth.h"
  23. #include "lib/mgmt.h"
  24. #include "mesh/mesh.h"
  25. #include "mesh/crypto.h"
  26. #include "mesh/dbus.h"
  27. #include "mesh/mesh-io.h"
  28. #include "mesh/util.h"
  29. static const char *storage_dir;
  30. static const char *mesh_conf_fname;
  31. static enum mesh_io_type io_type;
  32. static void *io_opts;
  33. static const struct option main_options[] = {
  34. { "io", required_argument, NULL, 'i' },
  35. { "storage", required_argument, NULL, 's' },
  36. { "config", required_argument, NULL, 'c' },
  37. { "nodetach", no_argument, NULL, 'n' },
  38. { "debug", no_argument, NULL, 'd' },
  39. { "dbus-debug", no_argument, NULL, 'b' },
  40. { "help", no_argument, NULL, 'h' },
  41. { }
  42. };
  43. static void usage(void)
  44. {
  45. fprintf(stderr,
  46. "Usage:\n"
  47. "\tbluetooth-meshd [options]\n");
  48. fprintf(stderr,
  49. "Options:\n"
  50. "\t--io <io> Use specified io (default: generic)\n"
  51. "\t--config Daemon configuration directory\n"
  52. "\t--storage Mesh node(s) configuration directory\n"
  53. "\t--nodetach Run in foreground\n"
  54. "\t--debug Enable debug output\n"
  55. "\t--dbus-debug Enable D-Bus debugging\n"
  56. "\t--help Show %s information\n", __func__);
  57. fprintf(stderr,
  58. "io:\n"
  59. "\t([hci]<index> | generic[:[hci]<index>] | unit:<fd_path>)\n"
  60. "\t\tUse generic HCI io on interface hci<index>, or the first\n"
  61. "\t\tavailable one\n");
  62. }
  63. static void do_debug(const char *str, void *user_data)
  64. {
  65. const char *prefix = user_data;
  66. l_info("%s%s", prefix, str);
  67. }
  68. static void mesh_ready_callback(void *user_data, bool success)
  69. {
  70. struct l_dbus *dbus = user_data;
  71. l_info("mesh_ready_callback");
  72. if (!success) {
  73. l_error("Failed to start mesh");
  74. l_main_quit();
  75. return;
  76. }
  77. if (!dbus_init(dbus)) {
  78. l_error("Failed to initialize mesh D-Bus resources");
  79. l_main_quit();
  80. }
  81. }
  82. static void request_name_callback(struct l_dbus *dbus, bool success,
  83. bool queued, void *user_data)
  84. {
  85. if (!success && io_type != MESH_IO_TYPE_UNIT_TEST) {
  86. l_info("Request name failed");
  87. l_main_quit();
  88. return;
  89. }
  90. if (!mesh_init(storage_dir, mesh_conf_fname, io_type, io_opts,
  91. mesh_ready_callback, dbus)) {
  92. l_error("Failed to initialize mesh");
  93. l_main_quit();
  94. }
  95. }
  96. static void ready_callback(void *user_data)
  97. {
  98. struct l_dbus *dbus = user_data;
  99. l_info("D-Bus ready");
  100. l_dbus_name_acquire(dbus, BLUEZ_MESH_NAME, false, false, false,
  101. request_name_callback, NULL);
  102. }
  103. static void disconnect_callback(void *user_data)
  104. {
  105. l_main_quit();
  106. }
  107. static void signal_handler(uint32_t signo, void *user_data)
  108. {
  109. static bool terminated;
  110. if (terminated)
  111. return;
  112. l_info("Terminating");
  113. l_main_quit();
  114. terminated = true;
  115. }
  116. static bool parse_io(const char *optarg, enum mesh_io_type *type, void **opts)
  117. {
  118. if (strstr(optarg, "generic") == optarg) {
  119. int *index = l_new(int, 1);
  120. *type = MESH_IO_TYPE_GENERIC;
  121. *opts = index;
  122. optarg += strlen("generic");
  123. if (!*optarg) {
  124. *index = MGMT_INDEX_NONE;
  125. return true;
  126. }
  127. if (*optarg != ':')
  128. return false;
  129. optarg++;
  130. if (sscanf(optarg, "hci%d", index) == 1)
  131. return true;
  132. if (sscanf(optarg, "%d", index) == 1)
  133. return true;
  134. return false;
  135. } else if (strstr(optarg, "unit") == optarg) {
  136. char *test_path;
  137. *type = MESH_IO_TYPE_UNIT_TEST;
  138. optarg += strlen("unit");
  139. if (*optarg != ':')
  140. return false;
  141. optarg++;
  142. test_path = strdup(optarg);
  143. *opts = test_path;
  144. return true;
  145. }
  146. return false;
  147. }
  148. int main(int argc, char *argv[])
  149. {
  150. int status;
  151. bool detached = true;
  152. bool dbus_debug = false;
  153. struct l_dbus *dbus = NULL;
  154. char *io = NULL;
  155. int hci_index;
  156. if (!l_main_init())
  157. return -1;
  158. l_log_set_stderr();
  159. if (!mesh_crypto_check_avail()) {
  160. l_error("Mesh Crypto functions unavailable");
  161. status = l_main_run_with_signal(signal_handler, NULL);
  162. goto done;
  163. }
  164. for (;;) {
  165. int opt;
  166. opt = getopt_long(argc, argv, "u:i:s:c:ndbh", main_options,
  167. NULL);
  168. if (opt < 0)
  169. break;
  170. switch (opt) {
  171. case 'u':
  172. if (sscanf(optarg, "%d", &hci_index) == 1 ||
  173. sscanf(optarg, "%d", &hci_index) == 1)
  174. io = l_strdup_printf("unit:%d", hci_index);
  175. else
  176. io = l_strdup(optarg);
  177. break;
  178. case 'i':
  179. if (sscanf(optarg, "hci%d", &hci_index) == 1 ||
  180. sscanf(optarg, "%d", &hci_index) == 1)
  181. io = l_strdup_printf("generic:%s", optarg);
  182. else
  183. io = l_strdup(optarg);
  184. break;
  185. case 'n':
  186. detached = false;
  187. break;
  188. case 'd':
  189. enable_debug();
  190. break;
  191. case 's':
  192. storage_dir = optarg;
  193. break;
  194. case 'c':
  195. mesh_conf_fname = optarg;
  196. break;
  197. case 'b':
  198. dbus_debug = true;
  199. break;
  200. case 'h':
  201. usage();
  202. status = EXIT_SUCCESS;
  203. goto done;
  204. default:
  205. usage();
  206. status = EXIT_FAILURE;
  207. goto done;
  208. }
  209. }
  210. if (!io)
  211. io = l_strdup_printf("generic");
  212. if (!parse_io(io, &io_type, &io_opts)) {
  213. l_error("Invalid io: %s", io);
  214. status = EXIT_FAILURE;
  215. goto done;
  216. }
  217. l_free(io);
  218. io = NULL;
  219. if (!detached)
  220. umask(0077);
  221. if (io_type != MESH_IO_TYPE_UNIT_TEST)
  222. dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
  223. else {
  224. dbus = l_dbus_new_default(L_DBUS_SESSION_BUS);
  225. prctl(PR_SET_PDEATHSIG, SIGSEGV);
  226. }
  227. if (!dbus) {
  228. l_error("unable to connect to D-Bus");
  229. status = EXIT_FAILURE;
  230. goto done;
  231. }
  232. if (dbus_debug)
  233. l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL);
  234. l_dbus_set_ready_handler(dbus, ready_callback, dbus, NULL);
  235. l_dbus_set_disconnect_handler(dbus, disconnect_callback, NULL, NULL);
  236. if (!l_dbus_object_manager_enable(dbus, "/")) {
  237. l_error("Failed to enable Object Manager");
  238. status = EXIT_FAILURE;
  239. goto done;
  240. }
  241. status = l_main_run_with_signal(signal_handler, NULL);
  242. done:
  243. l_free(io);
  244. l_free(io_opts);
  245. mesh_cleanup();
  246. l_dbus_destroy(dbus);
  247. l_main_exit();
  248. return status;
  249. }