2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INIT_DEVICES_H
|
|
|
|
#define _INIT_DEVICES_H
|
|
|
|
|
2010-04-21 21:04:20 +02:00
|
|
|
#include <sys/stat.h>
|
2017-04-06 02:55:46 +02:00
|
|
|
#include <sys/types.h>
|
2010-04-21 21:04:20 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
#include <algorithm>
|
2017-04-05 02:53:45 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-04-07 01:30:22 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <selinux/label.h>
|
2017-02-08 03:11:37 +01:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
#include "uevent.h"
|
2017-02-08 03:11:37 +01:00
|
|
|
|
2017-04-06 02:55:46 +02:00
|
|
|
class Permissions {
|
|
|
|
public:
|
|
|
|
Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
|
|
|
|
|
|
|
|
bool Match(const std::string& path) const;
|
|
|
|
|
|
|
|
mode_t perm() const { return perm_; }
|
|
|
|
uid_t uid() const { return uid_; }
|
|
|
|
gid_t gid() const { return gid_; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const std::string& name() const { return name_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
mode_t perm_;
|
|
|
|
uid_t uid_;
|
|
|
|
gid_t gid_;
|
|
|
|
bool prefix_;
|
|
|
|
bool wildcard_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SysfsPermissions : public Permissions {
|
|
|
|
public:
|
|
|
|
SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
|
|
|
|
gid_t gid)
|
|
|
|
: Permissions(name, perm, uid, gid), attribute_(attribute) {}
|
|
|
|
|
|
|
|
bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const;
|
|
|
|
void SetPermissions(const std::string& path) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string attribute_;
|
|
|
|
};
|
|
|
|
|
2017-04-25 01:59:05 +02:00
|
|
|
class Subsystem {
|
|
|
|
public:
|
|
|
|
friend class SubsystemParser;
|
|
|
|
|
|
|
|
Subsystem() {}
|
|
|
|
|
|
|
|
// Returns the full path for a uevent of a device that is a member of this subsystem,
|
|
|
|
// according to the rules parsed from ueventd.rc
|
2017-05-26 00:58:59 +02:00
|
|
|
std::string ParseDevPath(const Uevent& uevent) const {
|
|
|
|
std::string devname = devname_source_ == DevnameSource::DEVNAME_UEVENT_DEVNAME
|
|
|
|
? uevent.device_name
|
|
|
|
: android::base::Basename(uevent.path);
|
|
|
|
|
|
|
|
return dir_name_ + "/" + devname;
|
|
|
|
}
|
2017-04-25 01:59:05 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
bool operator==(const std::string& string_name) const { return name_ == string_name; }
|
2017-04-25 01:59:05 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum class DevnameSource {
|
|
|
|
DEVNAME_UEVENT_DEVNAME,
|
|
|
|
DEVNAME_UEVENT_DEVPATH,
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string name_;
|
|
|
|
std::string dir_name_ = "/dev";
|
|
|
|
DevnameSource devname_source_;
|
|
|
|
};
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
class PlatformDeviceList {
|
2017-04-25 01:59:05 +02:00
|
|
|
public:
|
2017-05-26 00:58:59 +02:00
|
|
|
void Add(const std::string& path) { platform_devices_.emplace_back(path); }
|
|
|
|
void Remove(const std::string& path) {
|
|
|
|
auto it = std::find(platform_devices_.begin(), platform_devices_.end(), path);
|
|
|
|
if (it != platform_devices_.end()) platform_devices_.erase(it);
|
|
|
|
}
|
|
|
|
bool Find(const std::string& path, std::string* out_path) const;
|
|
|
|
auto size() const { return platform_devices_.size(); }
|
2017-04-25 01:59:05 +02:00
|
|
|
|
|
|
|
private:
|
2017-05-26 00:58:59 +02:00
|
|
|
std::vector<std::string> platform_devices_;
|
2017-04-25 01:59:05 +02:00
|
|
|
};
|
2017-04-06 02:55:46 +02:00
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
class DeviceHandler {
|
|
|
|
public:
|
|
|
|
friend class DeviceHandlerTester;
|
|
|
|
|
|
|
|
DeviceHandler();
|
|
|
|
DeviceHandler(std::vector<Permissions> dev_permissions,
|
|
|
|
std::vector<SysfsPermissions> sysfs_permissions,
|
2017-05-17 00:35:41 +02:00
|
|
|
std::vector<Subsystem> subsystems, bool skip_restorecon);
|
2017-05-26 00:58:59 +02:00
|
|
|
~DeviceHandler(){};
|
|
|
|
|
|
|
|
void HandleDeviceEvent(const Uevent& uevent);
|
2017-05-17 00:35:41 +02:00
|
|
|
|
|
|
|
void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
|
|
|
|
|
|
|
|
void HandlePlatformDeviceEvent(const Uevent& uevent);
|
|
|
|
void HandleBlockDeviceEvent(const Uevent& uevent) const;
|
|
|
|
void HandleGenericDeviceEvent(const Uevent& uevent) const;
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
|
2017-05-17 00:35:41 +02:00
|
|
|
void set_skip_restorecon(bool value) { skip_restorecon_ = value; }
|
2017-05-26 00:58:59 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
|
|
|
|
const std::string& path, const std::vector<std::string>& links) const;
|
|
|
|
void MakeDevice(const std::string& path, int block, int major, int minor,
|
|
|
|
const std::vector<std::string>& links) const;
|
|
|
|
std::vector<std::string> GetCharacterDeviceSymlinks(const Uevent& uevent) const;
|
|
|
|
void HandleDevice(const std::string& action, const std::string& devpath, int block, int major,
|
|
|
|
int minor, const std::vector<std::string>& links) const;
|
|
|
|
|
|
|
|
std::vector<Permissions> dev_permissions_;
|
|
|
|
std::vector<SysfsPermissions> sysfs_permissions_;
|
|
|
|
std::vector<Subsystem> subsystems_;
|
|
|
|
PlatformDeviceList platform_devices_;
|
|
|
|
selabel_handle* sehandle_;
|
2017-05-17 00:35:41 +02:00
|
|
|
bool skip_restorecon_;
|
2017-05-26 00:58:59 +02:00
|
|
|
};
|
2015-02-04 02:12:07 +01:00
|
|
|
|
2017-04-06 00:58:31 +02:00
|
|
|
// Exposed for testing
|
2017-05-26 00:58:59 +02:00
|
|
|
void SanitizePartitionName(std::string* string);
|
|
|
|
|
|
|
|
#endif
|