Logo Search packages:      
Sourcecode: schroot version File versions  Download package

void session::run_impl (  )  [virtual]

Run a session. If a command has been specified, this will be run in each of the specified chroots. If no command has been specified, a login shell will run in the specified chroot.

An error will be thrown on failure.

Implements sbuild::auth.

Reimplemented in dchroot::session.

Definition at line 292 of file sbuild-session.cc.

References child_status, chroots, clear_sighup_handler(), config, sbuild::chroot::EXEC_START, sbuild::chroot::EXEC_STOP, sbuild::chroot_block_device::get_device(), sbuild::chroot::get_run_setup_scripts(), OPERATION_AUTOMATIC, OPERATION_BEGIN, OPERATION_RUN, run_chroot(), sbuild::chroot::SESSION_CREATE, session_id, session_operation, set_session_id(), set_sighup_handler(), sbuild::chroot_lvm_snapshot::set_snapshot_device(), setup_chroot(), sbuild::chroot::SETUP_RECOVER, sbuild::chroot::SETUP_START, and sbuild::chroot::SETUP_STOP.

Referenced by dchroot::session::run_impl().

{
  assert(this->config.get() != NULL);
  assert(!this->chroots.empty());

try
  {
    sighup_called = false;
    set_sighup_handler();

    for (string_list::const_iterator cur = this->chroots.begin();
       cur != this->chroots.end();
       ++cur)
      {
      log_debug(DEBUG_NOTICE)
        << format("Running session in %1% chroot:") % *cur
        << endl;
      const chroot::ptr ch = this->config->find_alias(*cur);
      if (!ch) // Should never happen, but cater for it anyway.
        {
          format fmt(_("%1%: Failed to find chroot"));
          fmt % *cur;
          throw error(fmt);
        }

      chroot::ptr chroot(ch->clone());

      /* If restoring a session, set the session ID from the
         chroot name, or else generate it.  Only chroots which
         support session creation append a UUID to the session
         ID. */
      if (chroot->get_active() ||
          !(chroot->get_session_flags() & chroot::SESSION_CREATE))
        {
          set_session_id(chroot->get_name());
        }
      else
        {
          uuid_t uuid;
          char uuid_str[37];
          uuid_generate(uuid);
          uuid_unparse(uuid, uuid_str);
          uuid_clear(uuid);
          std::string session_id(chroot->get_name() + "-" + uuid_str);
          set_session_id(session_id);
        }

      /* Activate chroot. */
      chroot->set_active(true);

      /* If a chroot mount location has not yet been set, and the
         chroot is not a plain chroot, set a mount location with the
         session id. */
      {
        chroot_plain *plain = dynamic_cast<chroot_plain *>(chroot.get());
        if (chroot->get_mount_location().empty() &&
            (plain == 0 || plain->get_run_setup_scripts() == true))
          {
            std::string location(std::string(SCHROOT_MOUNT_DIR) + "/" +
                           this->session_id);
            chroot->set_mount_location(location);
          }
      }

      /* Chroot types which create a session (e.g. LVM devices)
         need the chroot name respecifying. */
      if (chroot->get_session_flags() & chroot::SESSION_CREATE)
        {
          chroot->set_name(this->session_id);
          chroot->set_aliases(string_list());
        }

      /* LVM devices need the snapshot device name specifying. */
      chroot_lvm_snapshot *snapshot = 0;
      if ((snapshot = dynamic_cast<chroot_lvm_snapshot *>(chroot.get())) != 0)
        {
          std::string dir(dirname(snapshot->get_device(), '/'));
          std::string device(dir + "/" + this->session_id);
          snapshot->set_snapshot_device(device);
        }

      try
        {
          /* Run setup-start chroot setup scripts. */
          setup_chroot(chroot, chroot::SETUP_START);
          if (this->session_operation == OPERATION_BEGIN)
            cout << this->session_id << endl;

          /* Run recover scripts. */
          setup_chroot(chroot, chroot::SETUP_RECOVER);

          try
            {
            /* Run exec-start scripts. */
            setup_chroot(chroot, chroot::EXEC_START);

            /* Run session if setup succeeded. */
            if (this->session_operation == OPERATION_AUTOMATIC ||
                this->session_operation == OPERATION_RUN)
              run_chroot(chroot);

            /* Run exec-stop scripts whether or not there was an
               error. */
            setup_chroot(chroot, chroot::EXEC_STOP);
            }
          catch (error const& e)
            {
            setup_chroot(chroot, chroot::EXEC_STOP);
            throw;
            }

        }
      catch (error const& e)
        {
          try
            {
            setup_chroot(chroot, chroot::SETUP_STOP);
            }
          catch (error const& discard)
            {
            }
          chroot->set_active(false);
          throw;
        }

      /* Run setup-stop chroot setup scripts whether or not there
         was an error. */
      setup_chroot(chroot, chroot::SETUP_STOP);

      /* Deactivate chroot. */
      chroot->set_active(false);
      }

    clear_sighup_handler();
  }
catch (error const& e)
  {
    clear_sighup_handler();

    /* If a command was not run, but something failed, the exit
       status still needs setting. */
    if (this->child_status == 0)
      this->child_status = EXIT_FAILURE;
    throw;
  }
}


Generated by  Doxygen 1.6.0   Back to index