Index: ChangeLog
===================================================================
--- ChangeLog	(revision 5814)
+++ ChangeLog	(revision 5834)
@@ -6,4 +6,5 @@
 	* Made build system tests return correct error codes.
 	* Improved CSI: Make it easier to register and access services.
+	* Allow seperation of plugin's stdvios (See: #296)
 
 v. 1.0beta8 - Mon Dec 10 2012 12:42 CET
Index: libroar/roardl.c
===================================================================
--- libroar/roardl.c	(revision 5823)
+++ libroar/roardl.c	(revision 5834)
@@ -59,4 +59,5 @@
   void *  global_data_state;
   struct roar_notify_core * notifycore;
+  struct roar_vio_calls * stdvios[3];
  } context;
  unsigned int runtime_flags;
@@ -357,5 +358,6 @@
  struct roar_dl_lhandle * ret = NULL;
 #if defined(ROAR_HAVE_H_DLFCN)
-#ifdef RTLD_DEEPBIND
+#if defined(RTLD_DEEPBIND) && 0
+ // FIXME: this is current disabled. See #296.
  int libdl_flags = RTLD_DEEPBIND;
 #else
@@ -421,4 +423,11 @@
 
  ret->para = para;
+
+ if ( roar_vio_ref(roar_stdin) == 0 )
+  ret->context.stdvios[0] = roar_stdin;
+ if ( roar_vio_ref(roar_stdout) == 0 )
+  ret->context.stdvios[1] = roar_stdout;
+ if ( roar_vio_ref(roar_stderr) == 0 )
+  ret->context.stdvios[2] = roar_stderr; 
 
  if ( ra_init ) {
@@ -433,4 +442,7 @@
     FreeLibrary(ret->handle);
 #endif
+   roar_vio_unref(ret->context.stdvios[0]);
+   roar_vio_unref(ret->context.stdvios[1]);
+   roar_vio_unref(ret->context.stdvios[2]);
    roar_mm_free(ret);
    roar_error = err;
@@ -501,4 +513,8 @@
  ret = -1;
 #endif
+
+ roar_vio_unref(lhandle->context.stdvios[0]);
+ roar_vio_unref(lhandle->context.stdvios[1]);
+ roar_vio_unref(lhandle->context.stdvios[2]);
 
  if ( lhandle->context.global_data != NULL )
@@ -726,4 +742,18 @@
 }
 
+static inline void __swap_stdvios(struct roar_dl_lhandle * lhandle) {
+ struct roar_vio_calls * vio;
+
+ vio = roar_stdin;
+ roar_stdin = lhandle->context.stdvios[0];
+ lhandle->context.stdvios[0] = vio;
+ vio = roar_stdout;
+ roar_stdout = lhandle->context.stdvios[1];
+ lhandle->context.stdvios[1] = vio;
+ vio = roar_stderr;
+ roar_stderr = lhandle->context.stdvios[2];
+ lhandle->context.stdvios[2] = vio;
+}
+
 int                      roar_dl_context_restore(struct roar_dl_lhandle * lhandle) {
  struct roar_error_state error_state;
@@ -740,4 +770,6 @@
  roar_err_restore(&(lhandle->context.error_state));
  lhandle->context.error_state = error_state;
+
+ __swap_stdvios(lhandle);
 
  if ( lhandle->lib->global_data_pointer != NULL ) {
@@ -777,4 +809,6 @@
   *(lhandle->lib->global_data_pointer) = lhandle->context.global_data_state;
  }
+
+ __swap_stdvios(lhandle);
 
  roar_err_store(&error_state);
Index: libroar/vio.c
===================================================================
--- libroar/vio.c	(revision 5823)
+++ libroar/vio.c	(revision 5834)
@@ -218,4 +218,5 @@
 int     roar_vio_unref    (struct roar_vio_calls * vio) {
  int ret;
+ int err;
 
  ROAR_DBG("roar_vio_unref(vio=%p) = ?", vio);
@@ -240,7 +241,14 @@
  roar_err_update();
 
- if ( vio->flags & ROAR_VIO_FLAGS_FREESELF )
+ err = roar_error;
+
+ if ( vio->flags & ROAR_VIO_FLAGS_FREESELF ) {
+  roar_vio_clear_calls(vio);
   roar_mm_free(vio);
-
+ } else {
+  roar_vio_clear_calls(vio);
+ }
+
+ roar_error = err;
  return ret;
 }
Index: libroar/vio_stdvios.c
===================================================================
--- libroar/vio_stdvios.c	(revision 5823)
+++ libroar/vio_stdvios.c	(revision 5834)
@@ -39,5 +39,5 @@
  { .inst     = (void*)(ROAR_INSTINT)(ROAR_STDIN  + 1),
    .flags    = 0x00000000,
-   .refc     = 0,
+   .refc     = 1,
    .read     = roar_vio_basic_read,
    .write    = roar_vio_basic_write,
@@ -49,5 +49,5 @@
  { .inst     = (void*)(ROAR_INSTINT)(ROAR_STDOUT + 1),
    .flags    = 0x00000000,
-   .refc     = 0,
+   .refc     = 1,
    .read     = roar_vio_basic_read,
    .write    = roar_vio_basic_write,
@@ -59,5 +59,5 @@
  { .inst     = (void*)(ROAR_INSTINT)(ROAR_STDERR + 1),
    .flags    = 0x00000000,
-   .refc     = 0,
+   .refc     = 1,
    .read     = roar_vio_basic_read,
    .write    = roar_vio_basic_write,
