3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2015 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include <sys/types.h>
39 #include <sys/mount.h>
45 #include "src/shared/mainloop.h"
46 #include "peripheral/efivars.h"
47 #include "peripheral/attach.h"
48 #include "peripheral/gap.h"
49 #include "peripheral/log.h"
51 static bool is_init = false;
52 static pid_t shell_pid = -1;
58 { "/proc/self/fd", "/dev/fd" },
59 { "/proc/self/fd/0", "/dev/stdin" },
60 { "/proc/self/fd/1", "/dev/stdout" },
61 { "/proc/self/fd/2", "/dev/stderr" },
71 { "sysfs", "/sys", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
72 { "proc", "/proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
73 { "devtmpfs", "/dev", NULL, MS_NOSUID|MS_STRICTATIME },
74 { "efivarfs", "/sys/firmware/efi/efivars",
75 NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
76 { "pstore", "/sys/fs/pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
80 static void prepare_filesystem(void)
87 for (i = 0; mount_table[i].fstype; i++) {
90 if (lstat(mount_table[i].target, &st) < 0) {
91 printf("Creating %s\n", mount_table[i].target);
92 mkdir(mount_table[i].target, 0755);
95 printf("Mounting %s to %s\n", mount_table[i].fstype,
96 mount_table[i].target);
98 if (mount(mount_table[i].fstype,
99 mount_table[i].target,
100 mount_table[i].fstype,
101 mount_table[i].flags, NULL) < 0)
102 perror("Failed to mount filesystem");
105 for (i = 0; dev_table[i].target; i++) {
106 printf("Linking %s to %s\n", dev_table[i].linkpath,
107 dev_table[i].target);
109 if (symlink(dev_table[i].target, dev_table[i].linkpath) < 0)
110 perror("Failed to create device symlink");
114 static void run_shell(void)
118 printf("Starting shell\n");
122 perror("Failed to fork new process");
127 char *prg_argv[] = { "/bin/sh", NULL };
128 char *prg_envp[] = { NULL };
130 execve(prg_argv[0], prg_argv, prg_envp);
134 printf("PID %d created\n", pid);
139 static void exit_shell(void)
151 static void signal_callback(int signum, void *user_data)
163 pid = waitpid(WAIT_ANY, &status, WNOHANG);
164 if (pid < 0 || pid == 0)
167 if (WIFEXITED(status)) {
168 printf("PID %d exited (status=%d)\n",
169 pid, WEXITSTATUS(status));
171 if (pid == shell_pid)
173 } else if (WIFSIGNALED(status)) {
174 printf("PID %d terminated (signal=%d)\n",
175 pid, WTERMSIG(status));
177 if (pid == shell_pid)
185 int main(int argc, char *argv[])
190 if (getpid() == 1 && getppid() == 0)
196 sigaddset(&mask, SIGINT);
197 sigaddset(&mask, SIGTERM);
198 sigaddset(&mask, SIGCHLD);
200 mainloop_set_signal(&mask, signal_callback, NULL, NULL);
202 printf("Bluetooth periperhal ver %s\n", VERSION);
204 prepare_filesystem();
209 if (efivars_read("BluetoothStaticAddress", NULL,
211 printf("Generating new persistent static address\n");
220 efivars_write("BluetoothStaticAddress",
221 EFIVARS_NON_VOLATILE |
222 EFIVARS_BOOTSERVICE_ACCESS |
223 EFIVARS_RUNTIME_ACCESS,
227 gap_set_static_address(addr);
238 exit_status = mainloop_run();