blob: ba9ddb390cf163def4de81f65e937d7c29991c2c [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
2/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <stdlib.h>
19#include <string.h>
20#include <dirent.h>
21#include <errno.h>
22
23#include <sys/types.h>
24
25#include "vold.h"
26#include "switch.h"
27
28#define DEBUG_BOOTSTRAP 0
29
30static int mmc_bootstrap_switch(char *sysfs_path);
31
32int switch_bootstrap()
33{
34 DIR *d;
35 struct dirent *de;
36
37 if (!(d = opendir(SYSFS_CLASS_SWITCH_PATH))) {
38 LOG_ERROR("Unable to open '%s' (%m)", SYSFS_CLASS_SWITCH_PATH);
39 return -errno;
40 }
41
42 while ((de = readdir(d))) {
43 char tmp[255];
44
45 if (de->d_name[0] == '.')
46 continue;
47
48 sprintf(tmp, "%s/%s", SYSFS_CLASS_SWITCH_PATH, de->d_name);
49 if (mmc_bootstrap_switch(tmp))
50 LOG_ERROR("Error bootstrapping switch '%s' (%m)", tmp);
51 }
52
53 closedir(d);
54
55 return 0;
56}
57
58static int mmc_bootstrap_switch(char *sysfs_path)
59{
60#if DEBUG_BOOTSTRAP
61 LOG_VOL("bootstrap_switch(%s):", sysfs_path);
62#endif
63
64 char filename[255];
65 char name[255];
66 char state[255];
67 char tmp[255];
68 char *uevent_params[3];
69 char devpath[255];
70 FILE *fp;
71
72 /*
73 * Read switch name
74 */
75 sprintf(filename, "%s/name", sysfs_path);
76 if (!(fp = fopen(filename, "r"))) {
77 LOGE("Error opening switch name path '%s' (%s)",
78 sysfs_path, strerror(errno));
79 return -errno;
80 }
81 if (!fgets(name, sizeof(name), fp)) {
82 LOGE("Unable to read switch name");
83 fclose(fp);
84 return -EIO;
85 }
86 fclose(fp);
87
88 name[strlen(name) -1] = '\0';
89 sprintf(devpath, "/devices/virtual/switch/%s", name);
90 sprintf(tmp, "SWITCH_NAME=%s", name);
91 uevent_params[0] = (char *) strdup(tmp);
92
93 /*
94 * Read switch state
95 */
96 sprintf(filename, "%s/state", sysfs_path);
97 if (!(fp = fopen(filename, "r"))) {
98 LOGE("Error opening switch state path '%s' (%s)",
99 sysfs_path, strerror(errno));
100 return -errno;
101 }
102 if (!fgets(state, sizeof(state), fp)) {
103 LOGE("Unable to read switch state");
104 fclose(fp);
105 return -EIO;
106 }
107 fclose(fp);
108
109 state[strlen(state) -1] = '\0';
110 sprintf(tmp, "SWITCH_STATE=%s", state);
111 uevent_params[1] = (char *) strdup(tmp);
112
113 uevent_params[2] = (char *) NULL;
114
115 if (simulate_uevent("switch", devpath, "add", uevent_params) < 0) {
116 LOGE("Error simulating uevent (%s)", strerror(errno));
117 return -errno;
118 }
119
120 return 0;
121}