blob: f74c878495a1818437ee0df61f02cf45aea5f5c9 [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
2 * Copyright (C) 2017 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
17#include "ueventd_parser.h"
18
19#include <grp.h>
20#include <pwd.h>
21
22#include "keyword_map.h"
23
Tom Cherry81f5d3e2017-06-22 12:53:17 -070024namespace android {
25namespace init {
26
Tom Cherry89bcc852017-08-02 17:01:36 -070027Result<Success> ParsePermissionsLine(std::vector<std::string>&& args,
28 std::vector<SysfsPermissions>* out_sysfs_permissions,
29 std::vector<Permissions>* out_dev_permissions) {
Tom Cherryed506f72017-05-25 15:58:59 -070030 bool is_sysfs = out_sysfs_permissions != nullptr;
31 if (is_sysfs && args.size() != 5) {
Tom Cherry89bcc852017-08-02 17:01:36 -070032 return Error() << "/sys/ lines must have 5 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070033 }
34
35 if (!is_sysfs && args.size() != 4) {
Tom Cherry89bcc852017-08-02 17:01:36 -070036 return Error() << "/dev/ lines must have 4 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070037 }
38
39 auto it = args.begin();
40 const std::string& name = *it++;
41
42 std::string sysfs_attribute;
43 if (is_sysfs) sysfs_attribute = *it++;
44
45 // args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
46 std::string& perm_string = *it++;
47 char* end_pointer = 0;
48 mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
49 if (end_pointer == nullptr || *end_pointer != '\0') {
Tom Cherry89bcc852017-08-02 17:01:36 -070050 return Error() << "invalid mode '" << perm_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070051 }
52
53 std::string& uid_string = *it++;
54 passwd* pwd = getpwnam(uid_string.c_str());
55 if (!pwd) {
Tom Cherry89bcc852017-08-02 17:01:36 -070056 return Error() << "invalid uid '" << uid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070057 }
58 uid_t uid = pwd->pw_uid;
59
60 std::string& gid_string = *it++;
61 struct group* grp = getgrnam(gid_string.c_str());
62 if (!grp) {
Tom Cherry89bcc852017-08-02 17:01:36 -070063 return Error() << "invalid gid '" << gid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070064 }
65 gid_t gid = grp->gr_gid;
66
67 if (is_sysfs) {
68 out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid);
69 } else {
70 out_dev_permissions->emplace_back(name, perm, uid, gid);
71 }
Tom Cherry89bcc852017-08-02 17:01:36 -070072 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -070073}
74
Tom Cherry89bcc852017-08-02 17:01:36 -070075Result<Success> SubsystemParser::ParseSection(std::vector<std::string>&& args,
76 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -070077 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -070078 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -070079 }
80
81 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -070082 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -070083 }
84
Tom Cherry9c8d6dd2017-08-17 09:38:01 -070085 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -070086
Tom Cherry89bcc852017-08-02 17:01:36 -070087 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -070088}
89
Tom Cherry89bcc852017-08-02 17:01:36 -070090Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -070091 if (args[1] == "uevent_devname") {
92 subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVNAME;
Tom Cherry89bcc852017-08-02 17:01:36 -070093 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -070094 }
95 if (args[1] == "uevent_devpath") {
96 subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVPATH;
Tom Cherry89bcc852017-08-02 17:01:36 -070097 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -070098 }
99
Tom Cherry89bcc852017-08-02 17:01:36 -0700100 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700101}
102
Tom Cherry89bcc852017-08-02 17:01:36 -0700103Result<Success> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700104 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700105 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700106 }
107
108 subsystem_.dir_name_ = args[1];
Tom Cherry89bcc852017-08-02 17:01:36 -0700109 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700110}
111
Tom Cherry89bcc852017-08-02 17:01:36 -0700112Result<Success> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
113 using OptionParser = Result<Success> (SubsystemParser::*)(std::vector<std::string> && args);
114
Tom Cherryed506f72017-05-25 15:58:59 -0700115 static class OptionParserMap : public KeywordMap<OptionParser> {
116 private:
117 const Map& map() const override {
118 // clang-format off
119 static const Map option_parsers = {
120 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
121 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
122 };
123 // clang-format on
124 return option_parsers;
125 }
126 } parser_map;
127
Tom Cherry89bcc852017-08-02 17:01:36 -0700128 auto parser = parser_map.FindFunction(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700129
Tom Cherry89bcc852017-08-02 17:01:36 -0700130 if (!parser) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700131
Tom Cherry89bcc852017-08-02 17:01:36 -0700132 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700133}
134
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800135Result<Success> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700136 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800137
138 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700139}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700140
141} // namespace init
142} // namespace android