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