| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 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 <poll.h> | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 18 | #include <fcntl.h> | 
|  | 19 | #include <string.h> | 
|  | 20 | #include <stdlib.h> | 
|  | 21 | #include <stdio.h> | 
|  | 22 | #include <ctype.h> | 
| Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 23 | #include <signal.h> | 
| Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 24 | #include <selinux/selinux.h> | 
| Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 25 |  | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | #include "ueventd.h" | 
|  | 29 | #include "log.h" | 
|  | 30 | #include "util.h" | 
|  | 31 | #include "devices.h" | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 32 | #include "ueventd_parser.h" | 
| Rom Lemarchand | 74b34f3 | 2015-02-27 17:20:29 -0800 | [diff] [blame] | 33 | #include "property_service.h" | 
| Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 34 |  | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 35 | int ueventd_main(int argc, char **argv) | 
|  | 36 | { | 
|  | 37 | struct pollfd ufd; | 
|  | 38 | int nr; | 
| Rom Lemarchand | 74b34f3 | 2015-02-27 17:20:29 -0800 | [diff] [blame] | 39 | char hardware[PROP_VALUE_MAX]; | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 40 | char tmp[32]; | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 41 |  | 
| Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 42 | /* | 
|  | 43 | * init sets the umask to 077 for forked processes. We need to | 
|  | 44 | * create files with exact permissions, without modification by | 
|  | 45 | * the umask. | 
|  | 46 | */ | 
|  | 47 | umask(000); | 
|  | 48 |  | 
|  | 49 | /* Prevent fire-and-forget children from becoming zombies. | 
|  | 50 | * If we should need to wait() for some children in the future | 
|  | 51 | * (as opposed to none right now), double-forking here instead | 
|  | 52 | * of ignoring SIGCHLD may be the better solution. | 
|  | 53 | */ | 
| Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 54 | signal(SIGCHLD, SIG_IGN); | 
|  | 55 |  | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 56 | open_devnull_stdio(); | 
| Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 57 | klog_init(); | 
| Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 58 | if (LOG_UEVENTS) { | 
|  | 59 | /* Ensure we're at a logging level that will show the events */ | 
|  | 60 | if (klog_get_level() < KLOG_INFO_LEVEL) { | 
|  | 61 | klog_set_level(KLOG_INFO_LEVEL); | 
|  | 62 | } | 
| Alex Ray | 18ccc1b | 2014-03-06 15:07:42 -0800 | [diff] [blame] | 63 | } | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 64 |  | 
| Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 65 | union selinux_callback cb; | 
|  | 66 | cb.func_log = log_callback; | 
|  | 67 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
|  | 68 |  | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 69 | INFO("starting ueventd\n"); | 
|  | 70 |  | 
| Rom Lemarchand | 74b34f3 | 2015-02-27 17:20:29 -0800 | [diff] [blame] | 71 | property_get("ro.hardware", hardware); | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | ueventd_parse_config_file("/ueventd.rc"); | 
|  | 74 |  | 
|  | 75 | snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware); | 
|  | 76 | ueventd_parse_config_file(tmp); | 
|  | 77 |  | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 78 | device_init(); | 
|  | 79 |  | 
|  | 80 | ufd.events = POLLIN; | 
|  | 81 | ufd.fd = get_device_fd(); | 
|  | 82 |  | 
|  | 83 | while(1) { | 
|  | 84 | ufd.revents = 0; | 
|  | 85 | nr = poll(&ufd, 1, -1); | 
|  | 86 | if (nr <= 0) | 
|  | 87 | continue; | 
| Amir Goldstein | 1d4e86c | 2013-11-10 15:36:58 +0200 | [diff] [blame] | 88 | if (ufd.revents & POLLIN) | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 89 | handle_device_fd(); | 
|  | 90 | } | 
| Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 91 |  | 
|  | 92 | return 0; | 
| Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 93 | } | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | static int get_android_id(const char *id) | 
|  | 96 | { | 
|  | 97 | unsigned int i; | 
|  | 98 | for (i = 0; i < ARRAY_SIZE(android_ids); i++) | 
|  | 99 | if (!strcmp(id, android_ids[i].name)) | 
|  | 100 | return android_ids[i].aid; | 
| Veeren Mandalia | 4f97fd9 | 2012-08-02 15:20:49 -0700 | [diff] [blame] | 101 | return -1; | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | void set_device_permission(int nargs, char **args) | 
|  | 105 | { | 
|  | 106 | char *name; | 
| Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 107 | char *attr = 0; | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 108 | mode_t perm; | 
|  | 109 | uid_t uid; | 
|  | 110 | gid_t gid; | 
|  | 111 | int prefix = 0; | 
| Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 112 | int wildcard = 0; | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 113 | char *endptr; | 
|  | 114 | int ret; | 
|  | 115 | char *tmp = 0; | 
|  | 116 |  | 
|  | 117 | if (nargs == 0) | 
|  | 118 | return; | 
|  | 119 |  | 
|  | 120 | if (args[0][0] == '#') | 
|  | 121 | return; | 
|  | 122 |  | 
| Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 123 | name = args[0]; | 
|  | 124 |  | 
|  | 125 | if (!strncmp(name,"/sys/", 5) && (nargs == 5)) { | 
|  | 126 | INFO("/sys/ rule %s %s\n",args[0],args[1]); | 
|  | 127 | attr = args[1]; | 
|  | 128 | args++; | 
|  | 129 | nargs--; | 
|  | 130 | } | 
|  | 131 |  | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 132 | if (nargs != 4) { | 
|  | 133 | ERROR("invalid line ueventd.rc line for '%s'\n", args[0]); | 
|  | 134 | return; | 
|  | 135 | } | 
|  | 136 |  | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 137 | /* If path starts with mtd@ lookup the mount number. */ | 
|  | 138 | if (!strncmp(name, "mtd@", 4)) { | 
|  | 139 | int n = mtd_name_to_number(name + 4); | 
|  | 140 | if (n >= 0) | 
|  | 141 | asprintf(&tmp, "/dev/mtd/mtd%d", n); | 
|  | 142 | name = tmp; | 
|  | 143 | } else { | 
|  | 144 | int len = strlen(name); | 
| Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 145 | char *wildcard_chr = strchr(name, '*'); | 
|  | 146 | if ((name[len - 1] == '*') && | 
|  | 147 | (wildcard_chr == (name + len - 1))) { | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 148 | prefix = 1; | 
|  | 149 | name[len - 1] = '\0'; | 
| Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 150 | } else if (wildcard_chr) { | 
|  | 151 | wildcard = 1; | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | perm = strtol(args[1], &endptr, 8); | 
|  | 156 | if (!endptr || *endptr != '\0') { | 
|  | 157 | ERROR("invalid mode '%s'\n", args[1]); | 
|  | 158 | free(tmp); | 
|  | 159 | return; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | ret = get_android_id(args[2]); | 
|  | 163 | if (ret < 0) { | 
|  | 164 | ERROR("invalid uid '%s'\n", args[2]); | 
|  | 165 | free(tmp); | 
|  | 166 | return; | 
|  | 167 | } | 
|  | 168 | uid = ret; | 
|  | 169 |  | 
|  | 170 | ret = get_android_id(args[3]); | 
|  | 171 | if (ret < 0) { | 
|  | 172 | ERROR("invalid gid '%s'\n", args[3]); | 
|  | 173 | free(tmp); | 
|  | 174 | return; | 
|  | 175 | } | 
|  | 176 | gid = ret; | 
|  | 177 |  | 
| Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 178 | add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard); | 
| Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 179 | free(tmp); | 
|  | 180 | } |