1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2015 Intel Corporation. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/mount.h>
32 #include "src/shared/mainloop.h"
33 #include "peripheral/efivars.h"
34 #include "peripheral/attach.h"
35 #include "peripheral/gap.h"
36 #include "peripheral/log.h"
38 static bool is_init = false;
39 static pid_t shell_pid = -1;
45 { "/proc/self/fd", "/dev/fd" },
46 { "/proc/self/fd/0", "/dev/stdin" },
47 { "/proc/self/fd/1", "/dev/stdout" },
48 { "/proc/self/fd/2", "/dev/stderr" },
58 { "sysfs", "/sys", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
59 { "proc", "/proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
60 { "devtmpfs", "/dev", NULL, MS_NOSUID|MS_STRICTATIME },
61 { "efivarfs", "/sys/firmware/efi/efivars",
62 NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
63 { "pstore", "/sys/fs/pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
67 static void prepare_filesystem(void)
74 for (i = 0; mount_table[i].fstype; i++) {
77 if (lstat(mount_table[i].target, &st) < 0) {
78 printf("Creating %s\n", mount_table[i].target);
79 if (mkdir(mount_table[i].target, 0755) < 0)
80 perror("Failed to create dir");
83 printf("Mounting %s to %s\n", mount_table[i].fstype,
84 mount_table[i].target);
86 if (mount(mount_table[i].fstype,
87 mount_table[i].target,
88 mount_table[i].fstype,
89 mount_table[i].flags, NULL) < 0)
90 perror("Failed to mount filesystem");
93 for (i = 0; dev_table[i].target; i++) {
94 printf("Linking %s to %s\n", dev_table[i].linkpath,
97 if (symlink(dev_table[i].target, dev_table[i].linkpath) < 0)
98 perror("Failed to create device symlink");
102 static void run_shell(void)
106 printf("Starting shell\n");
110 perror("Failed to fork new process");
115 char *prg_argv[] = { "/bin/sh", NULL };
116 char *prg_envp[] = { NULL };
118 execve(prg_argv[0], prg_argv, prg_envp);
122 printf("PID %d created\n", pid);
127 static void exit_shell(void)
139 static void signal_callback(int signum, void *user_data)
151 pid = waitpid(WAIT_ANY, &status, WNOHANG);
152 if (pid < 0 || pid == 0)
155 if (WIFEXITED(status)) {
156 printf("PID %d exited (status=%d)\n",
157 pid, WEXITSTATUS(status));
159 if (pid == shell_pid)
161 } else if (WIFSIGNALED(status)) {
162 printf("PID %d terminated (signal=%d)\n",
163 pid, WTERMSIG(status));
165 if (pid == shell_pid)
173 int main(int argc, char *argv[])
177 if (getpid() == 1 && getppid() == 0)
182 printf("Bluetooth periperhal ver %s\n", VERSION);
184 prepare_filesystem();
189 if (efivars_read("BluetoothStaticAddress", NULL,
191 printf("Generating new persistent static address\n");
200 efivars_write("BluetoothStaticAddress",
201 EFIVARS_NON_VOLATILE |
202 EFIVARS_BOOTSERVICE_ACCESS |
203 EFIVARS_RUNTIME_ACCESS,
207 gap_set_static_address(addr);
218 exit_status = mainloop_run_with_signal(signal_callback, NULL);