From b6adf09c4d2cc3f1d0285950c151b1fd7688ec67 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 4 Jun 2009 14:59:16 +0100 Subject: [PATCH] Generated code for the 'sleep' command. --- capitests/tests.c | 61 ++++++++++++++++++++- daemon/actions.h | 1 + daemon/stubs.c | 27 ++++++++++ fish/cmds.c | 21 ++++++++ fish/completion.c | 1 + guestfish-actions.pod | 6 +++ guestfs-actions.pod | 9 ++++ java/com/redhat/et/libguestfs/GuestFS.java | 17 ++++++ java/com_redhat_et_libguestfs_GuestFS.c | 16 ++++++ ocaml/guestfs.ml | 1 + ocaml/guestfs.mli | 3 ++ ocaml/guestfs_c_actions.c | 23 ++++++++ perl/Guestfs.xs | 11 ++++ perl/lib/Sys/Guestfs.pm | 4 ++ python/guestfs-py.c | 26 +++++++++ python/guestfs.py | 5 ++ ruby/ext/guestfs/_guestfs.c | 20 +++++++ src/guestfs-actions.c | 87 ++++++++++++++++++++++++++++++ src/guestfs-actions.h | 1 + src/guestfs_protocol.c | 10 ++++ src/guestfs_protocol.h | 10 +++- src/guestfs_protocol.x | 5 ++ 22 files changed, 363 insertions(+), 2 deletions(-) diff --git a/capitests/tests.c b/capitests/tests.c index 1b751e8..d263862 100644 --- a/capitests/tests.c +++ b/capitests/tests.c @@ -155,6 +155,59 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_e2fsck_f\" has no tests\n"); } +static int test_sleep_0_skip (void) +{ + const char *str; + + str = getenv ("SKIP_TEST_SLEEP_0"); + if (str && strcmp (str, "1") == 0) return 1; + str = getenv ("SKIP_TEST_SLEEP"); + if (str && strcmp (str, "1") == 0) return 1; + return 0; +} + +static int test_sleep_0 (void) +{ + if (test_sleep_0_skip ()) { + printf ("%s skipped (reason: SKIP_TEST_* variable set)\n", "test_sleep_0"); + return 0; + } + + /* InitNone|InitEmpty for test_sleep_0 */ + { + char device[] = "/dev/sda"; + device[5] = devchar; + int r; + suppress_error = 0; + r = guestfs_blockdev_setrw (g, device); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + /* TestRun for sleep (0) */ + { + int r; + suppress_error = 0; + r = guestfs_sleep (g, 1); + if (r == -1) + return -1; + } + return 0; +} + static int test_find_0_skip (void) { const char *str; @@ -16158,9 +16211,15 @@ int main (int argc, char *argv[]) free (devs[i]); free (devs); - nr_tests = 143; + nr_tests = 144; test_num++; + printf ("%3d/%3d test_sleep_0\n", test_num, nr_tests); + if (test_sleep_0 () == -1) { + printf ("test_sleep_0 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_find_0\n", test_num, nr_tests); if (test_find_0 () == -1) { printf ("test_find_0 FAILED\n"); diff --git a/daemon/actions.h b/daemon/actions.h index 1615cfb..2c86947 100644 --- a/daemon/actions.h +++ b/daemon/actions.h @@ -129,3 +129,4 @@ extern int do_lvresize (const char *device, int mbytes); extern int do_resize2fs (const char *device); extern char **do_find (const char *directory); extern int do_e2fsck_f (const char *device); +extern int do_sleep (int secs); diff --git a/daemon/stubs.c b/daemon/stubs.c index 51a7562..0e6af84 100644 --- a/daemon/stubs.c +++ b/daemon/stubs.c @@ -2719,6 +2719,30 @@ done: xdr_free ((xdrproc_t) xdr_guestfs_e2fsck_f_args, (char *) &args); } +static void sleep_stub (XDR *xdr_in) +{ + int r; + struct guestfs_sleep_args args; + int secs; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_sleep_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "sleep"); + return; + } + secs = args.secs; + + r = do_sleep (secs); + if (r == -1) + /* do_sleep has already called reply_with_error */ + goto done; + + reply (NULL, NULL); +done: + xdr_free ((xdrproc_t) xdr_guestfs_sleep_args, (char *) &args); +} + void dispatch_incoming_message (XDR *xdr_in) { switch (proc_nr) { @@ -3046,6 +3070,9 @@ void dispatch_incoming_message (XDR *xdr_in) case GUESTFS_PROC_E2FSCK_F: e2fsck_f_stub (xdr_in); break; + case GUESTFS_PROC_SLEEP: + sleep_stub (xdr_in); + break; default: reply_with_error ("dispatch_incoming_message: unknown procedure number %d", proc_nr); } diff --git a/fish/cmds.c b/fish/cmds.c index bb982d4..8e8e08a 100644 --- a/fish/cmds.c +++ b/fish/cmds.c @@ -137,6 +137,7 @@ void list_commands (void) printf ("%-20s %s\n", "sfdisk-disk-geometry", "display the disk geometry from the partition table"); printf ("%-20s %s\n", "sfdisk-kernel-geometry", "display the kernel geometry"); printf ("%-20s %s\n", "sfdisk-l", "display the partition table"); + printf ("%-20s %s\n", "sleep", "sleep for some seconds"); printf ("%-20s %s\n", "stat", "get file information"); printf ("%-20s %s\n", "statvfs", "get file system statistics"); printf ("%-20s %s\n", "strings", "print the printable strings in a file"); @@ -552,6 +553,9 @@ void display_command (const char *cmd) if (strcasecmp (cmd, "e2fsck_f") == 0 || strcasecmp (cmd, "e2fsck-f") == 0) pod2text ("e2fsck-f - check an ext2/ext3 filesystem", " e2fsck-f \n\nThis runs C, ie. runs the ext2/ext3\nfilesystem checker on C, noninteractively (C<-p>),\neven if the filesystem appears to be clean (C<-f>).\n\nThis command is only needed because of C\n(q.v.). Normally you should use C."); else + if (strcasecmp (cmd, "sleep") == 0) + pod2text ("sleep - sleep for some seconds", " sleep \n\nSleep for C seconds."); + else display_builtin_command (cmd); } @@ -2698,6 +2702,20 @@ static int run_e2fsck_f (const char *cmd, int argc, char *argv[]) return r; } +static int run_sleep (const char *cmd, int argc, char *argv[]) +{ + int r; + int secs; + if (argc != 1) { + fprintf (stderr, "%s should have 1 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + secs = atoi (argv[0]); + r = guestfs_sleep (g, secs); + return r; +} + int run_action (const char *cmd, int argc, char *argv[]) { if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0) @@ -3087,6 +3105,9 @@ int run_action (const char *cmd, int argc, char *argv[]) if (strcasecmp (cmd, "e2fsck_f") == 0 || strcasecmp (cmd, "e2fsck-f") == 0) return run_e2fsck_f (cmd, argc, argv); else + if (strcasecmp (cmd, "sleep") == 0) + return run_sleep (cmd, argc, argv); + else { fprintf (stderr, "%s: unknown command\n", cmd); return -1; diff --git a/fish/completion.c b/fish/completion.c index 287c5aa..95c3e5d 100644 --- a/fish/completion.c +++ b/fish/completion.c @@ -175,6 +175,7 @@ static const char *const commands[] = { "resize2fs", "find", "e2fsck-f", + "sleep", NULL }; diff --git a/guestfish-actions.pod b/guestfish-actions.pod index 9b07c40..5d0b5d2 100644 --- a/guestfish-actions.pod +++ b/guestfish-actions.pod @@ -1210,6 +1210,12 @@ This displays the partition table on C, in the human-readable output of the L command. It is not intended to be parsed. +=head2 sleep + + sleep secs + +Sleep for C seconds. + =head2 stat stat path diff --git a/guestfs-actions.pod b/guestfs-actions.pod index 2705c3b..054b65c 100644 --- a/guestfs-actions.pod +++ b/guestfs-actions.pod @@ -1629,6 +1629,15 @@ not intended to be parsed. This function returns a string, or NULL on error. I. +=head2 guestfs_sleep + + int guestfs_sleep (guestfs_h *handle, + int secs); + +Sleep for C seconds. + +This function returns 0 on success or -1 on error. + =head2 guestfs_stat struct guestfs_stat *guestfs_stat (guestfs_h *handle, diff --git a/java/com/redhat/et/libguestfs/GuestFS.java b/java/com/redhat/et/libguestfs/GuestFS.java index 1d00547..3f03d75 100644 --- a/java/com/redhat/et/libguestfs/GuestFS.java +++ b/java/com/redhat/et/libguestfs/GuestFS.java @@ -3337,4 +3337,21 @@ public HashMap test0rhashtableerr () private native void _e2fsck_f (long g, String device) throws LibGuestFSException; + /** + * sleep for some seconds + *

+ * Sleep for "secs" seconds. + *

+ * @throws LibGuestFSException + */ + public void sleep (int secs) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("sleep: handle is closed"); + _sleep (g, secs); + } + private native void _sleep (long g, int secs) + throws LibGuestFSException; + } diff --git a/java/com_redhat_et_libguestfs_GuestFS.c b/java/com_redhat_et_libguestfs_GuestFS.c index fc858f6..fc207c1 100644 --- a/java/com_redhat_et_libguestfs_GuestFS.c +++ b/java/com_redhat_et_libguestfs_GuestFS.c @@ -3950,3 +3950,19 @@ Java_com_redhat_et_libguestfs_GuestFS__1e2fsck_1f } } +JNIEXPORT void JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1sleep + (JNIEnv *env, jobject obj, jlong jg, jint jsecs) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + int r; + int secs; + + secs = jsecs; + r = guestfs_sleep (g, secs); + if (r == -1) { + throw_exception (env, guestfs_last_error (g)); + return ; + } +} + diff --git a/ocaml/guestfs.ml b/ocaml/guestfs.ml index cf75d7b..7df3308 100644 --- a/ocaml/guestfs.ml +++ b/ocaml/guestfs.ml @@ -275,3 +275,4 @@ external lvresize : t -> string -> int -> unit = "ocaml_guestfs_lvresize" external resize2fs : t -> string -> unit = "ocaml_guestfs_resize2fs" external find : t -> string -> string array = "ocaml_guestfs_find" external e2fsck_f : t -> string -> unit = "ocaml_guestfs_e2fsck_f" +external sleep : t -> int -> unit = "ocaml_guestfs_sleep" diff --git a/ocaml/guestfs.mli b/ocaml/guestfs.mli index 686bc60..6f77bb9 100644 --- a/ocaml/guestfs.mli +++ b/ocaml/guestfs.mli @@ -604,3 +604,6 @@ val find : t -> string -> string array val e2fsck_f : t -> string -> unit (** check an ext2/ext3 filesystem *) +val sleep : t -> int -> unit +(** sleep for some seconds *) + diff --git a/ocaml/guestfs_c_actions.c b/ocaml/guestfs_c_actions.c index 73dfdd9..615d209 100644 --- a/ocaml/guestfs_c_actions.c +++ b/ocaml/guestfs_c_actions.c @@ -4151,3 +4151,26 @@ ocaml_guestfs_e2fsck_f (value gv, value devicev) CAMLreturn (rv); } +CAMLprim value +ocaml_guestfs_sleep (value gv, value secsv) +{ + CAMLparam2 (gv, secsv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("sleep: used handle after closing it"); + + int secs = Int_val (secsv); + int r; + + caml_enter_blocking_section (); + r = guestfs_sleep (g, secs); + caml_leave_blocking_section (); + if (r == -1) + ocaml_guestfs_raise_error (g, "sleep"); + + rv = Val_unit; + CAMLreturn (rv); +} + diff --git a/perl/Guestfs.xs b/perl/Guestfs.xs index 08b55b8..7b923f6 100644 --- a/perl/Guestfs.xs +++ b/perl/Guestfs.xs @@ -2523,3 +2523,14 @@ PREINIT: if (r == -1) croak ("e2fsck_f: %s", guestfs_last_error (g)); +void +sleep (g, secs) + guestfs_h *g; + int secs; +PREINIT: + int r; + PPCODE: + r = guestfs_sleep (g, secs); + if (r == -1) + croak ("sleep: %s", guestfs_last_error (g)); + diff --git a/perl/lib/Sys/Guestfs.pm b/perl/lib/Sys/Guestfs.pm index f8e6b77..445d9b8 100644 --- a/perl/lib/Sys/Guestfs.pm +++ b/perl/lib/Sys/Guestfs.pm @@ -1111,6 +1111,10 @@ This displays the partition table on C, in the human-readable output of the L command. It is not intended to be parsed. +=item $h->sleep ($secs); + +Sleep for C seconds. + =item %statbuf = $h->stat ($path); Returns file information for the given C. diff --git a/python/guestfs-py.c b/python/guestfs-py.c index 71ef85e..dcfa5fb 100644 --- a/python/guestfs-py.c +++ b/python/guestfs-py.c @@ -4406,6 +4406,31 @@ py_guestfs_e2fsck_f (PyObject *self, PyObject *args) return py_r; } +static PyObject * +py_guestfs_sleep (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + int r; + int secs; + + if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_sleep", + &py_g, &secs)) + return NULL; + g = get_handle (py_g); + + r = guestfs_sleep (g, secs); + if (r == -1) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + Py_INCREF (Py_None); + py_r = Py_None; + return py_r; +} + static PyMethodDef methods[] = { { (char *) "create", py_guestfs_create, METH_VARARGS, NULL }, { (char *) "close", py_guestfs_close, METH_VARARGS, NULL }, @@ -4569,6 +4594,7 @@ static PyMethodDef methods[] = { { (char *) "resize2fs", py_guestfs_resize2fs, METH_VARARGS, NULL }, { (char *) "find", py_guestfs_find, METH_VARARGS, NULL }, { (char *) "e2fsck_f", py_guestfs_e2fsck_f, METH_VARARGS, NULL }, + { (char *) "sleep", py_guestfs_sleep, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/python/guestfs.py b/python/guestfs.py index a75148e..dd9fa58 100644 --- a/python/guestfs.py +++ b/python/guestfs.py @@ -1599,3 +1599,8 @@ class GuestFS: """ return libguestfsmod.e2fsck_f (self._o, device) + def sleep (self, secs): + u"""Sleep for "secs" seconds. + """ + return libguestfsmod.sleep (self._o, secs) + diff --git a/ruby/ext/guestfs/_guestfs.c b/ruby/ext/guestfs/_guestfs.c index 4983932..661e371 100644 --- a/ruby/ext/guestfs/_guestfs.c +++ b/ruby/ext/guestfs/_guestfs.c @@ -3963,6 +3963,24 @@ static VALUE ruby_guestfs_e2fsck_f (VALUE gv, VALUE devicev) return Qnil; } +static VALUE ruby_guestfs_sleep (VALUE gv, VALUE secsv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "sleep"); + + int secs = NUM2INT (secsv); + + int r; + + r = guestfs_sleep (g, secs); + if (r == -1) + rb_raise (e_Error, "%s", guestfs_last_error (g)); + + return Qnil; +} + /* Initialize the module. */ void Init__guestfs () { @@ -4293,4 +4311,6 @@ void Init__guestfs () ruby_guestfs_find, 1); rb_define_method (c_guestfs, "e2fsck_f", ruby_guestfs_e2fsck_f, 1); + rb_define_method (c_guestfs, "sleep", + ruby_guestfs_sleep, 1); } diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index dfca204..955cfff 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -9893,3 +9893,90 @@ int guestfs_e2fsck_f (guestfs_h *g, return 0; } +struct sleep_ctx { + /* This flag is set by the callbacks, so we know we've done + * the callbacks as expected, and in the right sequence. + * 0 = not called, 1 = reply_cb called. + */ + int cb_sequence; + struct guestfs_message_header hdr; + struct guestfs_message_error err; +}; + +static void sleep_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct sleep_ctx *ctx = (struct sleep_ctx *) data; + + /* This should definitely not happen. */ + if (ctx->cb_sequence != 0) { + ctx->cb_sequence = 9999; + error (g, "%s: internal error: reply callback called twice", "guestfs_sleep"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_sleep"); + return; + } + if (ctx->hdr.status == GUESTFS_STATUS_ERROR) { + if (!xdr_guestfs_message_error (xdr, &ctx->err)) { + error (g, "%s: failed to parse reply error", "guestfs_sleep"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_sleep (guestfs_h *g, + int secs) +{ + struct guestfs_sleep_args args; + struct sleep_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_sleep") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.secs = secs; + serial = guestfs__send_sync (g, GUESTFS_PROC_SLEEP, + (xdrproc_t) xdr_guestfs_sleep_args, (char *) &args); + if (serial == -1) { + guestfs_end_busy (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, sleep_reply_cb, &ctx); + (void) ml->main_loop_run (ml, g); + guestfs_set_reply_callback (g, NULL, NULL); + if (ctx.cb_sequence != 1) { + error (g, "%s reply failed, see earlier error messages", "guestfs_sleep"); + guestfs_end_busy (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SLEEP, serial) == -1) { + guestfs_end_busy (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + free (ctx.err.error_message); + guestfs_end_busy (g); + return -1; + } + + guestfs_end_busy (g); + return 0; +} + diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h index 2cd3bdb..1afb4f0 100644 --- a/src/guestfs-actions.h +++ b/src/guestfs-actions.h @@ -179,3 +179,4 @@ extern int guestfs_lvresize (guestfs_h *handle, const char *device, int mbytes); extern int guestfs_resize2fs (guestfs_h *handle, const char *device); extern char **guestfs_find (guestfs_h *handle, const char *directory); extern int guestfs_e2fsck_f (guestfs_h *handle, const char *device); +extern int guestfs_sleep (guestfs_h *handle, int secs); diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c index 2838d95..df49420 100644 --- a/src/guestfs_protocol.c +++ b/src/guestfs_protocol.c @@ -1839,6 +1839,16 @@ xdr_guestfs_e2fsck_f_args (XDR *xdrs, guestfs_e2fsck_f_args *objp) } bool_t +xdr_guestfs_sleep_args (XDR *xdrs, guestfs_sleep_args *objp) +{ + register int32_t *buf; + + if (!xdr_int (xdrs, &objp->secs)) + return FALSE; + return TRUE; +} + +bool_t xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp) { register int32_t *buf; diff --git a/src/guestfs_protocol.h b/src/guestfs_protocol.h index dd5da00..68f4ec6 100644 --- a/src/guestfs_protocol.h +++ b/src/guestfs_protocol.h @@ -930,6 +930,11 @@ struct guestfs_e2fsck_f_args { }; typedef struct guestfs_e2fsck_f_args guestfs_e2fsck_f_args; +struct guestfs_sleep_args { + int secs; +}; +typedef struct guestfs_sleep_args guestfs_sleep_args; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -1039,7 +1044,8 @@ enum guestfs_procedure { GUESTFS_PROC_RESIZE2FS = 106, GUESTFS_PROC_FIND = 107, GUESTFS_PROC_E2FSCK_F = 108, - GUESTFS_PROC_NR_PROCS = 108 + 1, + GUESTFS_PROC_SLEEP = 109, + GUESTFS_PROC_NR_PROCS = 109 + 1, }; typedef enum guestfs_procedure guestfs_procedure; #define GUESTFS_MESSAGE_MAX 4194304 @@ -1237,6 +1243,7 @@ extern bool_t xdr_guestfs_resize2fs_args (XDR *, guestfs_resize2fs_args*); extern bool_t xdr_guestfs_find_args (XDR *, guestfs_find_args*); extern bool_t xdr_guestfs_find_ret (XDR *, guestfs_find_ret*); extern bool_t xdr_guestfs_e2fsck_f_args (XDR *, guestfs_e2fsck_f_args*); +extern bool_t xdr_guestfs_sleep_args (XDR *, guestfs_sleep_args*); extern bool_t xdr_guestfs_procedure (XDR *, guestfs_procedure*); extern bool_t xdr_guestfs_message_direction (XDR *, guestfs_message_direction*); extern bool_t xdr_guestfs_message_status (XDR *, guestfs_message_status*); @@ -1393,6 +1400,7 @@ extern bool_t xdr_guestfs_resize2fs_args (); extern bool_t xdr_guestfs_find_args (); extern bool_t xdr_guestfs_find_ret (); extern bool_t xdr_guestfs_e2fsck_f_args (); +extern bool_t xdr_guestfs_sleep_args (); extern bool_t xdr_guestfs_procedure (); extern bool_t xdr_guestfs_message_direction (); extern bool_t xdr_guestfs_message_status (); diff --git a/src/guestfs_protocol.x b/src/guestfs_protocol.x index b2baab5..556ed60 100644 --- a/src/guestfs_protocol.x +++ b/src/guestfs_protocol.x @@ -719,6 +719,10 @@ struct guestfs_e2fsck_f_args { string device<>; }; +struct guestfs_sleep_args { + int secs; +}; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -828,6 +832,7 @@ enum guestfs_procedure { GUESTFS_PROC_RESIZE2FS = 106, GUESTFS_PROC_FIND = 107, GUESTFS_PROC_E2FSCK_F = 108, + GUESTFS_PROC_SLEEP = 109, GUESTFS_PROC_NR_PROCS }; -- 1.8.3.1