blob: 361b925b85167cacb5b7a084c5eb66557c55ec0b [file] [log] [blame]
Colin Crossf83d0b92010-04-21 12:04:20 -07001/*
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 Cross44b65d02010-04-20 14:32:50 -070017#include <ctype.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070018#include <fcntl.h>
William Roberts5b5a8ac2016-04-06 20:09:24 -070019#include <grp.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070020#include <poll.h>
William Roberts5b5a8ac2016-04-06 20:09:24 -070021#include <pwd.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070022#include <signal.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070026
William Roberts5b5a8ac2016-04-06 20:09:24 -070027#include <sys/types.h>
28
Elliott Hughes4f713192015-12-04 22:00:26 -080029#include <android-base/stringprintf.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070030#include <selinux/selinux.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070031
32#include "ueventd.h"
33#include "log.h"
34#include "util.h"
35#include "devices.h"
Colin Cross44b65d02010-04-20 14:32:50 -070036#include "ueventd_parser.h"
Rom Lemarchand74b34f32015-02-27 17:20:29 -080037#include "property_service.h"
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070038
Colin Crossf83d0b92010-04-21 12:04:20 -070039int ueventd_main(int argc, char **argv)
40{
Nick Kralevich6ebf12f2012-03-26 09:09:11 -070041 /*
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 Swetland8d48c8e2011-03-24 15:45:30 -070053 signal(SIGCHLD, SIG_IGN);
54
Elliott Hughesf86b5a62016-06-24 15:12:21 -070055 InitKernelLogging(argv);
Colin Crossf83d0b92010-04-21 12:04:20 -070056
Elliott Hughesf86b5a62016-06-24 15:12:21 -070057 LOG(INFO) << "ueventd started!";
Elliott Hughesda40c002015-03-27 23:20:44 -070058
59 selinux_callback cb;
60 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040061 selinux_set_callback(SELINUX_CB_LOG, cb);
62
Yabin Cui74edcea2015-07-24 10:11:05 -070063 std::string hardware = property_get("ro.hardware");
Colin Cross44b65d02010-04-20 14:32:50 -070064
65 ueventd_parse_config_file("/ueventd.rc");
Yabin Cui74edcea2015-07-24 10:11:05 -070066 ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
Colin Cross44b65d02010-04-20 14:32:50 -070067
Colin Crossf83d0b92010-04-21 12:04:20 -070068 device_init();
69
Elliott Hughesda40c002015-03-27 23:20:44 -070070 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070071 ufd.events = POLLIN;
72 ufd.fd = get_device_fd();
73
Elliott Hughesda40c002015-03-27 23:20:44 -070074 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070075 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070076 int nr = poll(&ufd, 1, -1);
77 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070078 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070079 }
80 if (ufd.revents & POLLIN) {
81 handle_device_fd();
82 }
Colin Crossf83d0b92010-04-21 12:04:20 -070083 }
Elliott Hughes21457792015-02-04 10:19:50 -080084
85 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070086}
Colin Cross44b65d02010-04-20 14:32:50 -070087
Colin Cross44b65d02010-04-20 14:32:50 -070088void set_device_permission(int nargs, char **args)
89{
90 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070091 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070092 mode_t perm;
93 uid_t uid;
94 gid_t gid;
95 int prefix = 0;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070096 int wildcard = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070097 char *endptr;
Colin Cross44b65d02010-04-20 14:32:50 -070098
99 if (nargs == 0)
100 return;
101
102 if (args[0][0] == '#')
103 return;
104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105 name = args[0];
106
107 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700108 LOG(INFO) << "/sys/ rule " << args[0] << " " << args[1];
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 attr = args[1];
110 args++;
111 nargs--;
112 }
113
Colin Cross44b65d02010-04-20 14:32:50 -0700114 if (nargs != 4) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700115 LOG(ERROR) << "invalid line ueventd.rc line for '" << args[0] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700116 return;
117 }
118
Elliott Hughes31951162016-06-24 15:15:03 -0700119 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 Cross44b65d02010-04-20 14:32:50 -0700126 }
127
128 perm = strtol(args[1], &endptr, 8);
129 if (!endptr || *endptr != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700130 LOG(ERROR) << "invalid mode '" << args[1] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700131 return;
132 }
133
William Roberts5b5a8ac2016-04-06 20:09:24 -0700134 struct passwd* pwd = getpwnam(args[2]);
135 if (!pwd) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700136 LOG(ERROR) << "invalid uid '" << args[2] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700137 return;
138 }
William Roberts5b5a8ac2016-04-06 20:09:24 -0700139 uid = pwd->pw_uid;
Colin Cross44b65d02010-04-20 14:32:50 -0700140
William Roberts5b5a8ac2016-04-06 20:09:24 -0700141 struct group* grp = getgrnam(args[3]);
142 if (!grp) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700143 LOG(ERROR) << "invalid gid '" << args[3] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700144 return;
145 }
William Roberts5b5a8ac2016-04-06 20:09:24 -0700146 gid = grp->gr_gid;
Colin Cross44b65d02010-04-20 14:32:50 -0700147
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800148 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 Cross44b65d02010-04-20 14:32:50 -0700157}