summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/guestfs-actions.c189
-rw-r--r--src/guestfs-actions.h2
-rw-r--r--src/guestfs_protocol.c41
-rw-r--r--src/guestfs_protocol.h35
-rw-r--r--src/guestfs_protocol.x18
5 files changed, 284 insertions, 1 deletions
diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c
index ecaa5662..f36fcf06 100644
--- a/src/guestfs-actions.c
+++ b/src/guestfs-actions.c
@@ -10074,3 +10074,192 @@ int guestfs_ntfs_3g_probe (guestfs_h *g,
return ctx.ret.status;
}
+struct sh_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;
+ struct guestfs_sh_ret ret;
+};
+
+static void sh_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ struct sh_ctx *ctx = (struct sh_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_sh");
+ return;
+ }
+
+ ml->main_loop_quit (ml, g);
+
+ if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+ error (g, "%s: failed to parse reply header", "guestfs_sh");
+ 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_sh");
+ return;
+ }
+ goto done;
+ }
+ if (!xdr_guestfs_sh_ret (xdr, &ctx->ret)) {
+ error (g, "%s: failed to parse reply", "guestfs_sh");
+ return;
+ }
+ done:
+ ctx->cb_sequence = 1;
+}
+
+char *guestfs_sh (guestfs_h *g,
+ const char *command)
+{
+ struct guestfs_sh_args args;
+ struct sh_ctx ctx;
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ int serial;
+
+ if (check_state (g, "guestfs_sh") == -1) return NULL;
+ guestfs_set_busy (g);
+
+ memset (&ctx, 0, sizeof ctx);
+
+ args.command = (char *) command;
+ serial = guestfs__send_sync (g, GUESTFS_PROC_SH,
+ (xdrproc_t) xdr_guestfs_sh_args, (char *) &args);
+ if (serial == -1) {
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ guestfs__switch_to_receiving (g);
+ ctx.cb_sequence = 0;
+ guestfs_set_reply_callback (g, sh_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_sh");
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SH, serial) == -1) {
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+ error (g, "%s", ctx.err.error_message);
+ free (ctx.err.error_message);
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ guestfs_end_busy (g);
+ return ctx.ret.output; /* caller will free */
+}
+
+struct sh_lines_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;
+ struct guestfs_sh_lines_ret ret;
+};
+
+static void sh_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ struct sh_lines_ctx *ctx = (struct sh_lines_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_sh_lines");
+ return;
+ }
+
+ ml->main_loop_quit (ml, g);
+
+ if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+ error (g, "%s: failed to parse reply header", "guestfs_sh_lines");
+ 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_sh_lines");
+ return;
+ }
+ goto done;
+ }
+ if (!xdr_guestfs_sh_lines_ret (xdr, &ctx->ret)) {
+ error (g, "%s: failed to parse reply", "guestfs_sh_lines");
+ return;
+ }
+ done:
+ ctx->cb_sequence = 1;
+}
+
+char **guestfs_sh_lines (guestfs_h *g,
+ const char *command)
+{
+ struct guestfs_sh_lines_args args;
+ struct sh_lines_ctx ctx;
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ int serial;
+
+ if (check_state (g, "guestfs_sh_lines") == -1) return NULL;
+ guestfs_set_busy (g);
+
+ memset (&ctx, 0, sizeof ctx);
+
+ args.command = (char *) command;
+ serial = guestfs__send_sync (g, GUESTFS_PROC_SH_LINES,
+ (xdrproc_t) xdr_guestfs_sh_lines_args, (char *) &args);
+ if (serial == -1) {
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ guestfs__switch_to_receiving (g);
+ ctx.cb_sequence = 0;
+ guestfs_set_reply_callback (g, sh_lines_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_sh_lines");
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SH_LINES, serial) == -1) {
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+ error (g, "%s", ctx.err.error_message);
+ free (ctx.err.error_message);
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ guestfs_end_busy (g);
+ /* caller will free this, but we need to add a NULL entry */
+ ctx.ret.lines.lines_val =
+ safe_realloc (g, ctx.ret.lines.lines_val,
+ sizeof (char *) * (ctx.ret.lines.lines_len + 1));
+ ctx.ret.lines.lines_val[ctx.ret.lines.lines_len] = NULL;
+ return ctx.ret.lines.lines_val;
+}
+
diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h
index 7f8f0deb..fe9967f2 100644
--- a/src/guestfs-actions.h
+++ b/src/guestfs-actions.h
@@ -181,3 +181,5 @@ 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);
extern int guestfs_ntfs_3g_probe (guestfs_h *handle, int rw, const char *device);
+extern char *guestfs_sh (guestfs_h *handle, const char *command);
+extern char **guestfs_sh_lines (guestfs_h *handle, const char *command);
diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c
index fc88d05f..96951d9d 100644
--- a/src/guestfs_protocol.c
+++ b/src/guestfs_protocol.c
@@ -1871,6 +1871,47 @@ xdr_guestfs_ntfs_3g_probe_ret (XDR *xdrs, guestfs_ntfs_3g_probe_ret *objp)
}
bool_t
+xdr_guestfs_sh_args (XDR *xdrs, guestfs_sh_args *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->command, ~0))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
+xdr_guestfs_sh_ret (XDR *xdrs, guestfs_sh_ret *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->output, ~0))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
+xdr_guestfs_sh_lines_args (XDR *xdrs, guestfs_sh_lines_args *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->command, ~0))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
+xdr_guestfs_sh_lines_ret (XDR *xdrs, guestfs_sh_lines_ret *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_array (xdrs, (char **)&objp->lines.lines_val, (u_int *) &objp->lines.lines_len, ~0,
+ sizeof (str), (xdrproc_t) xdr_str))
+ 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 9f3bd8d0..a9863ccf 100644
--- a/src/guestfs_protocol.h
+++ b/src/guestfs_protocol.h
@@ -946,6 +946,29 @@ struct guestfs_ntfs_3g_probe_ret {
};
typedef struct guestfs_ntfs_3g_probe_ret guestfs_ntfs_3g_probe_ret;
+struct guestfs_sh_args {
+ char *command;
+};
+typedef struct guestfs_sh_args guestfs_sh_args;
+
+struct guestfs_sh_ret {
+ char *output;
+};
+typedef struct guestfs_sh_ret guestfs_sh_ret;
+
+struct guestfs_sh_lines_args {
+ char *command;
+};
+typedef struct guestfs_sh_lines_args guestfs_sh_lines_args;
+
+struct guestfs_sh_lines_ret {
+ struct {
+ u_int lines_len;
+ str *lines_val;
+ } lines;
+};
+typedef struct guestfs_sh_lines_ret guestfs_sh_lines_ret;
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
@@ -1057,7 +1080,9 @@ enum guestfs_procedure {
GUESTFS_PROC_E2FSCK_F = 108,
GUESTFS_PROC_SLEEP = 109,
GUESTFS_PROC_NTFS_3G_PROBE = 110,
- GUESTFS_PROC_NR_PROCS = 110 + 1,
+ GUESTFS_PROC_SH = 111,
+ GUESTFS_PROC_SH_LINES = 112,
+ GUESTFS_PROC_NR_PROCS = 112 + 1,
};
typedef enum guestfs_procedure guestfs_procedure;
#define GUESTFS_MESSAGE_MAX 4194304
@@ -1258,6 +1283,10 @@ 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_ntfs_3g_probe_args (XDR *, guestfs_ntfs_3g_probe_args*);
extern bool_t xdr_guestfs_ntfs_3g_probe_ret (XDR *, guestfs_ntfs_3g_probe_ret*);
+extern bool_t xdr_guestfs_sh_args (XDR *, guestfs_sh_args*);
+extern bool_t xdr_guestfs_sh_ret (XDR *, guestfs_sh_ret*);
+extern bool_t xdr_guestfs_sh_lines_args (XDR *, guestfs_sh_lines_args*);
+extern bool_t xdr_guestfs_sh_lines_ret (XDR *, guestfs_sh_lines_ret*);
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*);
@@ -1417,6 +1446,10 @@ extern bool_t xdr_guestfs_e2fsck_f_args ();
extern bool_t xdr_guestfs_sleep_args ();
extern bool_t xdr_guestfs_ntfs_3g_probe_args ();
extern bool_t xdr_guestfs_ntfs_3g_probe_ret ();
+extern bool_t xdr_guestfs_sh_args ();
+extern bool_t xdr_guestfs_sh_ret ();
+extern bool_t xdr_guestfs_sh_lines_args ();
+extern bool_t xdr_guestfs_sh_lines_ret ();
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 cc514e74..94d2e849 100644
--- a/src/guestfs_protocol.x
+++ b/src/guestfs_protocol.x
@@ -732,6 +732,22 @@ struct guestfs_ntfs_3g_probe_ret {
int status;
};
+struct guestfs_sh_args {
+ string command<>;
+};
+
+struct guestfs_sh_ret {
+ string output<>;
+};
+
+struct guestfs_sh_lines_args {
+ string command<>;
+};
+
+struct guestfs_sh_lines_ret {
+ str lines<>;
+};
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
@@ -843,6 +859,8 @@ enum guestfs_procedure {
GUESTFS_PROC_E2FSCK_F = 108,
GUESTFS_PROC_SLEEP = 109,
GUESTFS_PROC_NTFS_3G_PROBE = 110,
+ GUESTFS_PROC_SH = 111,
+ GUESTFS_PROC_SH_LINES = 112,
GUESTFS_PROC_NR_PROCS
};