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