test-runner.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012-2014 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 <stdio.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <signal.h>
  21. #include <string.h>
  22. #include <getopt.h>
  23. #include <poll.h>
  24. #include <sys/wait.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/mount.h>
  28. #include <sys/param.h>
  29. #include <sys/reboot.h>
  30. #include "lib/bluetooth.h"
  31. #include "lib/hci.h"
  32. #include "lib/hci_lib.h"
  33. #include "tools/hciattach.h"
  34. #ifndef WAIT_ANY
  35. #define WAIT_ANY (-1)
  36. #endif
  37. #define CMDLINE_MAX (2048 * 10)
  38. static const char *own_binary;
  39. static char **test_argv;
  40. static int test_argc;
  41. static bool run_auto = false;
  42. static bool start_dbus = false;
  43. static bool start_monitor = false;
  44. static int num_devs = 0;
  45. static const char *qemu_binary = NULL;
  46. static const char *kernel_image = NULL;
  47. static const char *qemu_table[] = {
  48. "qemu-system-x86_64",
  49. "qemu-system-i386",
  50. "/usr/bin/qemu-system-x86_64",
  51. "/usr/bin/qemu-system-i386",
  52. NULL
  53. };
  54. static const char *find_qemu(void)
  55. {
  56. int i;
  57. for (i = 0; qemu_table[i]; i++) {
  58. struct stat st;
  59. if (!stat(qemu_table[i], &st))
  60. return qemu_table[i];
  61. }
  62. return NULL;
  63. }
  64. static const char *kernel_table[] = {
  65. "bzImage",
  66. "arch/x86/boot/bzImage",
  67. "vmlinux",
  68. "arch/x86/boot/vmlinux",
  69. NULL
  70. };
  71. static const char *find_kernel(void)
  72. {
  73. int i;
  74. for (i = 0; kernel_table[i]; i++) {
  75. struct stat st;
  76. if (!stat(kernel_table[i], &st))
  77. return kernel_table[i];
  78. }
  79. return NULL;
  80. }
  81. static const struct {
  82. const char *target;
  83. const char *linkpath;
  84. } dev_table[] = {
  85. { "/proc/self/fd", "/dev/fd" },
  86. { "/proc/self/fd/0", "/dev/stdin" },
  87. { "/proc/self/fd/1", "/dev/stdout" },
  88. { "/proc/self/fd/2", "/dev/stderr" },
  89. { }
  90. };
  91. static const struct {
  92. const char *fstype;
  93. const char *target;
  94. const char *options;
  95. unsigned long flags;
  96. } mount_table[] = {
  97. { "sysfs", "/sys", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
  98. { "proc", "/proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
  99. { "devtmpfs", "/dev", "mode=0755", MS_NOSUID|MS_STRICTATIME },
  100. { "devpts", "/dev/pts", "mode=0620", MS_NOSUID|MS_NOEXEC },
  101. { "tmpfs", "/dev/shm", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME },
  102. { "tmpfs", "/run", "mode=0755", MS_NOSUID|MS_NODEV|MS_STRICTATIME },
  103. { "tmpfs", "/tmp", NULL, 0 },
  104. { "debugfs", "/sys/kernel/debug", NULL, 0 },
  105. { }
  106. };
  107. static const char *config_table[] = {
  108. "/var/lib/bluetooth",
  109. "/etc/bluetooth",
  110. "/etc/dbus-1",
  111. "/usr/share/dbus-1",
  112. NULL
  113. };
  114. static void prepare_sandbox(void)
  115. {
  116. int i;
  117. for (i = 0; mount_table[i].fstype; i++) {
  118. struct stat st;
  119. if (lstat(mount_table[i].target, &st) < 0) {
  120. printf("Creating %s\n", mount_table[i].target);
  121. mkdir(mount_table[i].target, 0755);
  122. }
  123. printf("Mounting %s to %s\n", mount_table[i].fstype,
  124. mount_table[i].target);
  125. if (mount(mount_table[i].fstype,
  126. mount_table[i].target,
  127. mount_table[i].fstype,
  128. mount_table[i].flags,
  129. mount_table[i].options) < 0)
  130. perror("Failed to mount filesystem");
  131. }
  132. for (i = 0; dev_table[i].target; i++) {
  133. printf("Linking %s to %s\n", dev_table[i].linkpath,
  134. dev_table[i].target);
  135. if (symlink(dev_table[i].target, dev_table[i].linkpath) < 0)
  136. perror("Failed to create device symlink");
  137. }
  138. printf("Creating new session group leader\n");
  139. setsid();
  140. printf("Setting controlling terminal\n");
  141. ioctl(STDIN_FILENO, TIOCSCTTY, 1);
  142. for (i = 0; config_table[i]; i++) {
  143. printf("Creating %s\n", config_table[i]);
  144. if (mount("tmpfs", config_table[i], "tmpfs",
  145. MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
  146. "mode=0755") < 0)
  147. perror("Failed to create filesystem");
  148. }
  149. }
  150. static char *const qemu_argv[] = {
  151. "",
  152. "-nodefaults",
  153. "-no-user-config",
  154. "-monitor", "none",
  155. "-display", "none",
  156. "-machine", "type=q35,accel=kvm:tcg",
  157. "-m", "192M",
  158. "-nographic",
  159. "-vga", "none",
  160. "-net", "none",
  161. "-no-acpi",
  162. "-no-hpet",
  163. "-no-reboot",
  164. "-fsdev", "local,id=fsdev-root,path=/,readonly,security_model=none,"
  165. "multidevs=remap",
  166. "-device", "virtio-9p-pci,fsdev=fsdev-root,mount_tag=/dev/root",
  167. "-chardev", "stdio,id=con,mux=on",
  168. "-serial", "chardev:con",
  169. NULL
  170. };
  171. static char *const qemu_envp[] = {
  172. "HOME=/",
  173. NULL
  174. };
  175. static void check_virtualization(void)
  176. {
  177. #if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__))
  178. uint32_t ecx;
  179. __asm__ __volatile__("cpuid" : "=c" (ecx) : "a" (1) : "memory");
  180. if (!!(ecx & (1 << 5)))
  181. printf("Found support for Virtual Machine eXtensions\n");
  182. #endif
  183. }
  184. static void start_qemu(void)
  185. {
  186. char cwd[PATH_MAX/2], initcmd[PATH_MAX], testargs[PATH_MAX];
  187. char cmdline[CMDLINE_MAX];
  188. char **argv;
  189. int i, pos;
  190. check_virtualization();
  191. if (!getcwd(cwd, sizeof(cwd)))
  192. strcat(cwd, "/");
  193. if (own_binary[0] == '/')
  194. snprintf(initcmd, sizeof(initcmd), "%s", own_binary);
  195. else
  196. snprintf(initcmd, sizeof(initcmd), "%s/%s", cwd, own_binary);
  197. pos = snprintf(testargs, sizeof(testargs), "%s", test_argv[0]);
  198. for (i = 1; i < test_argc; i++) {
  199. int len = sizeof(testargs) - pos;
  200. pos += snprintf(testargs + pos, len, " %s", test_argv[i]);
  201. }
  202. snprintf(cmdline, sizeof(cmdline),
  203. "console=ttyS0,115200n8 earlyprintk=serial "
  204. "rootfstype=9p "
  205. "rootflags=trans=virtio,version=9p2000.L "
  206. "acpi=off pci=noacpi noapic quiet ro init=%s "
  207. "bluetooth.enable_ecred=1"
  208. "TESTHOME=%s TESTDBUS=%u TESTMONITOR=%u "
  209. "TESTDEVS=%d TESTAUTO=%u TESTARGS=\'%s\'",
  210. initcmd, cwd, start_dbus, start_monitor,
  211. num_devs, run_auto, testargs);
  212. argv = alloca(sizeof(qemu_argv) +
  213. (sizeof(char *) * (4 + (num_devs * 4))));
  214. memcpy(argv, qemu_argv, sizeof(qemu_argv));
  215. pos = (sizeof(qemu_argv) / sizeof(char *)) - 1;
  216. argv[0] = (char *) qemu_binary;
  217. argv[pos++] = "-kernel";
  218. argv[pos++] = (char *) kernel_image;
  219. argv[pos++] = "-append";
  220. argv[pos++] = (char *) cmdline;
  221. for (i = 0; i < num_devs; i++) {
  222. const char *path = "/tmp/bt-server-bredr";
  223. char *chrdev, *serdev;
  224. chrdev = alloca(32 + strlen(path));
  225. sprintf(chrdev, "socket,path=%s,id=bt%d", path, i);
  226. serdev = alloca(32);
  227. sprintf(serdev, "pci-serial,chardev=bt%d", i);
  228. argv[pos++] = "-chardev";
  229. argv[pos++] = chrdev;
  230. argv[pos++] = "-device";
  231. argv[pos++] = serdev;
  232. }
  233. argv[pos] = NULL;
  234. execve(argv[0], argv, qemu_envp);
  235. }
  236. static int open_serial(const char *path)
  237. {
  238. struct termios ti;
  239. int fd, saved_ldisc, ldisc = N_HCI;
  240. fd = open(path, O_RDWR | O_NOCTTY);
  241. if (fd < 0) {
  242. perror("Failed to open serial port");
  243. return -1;
  244. }
  245. if (tcflush(fd, TCIOFLUSH) < 0) {
  246. perror("Failed to flush serial port");
  247. close(fd);
  248. return -1;
  249. }
  250. if (ioctl(fd, TIOCGETD, &saved_ldisc) < 0) {
  251. perror("Failed get serial line discipline");
  252. close(fd);
  253. return -1;
  254. }
  255. /* Switch TTY to raw mode */
  256. memset(&ti, 0, sizeof(ti));
  257. cfmakeraw(&ti);
  258. ti.c_cflag |= (B115200 | CLOCAL | CREAD);
  259. /* Set flow control */
  260. ti.c_cflag |= CRTSCTS;
  261. if (tcsetattr(fd, TCSANOW, &ti) < 0) {
  262. perror("Failed to set serial port settings");
  263. close(fd);
  264. return -1;
  265. }
  266. if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
  267. perror("Failed set serial line discipline");
  268. close(fd);
  269. return -1;
  270. }
  271. printf("Switched line discipline from %d to %d\n", saved_ldisc, ldisc);
  272. return fd;
  273. }
  274. static int attach_proto(const char *path, unsigned int proto,
  275. unsigned int mandatory_flags,
  276. unsigned int optional_flags)
  277. {
  278. unsigned int flags = mandatory_flags | optional_flags;
  279. int fd, dev_id;
  280. fd = open_serial(path);
  281. if (fd < 0)
  282. return -1;
  283. if (ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
  284. if (errno == EINVAL) {
  285. if (ioctl(fd, HCIUARTSETFLAGS, mandatory_flags) < 0) {
  286. perror("Failed to set mandatory flags");
  287. close(fd);
  288. return -1;
  289. }
  290. } else {
  291. perror("Failed to set flags");
  292. close(fd);
  293. return -1;
  294. }
  295. }
  296. if (ioctl(fd, HCIUARTSETPROTO, proto) < 0) {
  297. perror("Failed to set protocol");
  298. close(fd);
  299. return -1;
  300. }
  301. dev_id = ioctl(fd, HCIUARTGETDEVICE);
  302. if (dev_id < 0) {
  303. perror("Failed to get device id");
  304. close(fd);
  305. return -1;
  306. }
  307. printf("Device index %d attached\n", dev_id);
  308. return fd;
  309. }
  310. static void create_dbus_system_conf(void)
  311. {
  312. FILE *fp;
  313. fp = fopen("/etc/dbus-1/system.conf", "we");
  314. if (!fp)
  315. return;
  316. fputs("<!DOCTYPE busconfig PUBLIC "
  317. "\"-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN\" "
  318. "\"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd\">\n", fp);
  319. fputs("<busconfig>\n", fp);
  320. fputs("<type>system</type>\n", fp);
  321. fputs("<listen>unix:path=/run/dbus/system_bus_socket</listen>\n", fp);
  322. fputs("<policy context=\"default\">\n", fp);
  323. fputs("<allow user=\"*\"/>\n", fp);
  324. fputs("<allow own=\"*\"/>\n", fp);
  325. fputs("<allow send_type=\"method_call\"/>\n",fp);
  326. fputs("<allow send_type=\"signal\"/>\n", fp);
  327. fputs("<allow send_type=\"method_return\"/>\n", fp);
  328. fputs("<allow send_type=\"error\"/>\n", fp);
  329. fputs("<allow receive_type=\"method_call\"/>\n",fp);
  330. fputs("<allow receive_type=\"signal\"/>\n", fp);
  331. fputs("<allow receive_type=\"method_return\"/>\n", fp);
  332. fputs("<allow receive_type=\"error\"/>\n", fp);
  333. fputs("</policy>\n", fp);
  334. fputs("</busconfig>\n", fp);
  335. fclose(fp);
  336. if (symlink("/etc/dbus-1/system.conf",
  337. "/usr/share/dbus-1/system.conf") < 0)
  338. perror("Failed to create system.conf symlink");
  339. mkdir("/run/dbus", 0755);
  340. }
  341. static pid_t start_dbus_daemon(void)
  342. {
  343. char *argv[3], *envp[1];
  344. pid_t pid;
  345. int i;
  346. argv[0] = "/usr/bin/dbus-daemon";
  347. argv[1] = "--system";
  348. argv[2] = NULL;
  349. envp[0] = NULL;
  350. printf("Starting D-Bus daemon\n");
  351. pid = fork();
  352. if (pid < 0) {
  353. perror("Failed to fork new process");
  354. return -1;
  355. }
  356. if (pid == 0) {
  357. execve(argv[0], argv, envp);
  358. exit(EXIT_SUCCESS);
  359. }
  360. printf("D-Bus daemon process %d created\n", pid);
  361. for (i = 0; i < 20; i++) {
  362. struct stat st;
  363. if (!stat("/run/dbus/system_bus_socket", &st)) {
  364. printf("Found D-Bus daemon socket\n");
  365. break;
  366. }
  367. usleep(25 * 1000);
  368. }
  369. return pid;
  370. }
  371. static const char *daemon_table[] = {
  372. "bluetoothd",
  373. "src/bluetoothd",
  374. "/usr/sbin/bluetoothd",
  375. "/usr/libexec/bluetooth/bluetoothd",
  376. NULL
  377. };
  378. static pid_t start_bluetooth_daemon(const char *home)
  379. {
  380. const char *daemon = NULL;
  381. char *argv[6], *envp[2];
  382. pid_t pid;
  383. struct stat st;
  384. int i;
  385. if (chdir(home + 5) < 0) {
  386. perror("Failed to change home directory for daemon");
  387. return -1;
  388. }
  389. for (i = 0; daemon_table[i]; i++) {
  390. if (!stat(daemon_table[i], &st)) {
  391. daemon = daemon_table[i];
  392. break;
  393. }
  394. }
  395. if (!daemon) {
  396. fprintf(stderr, "Failed to locate Bluetooth daemon binary\n");
  397. return -1;
  398. }
  399. printf("Using Bluetooth daemon %s\n", daemon);
  400. argv[0] = (char *) daemon;
  401. argv[1] = "--nodetach";
  402. argv[2] = "-d";
  403. argv[3] = NULL;
  404. if (!stat("src/main.conf", &st)) {
  405. argv[3] = "-f";
  406. argv[4] = "src/main.conf";
  407. argv[5] = NULL;
  408. }
  409. envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket";
  410. envp[1] = NULL;
  411. printf("Starting Bluetooth daemon\n");
  412. pid = fork();
  413. if (pid < 0) {
  414. perror("Failed to fork new process");
  415. return -1;
  416. }
  417. if (pid == 0) {
  418. execve(argv[0], argv, envp);
  419. exit(EXIT_SUCCESS);
  420. }
  421. printf("Bluetooth daemon process %d created\n", pid);
  422. return pid;
  423. }
  424. static const char *test_table[] = {
  425. "mgmt-tester",
  426. "smp-tester",
  427. "l2cap-tester",
  428. "rfcomm-tester",
  429. "sco-tester",
  430. "bnep-tester",
  431. "check-selftest",
  432. "tools/mgmt-tester",
  433. "tools/smp-tester",
  434. "tools/l2cap-tester",
  435. "tools/rfcomm-tester",
  436. "tools/sco-tester",
  437. "tools/bnep-tester",
  438. "tools/check-selftest",
  439. NULL
  440. };
  441. static const char *monitor_table[] = {
  442. "btmon",
  443. "monitor/btmon",
  444. "/usr/sbin/btmon",
  445. NULL
  446. };
  447. static pid_t start_btmon(const char *home)
  448. {
  449. const char *monitor = NULL;
  450. char *argv[3], *envp[2];
  451. pid_t pid;
  452. int i;
  453. if (chdir(home + 5) < 0) {
  454. perror("Failed to change home directory for monitor");
  455. return -1;
  456. }
  457. for (i = 0; monitor_table[i]; i++) {
  458. struct stat st;
  459. if (!stat(monitor_table[i], &st)) {
  460. monitor = monitor_table[i];
  461. break;
  462. }
  463. }
  464. if (!monitor) {
  465. fprintf(stderr, "Failed to locate Monitor binary\n");
  466. return -1;
  467. }
  468. printf("Using Monitor %s\n", monitor);
  469. argv[0] = (char *) monitor;
  470. argv[1] = "-t";
  471. argv[2] = NULL;
  472. printf("Starting Monitor\n");
  473. pid = fork();
  474. if (pid < 0) {
  475. perror("Failed to fork new process");
  476. return -1;
  477. }
  478. if (pid == 0) {
  479. execve(argv[0], argv, envp);
  480. exit(EXIT_SUCCESS);
  481. }
  482. printf("Monitor process %d created\n", pid);
  483. return pid;
  484. }
  485. static void run_command(char *cmdname, char *home)
  486. {
  487. char *argv[9], *envp[3];
  488. int pos = 0, idx = 0;
  489. int serial_fd;
  490. pid_t pid, dbus_pid, daemon_pid, monitor_pid;
  491. if (num_devs) {
  492. const char *node = "/dev/ttyS1";
  493. unsigned int basic_flags, extra_flags;
  494. printf("Attaching BR/EDR controller to %s\n", node);
  495. basic_flags = (1 << HCI_UART_RESET_ON_INIT);
  496. extra_flags = (1 << HCI_UART_VND_DETECT);
  497. serial_fd = attach_proto(node, HCI_UART_H4, basic_flags,
  498. extra_flags);
  499. } else
  500. serial_fd = -1;
  501. if (start_dbus) {
  502. create_dbus_system_conf();
  503. dbus_pid = start_dbus_daemon();
  504. daemon_pid = start_bluetooth_daemon(home);
  505. } else {
  506. dbus_pid = -1;
  507. daemon_pid = -1;
  508. }
  509. if (start_monitor)
  510. monitor_pid = start_btmon(home);
  511. else
  512. monitor_pid = -1;
  513. start_next:
  514. if (run_auto) {
  515. if (chdir(home + 5) < 0) {
  516. perror("Failed to change home test directory");
  517. return;
  518. }
  519. while (1) {
  520. struct stat st;
  521. if (!test_table[idx])
  522. return;
  523. if (!stat(test_table[idx], &st))
  524. break;
  525. idx++;
  526. }
  527. argv[0] = (char *) test_table[idx];
  528. argv[1] = "-q";
  529. argv[2] = NULL;
  530. } else {
  531. while (1) {
  532. char *ptr;
  533. ptr = strchr(cmdname, ' ');
  534. if (!ptr) {
  535. argv[pos++] = cmdname;
  536. break;
  537. }
  538. *ptr = '\0';
  539. argv[pos++] = cmdname;
  540. if (pos > 8)
  541. break;
  542. cmdname = ptr + 1;
  543. }
  544. argv[pos] = NULL;
  545. }
  546. pos = 0;
  547. envp[pos++] = "TERM=linux";
  548. if (home)
  549. envp[pos++] = home;
  550. envp[pos] = NULL;
  551. printf("Running command %s\n", argv[0]);
  552. pid = fork();
  553. if (pid < 0) {
  554. perror("Failed to fork new process");
  555. return;
  556. }
  557. if (pid == 0) {
  558. if (home) {
  559. printf("Changing into directory %s\n", home + 5);
  560. if (chdir(home + 5) < 0)
  561. perror("Failed to change directory");
  562. }
  563. execve(argv[0], argv, envp);
  564. exit(EXIT_SUCCESS);
  565. }
  566. printf("New process %d created\n", pid);
  567. while (1) {
  568. pid_t corpse;
  569. int status;
  570. corpse = waitpid(WAIT_ANY, &status, 0);
  571. if (corpse < 0 || corpse == 0)
  572. continue;
  573. if (WIFEXITED(status))
  574. printf("Process %d exited with status %d\n",
  575. corpse, WEXITSTATUS(status));
  576. else if (WIFSIGNALED(status))
  577. printf("Process %d terminated with signal %d\n",
  578. corpse, WTERMSIG(status));
  579. else if (WIFSTOPPED(status))
  580. printf("Process %d stopped with signal %d\n",
  581. corpse, WSTOPSIG(status));
  582. else if (WIFCONTINUED(status))
  583. printf("Process %d continued\n", corpse);
  584. if (corpse == dbus_pid) {
  585. printf("D-Bus daemon terminated\n");
  586. dbus_pid = -1;
  587. }
  588. if (corpse == daemon_pid) {
  589. printf("Bluetooth daemon terminated\n");
  590. daemon_pid = -1;
  591. }
  592. if (corpse == monitor_pid) {
  593. printf("Bluetooth monitor terminated\n");
  594. monitor_pid = -1;
  595. }
  596. if (corpse == pid)
  597. break;
  598. }
  599. if (run_auto) {
  600. idx++;
  601. goto start_next;
  602. }
  603. if (daemon_pid > 0)
  604. kill(daemon_pid, SIGTERM);
  605. if (dbus_pid > 0)
  606. kill(dbus_pid, SIGTERM);
  607. if (monitor_pid > 0)
  608. kill(monitor_pid, SIGTERM);
  609. if (serial_fd >= 0) {
  610. close(serial_fd);
  611. serial_fd = -1;
  612. }
  613. }
  614. static void run_tests(void)
  615. {
  616. char cmdline[CMDLINE_MAX], *ptr, *cmds, *home = NULL;
  617. FILE *fp;
  618. fp = fopen("/proc/cmdline", "re");
  619. if (!fp) {
  620. fprintf(stderr, "Failed to open kernel command line\n");
  621. return;
  622. }
  623. ptr = fgets(cmdline, sizeof(cmdline), fp);
  624. fclose(fp);
  625. if (!ptr) {
  626. fprintf(stderr, "Failed to read kernel command line\n");
  627. return;
  628. }
  629. ptr = strstr(cmdline, "TESTARGS=");
  630. if (!ptr) {
  631. fprintf(stderr, "No test command section found\n");
  632. return;
  633. }
  634. cmds = ptr + 10;
  635. ptr = strchr(cmds, '\'');
  636. if (!ptr) {
  637. fprintf(stderr, "Malformed test command section\n");
  638. return;
  639. }
  640. *ptr = '\0';
  641. ptr = strstr(cmdline, "TESTAUTO=1");
  642. if (ptr) {
  643. printf("Automatic test execution requested\n");
  644. run_auto= true;
  645. }
  646. ptr = strstr(cmdline, "TESTDEVS=1");
  647. if (ptr) {
  648. printf("Attachment of devices requested\n");
  649. num_devs = 1;
  650. }
  651. ptr = strstr(cmdline, "TESTDBUS=1");
  652. if (ptr) {
  653. printf("D-Bus daemon requested\n");
  654. start_dbus = true;
  655. }
  656. ptr = strstr(cmdline, "TESTMONITOR=1");
  657. if (ptr) {
  658. printf("Monitor requested\n");
  659. start_monitor = true;
  660. }
  661. ptr = strstr(cmdline, "TESTHOME=");
  662. if (ptr) {
  663. home = ptr + 4;
  664. ptr = strpbrk(home + 9, " \r\n");
  665. if (ptr)
  666. *ptr = '\0';
  667. }
  668. run_command(cmds, home);
  669. }
  670. static void usage(void)
  671. {
  672. printf("test-runner - Automated test execution utility\n"
  673. "Usage:\n");
  674. printf("\ttest-runner [options] [--] <command> [args]\n");
  675. printf("Options:\n"
  676. "\t-a, --auto Find tests and run them\n"
  677. "\t-d, --dbus Start D-Bus daemon\n"
  678. "\t-m, --monitor Start btmon\n"
  679. "\t-u, --unix [path] Provide serial device\n"
  680. "\t-q, --qemu <path> QEMU binary\n"
  681. "\t-k, --kernel <image> Kernel image (bzImage)\n"
  682. "\t-h, --help Show help options\n");
  683. }
  684. static const struct option main_options[] = {
  685. { "all", no_argument, NULL, 'a' },
  686. { "auto", no_argument, NULL, 'a' },
  687. { "unix", no_argument, NULL, 'u' },
  688. { "dbus", no_argument, NULL, 'd' },
  689. { "monitor", no_argument, NULL, 'm' },
  690. { "qemu", required_argument, NULL, 'q' },
  691. { "kernel", required_argument, NULL, 'k' },
  692. { "version", no_argument, NULL, 'v' },
  693. { "help", no_argument, NULL, 'h' },
  694. { }
  695. };
  696. int main(int argc, char *argv[])
  697. {
  698. if (getpid() == 1 && getppid() == 0) {
  699. prepare_sandbox();
  700. run_tests();
  701. sync();
  702. reboot(RB_AUTOBOOT);
  703. return EXIT_SUCCESS;
  704. }
  705. for (;;) {
  706. int opt;
  707. opt = getopt_long(argc, argv, "audmq:k:vh", main_options, NULL);
  708. if (opt < 0)
  709. break;
  710. switch (opt) {
  711. case 'a':
  712. run_auto = true;
  713. break;
  714. case 'u':
  715. num_devs = 1;
  716. break;
  717. case 'd':
  718. start_dbus = true;
  719. break;
  720. case 'm':
  721. start_monitor = true;
  722. break;
  723. case 'q':
  724. qemu_binary = optarg;
  725. break;
  726. case 'k':
  727. kernel_image = optarg;
  728. break;
  729. case 'v':
  730. printf("%s\n", VERSION);
  731. return EXIT_SUCCESS;
  732. case 'h':
  733. usage();
  734. return EXIT_SUCCESS;
  735. default:
  736. return EXIT_FAILURE;
  737. }
  738. }
  739. if (run_auto) {
  740. if (argc - optind > 0) {
  741. fprintf(stderr, "Invalid command line parameters\n");
  742. return EXIT_FAILURE;
  743. }
  744. } else {
  745. if (argc - optind < 1) {
  746. fprintf(stderr, "Failed to specify test command\n");
  747. return EXIT_FAILURE;
  748. }
  749. }
  750. own_binary = argv[0];
  751. test_argv = argv + optind;
  752. test_argc = argc - optind;
  753. if (!qemu_binary) {
  754. qemu_binary = find_qemu();
  755. if (!qemu_binary) {
  756. fprintf(stderr, "No default QEMU binary found\n");
  757. return EXIT_FAILURE;
  758. }
  759. }
  760. if (!kernel_image) {
  761. kernel_image = find_kernel();
  762. if (!kernel_image) {
  763. fprintf(stderr, "No default kernel image found\n");
  764. return EXIT_FAILURE;
  765. }
  766. }
  767. printf("Using QEMU binary %s\n", qemu_binary);
  768. printf("Using kernel image %s\n", kernel_image);
  769. start_qemu();
  770. return EXIT_SUCCESS;
  771. }