NetlinkManager.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/select.h>
  20. #include <sys/socket.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <sys/un.h>
  24. #include <linux/netlink.h>
  25. #include <android-base/logging.h>
  26. #include "NetlinkHandler.h"
  27. #include "NetlinkManager.h"
  28. NetlinkManager* NetlinkManager::sInstance = NULL;
  29. NetlinkManager* NetlinkManager::Instance() {
  30. if (!sInstance) sInstance = new NetlinkManager();
  31. return sInstance;
  32. }
  33. NetlinkManager::NetlinkManager() {
  34. mBroadcaster = NULL;
  35. }
  36. NetlinkManager::~NetlinkManager() {}
  37. int NetlinkManager::start() {
  38. struct sockaddr_nl nladdr;
  39. int sz = 64 * 1024;
  40. int on = 1;
  41. memset(&nladdr, 0, sizeof(nladdr));
  42. nladdr.nl_family = AF_NETLINK;
  43. nladdr.nl_pid = getpid();
  44. nladdr.nl_groups = 0xffffffff;
  45. if ((mSock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) < 0) {
  46. PLOG(ERROR) << "Unable to create uevent socket";
  47. return -1;
  48. }
  49. // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
  50. // it will check for the CAP_NET_ADMIN capability in the root namespace.
  51. // Try using SO_RCVBUF if that fails.
  52. if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
  53. (setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
  54. PLOG(ERROR) << "Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option";
  55. goto out;
  56. }
  57. if (setsockopt(mSock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
  58. PLOG(ERROR) << "Unable to set uevent socket SO_PASSCRED option";
  59. goto out;
  60. }
  61. if (bind(mSock, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
  62. PLOG(ERROR) << "Unable to bind uevent socket";
  63. goto out;
  64. }
  65. mHandler = new NetlinkHandler(mSock);
  66. if (mHandler->start()) {
  67. PLOG(ERROR) << "Unable to start NetlinkHandler";
  68. goto out;
  69. }
  70. return 0;
  71. out:
  72. close(mSock);
  73. return -1;
  74. }
  75. int NetlinkManager::stop() {
  76. int status = 0;
  77. if (mHandler->stop()) {
  78. PLOG(ERROR) << "Unable to stop NetlinkHandler";
  79. status = -1;
  80. }
  81. delete mHandler;
  82. mHandler = NULL;
  83. close(mSock);
  84. mSock = -1;
  85. return status;
  86. }