source: trunk/debathena/third/schroot/bin/dchroot/dchroot-session.cc @ 24167

Revision 24167, 3.7 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2005-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 "dchroot-session.h"
22
23#include <cassert>
24#include <cerrno>
25#include <cstdlib>
26#include <cstring>
27#include <iostream>
28#include <memory>
29
30#include <unistd.h>
31
32#include <syslog.h>
33
34#include <boost/format.hpp>
35
36using std::cout;
37using std::endl;
38using sbuild::_;
39using sbuild::auth;
40using boost::format;
41using namespace dchroot;
42
43session::session (std::string const&         service,
44                  config_ptr&                config,
45                  operation                  operation,
46                  sbuild::string_list const& chroots,
47                  bool                       compat):
48  session_base(service, config, operation, chroots, compat)
49{
50}
51
52session::~session ()
53{
54}
55
56sbuild::auth::status
57session::get_chroot_auth_status (sbuild::auth::status status,
58                                 sbuild::chroot::ptr const& chroot) const
59{
60  if (get_compat() == true)
61    status = auth::change_auth(status, auth::STATUS_NONE);
62  else
63    status = auth::change_auth(status,
64                               sbuild::session::get_chroot_auth_status(status,
65                                                                       chroot));
66
67  return status;
68}
69
70sbuild::string_list
71session::get_login_directories () const
72{
73  sbuild::string_list ret;
74
75  std::string const& wd(get_auth()->get_wd());
76  if (!wd.empty())
77    {
78      // Set specified working directory.
79      ret.push_back(wd);
80    }
81  else
82    {
83      // Set current working directory only if preserving environment.
84      // Only change to home if not preserving the environment.
85      if (!get_auth()->get_environment().empty())
86        ret.push_back(this->sbuild::session::cwd);
87      else
88        ret.push_back(get_auth()->get_home());
89
90      // Final fallback to root.
91      if (std::find(ret.begin(), ret.end(), "/") == ret.end())
92        ret.push_back("/");
93    }
94
95  return ret;
96}
97
98void
99session::get_user_command (sbuild::chroot::ptr& session_chroot,
100                           std::string&         file,
101                           sbuild::string_list& command) const
102{
103  std::string programstring = sbuild::string_list_to_string(command, " ");
104
105  command.clear();
106  command.push_back(get_shell());
107  command.push_back("-c");
108  command.push_back(programstring);
109
110  file = command[0];
111
112  sbuild::log_debug(sbuild::DEBUG_NOTICE) << "file=" << file << endl;
113
114  std::string commandstring = sbuild::string_list_to_string(command, " ");
115  sbuild::log_debug(sbuild::DEBUG_NOTICE)
116    << format("Running command: %1%") % commandstring << endl;
117  if (get_auth()->get_uid() == 0 ||
118      get_auth()->get_ruid() != get_auth()->get_uid())
119    syslog(LOG_USER|LOG_NOTICE, "[%s chroot] (%s->%s) Running command: \"%s\"",
120           session_chroot->get_name().c_str(),
121           get_auth()->get_ruser().c_str(),
122           get_auth()->get_user().c_str(),
123           commandstring.c_str());
124
125  if (get_auth()->get_verbosity() != auth::VERBOSITY_QUIET)
126    {
127      std::string format_string;
128      // TRANSLATORS: %1% = chroot name
129      // TRANSLATORS: %2% = command
130      format_string = (_("[%1% chroot] Running command: \"%2%\""));
131
132      format fmt(format_string);
133      fmt % session_chroot->get_name()
134        % programstring;
135      sbuild::log_info() << fmt << endl;
136    }
137}
Note: See TracBrowser for help on using the repository browser.