~ubuntu-branches/ubuntu/intrepid/schroot/intrepid

« back to all changes in this revision

Viewing changes to schroot/sbuild-session.h

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-07-08 18:33:28 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060708183328-rlo4mpldmyoda55q
Tags: 0.99.2-2ubuntu1
* remerge ubuntu changes:
  + debian/control: libpam-dev (>> 0.79-3ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright © 2005-2006  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 2 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, write to the Free Software
15
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
16
 
 * MA  02111-1307  USA
17
 
 *
18
 
 *********************************************************************/
19
 
 
20
 
#ifndef SBUILD_SESSION_H
21
 
#define SBUILD_SESSION_H
22
 
 
23
 
#include <string>
24
 
 
25
 
#include <sys/types.h>
26
 
#include <sys/wait.h>
27
 
#include <grp.h>
28
 
#include <pwd.h>
29
 
#include <unistd.h>
30
 
 
31
 
#include "sbuild-auth.h"
32
 
#include "sbuild-chroot-config.h"
33
 
#include "sbuild-error.h"
34
 
 
35
 
namespace sbuild
36
 
{
37
 
 
38
 
  /**
39
 
   * Session handler.
40
 
   *
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).
48
 
   */
49
 
  class session : public auth
50
 
  {
51
 
  public:
52
 
    /// Session operations.
53
 
    enum operation
54
 
      {
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.
60
 
      };
61
 
 
62
 
    /// Exception type.
63
 
    typedef runtime_error_custom<session> error;
64
 
 
65
 
    /// A shared_ptr to a chroot_config object.
66
 
    typedef std::tr1::shared_ptr<chroot_config> config_ptr;
67
 
 
68
 
    /// A shared_ptr to a session object.
69
 
    typedef std::tr1::shared_ptr<session> ptr;
70
 
 
71
 
    /**
72
 
     * The constructor.
73
 
     *
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.
78
 
     */
79
 
    session (std::string const& service,
80
 
             config_ptr&        config,
81
 
             operation          operation,
82
 
             string_list const& chroots);
83
 
 
84
 
    /// The destructor.
85
 
    virtual ~session ();
86
 
 
87
 
    /**
88
 
     * Get the configuration associated with this session.
89
 
     *
90
 
     * @returns a shared_ptr to the configuration.
91
 
     */
92
 
    config_ptr&
93
 
    get_config ();
94
 
 
95
 
    /**
96
 
     * Set the configuration associated with this session.
97
 
     *
98
 
     * @param config a shared_ptr to the configuration.
99
 
     */
100
 
    void
101
 
    set_config (config_ptr& config);
102
 
 
103
 
    /**
104
 
     * Get the chroots to use in this session.
105
 
     *
106
 
     * @returns a list of chroots.
107
 
     */
108
 
    string_list const&
109
 
    get_chroots () const;
110
 
 
111
 
    /**
112
 
     * Set the chroots to use in this session.
113
 
     *
114
 
     * @param chroots a list of chroots.
115
 
     */
116
 
    void
117
 
    set_chroots (string_list const& chroots);
118
 
 
119
 
    /**
120
 
     * Get the operation this session will perform.
121
 
     *
122
 
     * @returns the operation.
123
 
     */
124
 
    operation
125
 
    get_operation () const;
126
 
 
127
 
    /**
128
 
     * Set the operation this session will perform.
129
 
     *
130
 
     * @param operation the operation.
131
 
     */
132
 
    void
133
 
    set_operation (operation operation);
134
 
 
135
 
    /**
136
 
     * Get the session identifier.  The session identifier is a unique
137
 
     * string to identify a session.
138
 
     *
139
 
     * @returns the session id.
140
 
     */
141
 
    std::string const&
142
 
    get_session_id () const;
143
 
 
144
 
    /**
145
 
     * Set the session identifier.  The session identifier is a unique
146
 
     * string to identify a session.
147
 
     *
148
 
     * @param session_id the session id.
149
 
     */
150
 
    void
151
 
    set_session_id (std::string const& session_id);
152
 
 
153
 
    /**
154
 
     * Get the force status of this session.
155
 
     *
156
 
     * @returns true if operation will be forced, otherwise false.
157
 
     */
158
 
    bool
159
 
    get_force () const;
160
 
 
161
 
    /**
162
 
     * Set the force status of this session.
163
 
     *
164
 
     * @param force true to force operation, otherwise false.
165
 
     */
166
 
    void
167
 
    set_force (bool force);
168
 
 
169
 
    /**
170
 
     * Get the exit (wait) status of the last child process to run in this
171
 
     * session.
172
 
     *
173
 
     * @returns the exit status.
174
 
     */
175
 
    int
176
 
    get_child_status () const;
177
 
 
178
 
    /**
179
 
     * Check if authentication is required, taking groups and
180
 
     * root-groups membership or all chroots specified into account.
181
 
     */
182
 
    virtual sbuild::auth::status
183
 
    get_auth_status () const;
184
 
 
185
 
    /**
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.
189
 
     *
190
 
     * An error will be thrown on failure.
191
 
     */
192
 
    virtual void
193
 
    run_impl ();
194
 
 
195
 
  private:
196
 
    /**
197
 
     * execve wrapper.  Run the command specified by file (an absolute
198
 
     * pathname), using command and env as the argv and environment,
199
 
     * respectively.
200
 
     *
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.
205
 
     */
206
 
    int
207
 
    exec (std::string const& file,
208
 
          string_list const& command,
209
 
          environment const& env);
210
 
    /**
211
 
     * Setup a chroot.  This runs all of the commands in setup.d or run.d.
212
 
     *
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.
216
 
     *
217
 
     * An error will be thrown on failure.
218
 
     *
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.
222
 
     */
223
 
    void
224
 
    setup_chroot (chroot::ptr&       session_chroot,
225
 
                  chroot::setup_type setup_type);
226
 
 
227
 
    /**
228
 
     * Run command or login shell in the specified chroot.
229
 
     *
230
 
     * An error will be thrown on failure.
231
 
     *
232
 
     * @param session_chroot the chroot to setup.  This must be
233
 
     * present in the chroot list and the chroot configuration object.
234
 
     */
235
 
    void
236
 
    run_chroot (chroot::ptr& session_chroot);
237
 
 
238
 
    /**
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.
242
 
     *
243
 
     * @param session_chroot the chroot to setup.  This must be
244
 
     * present in the chroot list and the chroot configuration object.
245
 
     */
246
 
    void
247
 
    run_child (chroot::ptr& session_chroot);
248
 
 
249
 
    /**
250
 
     * Wait for a child process to complete, and check its exit status.
251
 
     *
252
 
     * An error will be thrown on failure.
253
 
     *
254
 
     * @param pid the pid to wait for.
255
 
     * @param child_status the place to store the child exit status.
256
 
     */
257
 
    void
258
 
    wait_for_child (int  pid,
259
 
                    int& child_status);
260
 
 
261
 
    /**
262
 
     * Set the SIGHUP handler.
263
 
     *
264
 
     * An error will be thrown on failure.
265
 
     */
266
 
    void
267
 
    set_sighup_handler ();
268
 
 
269
 
    /**
270
 
     * Restore the state of SIGHUP prior to setting the handler.
271
 
     */
272
 
    void
273
 
    clear_sighup_handler ();
274
 
 
275
 
    /// The chroot configuration.
276
 
    config_ptr       config;
277
 
    /// The chroots to run the session operation in.
278
 
    string_list      chroots;
279
 
    /// The current chroot status.
280
 
    int              chroot_status;
281
 
    /// The child exit status.
282
 
    int              child_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.
288
 
    bool             force;
289
 
    /// Signals saved while sighup handler is set.
290
 
    struct sigaction saved_signals;
291
 
  };
292
 
 
293
 
}
294
 
 
295
 
#endif /* SBUILD_SESSION_H */
296
 
 
297
 
/*
298
 
 * Local Variables:
299
 
 * mode:C++
300
 
 * End:
301
 
 */