Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 18 | |
| 19 | #include <sys/resource.h> |
| 20 | #include <sys/types.h> |
| 21 | |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 22 | #include <optional> |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Tom Cherry | 5241d10 | 2019-09-10 14:20:35 -0700 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 27 | #include <cutils/iosched_policy.h> |
| 28 | |
Bart Van Assche | 01e6669 | 2022-11-14 16:45:47 -0800 | [diff] [blame] | 29 | #include "interprocess_fifo.h" |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 30 | #include "mount_namespace.h" |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 31 | #include "result.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace init { |
| 35 | |
Bart Van Assche | c8f3425 | 2022-11-18 09:12:52 -0800 | [diff] [blame] | 36 | // Constants used by Service::Start() for communication between parent and child. |
| 37 | enum ServiceCode : uint8_t { |
| 38 | kActivatingCgroupsFailed, |
| 39 | kCgroupsActivated, |
Bart Van Assche | 01e6669 | 2022-11-14 16:45:47 -0800 | [diff] [blame] | 40 | kSetSidFinished, |
Bart Van Assche | c8f3425 | 2022-11-18 09:12:52 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Tom Cherry | 5241d10 | 2019-09-10 14:20:35 -0700 | [diff] [blame] | 43 | class Descriptor { |
| 44 | public: |
| 45 | Descriptor(const std::string& name, android::base::unique_fd fd) |
| 46 | : name_(name), fd_(std::move(fd)){}; |
| 47 | |
Tom Cherry | c9bc6bb | 2020-11-24 11:34:40 -0800 | [diff] [blame] | 48 | // Publish() unsets FD_CLOEXEC from the FD and publishes its name via setenv(). It should be |
| 49 | // called when starting a service after fork() and before exec(). |
Tom Cherry | 5241d10 | 2019-09-10 14:20:35 -0700 | [diff] [blame] | 50 | void Publish() const; |
| 51 | |
| 52 | private: |
| 53 | std::string name_; |
| 54 | android::base::unique_fd fd_; |
| 55 | }; |
| 56 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 57 | struct SocketDescriptor { |
| 58 | std::string name; |
| 59 | int type = 0; |
| 60 | uid_t uid = 0; |
| 61 | gid_t gid = 0; |
| 62 | int perm = 0; |
| 63 | std::string context; |
| 64 | bool passcred = false; |
Adam Langley | ecc14a5 | 2022-05-11 22:32:47 +0000 | [diff] [blame] | 65 | bool listen = false; |
David Anderson | 0e5ad5a | 2021-07-21 21:53:28 -0700 | [diff] [blame] | 66 | bool persist = false; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 67 | |
Tom Cherry | c9bc6bb | 2020-11-24 11:34:40 -0800 | [diff] [blame] | 68 | // Create() creates the named unix domain socket in /dev/socket and returns a Descriptor object. |
| 69 | // It should be called when starting a service, before calling fork(), such that the socket is |
| 70 | // synchronously created before starting any other services, which may depend on it. |
Tom Cherry | 5241d10 | 2019-09-10 14:20:35 -0700 | [diff] [blame] | 71 | Result<Descriptor> Create(const std::string& global_context) const; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | struct FileDescriptor { |
| 75 | std::string name; |
| 76 | std::string type; |
| 77 | |
Tom Cherry | 5241d10 | 2019-09-10 14:20:35 -0700 | [diff] [blame] | 78 | Result<Descriptor> Create() const; |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 81 | struct NamespaceInfo { |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 82 | int flags; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 83 | // Pair of namespace type, path to name. |
| 84 | std::vector<std::pair<int, std::string>> namespaces_to_enter; |
| 85 | }; |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 86 | Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name, |
| 87 | std::optional<MountNamespace> override_mount_namespace); |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 88 | |
| 89 | struct ProcessAttributes { |
| 90 | std::string console; |
| 91 | IoSchedClass ioprio_class; |
| 92 | int ioprio_pri; |
| 93 | std::vector<std::pair<int, rlimit>> rlimits; |
| 94 | uid_t uid; |
| 95 | gid_t gid; |
| 96 | std::vector<gid_t> supp_gids; |
| 97 | int priority; |
Tom Cherry | f74b7f5 | 2019-09-23 16:16:54 -0700 | [diff] [blame] | 98 | bool stdio_to_kmsg; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 99 | }; |
Bart Van Assche | 9873916 | 2022-11-14 16:54:03 -0800 | [diff] [blame] | 100 | |
| 101 | inline bool RequiresConsole(const ProcessAttributes& attr) { |
| 102 | return !attr.console.empty(); |
| 103 | } |
| 104 | |
Bart Van Assche | 01e6669 | 2022-11-14 16:45:47 -0800 | [diff] [blame] | 105 | Result<void> SetProcessAttributes(const ProcessAttributes& attr, InterprocessFifo setsid_finished); |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 106 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 107 | Result<void> WritePidToFiles(std::vector<std::string>* files); |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 108 | |
| 109 | } // namespace init |
| 110 | } // namespace android |