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
17 #include <stdio.h>
18 #include <stdlib.h>
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
Controller(const char * name,PropertyManager * propMngr,IControllerHandler * handlers)37 Controller::Controller(const char *name, PropertyManager *propMngr,
38 IControllerHandler *handlers) {
39 mPropMngr = propMngr;
40 mName = strdup(name);
41 mHandlers = handlers;
42 mBoundInterface = NULL;
43 }
44
~Controller()45 Controller::~Controller() {
46 if (mBoundInterface)
47 free(mBoundInterface);
48 if (mName)
49 free(mName);
50 }
51
start()52 int Controller::start() {
53 return 0;
54 }
55
stop()56 int Controller::stop() {
57 return 0;
58 }
59
set(const char * name,const char * value)60 int Controller::set(const char *name, const char *value) {
61 errno = ENOENT;
62 return -1;
63 }
64
get(const char * name,char * buffer,size_t maxsize)65 const char *Controller::get(const char *name, char *buffer, size_t maxsize) {
66 errno = ENOENT;
67 return NULL;
68 }
69
loadKernelModule(char * modpath,const char * args)70 int Controller::loadKernelModule(char *modpath, const char *args) {
71 void *module;
72 unsigned int size;
73
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
unloadKernelModule(const char * modtag)85 int Controller::unloadKernelModule(const char *modtag) {
86 int rc = -1;
87 int retries = 10;
88
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) {
98 LOGW("Unable to unload kernel driver '%s' (%s)", modtag,
99 strerror(errno));
100 }
101 return rc;
102 }
103
isKernelModuleLoaded(const char * modtag)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
loadFile(char * filename,unsigned int * _size)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 }
164
bindInterface(const char * ifname)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
unbindInterface(const char * ifname)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 }
177