|
Can I use Aprobe to change the
command run by a call to system() from my
application to run my own little script instead?
Yes, replace the parameter to system() with a path to your script.
Imagine the possibilities.
For example, here is a small C program that calls system():
#include <stdlib.h>
main()
{
system( "/bin/ls -al" );
}
Here is my_ls.apc, which is a probe that you can apply to the program:
// change these 2 lines to work on
// a different command:
static char cmd_to_change[] = "/bin/ls";
static char my_script[] = "/tmp/my_ls
";
probe thread
{
// it's in "libc.so" for Solaris
// it's in "libc.a(shr.o)" for AIX
//
probe "system()" in "libc.so"
{
ap_NameT new_command = NULL;
on_entry /* to system()" */
{
char *command = (char *)$1;
// for debugging, let's give some
// info about where we are:
log( "system() called with ",
ap_StringValue($1));
ap_LogTraceback(99);
// make sure we only replace
// the correct command
{
char *cmdpos =
strstr(command, cmd_to_change);
if (cmdpos == command)
{
// replace it,
but preserve args
char *argstring =
command + strlen(cmd_to_change);
// allocate and
build a new_command
new_command =
ap_CatenateStrings(my_script,
argstring,
NULL);
// this replaces
it:
$1 = (int)new_command;
log("*** changed to: ",
ap_StringValue($1));
}
}
} /* on entry */
on_exit /* from system()" */
{
// indicate the return code for the
// system command:
log("system() returns ", $0);
// free our string:
ap_StrFree(new_command);
}
}
} /* probe thread */
Here is the contents my little script /tmp/my_ls:
echo "MY_LS: --->"
ls -ltF
echo "<---- MY_LS"
|