ueventd.c revision 6ebf12fe1bc2de7af4522349973e8bfcc71d6126
1f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross/* 2f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * Copyright (C) 2010 The Android Open Source Project 3f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * 4f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * Licensed under the Apache License, Version 2.0 (the "License"); 5f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * you may not use this file except in compliance with the License. 6f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * You may obtain a copy of the License at 7f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * 8f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * http://www.apache.org/licenses/LICENSE-2.0 9f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * 10f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * Unless required by applicable law or agreed to in writing, software 11f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * distributed under the License is distributed on an "AS IS" BASIS, 12f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * See the License for the specific language governing permissions and 14f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross * limitations under the License. 15f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross */ 16f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 17f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include <poll.h> 1844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <fcntl.h> 1944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <string.h> 2044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <stdlib.h> 2144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <stdio.h> 2244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <ctype.h> 238d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland#include <signal.h> 248d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland 2544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include <private/android_filesystem_config.h> 26f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 27f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include "ueventd.h" 28f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include "log.h" 29f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include "util.h" 30f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include "devices.h" 3144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#include "ueventd_parser.h" 3244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 3344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic char hardware[32]; 3444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic unsigned revision = 0; 35f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 362b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkinestatic void import_kernel_nv(char *name, int in_qemu) 372b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine{ 382b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine if (*name != '\0') { 392b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine char *value = strchr(name, '='); 402b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine if (value != NULL) { 412b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine *value++ = 0; 422b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine if (!strcmp(name,"androidboot.hardware")) 432b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine { 442b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine strlcpy(hardware, value, sizeof(hardware)); 452b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine } 462b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine } 472b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine } 482b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine} 492b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine 50f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Crossint ueventd_main(int argc, char **argv) 51f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross{ 52f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross struct pollfd ufd; 53f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross int nr; 5444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross char tmp[32]; 55f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 566ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich /* 576ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich * init sets the umask to 077 for forked processes. We need to 586ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich * create files with exact permissions, without modification by 596ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich * the umask. 606ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich */ 616ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich umask(000); 626ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich 636ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich /* Prevent fire-and-forget children from becoming zombies. 646ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich * If we should need to wait() for some children in the future 656ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich * (as opposed to none right now), double-forking here instead 666ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich * of ignoring SIGCHLD may be the better solution. 676ebf12fe1bc2de7af4522349973e8bfcc71d6126Nick Kralevich */ 688d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland signal(SIGCHLD, SIG_IGN); 698d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland 70f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross open_devnull_stdio(); 718f91282ebe1963b9d27f8779ad1342302b293bd2Dima Zavin klog_init(); 72f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 73f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross INFO("starting ueventd\n"); 74f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 752b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine /* Respect hardware passed in through the kernel cmd line. Here we will look 762b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine * for androidboot.hardware param in kernel cmdline, and save its value in 772b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine * hardware[]. */ 782b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine import_kernel_cmdline(0, import_kernel_nv); 792b99543cef1b8b0aa8cca39544939910035117b0Vladimir Chtchetkine 8044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross get_hardware_name(hardware, &revision); 8144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 8244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ueventd_parse_config_file("/ueventd.rc"); 8344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 8444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware); 8544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ueventd_parse_config_file(tmp); 8644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 87f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross device_init(); 88f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 89f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross ufd.events = POLLIN; 90f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross ufd.fd = get_device_fd(); 91f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 92f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross while(1) { 93f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross ufd.revents = 0; 94f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross nr = poll(&ufd, 1, -1); 95f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross if (nr <= 0) 96f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross continue; 97f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross if (ufd.revents == POLLIN) 98f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross handle_device_fd(); 99f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross } 100f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross} 10144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 10244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic int get_android_id(const char *id) 10344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross{ 10444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross unsigned int i; 10544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross for (i = 0; i < ARRAY_SIZE(android_ids); i++) 10644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (!strcmp(id, android_ids[i].name)) 10744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return android_ids[i].aid; 10844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return 0; 10944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross} 11044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 11144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossvoid set_device_permission(int nargs, char **args) 11244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross{ 11344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross char *name; 114bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland char *attr = 0; 11544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross mode_t perm; 11644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross uid_t uid; 11744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross gid_t gid; 11844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross int prefix = 0; 11944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross char *endptr; 12044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross int ret; 12144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross char *tmp = 0; 12244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 12344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (nargs == 0) 12444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 12544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 12644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (args[0][0] == '#') 12744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 12844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 129bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland name = args[0]; 130bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 131bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (!strncmp(name,"/sys/", 5) && (nargs == 5)) { 132bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland INFO("/sys/ rule %s %s\n",args[0],args[1]); 133bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland attr = args[1]; 134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland args++; 135bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland nargs--; 136bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland } 137bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 13844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (nargs != 4) { 13944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ERROR("invalid line ueventd.rc line for '%s'\n", args[0]); 14044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 14144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 14244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 14344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross /* If path starts with mtd@ lookup the mount number. */ 14444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (!strncmp(name, "mtd@", 4)) { 14544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross int n = mtd_name_to_number(name + 4); 14644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (n >= 0) 14744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross asprintf(&tmp, "/dev/mtd/mtd%d", n); 14844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross name = tmp; 14944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } else { 15044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross int len = strlen(name); 15144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (name[len - 1] == '*') { 15244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross prefix = 1; 15344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross name[len - 1] = '\0'; 15444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 15544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 15644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 15744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross perm = strtol(args[1], &endptr, 8); 15844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (!endptr || *endptr != '\0') { 15944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ERROR("invalid mode '%s'\n", args[1]); 16044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross free(tmp); 16144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 16244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 16344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 16444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ret = get_android_id(args[2]); 16544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (ret < 0) { 16644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ERROR("invalid uid '%s'\n", args[2]); 16744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross free(tmp); 16844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 16944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 17044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross uid = ret; 17144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 17244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ret = get_android_id(args[3]); 17344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (ret < 0) { 17444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross ERROR("invalid gid '%s'\n", args[3]); 17544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross free(tmp); 17644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 17744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 17844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross gid = ret; 17944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 180bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland add_dev_perms(name, attr, perm, uid, gid, prefix); 18144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross free(tmp); 18244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross} 183