2010-04-21 21:04:20 +02: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.
|
|
|
|
*/
|
|
|
|
|
2010-04-20 23:32:50 +02:00
|
|
|
#include <ctype.h>
|
2015-03-28 07:20:44 +01:00
|
|
|
#include <fcntl.h>
|
2016-04-07 05:09:24 +02:00
|
|
|
#include <grp.h>
|
2015-03-28 07:20:44 +01:00
|
|
|
#include <poll.h>
|
2016-04-07 05:09:24 +02:00
|
|
|
#include <pwd.h>
|
2011-03-24 23:45:30 +01:00
|
|
|
#include <signal.h>
|
2015-03-28 07:20:44 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-03-24 23:45:30 +01:00
|
|
|
|
2016-04-07 05:09:24 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-03-28 07:20:44 +01:00
|
|
|
#include <selinux/selinux.h>
|
2010-04-21 21:04:20 +02:00
|
|
|
|
|
|
|
#include "ueventd.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "devices.h"
|
2010-04-20 23:32:50 +02:00
|
|
|
#include "ueventd_parser.h"
|
2015-02-28 02:20:29 +01:00
|
|
|
#include "property_service.h"
|
2011-09-28 18:55:31 +02:00
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
int ueventd_main(int argc, char **argv)
|
|
|
|
{
|
2012-03-26 18:09:11 +02: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-24 23:45:30 +01:00
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
InitKernelLogging(argv);
|
2010-04-21 21:04:20 +02:00
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "ueventd started!";
|
2014-06-24 19:45:43 +02:00
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
selinux_callback cb;
|
|
|
|
cb.func_log = selinux_klog_callback;
|
|
|
|
selinux_set_callback(SELINUX_CB_LOG, cb);
|
2010-04-21 21:04:20 +02:00
|
|
|
|
2010-04-20 23:32:50 +02:00
|
|
|
ueventd_parse_config_file("/ueventd.rc");
|
2017-02-03 16:18:36 +01:00
|
|
|
ueventd_parse_config_file("/vendor/ueventd.rc");
|
|
|
|
ueventd_parse_config_file("/odm/ueventd.rc");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* keep the current product name base configuration so
|
|
|
|
* we remain backwards compatible and allow it to override
|
|
|
|
* everything
|
|
|
|
* TODO: cleanup platform ueventd.rc to remove vendor specific
|
|
|
|
* device node entries (b/34968103)
|
|
|
|
*/
|
|
|
|
std::string hardware = property_get("ro.hardware");
|
2015-07-24 19:11:05 +02:00
|
|
|
ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
|
2010-04-20 23:32:50 +02:00
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
device_init();
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
pollfd ufd;
|
2010-04-21 21:04:20 +02:00
|
|
|
ufd.events = POLLIN;
|
|
|
|
ufd.fd = get_device_fd();
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
while (true) {
|
2010-04-21 21:04:20 +02:00
|
|
|
ufd.revents = 0;
|
2015-03-28 07:20:44 +01:00
|
|
|
int nr = poll(&ufd, 1, -1);
|
|
|
|
if (nr <= 0) {
|
2010-04-21 21:04:20 +02:00
|
|
|
continue;
|
2015-03-28 07:20:44 +01:00
|
|
|
}
|
|
|
|
if (ufd.revents & POLLIN) {
|
|
|
|
handle_device_fd();
|
|
|
|
}
|
2010-04-21 21:04:20 +02:00
|
|
|
}
|
2015-02-04 19:19:50 +01:00
|
|
|
|
|
|
|
return 0;
|
2010-04-21 21:04:20 +02:00
|
|
|
}
|
2010-04-20 23:32:50 +02:00
|
|
|
|
|
|
|
void set_device_permission(int nargs, char **args)
|
|
|
|
{
|
|
|
|
char *name;
|
2010-10-27 00:09:43 +02:00
|
|
|
char *attr = 0;
|
2010-04-20 23:32:50 +02:00
|
|
|
mode_t perm;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
int prefix = 0;
|
2012-07-02 20:32:30 +02:00
|
|
|
int wildcard = 0;
|
2010-04-20 23:32:50 +02:00
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (nargs == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (args[0][0] == '#')
|
|
|
|
return;
|
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
name = args[0];
|
|
|
|
|
|
|
|
if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "/sys/ rule " << args[0] << " " << args[1];
|
2010-10-27 00:09:43 +02:00
|
|
|
attr = args[1];
|
|
|
|
args++;
|
|
|
|
nargs--;
|
|
|
|
}
|
|
|
|
|
2010-04-20 23:32:50 +02:00
|
|
|
if (nargs != 4) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "invalid line ueventd.rc line for '" << args[0] << "'";
|
2010-04-20 23:32:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-25 00:15:03 +02:00
|
|
|
int len = strlen(name);
|
|
|
|
char *wildcard_chr = strchr(name, '*');
|
|
|
|
if ((name[len - 1] == '*') && (wildcard_chr == (name + len - 1))) {
|
|
|
|
prefix = 1;
|
|
|
|
name[len - 1] = '\0';
|
|
|
|
} else if (wildcard_chr) {
|
|
|
|
wildcard = 1;
|
2010-04-20 23:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
perm = strtol(args[1], &endptr, 8);
|
|
|
|
if (!endptr || *endptr != '\0') {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "invalid mode '" << args[1] << "'";
|
2010-04-20 23:32:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 05:09:24 +02:00
|
|
|
struct passwd* pwd = getpwnam(args[2]);
|
|
|
|
if (!pwd) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "invalid uid '" << args[2] << "'";
|
2010-04-20 23:32:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-07 05:09:24 +02:00
|
|
|
uid = pwd->pw_uid;
|
2010-04-20 23:32:50 +02:00
|
|
|
|
2016-04-07 05:09:24 +02:00
|
|
|
struct group* grp = getgrnam(args[3]);
|
|
|
|
if (!grp) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "invalid gid '" << args[3] << "'";
|
2010-04-20 23:32:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-07 05:09:24 +02:00
|
|
|
gid = grp->gr_gid;
|
2010-04-20 23:32:50 +02:00
|
|
|
|
2016-11-16 00:35:16 +01:00
|
|
|
if (add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard) != 0) {
|
|
|
|
PLOG(ERROR) << "add_dev_perms(name=" << name <<
|
|
|
|
", attr=" << attr <<
|
|
|
|
", perm=" << std::oct << perm << std::dec <<
|
|
|
|
", uid=" << uid << ", gid=" << gid <<
|
|
|
|
", prefix=" << prefix << ", wildcard=" << wildcard <<
|
|
|
|
")";
|
|
|
|
return;
|
|
|
|
}
|
2010-04-20 23:32:50 +02:00
|
|
|
}
|