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

« back to all changes in this revision

Viewing changes to sbuild/sbuild-run-parts.cc

  • 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
#include <config.h>
 
21
 
 
22
#include "sbuild-dirstream.h"
 
23
#include "sbuild-run-parts.h"
 
24
#include "sbuild-util.h"
 
25
 
 
26
#include <cerrno>
 
27
 
 
28
#include <sys/wait.h>
 
29
 
 
30
#include <boost/format.hpp>
 
31
#include <boost/regex.hpp>
 
32
 
 
33
using boost::format;
 
34
using boost::regex;
 
35
using namespace sbuild;
 
36
 
 
37
namespace
 
38
{
 
39
 
 
40
  typedef std::pair<sbuild::run_parts::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
      emap(run_parts::CHILD_FORK, N_("Failed to fork child")),
 
49
      emap(run_parts::CHILD_WAIT, N_("Wait for child failed")),
 
50
      emap(run_parts::EXEC,       N_("Failed to execute"))
 
51
    };
 
52
 
 
53
}
 
54
 
 
55
template<>
 
56
std::map<run_parts::error_code,const char *>
 
57
custom_error<run_parts::error_code>::error_strings
 
58
(init_errors,
 
59
 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
 
60
 
 
61
run_parts::run_parts (std::string const& directory,
 
62
                      bool               lsb_mode,
 
63
                      bool               abort_on_error,
 
64
                      mode_t             umask):
 
65
  lsb_mode(true),
 
66
  abort_on_error(abort_on_error),
 
67
  umask(umask),
 
68
  verbose(false),
 
69
  reverse(false),
 
70
  //  restricted(true),
 
71
  directory(directory),
 
72
  programs()
 
73
{
 
74
  dirstream stream(directory);
 
75
  direntry de;
 
76
  while (stream >> de)
 
77
    {
 
78
      std::string name(de.name());
 
79
      if (check_filename(name))
 
80
        this->programs.insert(name);
 
81
    }
 
82
}
 
83
 
 
84
run_parts::~run_parts ()
 
85
{
 
86
}
 
87
 
 
88
bool
 
89
run_parts::get_verbose () const
 
90
{
 
91
  return this->verbose;
 
92
}
 
93
 
 
94
void
 
95
run_parts::set_verbose (bool verbose)
 
96
{
 
97
  this->verbose = verbose;
 
98
}
 
99
 
 
100
bool
 
101
run_parts::get_reverse () const
 
102
{
 
103
  return this->reverse;
 
104
}
 
105
 
 
106
void
 
107
run_parts::set_reverse (bool reverse)
 
108
{
 
109
  this->reverse = reverse;
 
110
}
 
111
 
 
112
int
 
113
run_parts::run (string_list const& command,
 
114
                environment const& env)
 
115
{
 
116
  int exit_status = 0;
 
117
 
 
118
  if (!this->reverse)
 
119
    {
 
120
      for (program_set::const_iterator pos = this->programs.begin();
 
121
           pos != this->programs.end();
 
122
           ++pos)
 
123
        {
 
124
          string_list real_command;
 
125
          real_command.push_back(*pos);
 
126
          for (string_list::const_iterator spos = command.begin();
 
127
               spos != command.end();
 
128
               ++spos)
 
129
            real_command.push_back(*spos);
 
130
 
 
131
          exit_status = run_child(*pos, real_command, env);
 
132
 
 
133
          if (exit_status && this->abort_on_error)
 
134
            return exit_status;
 
135
        }
 
136
    }
 
137
  else
 
138
    {
 
139
      for (program_set::const_reverse_iterator pos = this->programs.rbegin();
 
140
           pos != this->programs.rend();
 
141
           ++pos)
 
142
        {
 
143
          string_list real_command;
 
144
          real_command.push_back(*pos);
 
145
          for (string_list::const_iterator spos = command.begin();
 
146
               spos != command.end();
 
147
               ++spos)
 
148
            real_command.push_back(*spos);
 
149
 
 
150
          exit_status = run_child(*pos, real_command, env);
 
151
 
 
152
          if (exit_status && this->abort_on_error)
 
153
            return exit_status;
 
154
        }
 
155
    }
 
156
 
 
157
  return exit_status;
 
158
}
 
159
 
 
160
int
 
161
run_parts::run_child (std::string const& file,
 
162
                      string_list const& command,
 
163
                      environment const& env)
 
164
{
 
165
  int exit_status = 0;
 
166
  pid_t pid;
 
167
 
 
168
  if ((pid = fork()) == -1)
 
169
    {
 
170
      throw error(CHILD_FORK, errno);
 
171
    }
 
172
  else if (pid == 0)
 
173
    {
 
174
      if (this->verbose)
 
175
        log_info() << format(_("Executing %1%"))
 
176
          % string_list_to_string(command, " ")
 
177
                   << std::endl;
 
178
      ::umask(this->umask);
 
179
      exec(this->directory + '/' + file, command, env);
 
180
      error e(file, EXEC, errno);
 
181
      log_error() << e.what() << std::endl;
 
182
      exit(EXIT_FAILURE);
 
183
    }
 
184
  else
 
185
    {
 
186
      wait_for_child(pid, exit_status);
 
187
    }
 
188
 
 
189
  return exit_status;
 
190
}
 
191
 
 
192
void
 
193
run_parts::wait_for_child (pid_t pid,
 
194
                           int&  child_status)
 
195
{
 
196
  child_status = EXIT_FAILURE; // Default exit status
 
197
 
 
198
  int status;
 
199
 
 
200
  while (1)
 
201
    {
 
202
      if (wait(&status) != pid)
 
203
        {
 
204
          if (errno == EINTR)
 
205
            continue; // Wait again.
 
206
          else
 
207
            throw error(CHILD_WAIT, errno);
 
208
        }
 
209
      else
 
210
        break;
 
211
    }
 
212
 
 
213
  if (WIFEXITED(status))
 
214
    child_status = WEXITSTATUS(status);
 
215
}
 
216
 
 
217
bool
 
218
run_parts::check_filename (std::string const& name)
 
219
{
 
220
  bool match = false;
 
221
 
 
222
  if (this->lsb_mode)
 
223
    {
 
224
      static regex lanana_namespace("^[a-z0-9]+$", boost::regex::basic);
 
225
      static regex lsb_namespace("^_?([a-z0-9_.]+-)+[a-z0-9]+$",
 
226
                                 boost::regex::basic);
 
227
      static regex debian_cron_namespace("^[a-z0-9][a-z0-9-]*$",
 
228
                                         boost::regex::basic);
 
229
      static regex debian_dpkg_conffile_cruft("dpkg-(old|dist|new|tmp)$",
 
230
                                              boost::regex::extended);
 
231
 
 
232
      if ((regex_match(name, lanana_namespace) ||
 
233
           regex_match(name, lsb_namespace) ||
 
234
           regex_match(name, debian_cron_namespace)) &&
 
235
          !regex_match(name, debian_dpkg_conffile_cruft))
 
236
        match = true;
 
237
    }
 
238
  else
 
239
    {
 
240
      static regex traditional_namespace("^[a-zA-Z0-9_-]$",
 
241
                                         boost::regex::basic);
 
242
      if (regex_match(name, traditional_namespace))
 
243
        match = true;
 
244
    }
 
245
 
 
246
  return match;
 
247
}
 
248
 
 
249
/*
 
250
 * Local Variables:
 
251
 * mode:C++
 
252
 * End:
 
253
 */