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 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 19 | #include <grp.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <poll.h> |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 21 | #include <pwd.h> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 22 | #include <signal.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 26 | |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 30 | #include <selinux/selinux.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 31 | |
| 32 | #include "ueventd.h" |
| 33 | #include "log.h" |
| 34 | #include "util.h" |
| 35 | #include "devices.h" |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 36 | #include "ueventd_parser.h" |
Rom Lemarchand | 74b34f3 | 2015-02-27 17:20:29 -0800 | [diff] [blame] | 37 | #include "property_service.h" |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 38 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 39 | int ueventd_main(int argc, char **argv) |
| 40 | { |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 41 | /* |
| 42 | * init sets the umask to 077 for forked processes. We need to |
| 43 | * create files with exact permissions, without modification by |
| 44 | * the umask. |
| 45 | */ |
| 46 | umask(000); |
| 47 | |
| 48 | /* Prevent fire-and-forget children from becoming zombies. |
| 49 | * If we should need to wait() for some children in the future |
| 50 | * (as opposed to none right now), double-forking here instead |
| 51 | * of ignoring SIGCHLD may be the better solution. |
| 52 | */ |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 53 | signal(SIGCHLD, SIG_IGN); |
| 54 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 55 | InitKernelLogging(argv); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 57 | LOG(INFO) << "ueventd started!"; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 58 | |
| 59 | selinux_callback cb; |
| 60 | cb.func_log = selinux_klog_callback; |
Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 61 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 62 | |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 63 | std::string hardware = property_get("ro.hardware"); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 64 | |
| 65 | ueventd_parse_config_file("/ueventd.rc"); |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 66 | ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str()); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 67 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 68 | device_init(); |
| 69 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 70 | pollfd ufd; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 71 | ufd.events = POLLIN; |
| 72 | ufd.fd = get_device_fd(); |
| 73 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 74 | while (true) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 75 | ufd.revents = 0; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 76 | int nr = poll(&ufd, 1, -1); |
| 77 | if (nr <= 0) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 78 | continue; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 79 | } |
| 80 | if (ufd.revents & POLLIN) { |
| 81 | handle_device_fd(); |
| 82 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 83 | } |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 84 | |
| 85 | return 0; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 86 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 87 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 88 | void set_device_permission(int nargs, char **args) |
| 89 | { |
| 90 | char *name; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 91 | char *attr = 0; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 92 | mode_t perm; |
| 93 | uid_t uid; |
| 94 | gid_t gid; |
| 95 | int prefix = 0; |
Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 96 | int wildcard = 0; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 97 | char *endptr; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 98 | |
| 99 | if (nargs == 0) |
| 100 | return; |
| 101 | |
| 102 | if (args[0][0] == '#') |
| 103 | return; |
| 104 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 105 | name = args[0]; |
| 106 | |
| 107 | if (!strncmp(name,"/sys/", 5) && (nargs == 5)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 108 | LOG(INFO) << "/sys/ rule " << args[0] << " " << args[1]; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 109 | attr = args[1]; |
| 110 | args++; |
| 111 | nargs--; |
| 112 | } |
| 113 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 114 | if (nargs != 4) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 115 | LOG(ERROR) << "invalid line ueventd.rc line for '" << args[0] << "'"; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
Elliott Hughes | 3195116 | 2016-06-24 15:15:03 -0700 | [diff] [blame] | 119 | int len = strlen(name); |
| 120 | char *wildcard_chr = strchr(name, '*'); |
| 121 | if ((name[len - 1] == '*') && (wildcard_chr == (name + len - 1))) { |
| 122 | prefix = 1; |
| 123 | name[len - 1] = '\0'; |
| 124 | } else if (wildcard_chr) { |
| 125 | wildcard = 1; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | perm = strtol(args[1], &endptr, 8); |
| 129 | if (!endptr || *endptr != '\0') { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 130 | LOG(ERROR) << "invalid mode '" << args[1] << "'"; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 134 | struct passwd* pwd = getpwnam(args[2]); |
| 135 | if (!pwd) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 136 | LOG(ERROR) << "invalid uid '" << args[2] << "'"; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 139 | uid = pwd->pw_uid; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 140 | |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 141 | struct group* grp = getgrnam(args[3]); |
| 142 | if (!grp) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 143 | LOG(ERROR) << "invalid gid '" << args[3] << "'"; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 144 | return; |
| 145 | } |
William Roberts | 5b5a8ac | 2016-04-06 20:09:24 -0700 | [diff] [blame] | 146 | gid = grp->gr_gid; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 147 | |
Ting-Yuan Huang | 09bd41d | 2016-11-15 15:35:16 -0800 | [diff] [blame] | 148 | if (add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard) != 0) { |
| 149 | PLOG(ERROR) << "add_dev_perms(name=" << name << |
| 150 | ", attr=" << attr << |
| 151 | ", perm=" << std::oct << perm << std::dec << |
| 152 | ", uid=" << uid << ", gid=" << gid << |
| 153 | ", prefix=" << prefix << ", wildcard=" << wildcard << |
| 154 | ")"; |
| 155 | return; |
| 156 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 157 | } |