예제 #1
0
파일: reader.c 프로젝트: knusbaum/Wily
void
winReflectCmd(rdWin *w, char *cmd, char *arg)
{
	if (rpc_exec(wilyq, w->id,cmd,arg)) {
		DPRINT("could not reflect command to wily");
	}
}
예제 #2
0
파일: reader.c 프로젝트: knusbaum/Wily
void
closeWin(rdWin *w)
{
	assert(w);
	/* get wily to close the pane */
	if (rpc_exec(wilyq, w->id, "Del", "")) {
		DPRINT("Could not close window");
	}

	/* free up resources */
	free(w->title);
	free(w->body);
	if (w->wintype == rdList)
		freeItems(w);
	/* XXX - currently leak the stateinfo stuff */
	freeWin(w);
	return;
}
예제 #3
0
파일: plugin.c 프로젝트: kx2015/juci-rpcd
static bool
rpc_plugin_lookup_plugin(struct ubus_context *ctx, struct ubus_object *obj,
                         char *strptr)
{
	struct rpc_plugin_lookup_context c = { .id = obj->id, .name = strptr };

	if (ubus_lookup(ctx, NULL, rpc_plugin_lookup_plugin_cb, &c))
		return false;

	return c.found;
}

struct call_context {
	char path[PATH_MAX];
	const char *argv[4];
	char *method;
	char *input;
	json_tokener *tok;
	json_object *obj;
	bool input_done;
	bool output_done;
};

static int
rpc_plugin_call_stdin_cb(struct ustream *s, void *priv)
{
	struct call_context *c = priv;

	if (!c->input_done)
	{
		ustream_write(s, c->input, strlen(c->input), false);
		c->input_done = true;
	}

	return 0;
}

static int
rpc_plugin_call_stdout_cb(struct blob_buf *blob, char *buf, int len, void *priv)
{
	struct call_context *c = priv;

	if (!c->output_done)
	{
		c->obj = json_tokener_parse_ex(c->tok, buf, len);

		if (json_tokener_get_error(c->tok) != json_tokener_continue)
			c->output_done = true;
	}

	return len;
}

static int
rpc_plugin_call_stderr_cb(struct blob_buf *blob, char *buf, int len, void *priv)
{
	return len;
}

static int
rpc_plugin_call_finish_cb(struct blob_buf *blob, int stat, void *priv)
{
	struct call_context *c = priv;
	int rv = UBUS_STATUS_INVALID_ARGUMENT;

	if (json_tokener_get_error(c->tok) == json_tokener_success)
	{
		if (c->obj)
		{
			if (json_object_get_type(c->obj) == json_type_object &&
			    blobmsg_add_object(blob, c->obj))
				rv = UBUS_STATUS_OK;

			json_object_put(c->obj);
		}
		else
		{
			rv = UBUS_STATUS_NO_DATA;
		}
	}

	json_tokener_free(c->tok);

	free(c->input);
	free(c->method);

	return rv;
}

static int
rpc_plugin_call(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
{
	int rv = UBUS_STATUS_UNKNOWN_ERROR;
	struct call_context *c;
	char *plugin;

	c = calloc(1, sizeof(*c));

	if (!c)
		goto fail;

	c->method = strdup(method);
	c->input = blobmsg_format_json(msg, true);
	c->tok = json_tokener_new();

	if (!c->method || !c->input || !c->tok)
		goto fail;

	plugin = c->path + sprintf(c->path, "%s/", RPC_PLUGIN_DIRECTORY);

	if (!rpc_plugin_lookup_plugin(ctx, obj, plugin))
	{
		rv = UBUS_STATUS_NOT_FOUND;
		goto fail;
	}

	c->argv[0] = c->path;
	c->argv[1] = "call";
	c->argv[2] = c->method;

	return rpc_exec(c->argv, rpc_plugin_call_stdin_cb,
	                rpc_plugin_call_stdout_cb, rpc_plugin_call_stderr_cb,
	                rpc_plugin_call_finish_cb, c, ctx, req);

fail:
	if (c)
	{
		if (c->method)
			free(c->method);

		if (c->input)
			free(c->input);

		if (c->tok)
			json_tokener_free(c->tok);

		free(c);
	}

	return rv;
}