[24167] | 1 | /* Copyright © 2006-2007 Roger Leigh <rleigh@debian.org> |
---|
| 2 | * |
---|
| 3 | * schroot is free software: you can redistribute it and/or modify it |
---|
| 4 | * under the terms of the GNU General Public License as published by |
---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 6 | * (at your option) any later version. |
---|
| 7 | * |
---|
| 8 | * schroot is distributed in the hope that it will be useful, but |
---|
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 11 | * General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program. If not, see |
---|
| 15 | * <http://www.gnu.org/licenses/>. |
---|
| 16 | * |
---|
| 17 | *********************************************************************/ |
---|
| 18 | |
---|
| 19 | #ifndef SBUILD_PERSONALITY_H |
---|
| 20 | #define SBUILD_PERSONALITY_H |
---|
| 21 | |
---|
| 22 | #include <sbuild/sbuild-custom-error.h> |
---|
| 23 | |
---|
| 24 | #include <map> |
---|
| 25 | #include <ostream> |
---|
| 26 | #include <string> |
---|
| 27 | |
---|
| 28 | namespace sbuild |
---|
| 29 | { |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Chroot personality. A chroot may have a personality (also knows |
---|
| 33 | * as a process execution domain) which is used to run non-native |
---|
| 34 | * binaries. For example, running 32-bit Linux binaries on a 64-bit |
---|
| 35 | * Linux system, or an SVR4 binary on a 32-bit Linux system. This |
---|
| 36 | * is currently a Linux only feature; it does nothing on non-Linux |
---|
| 37 | * systems. This is a wrapper around the personality(2) system |
---|
| 38 | * call. |
---|
| 39 | */ |
---|
| 40 | class personality |
---|
| 41 | { |
---|
| 42 | public: |
---|
| 43 | /// Personality type. |
---|
| 44 | typedef unsigned long type; |
---|
| 45 | |
---|
| 46 | /// Error codes. |
---|
| 47 | enum error_code |
---|
| 48 | { |
---|
| 49 | BAD, ///< Personality is unknown. |
---|
| 50 | SET ///< Could not set personality. |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | /// Exception type. |
---|
| 54 | typedef custom_error<error_code> error; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * The constructor. On Linux systems, this is initialised with |
---|
| 58 | * the current process' personality. On non-Linux systems, it is |
---|
| 59 | * initialised as "undefined". |
---|
| 60 | */ |
---|
| 61 | personality (); |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * The constructor. |
---|
| 65 | * |
---|
| 66 | * @param persona the persona to set. |
---|
| 67 | */ |
---|
| 68 | personality (type persona); |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * The constructor. |
---|
| 72 | * |
---|
| 73 | * @param persona the persona to set. |
---|
| 74 | */ |
---|
| 75 | personality (std::string const& persona); |
---|
| 76 | |
---|
| 77 | ///* The destructor. |
---|
| 78 | ~personality (); |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Get the name of the personality. |
---|
| 82 | * |
---|
| 83 | * @returns the personality name. |
---|
| 84 | */ |
---|
| 85 | std::string const& get_name () const; |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Get the personality. |
---|
| 89 | * |
---|
| 90 | * @returns the personality. |
---|
| 91 | */ |
---|
| 92 | type |
---|
| 93 | get () const; |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Set the process personality. This sets the personality (if valid) using |
---|
| 97 | * the personality(2) system call. If setting the personality |
---|
| 98 | * fails, an error is thown. |
---|
| 99 | */ |
---|
| 100 | void |
---|
| 101 | set () const; |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Print a list of the available personalities. |
---|
| 105 | * |
---|
| 106 | * @returns a string of the available personalities. |
---|
| 107 | */ |
---|
| 108 | static std::string |
---|
| 109 | get_personalities (); |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Get the personality name from a stream. |
---|
| 113 | * |
---|
| 114 | * @param stream the stream to get input from. |
---|
| 115 | * @param rhs the personality to set. |
---|
| 116 | * @returns the stream. |
---|
| 117 | */ |
---|
| 118 | template <class charT, class traits> |
---|
| 119 | friend |
---|
| 120 | std::basic_istream<charT,traits>& |
---|
| 121 | operator >> (std::basic_istream<charT,traits>& stream, |
---|
| 122 | personality& rhs) |
---|
| 123 | { |
---|
| 124 | std::string personality_name; |
---|
| 125 | |
---|
| 126 | if (std::getline(stream, personality_name)) |
---|
| 127 | { |
---|
| 128 | rhs.persona = find_personality(personality_name); |
---|
| 129 | |
---|
| 130 | if (rhs.get_name() == "undefined" && |
---|
| 131 | rhs.get_name() != personality_name) |
---|
| 132 | { |
---|
| 133 | personality::error e(personality_name, personality::BAD); |
---|
| 134 | e.set_reason(personality::get_personalities()); |
---|
| 135 | throw e; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | return stream; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Print the personality name to a stream. |
---|
| 144 | * |
---|
| 145 | * @param stream the stream to output to. |
---|
| 146 | * @param rhs the personality to output. |
---|
| 147 | * @returns the stream. |
---|
| 148 | */ |
---|
| 149 | template <class charT, class traits> |
---|
| 150 | friend |
---|
| 151 | std::basic_ostream<charT,traits>& |
---|
| 152 | operator << (std::basic_ostream<charT,traits>& stream, |
---|
| 153 | personality const& rhs) |
---|
| 154 | { |
---|
| 155 | return stream << find_personality(rhs.persona); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | private: |
---|
| 159 | /** |
---|
| 160 | * Find a personality by name. |
---|
| 161 | * |
---|
| 162 | * @param persona the personality to find. |
---|
| 163 | * @returns the personality type; this is -1 if the personality |
---|
| 164 | * was undefined, or -2 if the personality was unknown (not |
---|
| 165 | * found). |
---|
| 166 | */ |
---|
| 167 | static type |
---|
| 168 | find_personality (std::string const& persona); |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * Find a personality by number. |
---|
| 172 | * |
---|
| 173 | * @param persona the personality to find. |
---|
| 174 | * @returns the personality name, "undefined" if the personality was |
---|
| 175 | * not defined, or "unknown" if the personality was not found. |
---|
| 176 | */ |
---|
| 177 | static std::string const& |
---|
| 178 | find_personality (type persona); |
---|
| 179 | |
---|
| 180 | /// The personality type. |
---|
| 181 | type persona; |
---|
| 182 | |
---|
| 183 | /// Mapping between personality name and type. |
---|
| 184 | static std::map<std::string,type> personalities; |
---|
| 185 | }; |
---|
| 186 | |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | #endif /* SBUILD_PERSONALITY_H */ |
---|
| 190 | |
---|
| 191 | /* |
---|
| 192 | * Local Variables: |
---|
| 193 | * mode:C++ |
---|
| 194 | * End: |
---|
| 195 | */ |
---|