Revert "out-of-tree build: daemon"
[libguestfs.git] / generator / generator_ruby.ml
index 7c8788d..38121b5 100644 (file)
@@ -214,7 +214,8 @@ ruby_event_callback_wrapper (guestfs_h *g,
                              const uint64_t *array, size_t array_len)
 {
   size_t i;
-  VALUE eventv, event_handlev, bufv, arrayv, argv;
+  VALUE eventv, event_handlev, bufv, arrayv;
+  VALUE argv[5];
 
   eventv = ULL2NUM (event);
   event_handlev = INT2NUM (event_handle);
@@ -225,35 +226,41 @@ ruby_event_callback_wrapper (guestfs_h *g,
   for (i = 0; i < array_len; ++i)
     rb_ary_push (arrayv, ULL2NUM (array[i]));
 
-  /* Wrap up the arguments in an array which will be unpacked
-   * and passed as multiple arguments.  This is a crap limitation
-   * of rb_rescue.
+  /* This is a crap limitation of rb_rescue.
    * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/~poffice/mail/ruby-talk/65698
    */
-  argv = rb_ary_new2 (5);
-  rb_ary_store (argv, 0, * (VALUE *) data /* function */);
-  rb_ary_store (argv, 1, eventv);
-  rb_ary_store (argv, 2, event_handlev);
-  rb_ary_store (argv, 3, bufv);
-  rb_ary_store (argv, 4, arrayv);
-
-  rb_rescue (ruby_event_callback_wrapper_wrapper, argv,
+  argv[0] = * (VALUE *) data; /* function */
+  argv[1] = eventv;
+  argv[2] = event_handlev;
+  argv[3] = bufv;
+  argv[4] = arrayv;
+
+  rb_rescue (ruby_event_callback_wrapper_wrapper, (VALUE) argv,
              ruby_event_callback_handle_exception, Qnil);
 }
 
 static VALUE
-ruby_event_callback_wrapper_wrapper (VALUE argv)
+ruby_event_callback_wrapper_wrapper (VALUE argvv)
 {
+  VALUE *argv = (VALUE *) argvv;
   VALUE fn, eventv, event_handlev, bufv, arrayv;
 
-  fn = rb_ary_entry (argv, 0);
-  eventv = rb_ary_entry (argv, 1);
-  event_handlev = rb_ary_entry (argv, 2);
-  bufv = rb_ary_entry (argv, 3);
-  arrayv = rb_ary_entry (argv, 4);
+  fn = argv[0];
 
-  rb_funcall (fn, rb_intern (\"call\"), 4,
-              eventv, event_handlev, bufv, arrayv);
+  /* Check the Ruby callback still exists.  For reasons which are not
+   * fully understood, even though we registered this as a global root,
+   * it is still possible for the callback to go away (fn value remains
+   * but its type changes from T_DATA to T_NONE).  (RHBZ#733297)
+   */
+  if (rb_type (fn) != T_NONE) {
+    eventv = argv[1];
+    event_handlev = argv[2];
+    bufv = argv[3];
+    arrayv = argv[4];
+
+    rb_funcall (fn, rb_intern (\"call\"), 4,
+                eventv, event_handlev, bufv, arrayv);
+  }
 
   return Qnil;
 }
@@ -264,7 +271,7 @@ ruby_event_callback_handle_exception (VALUE not_used, VALUE exn)
   /* Callbacks aren't supposed to throw exceptions.  The best we
    * can do is to print the error.
    */
-  fprintf (stderr, \"libguestfs: exception in callback: %%s\",
+  fprintf (stderr, \"libguestfs: exception in callback: %%s\\n\",
            StringValueCStr (exn));
 
   return Qnil;
@@ -303,6 +310,26 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
   return r;
 }
 
+/*
+ * call-seq:
+ *   g.user_cancel() -> nil
+ *
+ * Call
+ * +guestfs_user_cancel+[http://libguestfs.org/guestfs.3.html#guestfs_user_cancel]
+ * to cancel the current transfer.  This is safe to call from Ruby
+ * signal handlers and threads.
+ */
+static VALUE
+ruby_user_cancel (VALUE gv)
+{
+  guestfs_h *g;
+
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (g)
+    guestfs_user_cancel (g);
+  return Qnil;
+}
+
 ";
 
   List.iter (
@@ -570,6 +597,8 @@ void Init__guestfs ()
                     ruby_set_event_callback, 2);
   rb_define_method (c_guestfs, \"delete_event_callback\",
                     ruby_delete_event_callback, 1);
+  rb_define_method (c_guestfs, \"user_cancel\",
+                    ruby_user_cancel, 0);
 
 ";