blob: fffc8a478487bed6df354c503428cfcc211d084e [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>
Colin Cross504bc512010-04-13 19:35:09 -070024#include <time.h>
Nick Kralevichf29c5332013-07-11 15:38:26 -070025#include <ftw.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Stephen Smalleye46f9d52012-01-13 08:48:47 -050027#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050028
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33
34/* for ANDROID_SOCKET_* */
35#include <cutils/sockets.h>
36
37#include <private/android_filesystem_config.h>
38
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070040#include "log.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070041#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043/*
44 * android_name_to_id - returns the integer uid/gid associated with the given
45 * name, or -1U on error.
46 */
47static unsigned int android_name_to_id(const char *name)
48{
Edwin Vanede7f1ad2012-07-26 14:09:13 -040049 const struct android_id_info *info = android_ids;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050 unsigned int n;
51
52 for (n = 0; n < android_id_count; n++) {
53 if (!strcmp(info[n].name, name))
54 return info[n].aid;
55 }
56
57 return -1U;
58}
59
60/*
61 * decode_uid - decodes and returns the given string, which can be either the
62 * numeric or name representation, into the integer uid or gid. Returns -1U on
63 * error.
64 */
65unsigned int decode_uid(const char *s)
66{
67 unsigned int v;
68
69 if (!s || *s == '\0')
70 return -1U;
71 if (isalpha(s[0]))
72 return android_name_to_id(s);
73
74 errno = 0;
75 v = (unsigned int) strtoul(s, 0, 0);
76 if (errno)
77 return -1U;
78 return v;
79}
80
81/*
82 * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
83 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
84 * daemon. We communicate the file descriptor's value via the environment
85 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
86 */
87int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid)
88{
89 struct sockaddr_un addr;
90 int fd, ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050091 char *secon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
93 fd = socket(PF_UNIX, type, 0);
94 if (fd < 0) {
95 ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
96 return -1;
97 }
98
99 memset(&addr, 0 , sizeof(addr));
100 addr.sun_family = AF_UNIX;
101 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
102 name);
103
104 ret = unlink(addr.sun_path);
105 if (ret != 0 && errno != ENOENT) {
106 ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
107 goto out_close;
108 }
109
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500110 secon = NULL;
111 if (sehandle) {
112 ret = selabel_lookup(sehandle, &secon, addr.sun_path, S_IFSOCK);
113 if (ret == 0)
114 setfscreatecon(secon);
115 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500116
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
118 if (ret) {
119 ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
120 goto out_unlink;
121 }
122
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500123 setfscreatecon(NULL);
124 freecon(secon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500125
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 chown(addr.sun_path, uid, gid);
127 chmod(addr.sun_path, perm);
128
129 INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
130 addr.sun_path, perm, uid, gid);
131
132 return fd;
133
134out_unlink:
135 unlink(addr.sun_path);
136out_close:
137 close(fd);
138 return -1;
139}
140
141/* reads a file, making sure it is terminated with \n \0 */
142void *read_file(const char *fn, unsigned *_sz)
143{
144 char *data;
145 int sz;
146 int fd;
Nick Kralevich38f368c2012-01-18 10:39:01 -0800147 struct stat sb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
149 data = 0;
150 fd = open(fn, O_RDONLY);
151 if(fd < 0) return 0;
152
Nick Kralevich38f368c2012-01-18 10:39:01 -0800153 // for security reasons, disallow world-writable
154 // or group-writable files
155 if (fstat(fd, &sb) < 0) {
156 ERROR("fstat failed for '%s'\n", fn);
157 goto oops;
158 }
159 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
160 ERROR("skipping insecure file '%s'\n", fn);
161 goto oops;
162 }
163
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 sz = lseek(fd, 0, SEEK_END);
165 if(sz < 0) goto oops;
166
167 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
168
169 data = (char*) malloc(sz + 2);
170 if(data == 0) goto oops;
171
172 if(read(fd, data, sz) != sz) goto oops;
173 close(fd);
174 data[sz] = '\n';
175 data[sz+1] = 0;
176 if(_sz) *_sz = sz;
177 return data;
178
179oops:
180 close(fd);
181 if(data != 0) free(data);
182 return 0;
183}
184
Colin Crossf24ed8c2010-04-12 18:51:06 -0700185#define MAX_MTD_PARTITIONS 16
186
187static struct {
188 char name[16];
189 int number;
190} mtd_part_map[MAX_MTD_PARTITIONS];
191
192static int mtd_part_count = -1;
193
194static void find_mtd_partitions(void)
195{
196 int fd;
197 char buf[1024];
198 char *pmtdbufp;
199 ssize_t pmtdsize;
200 int r;
201
202 fd = open("/proc/mtd", O_RDONLY);
203 if (fd < 0)
204 return;
205
206 buf[sizeof(buf) - 1] = '\0';
207 pmtdsize = read(fd, buf, sizeof(buf) - 1);
208 pmtdbufp = buf;
209 while (pmtdsize > 0) {
210 int mtdnum, mtdsize, mtderasesize;
211 char mtdname[16];
212 mtdname[0] = '\0';
213 mtdnum = -1;
214 r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
215 &mtdnum, &mtdsize, &mtderasesize, mtdname);
216 if ((r == 4) && (mtdname[0] == '"')) {
217 char *x = strchr(mtdname + 1, '"');
218 if (x) {
219 *x = 0;
220 }
221 INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
222 if (mtd_part_count < MAX_MTD_PARTITIONS) {
223 strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
224 mtd_part_map[mtd_part_count].number = mtdnum;
225 mtd_part_count++;
226 } else {
227 ERROR("too many mtd partitions\n");
228 }
229 }
230 while (pmtdsize > 0 && *pmtdbufp != '\n') {
231 pmtdbufp++;
232 pmtdsize--;
233 }
234 if (pmtdsize > 0) {
235 pmtdbufp++;
236 pmtdsize--;
237 }
238 }
239 close(fd);
240}
241
242int mtd_name_to_number(const char *name)
243{
244 int n;
245 if (mtd_part_count < 0) {
246 mtd_part_count = 0;
247 find_mtd_partitions();
248 }
249 for (n = 0; n < mtd_part_count; n++) {
250 if (!strcmp(name, mtd_part_map[n].name)) {
251 return mtd_part_map[n].number;
252 }
253 }
254 return -1;
255}
Colin Cross504bc512010-04-13 19:35:09 -0700256
257/*
258 * gettime() - returns the time in seconds of the system's monotonic clock or
259 * zero on error.
260 */
261time_t gettime(void)
262{
263 struct timespec ts;
264 int ret;
265
266 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
267 if (ret < 0) {
268 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
269 return 0;
270 }
271
272 return ts.tv_sec;
273}
Colin Crossb0ab94b2010-04-08 16:16:20 -0700274
275int mkdir_recursive(const char *pathname, mode_t mode)
276{
277 char buf[128];
278 const char *slash;
279 const char *p = pathname;
280 int width;
281 int ret;
282 struct stat info;
283
284 while ((slash = strchr(p, '/')) != NULL) {
285 width = slash - pathname;
286 p = slash + 1;
287 if (width < 0)
288 break;
289 if (width == 0)
290 continue;
291 if ((unsigned int)width > sizeof(buf) - 1) {
292 ERROR("path too long for mkdir_recursive\n");
293 return -1;
294 }
295 memcpy(buf, pathname, width);
296 buf[width] = 0;
297 if (stat(buf, &info) != 0) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400298 ret = make_dir(buf, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700299 if (ret && errno != EEXIST)
300 return ret;
301 }
302 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400303 ret = make_dir(pathname, mode);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700304 if (ret && errno != EEXIST)
305 return ret;
306 return 0;
307}
308
309void sanitize(char *s)
310{
311 if (!s)
312 return;
313 while (isalnum(*s))
314 s++;
315 *s = 0;
316}
317void make_link(const char *oldpath, const char *newpath)
318{
319 int ret;
320 char buf[256];
321 char *slash;
322 int width;
323
324 slash = strrchr(newpath, '/');
325 if (!slash)
326 return;
327 width = slash - newpath;
328 if (width <= 0 || width > (int)sizeof(buf) - 1)
329 return;
330 memcpy(buf, newpath, width);
331 buf[width] = 0;
332 ret = mkdir_recursive(buf, 0755);
333 if (ret)
334 ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
335
336 ret = symlink(oldpath, newpath);
337 if (ret && errno != EEXIST)
338 ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
339}
340
341void remove_link(const char *oldpath, const char *newpath)
342{
343 char path[256];
344 ssize_t ret;
345 ret = readlink(newpath, path, sizeof(path) - 1);
346 if (ret <= 0)
347 return;
348 path[ret] = 0;
349 if (!strcmp(path, oldpath))
350 unlink(newpath);
351}
Colin Crosscd0f1732010-04-19 17:10:24 -0700352
353int wait_for_file(const char *filename, int timeout)
354{
355 struct stat info;
356 time_t timeout_time = gettime() + timeout;
357 int ret = -1;
358
359 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
360 usleep(10000);
361
362 return ret;
363}
Colin Crossf83d0b92010-04-21 12:04:20 -0700364
365void open_devnull_stdio(void)
366{
367 int fd;
368 static const char *name = "/dev/__null__";
369 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
370 fd = open(name, O_RDWR);
371 unlink(name);
372 if (fd >= 0) {
373 dup2(fd, 0);
374 dup2(fd, 1);
375 dup2(fd, 2);
376 if (fd > 2) {
377 close(fd);
378 }
379 return;
380 }
381 }
382
383 exit(1);
384}
385
386void get_hardware_name(char *hardware, unsigned int *revision)
387{
388 char data[1024];
389 int fd, n;
390 char *x, *hw, *rev;
391
392 /* Hardware string was provided on kernel command line */
393 if (hardware[0])
394 return;
395
396 fd = open("/proc/cpuinfo", O_RDONLY);
397 if (fd < 0) return;
398
399 n = read(fd, data, 1023);
400 close(fd);
401 if (n < 0) return;
402
403 data[n] = 0;
404 hw = strstr(data, "\nHardware");
405 rev = strstr(data, "\nRevision");
406
407 if (hw) {
408 x = strstr(hw, ": ");
409 if (x) {
410 x += 2;
411 n = 0;
The Android Open Source Project742150c2010-07-16 13:37:09 -0700412 while (*x && *x != '\n') {
413 if (!isspace(*x))
414 hardware[n++] = tolower(*x);
Colin Crossf83d0b92010-04-21 12:04:20 -0700415 x++;
416 if (n == 31) break;
417 }
418 hardware[n] = 0;
419 }
420 }
421
422 if (rev) {
423 x = strstr(rev, ": ");
424 if (x) {
425 *revision = strtoul(x + 2, 0, 16);
426 }
427 }
428}
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700429
430void import_kernel_cmdline(int in_qemu,
431 void (*import_kernel_nv)(char *name, int in_qemu))
432{
433 char cmdline[1024];
434 char *ptr;
435 int fd;
436
437 fd = open("/proc/cmdline", O_RDONLY);
438 if (fd >= 0) {
439 int n = read(fd, cmdline, 1023);
440 if (n < 0) n = 0;
441
442 /* get rid of trailing newline, it happens */
443 if (n > 0 && cmdline[n-1] == '\n') n--;
444
445 cmdline[n] = 0;
446 close(fd);
447 } else {
448 cmdline[0] = 0;
449 }
450
451 ptr = cmdline;
452 while (ptr && *ptr) {
453 char *x = strchr(ptr, ' ');
454 if (x != 0) *x++ = 0;
455 import_kernel_nv(ptr, in_qemu);
456 ptr = x;
457 }
458}
Stephen Smalleye096e362012-06-11 13:37:39 -0400459
460int make_dir(const char *path, mode_t mode)
461{
462 int rc;
463
Stephen Smalleye096e362012-06-11 13:37:39 -0400464 char *secontext = NULL;
465
466 if (sehandle) {
467 selabel_lookup(sehandle, &secontext, path, mode);
468 setfscreatecon(secontext);
469 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400470
471 rc = mkdir(path, mode);
472
Stephen Smalleye096e362012-06-11 13:37:39 -0400473 if (secontext) {
474 int save_errno = errno;
475 freecon(secontext);
476 setfscreatecon(NULL);
477 errno = save_errno;
478 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700479
Stephen Smalleye096e362012-06-11 13:37:39 -0400480 return rc;
481}
482
483int restorecon(const char *pathname)
484{
Stephen Smalleye096e362012-06-11 13:37:39 -0400485 char *secontext = NULL;
486 struct stat sb;
487 int i;
488
489 if (is_selinux_enabled() <= 0 || !sehandle)
490 return 0;
491
492 if (lstat(pathname, &sb) < 0)
493 return -errno;
494 if (selabel_lookup(sehandle, &secontext, pathname, sb.st_mode) < 0)
495 return -errno;
496 if (lsetfilecon(pathname, secontext) < 0) {
497 freecon(secontext);
498 return -errno;
499 }
500 freecon(secontext);
Stephen Smalleye096e362012-06-11 13:37:39 -0400501 return 0;
502}
Nick Kralevichf29c5332013-07-11 15:38:26 -0700503
504static int nftw_restorecon(const char* filename, const struct stat* statptr,
505 int fileflags, struct FTW* pftw)
506{
507 restorecon(filename);
508 return 0;
509}
510
511int restorecon_recursive(const char* pathname)
512{
513 int fd_limit = 20;
514 int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
515 return nftw(pathname, nftw_restorecon, fd_limit, flags);
516}