blob: 5af779c4201361142aee23a654081bda5beb4905 [file] [log] [blame]
Vic Yange01ca4d2019-05-29 15:58:32 -07001/*
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 Han4f23d5a2020-06-09 13:44:17 +090022#include <optional>
Vic Yange01ca4d2019-05-29 15:58:32 -070023#include <string>
24#include <vector>
25
Tom Cherry5241d102019-09-10 14:20:35 -070026#include <android-base/unique_fd.h>
Vic Yange01ca4d2019-05-29 15:58:32 -070027#include <cutils/iosched_policy.h>
28
Jooyung Han4f23d5a2020-06-09 13:44:17 +090029#include "mount_namespace.h"
Vic Yange01ca4d2019-05-29 15:58:32 -070030#include "result.h"
31
32namespace android {
33namespace init {
34
Bart Van Asschec8f34252022-11-18 09:12:52 -080035// Constants used by Service::Start() for communication between parent and child.
36enum ServiceCode : uint8_t {
37 kActivatingCgroupsFailed,
38 kCgroupsActivated,
39};
40
Tom Cherry5241d102019-09-10 14:20:35 -070041class Descriptor {
42 public:
43 Descriptor(const std::string& name, android::base::unique_fd fd)
44 : name_(name), fd_(std::move(fd)){};
45
Tom Cherryc9bc6bb2020-11-24 11:34:40 -080046 // Publish() unsets FD_CLOEXEC from the FD and publishes its name via setenv(). It should be
47 // called when starting a service after fork() and before exec().
Tom Cherry5241d102019-09-10 14:20:35 -070048 void Publish() const;
49
50 private:
51 std::string name_;
52 android::base::unique_fd fd_;
53};
54
Tom Cherry2e4c85f2019-07-09 13:33:36 -070055struct SocketDescriptor {
56 std::string name;
57 int type = 0;
58 uid_t uid = 0;
59 gid_t gid = 0;
60 int perm = 0;
61 std::string context;
62 bool passcred = false;
Adam Langleyecc14a52022-05-11 22:32:47 +000063 bool listen = false;
David Anderson0e5ad5a2021-07-21 21:53:28 -070064 bool persist = false;
Tom Cherry2e4c85f2019-07-09 13:33:36 -070065
Tom Cherryc9bc6bb2020-11-24 11:34:40 -080066 // Create() creates the named unix domain socket in /dev/socket and returns a Descriptor object.
67 // It should be called when starting a service, before calling fork(), such that the socket is
68 // synchronously created before starting any other services, which may depend on it.
Tom Cherry5241d102019-09-10 14:20:35 -070069 Result<Descriptor> Create(const std::string& global_context) const;
Tom Cherry2e4c85f2019-07-09 13:33:36 -070070};
71
72struct FileDescriptor {
73 std::string name;
74 std::string type;
75
Tom Cherry5241d102019-09-10 14:20:35 -070076 Result<Descriptor> Create() const;
Tom Cherry2e4c85f2019-07-09 13:33:36 -070077};
78
Vic Yange01ca4d2019-05-29 15:58:32 -070079struct NamespaceInfo {
Tom Cherry247ffbf2019-07-08 15:09:36 -070080 int flags;
Vic Yange01ca4d2019-05-29 15:58:32 -070081 // Pair of namespace type, path to name.
82 std::vector<std::pair<int, std::string>> namespaces_to_enter;
83};
Jooyung Han4f23d5a2020-06-09 13:44:17 +090084Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name,
85 std::optional<MountNamespace> override_mount_namespace);
Vic Yange01ca4d2019-05-29 15:58:32 -070086
87struct ProcessAttributes {
88 std::string console;
89 IoSchedClass ioprio_class;
90 int ioprio_pri;
91 std::vector<std::pair<int, rlimit>> rlimits;
92 uid_t uid;
93 gid_t gid;
94 std::vector<gid_t> supp_gids;
95 int priority;
Tom Cherryf74b7f52019-09-23 16:16:54 -070096 bool stdio_to_kmsg;
Vic Yange01ca4d2019-05-29 15:58:32 -070097};
Bart Van Assche98739162022-11-14 16:54:03 -080098
99inline bool RequiresConsole(const ProcessAttributes& attr) {
100 return !attr.console.empty();
101}
102
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700103Result<void> SetProcessAttributes(const ProcessAttributes& attr);
Vic Yange01ca4d2019-05-29 15:58:32 -0700104
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700105Result<void> WritePidToFiles(std::vector<std::string>* files);
Vic Yange01ca4d2019-05-29 15:58:32 -0700106
107} // namespace init
108} // namespace android