From 03ce0ef46cf209c69b88163422c600c8c4df076d Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Wed, 23 Sep 2015 21:37:42 +1000 Subject: [PATCH] Intercept exit then call gmt module --- test/gmttest.cpp | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/test/gmttest.cpp b/test/gmttest.cpp index dcdd18a..70e31ac 100644 --- a/test/gmttest.cpp +++ b/test/gmttest.cpp @@ -14,34 +14,36 @@ struct gmtworkthreadpars { void* api; const char* module; - void* opts; + struct GMT_OPTION* opts; int fd; int ret; }; +void gmtonexithandler(int ret, void* x) +{ + reinterpret_cast(x)->ret=ret; + close(reinterpret_cast(x)->fd); + pthread_exit(&(reinterpret_cast(x)->ret)); +} + void* gmtworkthread(void* x) { struct gmtworkthreadpars* p=reinterpret_cast(x); - char c='\n'; - - //unshare(CLONE_FILES); - //dup2(p->fd,1); - //write(p->fd,&c,1); - GMT_Append_Option(p->api,GMT_Make_Option(p->api,'>',const_cast(("/dev/fd/"+std::to_string(p->fd)).c_str())),reinterpret_cast(p->opts)); - //write(p->fd,&c,1); // Use this instead of mutex + on_exit(gmtonexithandler,x); + GMT_Append_Option(p->api,GMT_Make_Option(p->api,'>',const_cast(("/dev/fd/"+std::to_string(p->fd)).c_str())),p->opts); p->ret=GMT_Call_Module(p->api,p->module,GMT_MODULE_OPT,p->opts); - //close(1); - close(p->fd); + exit(p->ret); return 0; } -int callgmtmodule(void *api, const char *module, void *opts, std::string& res) +int callgmtmodule(void *api, const char *module, struct GMT_OPTION *opts, std::string& res) { int pipefd[2]; ssize_t br; char buffer[4096]; pthread_t wthr; struct gmtworkthreadpars p; + int *pret; pipe(pipefd); p.api=api; @@ -52,17 +54,19 @@ int callgmtmodule(void *api, const char *module, void *opts, std::string& res) pthread_create(&wthr,0,&gmtworkthread,&p); res.erase(); - read(pipefd[0],buffer,1); - close(pipefd[1]); + //read(pipefd[0],buffer,1); + //close(pipefd[1]); do { br=read(pipefd[0],buffer,4096); std::cout<(&pret)); + + return *pret; } @@ -77,15 +81,15 @@ int main() char fname[16]; std::string out; - gmtapi=GMT_Create_Session(text,2,0,print_func); + gmtapi=GMT_Create_Session(text,2,3,print_func); io=GMT_Register_IO(gmtapi,GMT_IS_TEXTSET,GMT_IS_DUPLICATE,GMT_IS_NONE,GMT_OUT,0,0); GMT_Encode_ID(gmtapi,fname,io); opts=GMT_Create_Options(gmtapi,0,(void*)args.c_str()); //ret=GMT_Init_IO(gmtapi,GMT_IS_TEXTSET,GMT_IS_NONE,GMT_OUT,5,0,opts); std::cout<