1
/* Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
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 2 of the License, or
6
* (at your option) any later version.
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.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
20
#ifndef SBUILD_SESSION_H
21
#define SBUILD_SESSION_H
25
#include <sys/types.h>
31
#include "sbuild-auth.h"
32
#include "sbuild-chroot-config.h"
33
#include "sbuild-error.h"
41
* This class provides the session handling for schroot. It derives
42
* from auth, which performs all the necessary PAM actions,
43
* specialising it by overriding its virtual functions. This allows
44
* more sophisticated handling of user authorisation (groups and
45
* root-groups membership in the configuration file) and session
46
* management (setting up the session, entering the chroot and
47
* running the requested command or shell).
49
class session : public auth
52
/// Session operations.
55
OPERATION_AUTOMATIC, ///< Begin, end and run a session automatically.
56
OPERATION_BEGIN, ///< Begin a session.
57
OPERATION_RECOVER, ///< Recover an existing (but inactive) session.
58
OPERATION_END, ///< End a session.
59
OPERATION_RUN ///< Run a command in an existing session.
63
typedef runtime_error_custom<session> error;
65
/// A shared_ptr to a chroot_config object.
66
typedef std::tr1::shared_ptr<chroot_config> config_ptr;
68
/// A shared_ptr to a session object.
69
typedef std::tr1::shared_ptr<session> ptr;
74
* @param service the PAM service name.
75
* @param config a shared_ptr to the chroot configuration.
76
* @param operation the session operation to perform.
77
* @param chroots the chroots to act upon.
79
session (std::string const& service,
82
string_list const& chroots);
88
* Get the configuration associated with this session.
90
* @returns a shared_ptr to the configuration.
96
* Set the configuration associated with this session.
98
* @param config a shared_ptr to the configuration.
101
set_config (config_ptr& config);
104
* Get the chroots to use in this session.
106
* @returns a list of chroots.
109
get_chroots () const;
112
* Set the chroots to use in this session.
114
* @param chroots a list of chroots.
117
set_chroots (string_list const& chroots);
120
* Get the operation this session will perform.
122
* @returns the operation.
125
get_operation () const;
128
* Set the operation this session will perform.
130
* @param operation the operation.
133
set_operation (operation operation);
136
* Get the session identifier. The session identifier is a unique
137
* string to identify a session.
139
* @returns the session id.
142
get_session_id () const;
145
* Set the session identifier. The session identifier is a unique
146
* string to identify a session.
148
* @param session_id the session id.
151
set_session_id (std::string const& session_id);
154
* Get the force status of this session.
156
* @returns true if operation will be forced, otherwise false.
162
* Set the force status of this session.
164
* @param force true to force operation, otherwise false.
167
set_force (bool force);
170
* Get the exit (wait) status of the last child process to run in this
173
* @returns the exit status.
176
get_child_status () const;
179
* Check if authentication is required, taking groups and
180
* root-groups membership or all chroots specified into account.
182
virtual sbuild::auth::status
183
get_auth_status () const;
186
* Run a session. If a command has been specified, this will be
187
* run in each of the specified chroots. If no command has been
188
* specified, a login shell will run in the specified chroot.
190
* An error will be thrown on failure.
197
* execve wrapper. Run the command specified by file (an absolute
198
* pathname), using command and env as the argv and environment,
201
* @param file the program to execute.
202
* @param command the arguments to pass to the executable.
203
* @param env the environment.
204
* @returns the return value of the execve system call on failure.
207
exec (std::string const& file,
208
string_list const& command,
209
environment const& env);
211
* Setup a chroot. This runs all of the commands in setup.d or run.d.
213
* The environment variables CHROOT_NAME, CHROOT_DESCRIPTION,
214
* CHROOT_LOCATION, AUTH_USER and AUTH_VERBOSITY are set for use in
215
* setup scripts. See schroot-setup(5) for a complete list.
217
* An error will be thrown on failure.
219
* @param session_chroot the chroot to setup. This must be
220
* present in the chroot list and the chroot configuration object.
221
* @param setup_type the type of setup to perform.
224
setup_chroot (chroot::ptr& session_chroot,
225
chroot::setup_type setup_type);
228
* Run command or login shell in the specified chroot.
230
* An error will be thrown on failure.
232
* @param session_chroot the chroot to setup. This must be
233
* present in the chroot list and the chroot configuration object.
236
run_chroot (chroot::ptr& session_chroot);
239
* Run a command or login shell as a child process in the
240
* specified chroot. This method is only ever to be run in a
241
* child process, and will never return.
243
* @param session_chroot the chroot to setup. This must be
244
* present in the chroot list and the chroot configuration object.
247
run_child (chroot::ptr& session_chroot);
250
* Wait for a child process to complete, and check its exit status.
252
* An error will be thrown on failure.
254
* @param pid the pid to wait for.
255
* @param child_status the place to store the child exit status.
258
wait_for_child (int pid,
262
* Set the SIGHUP handler.
264
* An error will be thrown on failure.
267
set_sighup_handler ();
270
* Restore the state of SIGHUP prior to setting the handler.
273
clear_sighup_handler ();
275
/// The chroot configuration.
277
/// The chroots to run the session operation in.
279
/// The current chroot status.
281
/// The child exit status.
283
/// The session operation to perform.
284
operation session_operation;
285
/// The session identifier.
286
std::string session_id;
287
/// The session force status.
289
/// Signals saved while sighup handler is set.
290
struct sigaction saved_signals;
295
#endif /* SBUILD_SESSION_H */