blob: 48f8d54cce2c84bb948c2ad9f9e5b185381d2290 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 <stdarg.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <errno.h>
24
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <sys/un.h>
29
30/* for ANDROID_SOCKET_* */
31#include <cutils/sockets.h>
32
33#include <private/android_filesystem_config.h>
34
35#include "init.h"
36
37static int log_fd = -1;
38/* Inital log level before init.rc is parsed and this this is reset. */
39static int log_level = LOG_DEFAULT_LEVEL;
40
41
42void log_set_level(int level) {
43 log_level = level;
44}
45
46void log_init(void)
47{
48 static const char *name = "/dev/__kmsg__";
49 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
50 log_fd = open(name, O_WRONLY);
51 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
52 unlink(name);
53 }
54}
55
56#define LOG_BUF_MAX 512
57
58void log_write(int level, const char *fmt, ...)
59{
60 char buf[LOG_BUF_MAX];
61 va_list ap;
62
63 if (level > log_level) return;
64 if (log_fd < 0) return;
65
66 va_start(ap, fmt);
67 vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
68 buf[LOG_BUF_MAX - 1] = 0;
69 va_end(ap);
70 write(log_fd, buf, strlen(buf));
71}
72
73/*
74 * android_name_to_id - returns the integer uid/gid associated with the given
75 * name, or -1U on error.
76 */
77static unsigned int android_name_to_id(const char *name)
78{
79 struct android_id_info *info = android_ids;
80 unsigned int n;
81
82 for (n = 0; n < android_id_count; n++) {
83 if (!strcmp(info[n].name, name))
84 return info[n].aid;
85 }
86
87 return -1U;
88}
89
90/*
91 * decode_uid - decodes and returns the given string, which can be either the
92 * numeric or name representation, into the integer uid or gid. Returns -1U on
93 * error.
94 */
95unsigned int decode_uid(const char *s)
96{
97 unsigned int v;
98
99 if (!s || *s == '\0')
100 return -1U;
101 if (isalpha(s[0]))
102 return android_name_to_id(s);
103
104 errno = 0;
105 v = (unsigned int) strtoul(s, 0, 0);
106 if (errno)
107 return -1U;
108 return v;
109}
110
111/*
112 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
113 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
114 * daemon. We communicate the file descriptor's value via the environment
115 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
116 */
117int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid)
118{
119 struct sockaddr_un addr;
120 int fd, ret;
121
122 fd = socket(PF_UNIX, type, 0);
123 if (fd < 0) {
124 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
125 return -1;
126 }
127
128 memset(&addr, 0 , sizeof(addr));
129 addr.sun_family = AF_UNIX;
130 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
131 name);
132
133 ret = unlink(addr.sun_path);
134 if (ret != 0 && errno != ENOENT) {
135 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
136 goto out_close;
137 }
138
139 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
140 if (ret) {
141 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
142 goto out_unlink;
143 }
144
145 chown(addr.sun_path, uid, gid);
146 chmod(addr.sun_path, perm);
147
148 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
149 addr.sun_path, perm, uid, gid);
150
151 return fd;
152
153out_unlink:
154 unlink(addr.sun_path);
155out_close:
156 close(fd);
157 return -1;
158}
159
160/* reads a file, making sure it is terminated with \n \0 */
161void *read_file(const char *fn, unsigned *_sz)
162{
163 char *data;
164 int sz;
165 int fd;
166
167 data = 0;
168 fd = open(fn, O_RDONLY);
169 if(fd < 0) return 0;
170
171 sz = lseek(fd, 0, SEEK_END);
172 if(sz < 0) goto oops;
173
174 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
175
176 data = (char*) malloc(sz + 2);
177 if(data == 0) goto oops;
178
179 if(read(fd, data, sz) != sz) goto oops;
180 close(fd);
181 data[sz] = '\n';
182 data[sz+1] = 0;
183 if(_sz) *_sz = sz;
184 return data;
185
186oops:
187 close(fd);
188 if(data != 0) free(data);
189 return 0;
190}
191
192void list_init(struct listnode *node)
193{
194 node->next = node;
195 node->prev = node;
196}
197
198void list_add_tail(struct listnode *head, struct listnode *item)
199{
200 item->next = head;
201 item->prev = head->prev;
202 head->prev->next = item;
203 head->prev = item;
204}
205
206void list_remove(struct listnode *item)
207{
208 item->next->prev = item->prev;
209 item->prev->next = item->next;
210}
211
Colin Crossf24ed8c2010-04-12 18:51:06 -0700212#define MAX_MTD_PARTITIONS 16
213
214static struct {
215 char name[16];
216 int number;
217} mtd_part_map[MAX_MTD_PARTITIONS];
218
219static int mtd_part_count = -1;
220
221static void find_mtd_partitions(void)
222{
223 int fd;
224 char buf[1024];
225 char *pmtdbufp;
226 ssize_t pmtdsize;
227 int r;
228
229 fd = open("/proc/mtd", O_RDONLY);
230 if (fd < 0)
231 return;
232
233 buf[sizeof(buf) - 1] = '\0';
234 pmtdsize = read(fd, buf, sizeof(buf) - 1);
235 pmtdbufp = buf;
236 while (pmtdsize > 0) {
237 int mtdnum, mtdsize, mtderasesize;
238 char mtdname[16];
239 mtdname[0] = '\0';
240 mtdnum = -1;
241 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
242 &mtdnum, &mtdsize, &mtderasesize, mtdname);
243 if ((r == 4) && (mtdname[0] == '"')) {
244 char *x = strchr(mtdname + 1, '"');
245 if (x) {
246 *x = 0;
247 }
248 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
249 if (mtd_part_count < MAX_MTD_PARTITIONS) {
250 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
251 mtd_part_map[mtd_part_count].number = mtdnum;
252 mtd_part_count++;
253 } else {
254 ERROR("too many mtd partitions\n");
255 }
256 }
257 while (pmtdsize > 0 && *pmtdbufp != '\n') {
258 pmtdbufp++;
259 pmtdsize--;
260 }
261 if (pmtdsize > 0) {
262 pmtdbufp++;
263 pmtdsize--;
264 }
265 }
266 close(fd);
267}
268
269int mtd_name_to_number(const char *name)
270{
271 int n;
272 if (mtd_part_count < 0) {
273 mtd_part_count = 0;
274 find_mtd_partitions();
275 }
276 for (n = 0; n < mtd_part_count; n++) {
277 if (!strcmp(name, mtd_part_map[n].name)) {
278 return mtd_part_map[n].number;
279 }
280 }
281 return -1;
282}