+ ret = LTTNG_ERR_UNK;
+ goto error;
+ }
+
+ return LTTNG_OK;
+
+error_io:
+ ret = LTTNG_ERR_UST_ENABLE_FAIL;
+error:
+ return ret;
+}
+
+/*
+ * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
+ */
+static
+int send_pstring(struct lttcomm_sock *sock, const char *str, uint32_t len)
+{
+ int ret;
+ uint32_t len_be;
+
+ len_be = htobe32(len);
+ ret = send_payload(sock, &len_be, sizeof(len_be));
+ if (ret) {
+ goto end;
+ }
+
+ ret = send_payload(sock, str, len);
+ if (ret) {
+ goto end;
+ }
+end:
+ return ret;
+}
+
+/*
+ * Internal enable application context on an agent application. This function
+ * communicates with the agent to enable a given application context.
+ *
+ * Return LTTNG_OK on success or else a LTTNG_ERR* code.
+ */
+static int app_context_op(struct agent_app *app,
+ struct agent_app_ctx *ctx, enum lttcomm_agent_command cmd)
+{
+ int ret;
+ uint32_t reply_ret_code;
+ struct lttcomm_agent_generic_reply reply;
+ size_t app_ctx_provider_name_len, app_ctx_name_len, data_size;
+
+ assert(app);
+ assert(app->sock);
+ assert(ctx);
+ assert(cmd == AGENT_CMD_APP_CTX_ENABLE ||
+ cmd == AGENT_CMD_APP_CTX_DISABLE);
+
+ DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
+ cmd == AGENT_CMD_APP_CTX_ENABLE ? "enabling" : "disabling",
+ ctx->provider_name, ctx->ctx_name,
+ app->pid, app->sock->fd);
+
+ /*
+ * Calculate the payload's size, which consists of the size (u32, BE)
+ * of the provider name, the NULL-terminated provider name string, the
+ * size (u32, BE) of the context name, followed by the NULL-terminated
+ * context name string.
+ */
+ app_ctx_provider_name_len = strlen(ctx->provider_name) + 1;
+ app_ctx_name_len = strlen(ctx->ctx_name) + 1;
+ data_size = sizeof(uint32_t) + app_ctx_provider_name_len +
+ sizeof(uint32_t) + app_ctx_name_len;
+
+ ret = send_header(app->sock, data_size, cmd, 0);
+ if (ret < 0) {
+ goto error_io;
+ }
+
+ if (app_ctx_provider_name_len > UINT32_MAX ||
+ app_ctx_name_len > UINT32_MAX) {
+ ERR("Application context name > MAX_UINT32");
+ ret = LTTNG_ERR_INVALID;
+ goto error;
+ }
+
+ ret = send_pstring(app->sock, ctx->provider_name,
+ (uint32_t) app_ctx_provider_name_len);
+ if (ret < 0) {
+ goto error_io;
+ }
+
+ ret = send_pstring(app->sock, ctx->ctx_name,
+ (uint32_t) app_ctx_name_len);
+ if (ret < 0) {
+ goto error_io;
+ }
+
+ ret = recv_reply(app->sock, &reply, sizeof(reply));
+ if (ret < 0) {
+ goto error_io;
+ }
+
+ reply_ret_code = be32toh(reply.ret_code);
+ log_reply_code(reply_ret_code);
+ switch (reply_ret_code) {
+ case AGENT_RET_CODE_SUCCESS:
+ break;
+ default:
+ ret = LTTNG_ERR_UNK;