main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2015 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 <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdbool.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #include <poll.h>
  22. #include <sys/wait.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <sys/mount.h>
  26. #ifndef WAIT_ANY
  27. #define WAIT_ANY (-1)
  28. #endif
  29. #include "src/shared/mainloop.h"
  30. #include "peripheral/efivars.h"
  31. #include "peripheral/attach.h"
  32. #include "peripheral/gap.h"
  33. #include "peripheral/log.h"
  34. static bool is_init = false;
  35. static pid_t shell_pid = -1;
  36. static const struct {
  37. const char *target;
  38. const char *linkpath;
  39. } dev_table[] = {
  40. { "/proc/self/fd", "/dev/fd" },
  41. { "/proc/self/fd/0", "/dev/stdin" },
  42. { "/proc/self/fd/1", "/dev/stdout" },
  43. { "/proc/self/fd/2", "/dev/stderr" },
  44. { }
  45. };
  46. static const struct {
  47. const char *fstype;
  48. const char *target;
  49. const char *options;
  50. unsigned long flags;
  51. } mount_table[] = {
  52. { "sysfs", "/sys", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
  53. { "proc", "/proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
  54. { "devtmpfs", "/dev", NULL, MS_NOSUID|MS_STRICTATIME },
  55. { "efivarfs", "/sys/firmware/efi/efivars",
  56. NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
  57. { "pstore", "/sys/fs/pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
  58. { }
  59. };
  60. static void prepare_filesystem(void)
  61. {
  62. int i;
  63. if (!is_init)
  64. return;
  65. for (i = 0; mount_table[i].fstype; i++) {
  66. struct stat st;
  67. if (lstat(mount_table[i].target, &st) < 0) {
  68. printf("Creating %s\n", mount_table[i].target);
  69. mkdir(mount_table[i].target, 0755);
  70. }
  71. printf("Mounting %s to %s\n", mount_table[i].fstype,
  72. mount_table[i].target);
  73. if (mount(mount_table[i].fstype,
  74. mount_table[i].target,
  75. mount_table[i].fstype,
  76. mount_table[i].flags, NULL) < 0)
  77. perror("Failed to mount filesystem");
  78. }
  79. for (i = 0; dev_table[i].target; i++) {
  80. printf("Linking %s to %s\n", dev_table[i].linkpath,
  81. dev_table[i].target);
  82. if (symlink(dev_table[i].target, dev_table[i].linkpath) < 0)
  83. perror("Failed to create device symlink");
  84. }
  85. }
  86. static void run_shell(void)
  87. {
  88. pid_t pid;
  89. printf("Starting shell\n");
  90. pid = fork();
  91. if (pid < 0) {
  92. perror("Failed to fork new process");
  93. return;
  94. }
  95. if (pid == 0) {
  96. char *prg_argv[] = { "/bin/sh", NULL };
  97. char *prg_envp[] = { NULL };
  98. execve(prg_argv[0], prg_argv, prg_envp);
  99. exit(0);
  100. }
  101. printf("PID %d created\n", pid);
  102. shell_pid = pid;
  103. }
  104. static void exit_shell(void)
  105. {
  106. shell_pid = -1;
  107. if (!is_init) {
  108. mainloop_quit();
  109. return;
  110. }
  111. run_shell();
  112. }
  113. static void signal_callback(int signum, void *user_data)
  114. {
  115. switch (signum) {
  116. case SIGINT:
  117. case SIGTERM:
  118. mainloop_quit();
  119. break;
  120. case SIGCHLD:
  121. while (1) {
  122. pid_t pid;
  123. int status;
  124. pid = waitpid(WAIT_ANY, &status, WNOHANG);
  125. if (pid < 0 || pid == 0)
  126. break;
  127. if (WIFEXITED(status)) {
  128. printf("PID %d exited (status=%d)\n",
  129. pid, WEXITSTATUS(status));
  130. if (pid == shell_pid)
  131. exit_shell();
  132. } else if (WIFSIGNALED(status)) {
  133. printf("PID %d terminated (signal=%d)\n",
  134. pid, WTERMSIG(status));
  135. if (pid == shell_pid)
  136. exit_shell();
  137. }
  138. }
  139. break;
  140. }
  141. }
  142. int main(int argc, char *argv[])
  143. {
  144. int exit_status;
  145. if (getpid() == 1 && getppid() == 0)
  146. is_init = true;
  147. mainloop_init();
  148. printf("Bluetooth periperhal ver %s\n", VERSION);
  149. prepare_filesystem();
  150. if (is_init) {
  151. uint8_t addr[6];
  152. if (efivars_read("BluetoothStaticAddress", NULL,
  153. addr, 6) < 0) {
  154. printf("Generating new persistent static address\n");
  155. addr[0] = rand();
  156. addr[1] = rand();
  157. addr[2] = rand();
  158. addr[3] = 0x34;
  159. addr[4] = 0x12;
  160. addr[5] = 0xc0;
  161. efivars_write("BluetoothStaticAddress",
  162. EFIVARS_NON_VOLATILE |
  163. EFIVARS_BOOTSERVICE_ACCESS |
  164. EFIVARS_RUNTIME_ACCESS,
  165. addr, 6);
  166. }
  167. gap_set_static_address(addr);
  168. run_shell();
  169. }
  170. log_open();
  171. gap_start();
  172. if (is_init)
  173. attach_start();
  174. exit_status = mainloop_run_with_signal(signal_callback, NULL);
  175. if (is_init)
  176. attach_stop();
  177. gap_stop();
  178. log_close();
  179. return exit_status;
  180. }