source: trunk/debathena/third/schroot/sbuild/sbuild-personality.cc @ 24167

Revision 24167, 4.9 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
RevLine 
[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#include <config.h>
20
21#include "sbuild-config.h"
22#include "sbuild-personality.h"
23
24#include <cstring>
25#include <cerrno>
26#include <utility>
27
28#ifdef SBUILD_FEATURE_PERSONALITY
29#include <sys/personality.h>
30#endif // SBUILD_FEATURE_PERSONALITY
31
32#include <boost/format.hpp>
33
34using boost::format;
35using namespace sbuild;
36
37namespace
38{
39
40  typedef std::pair<sbuild::personality::error_code,const char *> emap;
41
42  /**
43   * This is a list of the supported error codes.  It's used to
44   * construct the real error codes map.
45   */
46  emap init_errors[] =
47    {
48      // TRANSLATORS: %1% = integer personality ID
49      emap(sbuild::personality::BAD, N_("Personality '%1%' is unknown")),
50      // TRANSLATORS: %1% = personality name
51      emap(sbuild::personality::SET, N_("Failed to set personality '%1%'"))
52    };
53
54  typedef std::pair<std::string,sbuild::personality::type> pmap;
55
56  /**
57   * This is a list of the supported personalities.  It's used to
58   * construct the real personalities map.
59   */
60  pmap initial_personalities[] =
61    {
62      pmap("undefined", 0xffffffff),
63#if defined(SBUILD_FEATURE_PERSONALITY) && defined (__linux__)
64      pmap("linux", PER_LINUX),
65      pmap("linux_32bit", PER_LINUX_32BIT),
66      pmap("svr4", PER_SVR4),
67      pmap("scorvr3", PER_SCOSVR3),
68      pmap("osr5", PER_OSR5),
69      pmap("wysev386", PER_WYSEV386),
70      pmap("iscr4", PER_ISCR4),
71      pmap("bsd", PER_BSD),
72      pmap("sunos", PER_SUNOS),
73      pmap("xenix", PER_XENIX),
74      pmap("linux32", PER_LINUX32),
75      pmap("irix32", PER_IRIX32),
76      pmap("irixn32", PER_IRIXN32),
77      pmap("irix64", PER_IRIX64),
78      pmap("riscos", PER_RISCOS),
79      pmap("solaris", PER_SOLARIS),
80      pmap("uw7", PER_UW7),
81      pmap("hpux", PER_HPUX),
82      pmap("osf4", PER_OSF4),
83#endif // SBUILD_FEATURE_PERSONALITY && __linux__
84    };
85
86}
87
88template<>
89error<sbuild::personality::error_code>::map_type
90error<sbuild::personality::error_code>::error_strings
91(init_errors,
92 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
93
94std::map<std::string,sbuild::personality::type>
95sbuild::personality::personalities(initial_personalities,
96                                   initial_personalities + (sizeof(initial_personalities) / sizeof(initial_personalities[0])));
97
98sbuild::personality::personality ():
99  persona(
100#if defined(SBUILD_FEATURE_PERSONALITY)
101          ::personality(0xffffffff)
102#else
103          0xffffffff
104#endif // SBUILD_FEATURE_PERSONALITY
105          )
106{
107}
108
109sbuild::personality::personality (type persona):
110  persona(persona)
111{
112}
113
114sbuild::personality::personality (std::string const& persona):
115  persona(find_personality(persona))
116{
117}
118
119sbuild::personality::~personality ()
120{
121}
122
123sbuild::personality::type
124sbuild::personality::find_personality (std::string const& persona)
125{
126  std::map<std::string,type>::const_iterator pos =
127    personalities.find(persona);
128
129  if (pos != personalities.end())
130    return pos->second;
131
132  return 0xffffffff;
133}
134
135std::string const&
136sbuild::personality::find_personality (type persona)
137{
138  static const std::string unknown("unknown");
139
140  for (std::map<std::string,type>::const_iterator pos = personalities.begin();
141       pos != personalities.end();
142       ++pos)
143    if (pos->second == persona)
144      return pos->first;
145
146  return unknown;
147}
148
149std::string const&
150sbuild::personality::get_name () const
151{
152  return find_personality(this->persona);
153}
154
155sbuild::personality::type
156sbuild::personality::get () const
157{
158  return this->persona;
159}
160
161void
162sbuild::personality::set () const
163{
164#ifdef SBUILD_FEATURE_PERSONALITY
165  /* Set the process execution domain using personality(2). */
166  if (this->persona != 0xffffffff &&
167      ::personality (this->persona) < 0)
168    {
169      throw error(get_name(), SET, strerror(errno));
170    }
171#endif // SBUILD_FEATURE_PERSONALITY
172}
173
174std::string
175sbuild::personality::get_personalities ()
176{
177  // TRANSLATORS: %1% = a comma-separated list of personality names
178  format fmt(_("Valid personalities: %1%\n"));
179  std::string ps;
180
181  for (std::map<std::string,type>::const_iterator pos = personalities.begin();
182       pos != personalities.end();
183       ++pos)
184    {
185      ps += pos->first;
186      std::map<std::string,type>::const_iterator stpos = pos;
187      if (++stpos != personalities.end())
188        ps += ", ";
189    }
190
191  fmt % ps;
192
193  return fmt.str();
194}
Note: See TracBrowser for help on using the repository browser.