Skip to content

Commit

Permalink
Add ability to SCR component to operate on different root
Browse files Browse the repository at this point in the history
Before, the `scr` component was common agent server component. There is also scr
server program located at `/usr/share/YaST2/servers/scr`, that allows to call
scr as external program in chroot via ProgramCompnent.
Now to remove the need for `chroot(2)`, scr component can be instantiated with
target root, which is propagated to whole scr agent tree. It require API changes
because common SCRAgent template doesn't fit there.
  • Loading branch information
jreidinger committed Sep 3, 2013
1 parent da31966 commit 1943107
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 22 deletions.
2 changes: 1 addition & 1 deletion agent-ini/testsuite/runag_ini.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ main (int argc, char *argv[])
scrconf.replace (p, 4, ".scr");
}

SCRAgent *agent = new ScriptingAgent (scrconf);
SCRAgent *agent = new ScriptingAgent ("/", scrconf);
if (!agent)
{
fprintf (stderr, "Failed to create Agent\n");
Expand Down
14 changes: 7 additions & 7 deletions libscr/src/include/scr/Y2AgentComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ template <class Agent> class Y2AgentComp : public Y2Component
/**
* Clean up.
*/
~Y2AgentComp ();
virtual ~Y2AgentComp ();

/**
* Returns the name of the component.
*/
string name () const { return my_name; }
virtual string name () const { return my_name; }

/**
* Evaluates a command to the agent.
*/
YCPValue evaluate (const YCPValue &command);
virtual YCPValue evaluate (const YCPValue &command);

/**
* Returns the SCRAgent of the Y2Component.
*/
SCRAgent* getSCRAgent ();
virtual SCRAgent* getSCRAgent ();

YCPValue Read (const YCPPath &path);
private:
virtual YCPValue Read (const YCPPath &path);

protected:

/**
* Name of my agent.
Expand Down
3 changes: 2 additions & 1 deletion scr/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ serversdir = $(execcompdir)/servers
plugin_LTLIBRARIES = libpy2scr.la

libpy2scr_la_SOURCES = \
Y2CCSCR.cc \
Y2CCSCR.cc Y2CCSCR.h \
Y2SCRComponent.cc Y2SCRComponent.h \
ScriptingAgent.cc ScriptingAgent.h \
StdioSCRAgent.cc StdioSCRAgent.h \
SCRSubAgent.cc SCRSubAgent.h
Expand Down
15 changes: 11 additions & 4 deletions scr/src/ScriptingAgent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
#include "ScriptingAgent.h"


ScriptingAgent::ScriptingAgent ()
: done_sweep (false)
ScriptingAgent::ScriptingAgent (const string &root_)
: done_sweep (false),
root_path(root_)
{
InitRegDirs ();
// to test the old behavior
Expand All @@ -35,8 +36,9 @@ ScriptingAgent::ScriptingAgent ()
}


ScriptingAgent::ScriptingAgent (const string& file)
: done_sweep (false)
ScriptingAgent::ScriptingAgent (const string &root_, const string& file)
: done_sweep (false),
root_path(root_)
{
InitRegDirs ();
y2debug( "Scripting agent using only SCR %s", file.c_str () );
Expand Down Expand Up @@ -74,6 +76,11 @@ ScriptingAgent::~ScriptingAgent ()
UnregisterAllAgents ();
}

const char* ScriptingAgent::root() const
{
return root_path.c_str();
}

bool less_than_inodes (const pair<ino_t, string>& a,
const pair<ino_t, string>& b)
{
Expand Down
12 changes: 10 additions & 2 deletions scr/src/ScriptingAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ScriptingAgent : public SCRAgent
/**
* Constructor.
*/
ScriptingAgent ();
ScriptingAgent (const string& root = "/");

// used only in agent-ini/testsuite...?
// TODO try to eliminate it
Expand All @@ -36,13 +36,19 @@ class ScriptingAgent : public SCRAgent
*
* @param file SCR configuration file to be registered.
*/
ScriptingAgent (const string& file);
ScriptingAgent (const string& root, const string& file);

/**
* Destructor. Also deletes subagents.
*/
~ScriptingAgent ();

/**
* Overrides root getter to provide root from constructor
* \see SCRAgent#root
*/
virtual const char* root() const;

/**
* Reads data.
* @param path Specifies what part of the subtree should
Expand Down Expand Up @@ -123,6 +129,8 @@ class ScriptingAgent : public SCRAgent
// that we do not unnecessarily sweep again
bool done_sweep;

string root_path;

// FIXME rethink the caching
struct RegistrationDir {
string name;
Expand Down
55 changes: 48 additions & 7 deletions scr/src/Y2CCSCR.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,56 @@
*/


#include <scr/Y2AgentComponent.h>
#include <scr/Y2CCAgentComponent.h>
#include "Y2CCSCR.h"
#include "Y2SCRComponent.h"

#include "ScriptingAgent.h"
using namespace std;

Y2CCSCR::Y2CCSCR () :
Y2ComponentCreator (Y2ComponentBroker::BUILTIN)
{
}

// create Agent called Y2SCRComp out of ScriptingAgent
typedef Y2AgentComp <ScriptingAgent> Y2SCRComp;
Y2CCSCR::~Y2CCSCR () {
for (map<string, Y2SCRComponent*>::iterator i = scr_instances.begin();
i != scr_instances.end(); ++i)
delete i->second;
}

// create Component Creator for Y2SCRComp
Y2CCAgentComp <Y2SCRComp> g_y2ccscr ("scr");
/* local helper for CC#create */
static void split_name( const char *name, string &root, string &real_name)
{
real_name = name;
root = "/";

if (strncmp (name, "chroot=", 7) == 0)
{
const char *p = index (name, ':');

if (p) {
root = string (name, 7, p - name - 7);
real_name = string (p + 1);
}
}
}

Y2Component *Y2CCSCR::create( const char* name) const
{
string root, real_name;

split_name(name, root, real_name);

if (real_name != "scr")
return NULL;

map<string, Y2SCRComponent*>::iterator i = scr_instances.find(root);
if (i != scr_instances.end())
return i->second;

scr_instances[root] = new Y2SCRComponent(root.c_str());
return scr_instances[root];
}


// create Component Creator for Y2SCRComp
Y2CCSCR g_y2ccscr;
50 changes: 50 additions & 0 deletions scr/src/Y2CCSCR.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef Y2CCSCR_H
#define Y2CCSCR_H

#include <y2/Y2ComponentCreator.h>
#include <map>
#include <string>

#include "ScriptingAgent.h"
#include "Y2SCRComponent.h"

class Y2CCSCR : public Y2ComponentCreator
{
public:

/**
* Constructor of a SCR component creator.
*/
Y2CCSCR ();


/**
* Destructor of a SCR component creator.
*/
~Y2CCSCR ();


/**
* Returns true since all agents are server components.
*/
bool isServerCreator () const { return true; }

/**
* Creates a new @ref Y2Component if the name matches the one
* provided in the constructor.
*/
Y2Component* create (const char*) const;

/**
* Agent components do not provide any namespaces.
*/
Y2Component* provideNamespace (const char*) { return NULL; }

private:

/* SCR instances, owned by this class, keyed by target root.
* Target root must be valid absolute path like "/" or "/mnt".*/
mutable std::map<std::string, Y2SCRComponent*> scr_instances;
};

#endif
14 changes: 14 additions & 0 deletions scr/src/Y2SCRComponent.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Y2SCRComponent.h"
#include "ScriptingAgent.h"


Y2SCRComponent::Y2SCRComponent(const char *root) :
Y2AgentComp<ScriptingAgent>("scr")
{
agent = new ScriptingAgent(root);
}

Y2SCRComponent::~Y2SCRComponent()
{
//agent is destructed in parent
}
15 changes: 15 additions & 0 deletions scr/src/Y2SCRComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef Y2SCRCOMPONENT_H
#define Y2SCRCOMPONENT_H

#include <scr/Y2AgentComponent.h>
#include "ScriptingAgent.h"

class Y2SCRComponent : public Y2AgentComp<ScriptingAgent>
{
public:
Y2SCRComponent(const char* root);

~Y2SCRComponent();
};

#endif

0 comments on commit 1943107

Please sign in to comment.