btmon-logger.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017-2018 Codecoup
  7. * Copyright (C) 2011-2014 Intel Corporation
  8. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  9. *
  10. *
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #define _GNU_SOURCE
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <limits.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <getopt.h>
  22. #include <unistd.h>
  23. #include <sys/socket.h>
  24. #include <sys/stat.h>
  25. #include <libgen.h>
  26. #include <errno.h>
  27. #include <linux/capability.h>
  28. #include "lib/bluetooth.h"
  29. #include "lib/hci.h"
  30. #include "src/shared/util.h"
  31. #include "src/shared/mainloop.h"
  32. #include "src/shared/btsnoop.h"
  33. #define MONITOR_INDEX_NONE 0xffff
  34. struct monitor_hdr {
  35. uint16_t opcode;
  36. uint16_t index;
  37. uint16_t len;
  38. } __attribute__ ((packed));
  39. static struct btsnoop *btsnoop_file = NULL;
  40. static void data_callback(int fd, uint32_t events, void *user_data)
  41. {
  42. uint8_t buf[BTSNOOP_MAX_PACKET_SIZE];
  43. unsigned char control[64];
  44. struct monitor_hdr hdr;
  45. struct msghdr msg;
  46. struct iovec iov[2];
  47. if (events & (EPOLLERR | EPOLLHUP)) {
  48. mainloop_exit_failure();
  49. return;
  50. }
  51. iov[0].iov_base = &hdr;
  52. iov[0].iov_len = sizeof(hdr);
  53. iov[1].iov_base = buf;
  54. iov[1].iov_len = sizeof(buf);
  55. memset(&msg, 0, sizeof(msg));
  56. msg.msg_iov = iov;
  57. msg.msg_iovlen = 2;
  58. msg.msg_control = control;
  59. msg.msg_controllen = sizeof(control);
  60. while (1) {
  61. struct cmsghdr *cmsg;
  62. struct timeval *tv = NULL;
  63. struct timeval ctv;
  64. uint16_t opcode, index, pktlen;
  65. ssize_t len;
  66. len = recvmsg(fd, &msg, MSG_DONTWAIT);
  67. if (len < 0)
  68. break;
  69. if (len < (ssize_t) sizeof(hdr))
  70. break;
  71. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
  72. cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  73. if (cmsg->cmsg_level != SOL_SOCKET)
  74. continue;
  75. if (cmsg->cmsg_type == SCM_TIMESTAMP) {
  76. memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
  77. tv = &ctv;
  78. }
  79. }
  80. opcode = le16_to_cpu(hdr.opcode);
  81. index = le16_to_cpu(hdr.index);
  82. pktlen = le16_to_cpu(hdr.len);
  83. btsnoop_write_hci(btsnoop_file, tv, index, opcode, 0, buf,
  84. pktlen);
  85. }
  86. }
  87. static bool open_monitor_channel(void)
  88. {
  89. struct sockaddr_hci addr;
  90. int fd, opt = 1;
  91. fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
  92. if (fd < 0) {
  93. perror("Failed to open monitor channel");
  94. return false;
  95. }
  96. memset(&addr, 0, sizeof(addr));
  97. addr.hci_family = AF_BLUETOOTH;
  98. addr.hci_dev = HCI_DEV_NONE;
  99. addr.hci_channel = HCI_CHANNEL_MONITOR;
  100. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  101. perror("Failed to bind monitor channel");
  102. close(fd);
  103. return false;
  104. }
  105. if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
  106. perror("Failed to enable timestamps");
  107. close(fd);
  108. return false;
  109. }
  110. if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt)) < 0) {
  111. perror("Failed to enable credentials");
  112. close(fd);
  113. return false;
  114. }
  115. mainloop_add_fd(fd, EPOLLIN, data_callback, NULL, NULL);
  116. return true;
  117. }
  118. static void signal_callback(int signum, void *user_data)
  119. {
  120. switch (signum) {
  121. case SIGINT:
  122. case SIGTERM:
  123. mainloop_quit();
  124. break;
  125. }
  126. }
  127. extern int capget(struct __user_cap_header_struct *header,
  128. struct __user_cap_data_struct *data);
  129. extern int capset(struct __user_cap_header_struct *header,
  130. const struct __user_cap_data_struct *data);
  131. static void drop_capabilities(void)
  132. {
  133. struct __user_cap_header_struct header;
  134. struct __user_cap_data_struct cap;
  135. unsigned int mask;
  136. int err;
  137. header.version = _LINUX_CAPABILITY_VERSION_3;
  138. header.pid = 0;
  139. err = capget(&header, &cap);
  140. if (err) {
  141. perror("Unable to get current capabilities");
  142. return;
  143. }
  144. /* not needed anymore since monitor socket is already open */
  145. mask = ~CAP_TO_MASK(CAP_NET_RAW);
  146. cap.effective &= mask;
  147. cap.permitted &= mask;
  148. cap.inheritable &= mask;
  149. err = capset(&header, &cap);
  150. if (err)
  151. perror("Failed to set capabilities");
  152. }
  153. static void usage(void)
  154. {
  155. printf("btmon-logger - Bluetooth monitor\n"
  156. "Usage:\n");
  157. printf("\tbtmon-logger [options]\n");
  158. printf("options:\n"
  159. "\t-b, --basename <path> Save traces in specified path\n"
  160. "\t-p, --parents Create basename parent directories\n"
  161. "\t-l, --limit <limit> Limit traces file size (rotate)\n"
  162. "\t-c, --count <count> Limit number of rotated files\n"
  163. "\t-v, --version Show version\n"
  164. "\t-h, --help Show help options\n");
  165. }
  166. static const struct option main_options[] = {
  167. { "basename", required_argument, NULL, 'b' },
  168. { "parents", no_argument, NULL, 'p' },
  169. { "limit", required_argument, NULL, 'l' },
  170. { "count", required_argument, NULL, 'c' },
  171. { "version", no_argument, NULL, 'v' },
  172. { "help", no_argument, NULL, 'h' },
  173. { }
  174. };
  175. static int create_dir(const char *filename)
  176. {
  177. char *dirc;
  178. char *dir;
  179. char *p;
  180. int err = 0;
  181. /* get base directory */
  182. dirc = strdup(filename);
  183. dir = dirname(dirc);
  184. p = dir;
  185. /* preserve leading / if present */
  186. if (*p == '/')
  187. p++;
  188. /* create any intermediate directories */
  189. p = strchrnul(p, '/');
  190. while (*p) {
  191. /* cut directory path */
  192. *p = '\0';
  193. if (mkdir(dir, 0700) < 0 && errno != EEXIST) {
  194. err = errno;
  195. goto done;
  196. }
  197. /* restore directory path */
  198. *p = '/';
  199. p = strchrnul(p + 1, '/');
  200. }
  201. /* create leaf directory */
  202. if (mkdir(dir, 0700) < 0 && errno != EEXIST)
  203. err = errno;
  204. done:
  205. free(dirc);
  206. if (err)
  207. printf("Failed to create parent directories for %s\n",
  208. filename);
  209. return err;
  210. }
  211. int main(int argc, char *argv[])
  212. {
  213. const char *path = "hci.log";
  214. unsigned long max_count = 0;
  215. size_t size_limit = 0;
  216. bool parents = false;
  217. int exit_status;
  218. char *endptr;
  219. mainloop_init();
  220. mainloop_sd_notify("STATUS=Starting up");
  221. while (true) {
  222. int opt;
  223. opt = getopt_long(argc, argv, "b:l:c:vhp", main_options,
  224. NULL);
  225. if (opt < 0)
  226. break;
  227. switch (opt) {
  228. case 'b':
  229. path = optarg;
  230. if (strlen(path) > PATH_MAX) {
  231. fprintf(stderr, "Too long path\n");
  232. return EXIT_FAILURE;
  233. }
  234. break;
  235. case 'l':
  236. size_limit = strtoul(optarg, &endptr, 10);
  237. if (size_limit == ULONG_MAX) {
  238. fprintf(stderr, "Invalid limit\n");
  239. return EXIT_FAILURE;
  240. }
  241. if (*endptr != '\0') {
  242. if (*endptr == 'K' || *endptr == 'k') {
  243. size_limit *= 1024;
  244. } else if (*endptr == 'M' || *endptr == 'm') {
  245. size_limit *= 1024 * 1024;
  246. } else {
  247. fprintf(stderr, "Invalid limit\n");
  248. return EXIT_FAILURE;
  249. }
  250. }
  251. /* limit this to reasonable size */
  252. if (size_limit < 4096) {
  253. fprintf(stderr, "Too small limit value\n");
  254. return EXIT_FAILURE;
  255. }
  256. break;
  257. case 'c':
  258. max_count = strtoul(optarg, &endptr, 10);
  259. break;
  260. case 'p':
  261. if (getppid() != 1) {
  262. fprintf(stderr, "Parents option allowed only "
  263. "when running as a service\n");
  264. return EXIT_FAILURE;
  265. }
  266. parents = true;
  267. break;
  268. case 'v':
  269. printf("%s\n", VERSION);
  270. return EXIT_SUCCESS;
  271. case 'h':
  272. usage();
  273. return EXIT_SUCCESS;
  274. default:
  275. return EXIT_FAILURE;
  276. }
  277. }
  278. if (argc - optind > 0) {
  279. fprintf(stderr, "Invalid command line parameters\n");
  280. return EXIT_FAILURE;
  281. }
  282. if (!open_monitor_channel())
  283. return EXIT_FAILURE;
  284. if (parents && create_dir(path) < 0)
  285. return EXIT_FAILURE;
  286. btsnoop_file = btsnoop_create(path, size_limit, max_count,
  287. BTSNOOP_FORMAT_MONITOR);
  288. if (!btsnoop_file)
  289. return EXIT_FAILURE;
  290. drop_capabilities();
  291. printf("Bluetooth monitor logger ver %s\n", VERSION);
  292. mainloop_sd_notify("STATUS=Running");
  293. mainloop_sd_notify("READY=1");
  294. exit_status = mainloop_run_with_signal(signal_callback, NULL);
  295. mainloop_sd_notify("STATUS=Quitting");
  296. btsnoop_unref(btsnoop_file);
  297. return exit_status;
  298. }