San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 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 | */ |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 16 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | #include <malloc.h> |
| 23 | #include <errno.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #define LOG_TAG "Controller" |
| 29 | |
| 30 | #include <cutils/log.h> |
| 31 | |
| 32 | #include "Controller.h" |
| 33 | |
| 34 | extern "C" int init_module(void *, unsigned int, const char *); |
| 35 | extern "C" int delete_module(const char *, unsigned int); |
| 36 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 37 | Controller::Controller(const char *name, PropertyManager *propMngr, |
| 38 | IControllerHandler *handlers) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 39 | mPropMngr = propMngr; |
| 40 | mName = strdup(name); |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 41 | mHandlers = handlers; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 42 | mBoundInterface = NULL; |
| 43 | } |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 44 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 45 | Controller::~Controller() { |
| 46 | if (mBoundInterface) |
| 47 | free(mBoundInterface); |
| 48 | if (mName) |
| 49 | free(mName); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int Controller::start() { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | int Controller::stop() { |
| 57 | return 0; |
| 58 | } |
| 59 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 60 | int Controller::set(const char *name, const char *value) { |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 61 | errno = ENOENT; |
| 62 | return -1; |
| 63 | } |
| 64 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 65 | const char *Controller::get(const char *name, char *buffer, size_t maxsize) { |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 66 | errno = ENOENT; |
| 67 | return NULL; |
| 68 | } |
| 69 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 70 | int Controller::loadKernelModule(char *modpath, const char *args) { |
| 71 | void *module; |
| 72 | unsigned int size; |
| 73 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 74 | module = loadFile(modpath, &size); |
| 75 | if (!module) { |
| 76 | errno = -EIO; |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | int rc = init_module(module, size, args); |
| 81 | free (module); |
| 82 | return rc; |
| 83 | } |
| 84 | |
| 85 | int Controller::unloadKernelModule(const char *modtag) { |
| 86 | int rc = -1; |
| 87 | int retries = 10; |
| 88 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 89 | while (retries--) { |
| 90 | rc = delete_module(modtag, O_NONBLOCK | O_EXCL); |
| 91 | if (rc < 0 && errno == EAGAIN) |
| 92 | usleep(1000*500); |
| 93 | else |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | if (rc != 0) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 98 | LOGW("Unable to unload kernel driver '%s' (%s)", modtag, |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 99 | strerror(errno)); |
| 100 | } |
| 101 | return rc; |
| 102 | } |
| 103 | |
| 104 | bool Controller::isKernelModuleLoaded(const char *modtag) { |
| 105 | FILE *fp = fopen("/proc/modules", "r"); |
| 106 | |
| 107 | if (!fp) { |
| 108 | LOGE("Unable to open /proc/modules (%s)", strerror(errno)); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | char line[255]; |
| 113 | while(fgets(line, sizeof(line), fp)) { |
| 114 | char *endTag = strchr(line, ' '); |
| 115 | |
| 116 | if (!endTag) { |
| 117 | LOGW("Unable to find tag for line '%s'", line); |
| 118 | continue; |
| 119 | } |
| 120 | if (!strncmp(line, modtag, (endTag - line))) { |
| 121 | fclose(fp); |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | fclose(fp); |
| 127 | return false; |
| 128 | } |
| 129 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 130 | void *Controller::loadFile(char *filename, unsigned int *_size) |
| 131 | { |
| 132 | int ret, fd; |
| 133 | struct stat sb; |
| 134 | ssize_t size; |
| 135 | void *buffer = NULL; |
| 136 | |
| 137 | /* open the file */ |
| 138 | fd = open(filename, O_RDONLY); |
| 139 | if (fd < 0) |
| 140 | return NULL; |
| 141 | |
| 142 | /* find out how big it is */ |
| 143 | if (fstat(fd, &sb) < 0) |
| 144 | goto bail; |
| 145 | size = sb.st_size; |
| 146 | |
| 147 | /* allocate memory for it to be read into */ |
| 148 | buffer = malloc(size); |
| 149 | if (!buffer) |
| 150 | goto bail; |
| 151 | |
| 152 | /* slurp it into our buffer */ |
| 153 | ret = read(fd, buffer, size); |
| 154 | if (ret != size) |
| 155 | goto bail; |
| 156 | |
| 157 | /* let the caller know how big it is */ |
| 158 | *_size = size; |
| 159 | |
| 160 | bail: |
| 161 | close(fd); |
| 162 | return buffer; |
| 163 | } |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 164 | |
| 165 | int Controller::bindInterface(const char *ifname) { |
| 166 | mBoundInterface = strdup(ifname); |
| 167 | LOGD("Controller %s bound to %s", mName, ifname); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int Controller::unbindInterface(const char *ifname) { |
| 172 | free(mBoundInterface); |
| 173 | mBoundInterface = NULL; |
| 174 | LOGD("Controller %s unbound from %s", mName, ifname); |
| 175 | return 0; |
| 176 | } |