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 
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
26 #include <android-base/unique_fd.h>
27 #include <cutils/iosched_policy.h>
28 
29 #include "mount_namespace.h"
30 #include "result.h"
31 
32 namespace android {
33 namespace init {
34 
35 class Descriptor {
36   public:
Descriptor(const std::string & name,android::base::unique_fd fd)37     Descriptor(const std::string& name, android::base::unique_fd fd)
38         : name_(name), fd_(std::move(fd)){};
39 
40     void Publish() const;
41 
42   private:
43     std::string name_;
44     android::base::unique_fd fd_;
45 };
46 
47 struct SocketDescriptor {
48     std::string name;
49     int type = 0;
50     uid_t uid = 0;
51     gid_t gid = 0;
52     int perm = 0;
53     std::string context;
54     bool passcred = false;
55 
56     Result<Descriptor> Create(const std::string& global_context) const;
57 };
58 
59 struct FileDescriptor {
60     std::string name;
61     std::string type;
62 
63     Result<Descriptor> Create() const;
64 };
65 
66 struct NamespaceInfo {
67     int flags;
68     // Pair of namespace type, path to name.
69     std::vector<std::pair<int, std::string>> namespaces_to_enter;
70 };
71 Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name,
72                              std::optional<MountNamespace> override_mount_namespace);
73 
74 struct ProcessAttributes {
75     std::string console;
76     IoSchedClass ioprio_class;
77     int ioprio_pri;
78     std::vector<std::pair<int, rlimit>> rlimits;
79     uid_t uid;
80     gid_t gid;
81     std::vector<gid_t> supp_gids;
82     int priority;
83     bool stdio_to_kmsg;
84 };
85 Result<void> SetProcessAttributes(const ProcessAttributes& attr);
86 
87 Result<void> WritePidToFiles(std::vector<std::string>* files);
88 
89 }  // namespace init
90 }  // namespace android
91