1 /* 2 * Copyright (C) 2022 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/types.h> 20 21 #include <string> 22 23 #include "common/libs/utils/result.h" 24 #include "host/commands/cvd/flag.h" 25 26 namespace cuttlefish { 27 namespace selector { 28 29 /** The direct parent of auto-generated runtime directories, which 30 * is recommended to be short. 31 * 32 * Try these one by one in order, and append /.cf 33 * 34 * 1. $TMPDIR 35 * 2. $TEMP 36 * 3. $TMP 37 * 4. /tmp 38 * 5. /var/tmp 39 * 6. /usr/tmp 40 * 7. HOME of uid 41 * 42 */ 43 Result<std::string> ParentOfAutogeneratedHomes(const uid_t client_uid, 44 const gid_t client_gid); 45 46 /* 47 * These are fields in instance database 48 * 49 */ 50 constexpr char kGroupNameField[] = "group_name"; 51 constexpr char kHomeField[] = "home"; 52 constexpr char kInstanceIdField[] = "instance_id"; 53 /* per_instance_name 54 * 55 * by default, to_string(instance_id), and users can override it 56 */ 57 constexpr char kInstanceNameField[] = "instance_name"; 58 59 /** 60 * The authentic collection of selector flags 61 * 62 */ 63 // names of the flags, which are also used for search 64 65 class SelectorFlags { 66 public: 67 static constexpr char kGroupName[] = "group_name"; 68 static constexpr char kInstanceName[] = "instance_name"; 69 static constexpr char kAcquireFileLock[] = "acquire_file_lock"; 70 static constexpr char kAcquireFileLockEnv[] = "CVD_ACQUIRE_FILE_LOCK"; 71 static constexpr char kDisableDefaultGroup[] = "disable_default_group"; 72 static const SelectorFlags& Get(); 73 static const SelectorFlags New(); 74 GetFlag(const std::string & search_key)75 Result<CvdFlagProxy> GetFlag(const std::string& search_key) const { 76 auto flag = CF_EXPECT(flags_.GetFlag(search_key)); 77 return flag; 78 } 79 Flags()80 std::vector<CvdFlagProxy> Flags() const { return flags_.Flags(); } FlagsAsCollection()81 const auto& FlagsAsCollection() const { return flags_; } 82 83 private: SelectorFlags()84 SelectorFlags() { 85 flags_.EnrollFlag(GroupNameFlag(kGroupName)); 86 flags_.EnrollFlag(InstanceNameFlag(kInstanceName)); 87 flags_.EnrollFlag(DisableDefaultGroupFlag(kDisableDefaultGroup, false)); 88 flags_.EnrollFlag(AcquireFileLockFlag(kAcquireFileLock, true)); 89 } 90 91 CvdFlag<std::string> GroupNameFlag(const std::string& name); 92 CvdFlag<std::string> InstanceNameFlag(const std::string& name); 93 CvdFlag<bool> DisableDefaultGroupFlag(const std::string& name, 94 const bool default_val); 95 CvdFlag<bool> AcquireFileLockFlag(const std::string& name, 96 const bool default_val); 97 98 FlagCollection flags_; 99 }; 100 101 } // namespace selector 102 } // namespace cuttlefish 103