blob: 73b2136fb53364545e3f303a466c0b1022b89e48 [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();
Dima Zavin8f912822011-08-31 18:26:17 -070056 klog_init();
Elliott Hughesda40c002015-03-27 23:20:44 -070057 klog_set_level(KLOG_NOTICE_LEVEL);
Colin Crossf83d0b92010-04-21 12:04:20 -070058
Elliott Hughesda40c002015-03-27 23:20:44 -070059 NOTICE("ueventd started!\n");
60
61 selinux_callback cb;
62 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040063 selinux_set_callback(SELINUX_CB_LOG, cb);
64
Yabin Cui74edcea2015-07-24 10:11:05 -070065 std::string hardware = property_get("ro.hardware");
Colin Cross44b65d02010-04-20 14:32:50 -070066
67 ueventd_parse_config_file("/ueventd.rc");
Yabin Cui74edcea2015-07-24 10:11:05 -070068 ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
Colin Cross44b65d02010-04-20 14:32:50 -070069
Colin Crossf83d0b92010-04-21 12:04:20 -070070 device_init();
71
Elliott Hughesda40c002015-03-27 23:20:44 -070072 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070073 ufd.events = POLLIN;
74 ufd.fd = get_device_fd();
75
Elliott Hughesda40c002015-03-27 23:20:44 -070076 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070077 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070078 int nr = poll(&ufd, 1, -1);
79 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070080 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070081 }
82 if (ufd.revents & POLLIN) {
83 handle_device_fd();
84 }
Colin Crossf83d0b92010-04-21 12:04:20 -070085 }
Elliott Hughes21457792015-02-04 10:19:50 -080086
87 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070088}
Colin Cross44b65d02010-04-20 14:32:50 -070089
Colin Cross44b65d02010-04-20 14:32:50 -070090void set_device_permission(int nargs, char **args)
91{
92 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070094 mode_t perm;
95 uid_t uid;
96 gid_t gid;
97 int prefix = 0;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070098 int wildcard = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070099 char *endptr;
Colin Cross44b65d02010-04-20 14:32:50 -0700100 char *tmp = 0;
101
102 if (nargs == 0)
103 return;
104
105 if (args[0][0] == '#')
106 return;
107
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 name = args[0];
109
110 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
111 INFO("/sys/ rule %s %s\n",args[0],args[1]);
112 attr = args[1];
113 args++;
114 nargs--;
115 }
116
Colin Cross44b65d02010-04-20 14:32:50 -0700117 if (nargs != 4) {
118 ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
119 return;
120 }
121
Colin Cross44b65d02010-04-20 14:32:50 -0700122 /* If path starts with mtd@ lookup the mount number. */
123 if (!strncmp(name, "mtd@", 4)) {
124 int n = mtd_name_to_number(name + 4);
125 if (n >= 0)
126 asprintf(&tmp, "/dev/mtd/mtd%d", n);
127 name = tmp;
128 } else {
129 int len = strlen(name);
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700130 char *wildcard_chr = strchr(name, '*');
131 if ((name[len - 1] == '*') &&
132 (wildcard_chr == (name + len - 1))) {
Colin Cross44b65d02010-04-20 14:32:50 -0700133 prefix = 1;
134 name[len - 1] = '\0';
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700135 } else if (wildcard_chr) {
136 wildcard = 1;
Colin Cross44b65d02010-04-20 14:32:50 -0700137 }
138 }
139
140 perm = strtol(args[1], &endptr, 8);
141 if (!endptr || *endptr != '\0') {
142 ERROR("invalid mode '%s'\n", args[1]);
143 free(tmp);
144 return;
145 }
146
William Roberts5b5a8ac2016-04-06 20:09:24 -0700147 struct passwd* pwd = getpwnam(args[2]);
148 if (!pwd) {
Colin Cross44b65d02010-04-20 14:32:50 -0700149 ERROR("invalid uid '%s'\n", args[2]);
150 free(tmp);
151 return;
152 }
William Roberts5b5a8ac2016-04-06 20:09:24 -0700153 uid = pwd->pw_uid;
Colin Cross44b65d02010-04-20 14:32:50 -0700154
William Roberts5b5a8ac2016-04-06 20:09:24 -0700155 struct group* grp = getgrnam(args[3]);
156 if (!grp) {
Colin Cross44b65d02010-04-20 14:32:50 -0700157 ERROR("invalid gid '%s'\n", args[3]);
158 free(tmp);
159 return;
160 }
William Roberts5b5a8ac2016-04-06 20:09:24 -0700161 gid = grp->gr_gid;
Colin Cross44b65d02010-04-20 14:32:50 -0700162
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700163 add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
Colin Cross44b65d02010-04-20 14:32:50 -0700164 free(tmp);
165}