2010-04-22 03:04:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <poll.h>
|
2010-04-21 05:32:50 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
2011-03-25 06:45:30 +08:00
|
|
|
#include <signal.h>
|
2014-06-25 01:45:43 +08:00
|
|
|
#include <selinux/selinux.h>
|
2011-03-25 06:45:30 +08:00
|
|
|
|
2010-04-21 05:32:50 +08:00
|
|
|
#include <private/android_filesystem_config.h>
|
2010-04-22 03:04:20 +08:00
|
|
|
|
|
|
|
#include "ueventd.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "devices.h"
|
2010-04-21 05:32:50 +08:00
|
|
|
#include "ueventd_parser.h"
|
|
|
|
|
|
|
|
static char hardware[32];
|
|
|
|
static unsigned revision = 0;
|
2010-04-22 03:04:20 +08:00
|
|
|
|
2011-09-29 00:55:31 +08:00
|
|
|
static void import_kernel_nv(char *name, int in_qemu)
|
|
|
|
{
|
|
|
|
if (*name != '\0') {
|
|
|
|
char *value = strchr(name, '=');
|
|
|
|
if (value != NULL) {
|
|
|
|
*value++ = 0;
|
|
|
|
if (!strcmp(name,"androidboot.hardware"))
|
|
|
|
{
|
|
|
|
strlcpy(hardware, value, sizeof(hardware));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 03:04:20 +08:00
|
|
|
int ueventd_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct pollfd ufd;
|
|
|
|
int nr;
|
2010-04-21 05:32:50 +08:00
|
|
|
char tmp[32];
|
2010-04-22 03:04:20 +08:00
|
|
|
|
2012-03-27 00:09:11 +08:00
|
|
|
/*
|
|
|
|
* init sets the umask to 077 for forked processes. We need to
|
|
|
|
* create files with exact permissions, without modification by
|
|
|
|
* the umask.
|
|
|
|
*/
|
|
|
|
umask(000);
|
|
|
|
|
|
|
|
/* Prevent fire-and-forget children from becoming zombies.
|
|
|
|
* If we should need to wait() for some children in the future
|
|
|
|
* (as opposed to none right now), double-forking here instead
|
|
|
|
* of ignoring SIGCHLD may be the better solution.
|
|
|
|
*/
|
2011-03-25 06:45:30 +08:00
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2010-04-22 03:04:20 +08:00
|
|
|
open_devnull_stdio();
|
2011-09-01 09:26:17 +08:00
|
|
|
klog_init();
|
2014-03-07 07:07:42 +08:00
|
|
|
#if LOG_UEVENTS
|
|
|
|
/* Ensure we're at a logging level that will show the events */
|
2014-03-20 06:08:22 +08:00
|
|
|
if (klog_get_level() < KLOG_INFO_LEVEL) {
|
|
|
|
klog_set_level(KLOG_INFO_LEVEL);
|
2014-03-07 07:07:42 +08:00
|
|
|
}
|
|
|
|
#endif
|
2010-04-22 03:04:20 +08:00
|
|
|
|
2014-06-25 01:45:43 +08:00
|
|
|
union selinux_callback cb;
|
|
|
|
cb.func_log = log_callback;
|
|
|
|
selinux_set_callback(SELINUX_CB_LOG, cb);
|
|
|
|
|
2010-04-22 03:04:20 +08:00
|
|
|
INFO("starting ueventd\n");
|
|
|
|
|
2011-09-29 00:55:31 +08:00
|
|
|
/* Respect hardware passed in through the kernel cmd line. Here we will look
|
|
|
|
* for androidboot.hardware param in kernel cmdline, and save its value in
|
|
|
|
* hardware[]. */
|
|
|
|
import_kernel_cmdline(0, import_kernel_nv);
|
|
|
|
|
2010-04-21 05:32:50 +08:00
|
|
|
get_hardware_name(hardware, &revision);
|
|
|
|
|
|
|
|
ueventd_parse_config_file("/ueventd.rc");
|
|
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);
|
|
|
|
ueventd_parse_config_file(tmp);
|
|
|
|
|
2010-04-22 03:04:20 +08:00
|
|
|
device_init();
|
|
|
|
|
|
|
|
ufd.events = POLLIN;
|
|
|
|
ufd.fd = get_device_fd();
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
ufd.revents = 0;
|
|
|
|
nr = poll(&ufd, 1, -1);
|
|
|
|
if (nr <= 0)
|
|
|
|
continue;
|
2013-11-10 21:36:58 +08:00
|
|
|
if (ufd.revents & POLLIN)
|
2010-04-22 03:04:20 +08:00
|
|
|
handle_device_fd();
|
|
|
|
}
|
|
|
|
}
|
2010-04-21 05:32:50 +08:00
|
|
|
|
|
|
|
static int get_android_id(const char *id)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(android_ids); i++)
|
|
|
|
if (!strcmp(id, android_ids[i].name))
|
|
|
|
return android_ids[i].aid;
|
2012-08-03 06:20:49 +08:00
|
|
|
return -1;
|
2010-04-21 05:32:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_device_permission(int nargs, char **args)
|
|
|
|
{
|
|
|
|
char *name;
|
2010-10-27 06:09:43 +08:00
|
|
|
char *attr = 0;
|
2010-04-21 05:32:50 +08:00
|
|
|
mode_t perm;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
int prefix = 0;
|
2012-07-03 02:32:30 +08:00
|
|
|
int wildcard = 0;
|
2010-04-21 05:32:50 +08:00
|
|
|
char *endptr;
|
|
|
|
int ret;
|
|
|
|
char *tmp = 0;
|
|
|
|
|
|
|
|
if (nargs == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (args[0][0] == '#')
|
|
|
|
return;
|
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
name = args[0];
|
|
|
|
|
|
|
|
if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
|
|
|
|
INFO("/sys/ rule %s %s\n",args[0],args[1]);
|
|
|
|
attr = args[1];
|
|
|
|
args++;
|
|
|
|
nargs--;
|
|
|
|
}
|
|
|
|
|
2010-04-21 05:32:50 +08:00
|
|
|
if (nargs != 4) {
|
|
|
|
ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If path starts with mtd@ lookup the mount number. */
|
|
|
|
if (!strncmp(name, "mtd@", 4)) {
|
|
|
|
int n = mtd_name_to_number(name + 4);
|
|
|
|
if (n >= 0)
|
|
|
|
asprintf(&tmp, "/dev/mtd/mtd%d", n);
|
|
|
|
name = tmp;
|
|
|
|
} else {
|
|
|
|
int len = strlen(name);
|
2012-07-03 02:32:30 +08:00
|
|
|
char *wildcard_chr = strchr(name, '*');
|
|
|
|
if ((name[len - 1] == '*') &&
|
|
|
|
(wildcard_chr == (name + len - 1))) {
|
2010-04-21 05:32:50 +08:00
|
|
|
prefix = 1;
|
|
|
|
name[len - 1] = '\0';
|
2012-07-03 02:32:30 +08:00
|
|
|
} else if (wildcard_chr) {
|
|
|
|
wildcard = 1;
|
2010-04-21 05:32:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
perm = strtol(args[1], &endptr, 8);
|
|
|
|
if (!endptr || *endptr != '\0') {
|
|
|
|
ERROR("invalid mode '%s'\n", args[1]);
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = get_android_id(args[2]);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR("invalid uid '%s'\n", args[2]);
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uid = ret;
|
|
|
|
|
|
|
|
ret = get_android_id(args[3]);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERROR("invalid gid '%s'\n", args[3]);
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gid = ret;
|
|
|
|
|
2012-07-03 02:32:30 +08:00
|
|
|
add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
|
2010-04-21 05:32:50 +08:00
|
|
|
free(tmp);
|
|
|
|
}
|