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