How about adding method hook

Hi, guys.

I want An agent on machine, so that it can fake user actions.

class Agent
def initialize(user, dir)
@user = user
@dir = dir
end
def mkdir(dir)
bak = Process.euid
mkdir
Process.euid = bak
end
def create_file(file, content)
bak = Process.euid
echo #{content} > #{file}
Process.euid = bak
end
end

My problem is, I have a very long list of actions, like mv, touch,
mkdir,
rm, etc.
I don’t want to add save and restore euid for all methods.

I kind of guess here I should use some meta programming skills. here’s
what
i tried.

class Agent
def self.as_me_do(method_name, &block)
define_method method_name do
block
end
end

as_me_do mkdir do
mkdir #{dir}
end
end

but this still generate code duplications, and it doesn’t work.
I’m not sure how this could be done gracefully.

Any one been here before?

thanks

Yan Jiang

On Wed, Jun 27, 2012 at 10:12 AM, salamond [email protected] wrote:

bak = Process.euid
My problem is, I have a very long list of actions, like mv, touch, mkdir,
rm, etc.
I don’t want to add save and restore euid for all methods.

Btw, your code is not safe as it does not restore on exception.

Any one been here before?

Could be that AspectR is for you. I also once created code to insert
methods before and after a method invocation but I can’t seem to find
it in the archives.

A simple solution could look like this

Kind regards

robert

On 27/06/2012, at 8:12 PM, salamond [email protected] wrote:

Hi, guys.

I want An agent on machine, so that it can fake user actions.

My problem is, I have a very long list of actions, like mv, touch, mkdir, rm,
etc.
I don’t want to add save and restore euid for all methods.

First thing I’d do is remove all the duplication, maybe something like
this…

class Agent
def initialize(user, dir)
@user = user
@dir = dir
end

def as_me_do
bak = Process.euid
yield

ensure # hat tip Robert
Process.euid = bak
end

def mkdir(dir)
as_me_do {mkdir}
end

def create_file(file, content)
as_me_do {echo #{content} > #{file}}
end
end

The next step might be more obvious.

I kind of guess here I should use some meta programming skills. here’s what i
tried.

Defining methods is going to be tricky, because you don’t have
consistent method signatures and system calls. Maybe a have a hash of
methods => system calls and method missing?

Also, why back ticks, why not use ruby File methods?

Henry

thanks, robert.

I applied aspectr once in my project.
the code i’m trying here is that i want to implement my own rspec.
but failed. :slight_smile:

On Wed, Jun 27, 2012 at 5:18 PM, Robert K.

Hi, Henry.

thanks for the info.

method missing might be a good option.

class Process
def run; end
end

class FileUtils
def mkdir; end
end

class Agent
def method_missing
if Process.new.respond_to(method) || FileUtiles.response_to(method)
do_as_me
Process.new.send(method)
end
end
end

sort of like this. what do you think?

On Thu, Jun 28, 2012 at 5:21 AM, salamond [email protected] wrote:

sorry to robert.

Took me a night to figure out the git code. with method_added and
class_eval.

:slight_smile: So now you know what the thread local is for? If not comment
line 7 and see what happens.

nice piece of code.

Thank you!

it works too.

Yes, I actually tested it. :slight_smile:

Kind regards

robert

sorry to robert.

Took me a night to figure out the git code. with method_added and
class_eval.

nice piece of code.

it works too.