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