| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2016 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 "descriptors.h" | 
|  | 18 |  | 
|  | 19 | #include <ctype.h> | 
|  | 20 | #include <fcntl.h> | 
|  | 21 | #include <sys/stat.h> | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 22 | #include <unistd.h> | 
|  | 23 |  | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> | 
| Mark Salyzyn | b066fcc | 2017-05-05 14:44:35 -0700 | [diff] [blame] | 26 | #include <android-base/strings.h> | 
| Mark Salyzyn | 978fd0e | 2016-12-02 08:05:22 -0800 | [diff] [blame] | 27 | #include <android-base/unique_fd.h> | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 28 | #include <cutils/android_get_control_file.h> | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 29 | #include <cutils/sockets.h> | 
|  | 30 |  | 
|  | 31 | #include "init.h" | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 32 | #include "util.h" | 
|  | 33 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 34 | namespace android { | 
|  | 35 | namespace init { | 
|  | 36 |  | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 37 | DescriptorInfo::DescriptorInfo(const std::string& name, const std::string& type, uid_t uid, | 
|  | 38 | gid_t gid, int perm, const std::string& context) | 
|  | 39 | : name_(name), type_(type), uid_(uid), gid_(gid), perm_(perm), context_(context) { | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | DescriptorInfo::~DescriptorInfo() { | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | std::ostream& operator<<(std::ostream& os, const DescriptorInfo& info) { | 
|  | 46 | return os << "  descriptors " << info.name_ << " " << info.type_ << " " << std::oct << info.perm_; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | bool DescriptorInfo::operator==(const DescriptorInfo& other) const { | 
|  | 50 | return name_ == other.name_ && type_ == other.type_ && key() == other.key(); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | void DescriptorInfo::CreateAndPublish(const std::string& globalContext) const { | 
|  | 54 | // Create | 
|  | 55 | const std::string& contextStr = context_.empty() ? globalContext : context_; | 
|  | 56 | int fd = Create(contextStr); | 
|  | 57 | if (fd < 0) return; | 
|  | 58 |  | 
|  | 59 | // Publish | 
|  | 60 | std::string publishedName = key() + name_; | 
|  | 61 | std::for_each(publishedName.begin(), publishedName.end(), | 
|  | 62 | [] (char& c) { c = isalnum(c) ? c : '_'; }); | 
|  | 63 |  | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 64 | std::string val = std::to_string(fd); | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 65 | add_environment(publishedName.c_str(), val.c_str()); | 
|  | 66 |  | 
|  | 67 | // make sure we don't close on exec | 
|  | 68 | fcntl(fd, F_SETFD, 0); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | void DescriptorInfo::Clean() const { | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | SocketInfo::SocketInfo(const std::string& name, const std::string& type, uid_t uid, | 
|  | 75 | gid_t gid, int perm, const std::string& context) | 
|  | 76 | : DescriptorInfo(name, type, uid, gid, perm, context) { | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | void SocketInfo::Clean() const { | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 80 | std::string path = android::base::StringPrintf("%s/%s", ANDROID_SOCKET_DIR, name().c_str()); | 
|  | 81 | unlink(path.c_str()); | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | int SocketInfo::Create(const std::string& context) const { | 
| Mark Salyzyn | b066fcc | 2017-05-05 14:44:35 -0700 | [diff] [blame] | 85 | auto types = android::base::Split(type(), "+"); | 
|  | 86 | int flags = | 
|  | 87 | ((types[0] == "stream" ? SOCK_STREAM : (types[0] == "dgram" ? SOCK_DGRAM : SOCK_SEQPACKET))); | 
|  | 88 | bool passcred = types.size() > 1 && types[1] == "passcred"; | 
| Tom Cherry | c317009 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 89 | return CreateSocket(name().c_str(), flags, passcred, perm(), uid(), gid(), context.c_str()); | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | const std::string SocketInfo::key() const { | 
|  | 93 | return ANDROID_SOCKET_ENV_PREFIX; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | FileInfo::FileInfo(const std::string& name, const std::string& type, uid_t uid, | 
|  | 97 | gid_t gid, int perm, const std::string& context) | 
| Mark Salyzyn | 978fd0e | 2016-12-02 08:05:22 -0800 | [diff] [blame] | 98 | // defaults OK for uid,..., they are ignored for this class. | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 99 | : DescriptorInfo(name, type, uid, gid, perm, context) { | 
|  | 100 | } | 
|  | 101 |  | 
| Mark Salyzyn | 978fd0e | 2016-12-02 08:05:22 -0800 | [diff] [blame] | 102 | int FileInfo::Create(const std::string&) const { | 
|  | 103 | int flags = (type() == "r") ? O_RDONLY : | 
|  | 104 | (type() == "w") ? O_WRONLY : | 
|  | 105 | O_RDWR; | 
|  | 106 |  | 
|  | 107 | // Make sure we do not block on open (eg: devices can chose to block on | 
|  | 108 | // carrier detect).  Our intention is never to delay launch of a service | 
|  | 109 | // for such a condition.  The service can perform its own blocking on | 
|  | 110 | // carrier detect. | 
|  | 111 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(name().c_str(), | 
|  | 112 | flags | O_NONBLOCK))); | 
|  | 113 |  | 
|  | 114 | if (fd < 0) { | 
|  | 115 | PLOG(ERROR) << "Failed to open file '" << name().c_str() << "'"; | 
|  | 116 | return -1; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | // Fixup as we set O_NONBLOCK for open, the intent for fd is to block reads. | 
|  | 120 | fcntl(fd, F_SETFL, flags); | 
|  | 121 |  | 
|  | 122 | LOG(INFO) << "Opened file '" << name().c_str() << "'" | 
|  | 123 | << ", flags " << std::oct << flags << std::dec; | 
|  | 124 |  | 
|  | 125 | return fd.release(); | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
|  | 128 | const std::string FileInfo::key() const { | 
|  | 129 | return ANDROID_FILE_ENV_PREFIX; | 
|  | 130 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | }  // namespace init | 
|  | 133 | }  // namespace android |