blob: cc1a18730ba0bc729cb0a66aaf29e9e07370b38f [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 */
16#include <stdio.h>
17#include <string.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <malloc.h>
21#include <errno.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26#define LOG_TAG "Controller"
27
28#include <cutils/log.h>
29
30#include "Controller.h"
31
32extern "C" int init_module(void *, unsigned int, const char *);
33extern "C" int delete_module(const char *, unsigned int);
34
35Controller::Controller(const char *name) {
36 mName = name;
37}
38
39int Controller::start() {
40 return 0;
41}
42
43int Controller::stop() {
44 return 0;
45}
46
47int Controller::loadKernelModule(char *modpath, const char *args) {
48 void *module;
49 unsigned int size;
50
51 LOGD("loadKernelModule(%s, %s)", modpath, args);
52
53 module = loadFile(modpath, &size);
54 if (!module) {
55 errno = -EIO;
56 return -1;
57 }
58
59 int rc = init_module(module, size, args);
60 free (module);
61 return rc;
62}
63
64int Controller::unloadKernelModule(const char *modtag) {
65 int rc = -1;
66 int retries = 10;
67
68 LOGD("unloadKernelModule(%s)", modtag);
69 while (retries--) {
70 rc = delete_module(modtag, O_NONBLOCK | O_EXCL);
71 if (rc < 0 && errno == EAGAIN)
72 usleep(1000*500);
73 else
74 break;
75 }
76
77 if (rc != 0) {
78 LOGW("Unable to unload kernel driver '%s' (%s)", modtag,
79 strerror(errno));
80 }
81 return rc;
82}
83
84bool Controller::isKernelModuleLoaded(const char *modtag) {
85 FILE *fp = fopen("/proc/modules", "r");
86
87 if (!fp) {
88 LOGE("Unable to open /proc/modules (%s)", strerror(errno));
89 return false;
90 }
91
92 char line[255];
93 while(fgets(line, sizeof(line), fp)) {
94 char *endTag = strchr(line, ' ');
95
96 if (!endTag) {
97 LOGW("Unable to find tag for line '%s'", line);
98 continue;
99 }
100 if (!strncmp(line, modtag, (endTag - line))) {
101 fclose(fp);
102 return true;
103 }
104 }
105
106 fclose(fp);
107 return false;
108}
109
110
111void *Controller::loadFile(char *filename, unsigned int *_size)
112{
113 int ret, fd;
114 struct stat sb;
115 ssize_t size;
116 void *buffer = NULL;
117
118 /* open the file */
119 fd = open(filename, O_RDONLY);
120 if (fd < 0)
121 return NULL;
122
123 /* find out how big it is */
124 if (fstat(fd, &sb) < 0)
125 goto bail;
126 size = sb.st_size;
127
128 /* allocate memory for it to be read into */
129 buffer = malloc(size);
130 if (!buffer)
131 goto bail;
132
133 /* slurp it into our buffer */
134 ret = read(fd, buffer, size);
135 if (ret != size)
136 goto bail;
137
138 /* let the caller know how big it is */
139 *_size = size;
140
141bail:
142 close(fd);
143 return buffer;
144}