blob: 91f8b1ad286daa95c54b3a6cbbaff62157136003 [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
Colin Crossf83d0b92010-04-21 12:04:20 -070055 open_devnull_stdio();
Elliott Hughesf86b5a62016-06-24 15:12:21 -070056 InitKernelLogging(argv);
Colin Crossf83d0b92010-04-21 12:04:20 -070057
Elliott Hughesf86b5a62016-06-24 15:12:21 -070058 LOG(INFO) << "ueventd started!";
Elliott Hughesda40c002015-03-27 23:20:44 -070059
60 selinux_callback cb;
61 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040062 selinux_set_callback(SELINUX_CB_LOG, cb);
63
Yabin Cui74edcea2015-07-24 10:11:05 -070064 std::string hardware = property_get("ro.hardware");
Colin Cross44b65d02010-04-20 14:32:50 -070065
66 ueventd_parse_config_file("/ueventd.rc");
Yabin Cui74edcea2015-07-24 10:11:05 -070067 ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
Colin Cross44b65d02010-04-20 14:32:50 -070068
Colin Crossf83d0b92010-04-21 12:04:20 -070069 device_init();
70
Elliott Hughesda40c002015-03-27 23:20:44 -070071 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070072 ufd.events = POLLIN;
73 ufd.fd = get_device_fd();
74
Elliott Hughesda40c002015-03-27 23:20:44 -070075 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070076 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070077 int nr = poll(&ufd, 1, -1);
78 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070079 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070080 }
81 if (ufd.revents & POLLIN) {
82 handle_device_fd();
83 }
Colin Crossf83d0b92010-04-21 12:04:20 -070084 }
Elliott Hughes21457792015-02-04 10:19:50 -080085
86 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070087}
Colin Cross44b65d02010-04-20 14:32:50 -070088
Colin Cross44b65d02010-04-20 14:32:50 -070089void set_device_permission(int nargs, char **args)
90{
91 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070092 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070093 mode_t perm;
94 uid_t uid;
95 gid_t gid;
96 int prefix = 0;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070097 int wildcard = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070098 char *endptr;
Colin Cross44b65d02010-04-20 14:32:50 -070099 char *tmp = 0;
100
101 if (nargs == 0)
102 return;
103
104 if (args[0][0] == '#')
105 return;
106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 name = args[0];
108
109 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700110 LOG(INFO) << "/sys/ rule " << args[0] << " " << args[1];
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700111 attr = args[1];
112 args++;
113 nargs--;
114 }
115
Colin Cross44b65d02010-04-20 14:32:50 -0700116 if (nargs != 4) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700117 LOG(ERROR) << "invalid line ueventd.rc line for '" << args[0] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700118 return;
119 }
120
Elliott Hughes31951162016-06-24 15:15:03 -0700121 int len = strlen(name);
122 char *wildcard_chr = strchr(name, '*');
123 if ((name[len - 1] == '*') && (wildcard_chr == (name + len - 1))) {
124 prefix = 1;
125 name[len - 1] = '\0';
126 } else if (wildcard_chr) {
127 wildcard = 1;
Colin Cross44b65d02010-04-20 14:32:50 -0700128 }
129
130 perm = strtol(args[1], &endptr, 8);
131 if (!endptr || *endptr != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700132 LOG(ERROR) << "invalid mode '" << args[1] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700133 free(tmp);
134 return;
135 }
136
William Roberts5b5a8ac2016-04-06 20:09:24 -0700137 struct passwd* pwd = getpwnam(args[2]);
138 if (!pwd) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700139 LOG(ERROR) << "invalid uid '" << args[2] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700140 free(tmp);
141 return;
142 }
William Roberts5b5a8ac2016-04-06 20:09:24 -0700143 uid = pwd->pw_uid;
Colin Cross44b65d02010-04-20 14:32:50 -0700144
William Roberts5b5a8ac2016-04-06 20:09:24 -0700145 struct group* grp = getgrnam(args[3]);
146 if (!grp) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700147 LOG(ERROR) << "invalid gid '" << args[3] << "'";
Colin Cross44b65d02010-04-20 14:32:50 -0700148 free(tmp);
149 return;
150 }
William Roberts5b5a8ac2016-04-06 20:09:24 -0700151 gid = grp->gr_gid;
Colin Cross44b65d02010-04-20 14:32:50 -0700152
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700153 add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
Colin Cross44b65d02010-04-20 14:32:50 -0700154 free(tmp);
155}