blob: d4143aa0b9f1014d9cfd5ac6c96d7945850e9dfe [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
Bart Van Assche01e66692022-11-14 16:45:47 -080029#include "interprocess_fifo.h"
Jooyung Han4f23d5a2020-06-09 13:44:17 +090030#include "mount_namespace.h"
Vic Yange01ca4d2019-05-29 15:58:32 -070031#include "result.h"
32
33namespace android {
34namespace init {
35
Bart Van Asschec8f34252022-11-18 09:12:52 -080036// Constants used by Service::Start() for communication between parent and child.
37enum ServiceCode : uint8_t {
38 kActivatingCgroupsFailed,
39 kCgroupsActivated,
Bart Van Assche01e66692022-11-14 16:45:47 -080040 kSetSidFinished,
Bart Van Asschec8f34252022-11-18 09:12:52 -080041};
42
Tom Cherry5241d102019-09-10 14:20:35 -070043class Descriptor {
44 public:
45 Descriptor(const std::string& name, android::base::unique_fd fd)
46 : name_(name), fd_(std::move(fd)){};
47
Tom Cherryc9bc6bb2020-11-24 11:34:40 -080048 // 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 Cherry5241d102019-09-10 14:20:35 -070050 void Publish() const;
51
52 private:
53 std::string name_;
54 android::base::unique_fd fd_;
55};
56
Tom Cherry2e4c85f2019-07-09 13:33:36 -070057struct 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 Langleyecc14a52022-05-11 22:32:47 +000065 bool listen = false;
David Anderson0e5ad5a2021-07-21 21:53:28 -070066 bool persist = false;
Tom Cherry2e4c85f2019-07-09 13:33:36 -070067
Tom Cherryc9bc6bb2020-11-24 11:34:40 -080068 // 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 Cherry5241d102019-09-10 14:20:35 -070071 Result<Descriptor> Create(const std::string& global_context) const;
Tom Cherry2e4c85f2019-07-09 13:33:36 -070072};
73
74struct FileDescriptor {
75 std::string name;
76 std::string type;
77
Tom Cherry5241d102019-09-10 14:20:35 -070078 Result<Descriptor> Create() const;
Tom Cherry2e4c85f2019-07-09 13:33:36 -070079};
80
Vic Yange01ca4d2019-05-29 15:58:32 -070081struct NamespaceInfo {
Tom Cherry247ffbf2019-07-08 15:09:36 -070082 int flags;
Vic Yange01ca4d2019-05-29 15:58:32 -070083 // Pair of namespace type, path to name.
84 std::vector<std::pair<int, std::string>> namespaces_to_enter;
85};
Jooyung Han4f23d5a2020-06-09 13:44:17 +090086Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name,
87 std::optional<MountNamespace> override_mount_namespace);
Vic Yange01ca4d2019-05-29 15:58:32 -070088
89struct 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 Cherryf74b7f52019-09-23 16:16:54 -070098 bool stdio_to_kmsg;
Vic Yange01ca4d2019-05-29 15:58:32 -070099};
Bart Van Assche98739162022-11-14 16:54:03 -0800100
101inline bool RequiresConsole(const ProcessAttributes& attr) {
102 return !attr.console.empty();
103}
104
Bart Van Assche01e66692022-11-14 16:45:47 -0800105Result<void> SetProcessAttributes(const ProcessAttributes& attr, InterprocessFifo setsid_finished);
Vic Yange01ca4d2019-05-29 15:58:32 -0700106
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700107Result<void> WritePidToFiles(std::vector<std::string>* files);
Vic Yange01ca4d2019-05-29 15:58:32 -0700108
109} // namespace init
110} // namespace android