blob: 9d4ff3c65b7d39a56ae476517944b70f01c9b4be [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
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 Mehat3c5a6f02009-05-22 15:36:13 -070016
San Mehatdc266072009-05-06 11:16:52 -070017#include <stdio.h>
San Mehat48765672009-05-20 15:28:43 -070018#include <stdlib.h>
San Mehatdc266072009-05-06 11:16:52 -070019#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
34extern "C" int init_module(void *, unsigned int, const char *);
35extern "C" int delete_module(const char *, unsigned int);
36
San Mehat3c5a6f02009-05-22 15:36:13 -070037Controller::Controller(const char *name, PropertyManager *propMngr) {
38 mPropMngr = propMngr;
39 mName = strdup(name);
40 mBoundInterface = NULL;
41}
San Mehat48765672009-05-20 15:28:43 -070042
San Mehat3c5a6f02009-05-22 15:36:13 -070043Controller::~Controller() {
44 if (mBoundInterface)
45 free(mBoundInterface);
46 if (mName)
47 free(mName);
San Mehatdc266072009-05-06 11:16:52 -070048}
49
50int Controller::start() {
51 return 0;
52}
53
54int Controller::stop() {
55 return 0;
56}
57
San Mehat3c5a6f02009-05-22 15:36:13 -070058int Controller::set(const char *name, const char *value) {
San Mehat48765672009-05-20 15:28:43 -070059 errno = ENOENT;
60 return -1;
61}
62
San Mehat3c5a6f02009-05-22 15:36:13 -070063const char *Controller::get(const char *name, char *buffer, size_t maxsize) {
San Mehat48765672009-05-20 15:28:43 -070064 errno = ENOENT;
65 return NULL;
66}
67
San Mehatdc266072009-05-06 11:16:52 -070068int Controller::loadKernelModule(char *modpath, const char *args) {
69 void *module;
70 unsigned int size;
71
San Mehatdc266072009-05-06 11:16:52 -070072 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
83int Controller::unloadKernelModule(const char *modtag) {
84 int rc = -1;
85 int retries = 10;
86
San Mehatdc266072009-05-06 11:16:52 -070087 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 Mehat3c5a6f02009-05-22 15:36:13 -070096 LOGW("Unable to unload kernel driver '%s' (%s)", modtag,
San Mehatdc266072009-05-06 11:16:52 -070097 strerror(errno));
98 }
99 return rc;
100}
101
102bool 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 Mehatdc266072009-05-06 11:16:52 -0700128void *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
158bail:
159 close(fd);
160 return buffer;
161}
San Mehat3c5a6f02009-05-22 15:36:13 -0700162
163int Controller::bindInterface(const char *ifname) {
164 mBoundInterface = strdup(ifname);
165 LOGD("Controller %s bound to %s", mName, ifname);
166 return 0;
167}
168
169int Controller::unbindInterface(const char *ifname) {
170 free(mBoundInterface);
171 mBoundInterface = NULL;
172 LOGD("Controller %s unbound from %s", mName, ifname);
173 return 0;
174}