main.c revision 151046c5165860ddea6e84498cc0a0e191de1c40
1/* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2000-2001 Qualcomm Incorporated 6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> 7 * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org> 8 * 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 */ 25 26#ifdef HAVE_CONFIG_H 27#include <config.h> 28#endif 29 30#include <stdio.h> 31#include <errno.h> 32#include <fcntl.h> 33#include <unistd.h> 34#include <stdlib.h> 35#include <string.h> 36#include <signal.h> 37#include <sys/stat.h> 38#include <sys/ioctl.h> 39#include <sys/socket.h> 40#include <sys/types.h> 41#include <sys/wait.h> 42 43#include <bluetooth/bluetooth.h> 44#include <bluetooth/hci.h> 45#include <bluetooth/hci_lib.h> 46 47#include <glib.h> 48 49#include <dbus/dbus.h> 50 51#include "logging.h" 52 53#include "hcid.h" 54#include "sdpd.h" 55#include "adapter.h" 56#include "dbus-hci.h" 57#include "dbus-common.h" 58#include "agent.h" 59#include "manager.h" 60#include "storage.h" 61 62#define HCID_DEFAULT_DISCOVERABLE_TIMEOUT 180 /* 3 minutes */ 63 64enum { 65 HCID_SET_NAME, 66 HCID_SET_CLASS, 67 HCID_SET_PAGETO, 68 HCID_SET_DISCOVTO, 69 HCID_SET_LM, 70 HCID_SET_LP, 71}; 72 73struct main_opts main_opts; 74 75static int child_pipe[2]; 76 77static GKeyFile *load_config(const char *file) 78{ 79 GError *err = NULL; 80 GKeyFile *keyfile; 81 82 keyfile = g_key_file_new(); 83 84 g_key_file_set_list_separator(keyfile, ','); 85 86 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) { 87 error("Parsing %s failed: %s", file, err->message); 88 g_error_free(err); 89 g_key_file_free(keyfile); 90 return NULL; 91 } 92 93 return keyfile; 94} 95 96static void parse_config(GKeyFile *config) 97{ 98 GError *err = NULL; 99 char *str; 100 int val; 101 102 if (!config) 103 return; 104 105 debug("parsing main.conf"); 106 107 str = g_key_file_get_string(config, "General", 108 "OffMode", &err); 109 if (err) { 110 debug("%s", err->message); 111 g_error_free(err); 112 err = NULL; 113 } else { 114 debug("offmode=%s", str); 115 if (g_str_equal(str, "DevDown")) 116 main_opts.offmode = HCID_OFFMODE_DEVDOWN; 117 g_free(str); 118 } 119 120 val = g_key_file_get_integer(config, "General", 121 "DiscoverableTimeout", 122 &err); 123 if (err) { 124 debug("%s", err->message); 125 g_error_free(err); 126 err = NULL; 127 } else { 128 debug("discovto=%d", val); 129 main_opts.discovto = val; 130 main_opts.flags |= 1 << HCID_SET_DISCOVTO; 131 } 132 133 val = g_key_file_get_integer(config, "General", 134 "PageTimeout", 135 &err); 136 if (err) { 137 debug("%s", err->message); 138 g_error_free(err); 139 err = NULL; 140 } else { 141 debug("pageto=%d", val); 142 main_opts.pageto = val; 143 main_opts.flags |= 1 << HCID_SET_PAGETO; 144 } 145 146 str = g_key_file_get_string(config, "General", 147 "Name", &err); 148 if (err) { 149 debug("%s", err->message); 150 g_error_free(err); 151 err = NULL; 152 } else { 153 debug("name=%s", str); 154 g_free(main_opts.name); 155 main_opts.name = g_strdup(str); 156 main_opts.flags |= 1 << HCID_SET_NAME; 157 g_free(str); 158 } 159 160 str = g_key_file_get_string(config, "General", 161 "Class", &err); 162 if (err) { 163 debug("%s", err->message); 164 g_error_free(err); 165 err = NULL; 166 } else { 167 debug("class=%s", str); 168 main_opts.class = strtol(str, NULL, 16); 169 main_opts.flags |= 1 << HCID_SET_CLASS; 170 g_free(str); 171 } 172 173 val = g_key_file_get_integer(config, "General", 174 "DiscoverSchedulerInterval", 175 &err); 176 if (err) { 177 debug("%s", err->message); 178 g_error_free(err); 179 err = NULL; 180 } else { 181 debug("inqmode=%d", val); 182 main_opts.inqmode = val; 183 } 184 185 main_opts.link_mode = HCI_LM_ACCEPT; 186 main_opts.flags |= (1 << HCID_SET_LM); 187 188 main_opts.link_policy = HCI_LP_RSWITCH | HCI_LP_SNIFF | 189 HCI_LP_HOLD | HCI_LP_PARK; 190 main_opts.flags |= (1 << HCID_SET_LP); 191} 192 193static void update_service_classes(const bdaddr_t *bdaddr, uint8_t value) 194{ 195 struct hci_dev_list_req *dl; 196 struct hci_dev_req *dr; 197 int i, sk; 198 199 sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); 200 if (sk < 0) 201 return; 202 203 dl = g_malloc0(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)); 204 205 dl->dev_num = HCI_MAX_DEV; 206 dr = dl->dev_req; 207 208 if (ioctl(sk, HCIGETDEVLIST, dl) < 0) { 209 close(sk); 210 g_free(dl); 211 return; 212 } 213 214 dr = dl->dev_req; 215 216 for (i = 0; i < dl->dev_num; i++, dr++) { 217 struct hci_dev_info di; 218 uint8_t cls[3]; 219 int dd; 220 221 if (hci_devinfo(dr->dev_id, &di) < 0) 222 continue; 223 224 if (hci_test_bit(HCI_RAW, &di.flags)) 225 continue; 226 227 if (!hci_test_bit(HCI_UP, &di.flags)) 228 continue; 229 230 if (manager_get_adapter_class(di.dev_id, cls) < 0) 231 continue; 232 233 dd = hci_open_dev(di.dev_id); 234 if (dd < 0) 235 continue; 236 237 set_service_classes(dd, cls, value); 238 239 hci_close_dev(dd); 240 241 manager_update_adapter(di.dev_id); 242 } 243 244 g_free(dl); 245 246 close(sk); 247} 248 249/* 250 * Device name expansion 251 * %d - device id 252 */ 253static char *expand_name(char *dst, int size, char *str, int dev_id) 254{ 255 register int sp, np, olen; 256 char *opt, buf[10]; 257 258 if (!str && !dst) 259 return NULL; 260 261 sp = np = 0; 262 while (np < size - 1 && str[sp]) { 263 switch (str[sp]) { 264 case '%': 265 opt = NULL; 266 267 switch (str[sp+1]) { 268 case 'd': 269 sprintf(buf, "%d", dev_id); 270 opt = buf; 271 break; 272 273 case 'h': 274 opt = main_opts.host_name; 275 break; 276 277 case '%': 278 dst[np++] = str[sp++]; 279 /* fall through */ 280 default: 281 sp++; 282 continue; 283 } 284 285 if (opt) { 286 /* substitute */ 287 olen = strlen(opt); 288 if (np + olen < size - 1) 289 memcpy(dst + np, opt, olen); 290 np += olen; 291 } 292 sp += 2; 293 continue; 294 295 case '\\': 296 sp++; 297 /* fall through */ 298 default: 299 dst[np++] = str[sp++]; 300 break; 301 } 302 } 303 dst[np] = '\0'; 304 return dst; 305} 306 307static gboolean child_exit(GIOChannel *io, GIOCondition cond, void *user_data) 308{ 309 int status, fd = g_io_channel_unix_get_fd(io); 310 pid_t child_pid; 311 312 if (read(fd, &child_pid, sizeof(child_pid)) != sizeof(child_pid)) { 313 error("child_exit: unable to read child pid from pipe"); 314 return TRUE; 315 } 316 317 if (waitpid(child_pid, &status, 0) != child_pid) 318 error("waitpid(%d) failed", child_pid); 319 else 320 debug("child %d exited", child_pid); 321 322 return TRUE; 323} 324 325static void at_child_exit(void) 326{ 327 pid_t pid = getpid(); 328 329 if (write(child_pipe[1], &pid, sizeof(pid)) != sizeof(pid)) 330 error("unable to write to child pipe"); 331} 332 333static void configure_device(int dev_id) 334{ 335 struct hci_dev_req dr; 336 struct hci_dev_info di; 337 pid_t pid; 338 int dd; 339 340 if (hci_devinfo(dev_id, &di) < 0) 341 return; 342 343 if (hci_test_bit(HCI_RAW, &di.flags)) 344 return; 345 346 /* Do configuration in the separate process */ 347 pid = fork(); 348 switch (pid) { 349 case 0: 350 atexit(at_child_exit); 351 break; 352 case -1: 353 error("Fork failed. Can't init device hci%d: %s (%d)", 354 dev_id, strerror(errno), errno); 355 default: 356 debug("configuration child %d forked", pid); 357 return; 358 } 359 360 dd = hci_open_dev(dev_id); 361 if (dd < 0) { 362 error("Can't open device hci%d: %s (%d)", 363 dev_id, strerror(errno), errno); 364 exit(1); 365 } 366 367 memset(&dr, 0, sizeof(dr)); 368 dr.dev_id = dev_id; 369 370 /* Set link mode */ 371 if ((main_opts.flags & (1 << HCID_SET_LM))) { 372 dr.dev_opt = main_opts.link_mode; 373 if (ioctl(dd, HCISETLINKMODE, (unsigned long) &dr) < 0) { 374 error("Can't set link mode on hci%d: %s (%d)", 375 dev_id, strerror(errno), errno); 376 } 377 } 378 379 /* Set link policy */ 380 if ((main_opts.flags & (1 << HCID_SET_LP))) { 381 dr.dev_opt = main_opts.link_policy; 382 if (ioctl(dd, HCISETLINKPOL, (unsigned long) &dr) < 0) { 383 error("Can't set link policy on hci%d: %s (%d)", 384 dev_id, strerror(errno), errno); 385 } 386 } 387 388 /* Set device name */ 389 if ((main_opts.flags & (1 << HCID_SET_NAME)) && main_opts.name) { 390 change_local_name_cp cp; 391 392 memset(cp.name, 0, sizeof(cp.name)); 393 expand_name((char *) cp.name, sizeof(cp.name), 394 main_opts.name, dev_id); 395 396 hci_send_cmd(dd, OGF_HOST_CTL, OCF_CHANGE_LOCAL_NAME, 397 CHANGE_LOCAL_NAME_CP_SIZE, &cp); 398 } 399 400 /* Set device class */ 401 if ((main_opts.flags & (1 << HCID_SET_CLASS))) { 402 write_class_of_dev_cp cp; 403 uint32_t class; 404 uint8_t cls[3]; 405 406 if (read_local_class(&di.bdaddr, cls) < 0) { 407 class = htobl(main_opts.class); 408 cls[2] = get_service_classes(&di.bdaddr); 409 memcpy(cp.dev_class, &class, 3); 410 } else { 411 if (!(main_opts.scan & SCAN_INQUIRY)) 412 cls[1] &= 0xdf; /* Clear discoverable bit */ 413 cls[2] = get_service_classes(&di.bdaddr); 414 memcpy(cp.dev_class, cls, 3); 415 } 416 417 hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_CLASS_OF_DEV, 418 WRITE_CLASS_OF_DEV_CP_SIZE, &cp); 419 } 420 421 /* Set page timeout */ 422 if ((main_opts.flags & (1 << HCID_SET_PAGETO))) { 423 write_page_timeout_cp cp; 424 425 cp.timeout = htobs(main_opts.pageto); 426 hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_PAGE_TIMEOUT, 427 WRITE_PAGE_TIMEOUT_CP_SIZE, &cp); 428 } 429 430 exit(0); 431} 432 433static void init_device(int dev_id) 434{ 435 struct hci_dev_info di; 436 pid_t pid; 437 int dd; 438 439 /* Do initialization in the separate process */ 440 pid = fork(); 441 switch (pid) { 442 case 0: 443 atexit(at_child_exit); 444 break; 445 case -1: 446 error("Fork failed. Can't init device hci%d: %s (%d)", 447 dev_id, strerror(errno), errno); 448 default: 449 debug("initialization child %d forked", pid); 450 return; 451 } 452 453 dd = hci_open_dev(dev_id); 454 if (dd < 0) { 455 error("Can't open device hci%d: %s (%d)", 456 dev_id, strerror(errno), errno); 457 exit(1); 458 } 459 460 /* Start HCI device */ 461 if (ioctl(dd, HCIDEVUP, dev_id) < 0 && errno != EALREADY) { 462 error("Can't init device hci%d: %s (%d)", 463 dev_id, strerror(errno), errno); 464 goto fail; 465 } 466 467 if (hci_devinfo(dev_id, &di) < 0) 468 goto fail; 469 470 if (hci_test_bit(HCI_RAW, &di.flags)) 471 goto done; 472 473 if (main_opts.offmode == HCID_OFFMODE_DEVDOWN) { 474 char mode[16], src[18]; 475 476 ba2str(&di.bdaddr, src); 477 if (read_device_mode(src, mode, sizeof(mode)) == 0 && 478 strcmp(mode, "off") == 0) { 479 ioctl(dd, HCIDEVDOWN, dev_id); 480 goto done; 481 } 482 } 483 484done: 485 hci_close_dev(dd); 486 exit(0); 487 488fail: 489 hci_close_dev(dd); 490 exit(1); 491} 492 493static void device_devreg_setup(int dev_id) 494{ 495 struct hci_dev_info di; 496 497 init_device(dev_id); 498 499 if (hci_devinfo(dev_id, &di) < 0) 500 return; 501 502 if (!hci_test_bit(HCI_RAW, &di.flags)) 503 manager_register_adapter(dev_id); 504} 505 506static void device_devup_setup(int dev_id) 507{ 508 configure_device(dev_id); 509 510 manager_start_adapter(dev_id); 511 start_security_manager(dev_id); 512} 513 514static void init_all_devices(int ctl) 515{ 516 struct hci_dev_list_req *dl; 517 struct hci_dev_req *dr; 518 int i; 519 520 dl = g_try_malloc0(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)); 521 if (!dl) { 522 info("Can't allocate devlist buffer: %s (%d)", 523 strerror(errno), errno); 524 exit(1); 525 } 526 527 dl->dev_num = HCI_MAX_DEV; 528 dr = dl->dev_req; 529 530 if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) { 531 info("Can't get device list: %s (%d)", 532 strerror(errno), errno); 533 exit(1); 534 } 535 536 for (i = 0; i < dl->dev_num; i++, dr++) { 537 info("HCI dev %d registered", dr->dev_id); 538 device_devreg_setup(dr->dev_id); 539 if (hci_test_bit(HCI_UP, &dr->dev_opt)) { 540 info("HCI dev %d already up", dr->dev_id); 541 device_devup_setup(dr->dev_id); 542 } 543 } 544 545 g_free(dl); 546} 547 548static void init_defaults(void) 549{ 550 /* Default HCId settings */ 551 memset(&main_opts, 0, sizeof(main_opts)); 552 main_opts.offmode = HCID_OFFMODE_NOSCAN; 553 main_opts.scan = SCAN_PAGE; 554 main_opts.mode = MODE_CONNECTABLE; 555 main_opts.name = g_strdup("BlueZ"); 556 main_opts.discovto = HCID_DEFAULT_DISCOVERABLE_TIMEOUT; 557 558 if (gethostname(main_opts.host_name, sizeof(main_opts.host_name) - 1) < 0) 559 strcpy(main_opts.host_name, "noname"); 560} 561 562static inline void device_event(GIOChannel *chan, evt_stack_internal *si) 563{ 564 evt_si_device *sd = (void *) &si->data; 565 566 switch (sd->event) { 567 case HCI_DEV_REG: 568 info("HCI dev %d registered", sd->dev_id); 569 device_devreg_setup(sd->dev_id); 570 break; 571 572 case HCI_DEV_UNREG: 573 info("HCI dev %d unregistered", sd->dev_id); 574 manager_unregister_adapter(sd->dev_id); 575 break; 576 577 case HCI_DEV_UP: 578 info("HCI dev %d up", sd->dev_id); 579 device_devup_setup(sd->dev_id); 580 break; 581 582 case HCI_DEV_DOWN: 583 info("HCI dev %d down", sd->dev_id); 584 manager_stop_adapter(sd->dev_id); 585 stop_security_manager(sd->dev_id); 586 break; 587 } 588} 589 590static gboolean io_stack_event(GIOChannel *chan, GIOCondition cond, gpointer data) 591{ 592 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr; 593 evt_stack_internal *si; 594 hci_event_hdr *eh; 595 int type; 596 size_t len; 597 GIOError err; 598 599 ptr = buf; 600 601 if ((err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len))) { 602 if (err == G_IO_ERROR_AGAIN) 603 return TRUE; 604 605 error("Read from control socket failed: %s (%d)", 606 strerror(errno), errno); 607 return FALSE; 608 } 609 610 type = *ptr++; 611 612 if (type != HCI_EVENT_PKT) 613 return TRUE; 614 615 eh = (hci_event_hdr *) ptr; 616 if (eh->evt != EVT_STACK_INTERNAL) 617 return TRUE; 618 619 ptr += HCI_EVENT_HDR_SIZE; 620 621 si = (evt_stack_internal *) ptr; 622 switch (si->type) { 623 case EVT_SI_DEVICE: 624 device_event(chan, si); 625 break; 626 } 627 628 return TRUE; 629} 630 631static GMainLoop *event_loop; 632 633static void sig_term(int sig) 634{ 635 g_main_loop_quit(event_loop); 636} 637 638static void sig_debug(int sig) 639{ 640 toggle_debug(); 641} 642 643static gboolean option_detach = TRUE; 644static gboolean option_debug = FALSE; 645 646static GOptionEntry options[] = { 647 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE, 648 G_OPTION_ARG_NONE, &option_detach, 649 "Don't run as daemon in background" }, 650 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug, 651 "Enable debug information output" }, 652 { NULL }, 653}; 654 655int main(int argc, char *argv[]) 656{ 657 GOptionContext *context; 658 GError *err = NULL; 659 struct sockaddr_hci addr; 660 struct hci_filter flt; 661 struct sigaction sa; 662 GIOChannel *ctl_io, *child_io; 663 uint16_t mtu = 0; 664 GKeyFile *config; 665 666 init_defaults(); 667 668 context = g_option_context_new(NULL); 669 g_option_context_add_main_entries(context, options, NULL); 670 671 if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) { 672 if (err != NULL) { 673 g_printerr("%s\n", err->message); 674 g_error_free(err); 675 } else 676 g_printerr("An unknown error occurred\n"); 677 exit(1); 678 } 679 680 g_option_context_free(context); 681 682 if (option_detach == TRUE) { 683 if (daemon(0, 0)) { 684 perror("Can't start daemon"); 685 exit(1); 686 } 687 } 688 689 umask(0077); 690 691 start_logging("bluetoothd", "Bluetooth daemon"); 692 693 memset(&sa, 0, sizeof(sa)); 694 sa.sa_flags = SA_NOCLDSTOP; 695 sa.sa_handler = sig_term; 696 sigaction(SIGTERM, &sa, NULL); 697 sigaction(SIGINT, &sa, NULL); 698 699 sa.sa_handler = sig_debug; 700 sigaction(SIGUSR2, &sa, NULL); 701 702 sa.sa_handler = SIG_IGN; 703 sigaction(SIGPIPE, &sa, NULL); 704 705 if (option_debug == TRUE) { 706 info("Enabling debug information"); 707 enable_debug(); 708 } 709 710 /* Create and bind HCI socket */ 711 if ((main_opts.sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { 712 error("Can't open HCI socket: %s (%d)", 713 strerror(errno), errno); 714 exit(1); 715 } 716 717 /* Set filter */ 718 hci_filter_clear(&flt); 719 hci_filter_set_ptype(HCI_EVENT_PKT, &flt); 720 hci_filter_set_event(EVT_STACK_INTERNAL, &flt); 721 if (setsockopt(main_opts.sock, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { 722 error("Can't set filter: %s (%d)", 723 strerror(errno), errno); 724 exit(1); 725 } 726 727 addr.hci_family = AF_BLUETOOTH; 728 addr.hci_dev = HCI_DEV_NONE; 729 if (bind(main_opts.sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 730 error("Can't bind HCI socket: %s (%d)", 731 strerror(errno), errno); 732 exit(1); 733 } 734 735 config = load_config(CONFIGDIR "/main.conf"); 736 737 parse_config(config); 738 739 if (pipe(child_pipe) < 0) { 740 error("pipe(): %s (%d)", strerror(errno), errno); 741 exit(1); 742 } 743 744 child_io = g_io_channel_unix_new(child_pipe[0]); 745 g_io_channel_set_close_on_unref(child_io, TRUE); 746 g_io_add_watch(child_io, 747 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 748 child_exit, NULL); 749 g_io_channel_unref(child_io); 750 751 agent_init(); 752 753 if (hcid_dbus_init() < 0) { 754 error("Unable to get on D-Bus"); 755 exit(1); 756 } 757 758 start_sdp_server(mtu, main_opts.deviceid, SDP_SERVER_COMPAT); 759 set_service_classes_callback(update_service_classes); 760 761 /* Loading plugins has to be done after D-Bus has been setup since 762 * the plugins might wanna expose some paths on the bus. However the 763 * best order of how to init various subsystems of the Bluetooth 764 * daemon needs to be re-worked. */ 765 plugin_init(config); 766 767 event_loop = g_main_loop_new(NULL, FALSE); 768 769 ctl_io = g_io_channel_unix_new(main_opts.sock); 770 g_io_channel_set_close_on_unref(ctl_io, TRUE); 771 772 g_io_add_watch(ctl_io, G_IO_IN, io_stack_event, NULL); 773 774 g_io_channel_unref(ctl_io); 775 776 /* Initialize already connected devices */ 777 init_all_devices(main_opts.sock); 778 779 g_main_loop_run(event_loop); 780 781 hcid_dbus_unregister(); 782 783 plugin_cleanup(); 784 785 stop_sdp_server(); 786 787 agent_exit(); 788 789 hcid_dbus_exit(); 790 791 g_main_loop_unref(event_loop); 792 793 if (config) 794 g_key_file_free(config); 795 796 info("Exit"); 797 798 stop_logging(); 799 800 return 0; 801} 802