c++ - Calling a library function with a twist - modifying some of its behaviour -


i trying embed octave library within larger program. required can start , stop octave interpreter @ larger program. however, function stop octave interpreter cleanly calls exit(), kills larger program also. octave library function clean_up_and_exit().

ideally call cleanup part (do_octave_atexit), , skip call exit.

i tried following:

1) calling do_octave_atexit, symbol not exported in octave library. tried access anyway no avail.

2) tried hooking call exit, , replacing function not exit, via ld_preload. messed other calls exit hooked also.

3) tried detect when exit being called octave prevent then, detecting calling function call backtrace. reason did not show expect true call hierarchy. reason showed main function, , no contents of call hierarchy through octave library. in not able detect call coming octave.

the code using call octave functions looks like:

// // octave setup functions // extern "c" void oct_init (const char * path) {   string_vector argv (2);   argv(0) = "embedded";   argv(1) = "-q";    octave_main (2, argv.c_str_vec (), 1);   if(strlen(path) > 1) {     oct_addpath(path);   } }  extern "c" void oct_exit (void) {   printf("exiting!");   clean_up_and_exit (1,1); } 

the key function here clean_up_and_exit - implemented in octave source as:

void clean_up_and_exit (int retval, bool safe_to_return) {   do_octave_atexit ();    if (octave_link::exit (retval))   {     if (safe_to_return)       return;     else      {        gnulib::sleep (86400);      }  }  else  {    if (octave_exit)      (*octave_exit) (retval);  } } 

so above code calls function want (do_octave_atexit) proceeds call *octave_exit - function pointer exit().

ideally want either a) prevent call exit(), or b) trap call when comes octave , prevent it, , allow when comes other sources. have not been able a) or b) far!

so @ point out of ideas. recompile octave, solution supposed work stock octave install.

this had work on linux/gcc environment.

any , suggestions tricky issue appreciated.

you have fork, , run octave on separate process. here's simple example on how it:

#include <unistd.h> #include <iostream>  #include <octave/oct.h> #include <octave/octave.h> #include <octave/parse.h> #include <octave/toplev.h>  int main_octave (void) {   string_vector argv (2);   argv(0) = "embedded";   argv(1) = "-q";    octave_main (2, argv.c_str_vec (), 1);    octave_value_list out = feval ("pi", octave_value_list (0), 1);   if (! error_state && out.length () > 0)     std::cout << "pi " << out(0).double_value () << std::endl;   else     std::cout << "invalid\n";    clean_up_and_exit (0); }  int main (void) {   pid_t pid = fork();   if (pid == 0)     main_octave ();   else if (pid > 0)     {       std::cout << "parent process going nap" << std::endl;       sleep (5);     }   else     {       std::cout << "unable fork()" << std::endl;       return 1;     }   std::cout << "leaving standalone application" << std::endl;   return 0; } 

which on system returns:

$ mkoctfile --link-stand-alone embedded.cc -o embedded $ ./embedded parent process going nap pi 3.14159 leaving standalone application 

so can keep running application after exiting octave process. of course, if want start , stop octave multiple times, have fork multiple times. also, i'd recommend ask such things on octave mailing list, you're more helpful answers faster there.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -