+/*
+ * Set URL for a consumer for a session and domain.
+ *
+ * Return 0 on success, else a negative value.
+ */
+int lttng_set_consumer_url(struct lttng_handle *handle,
+ const char *control_url, const char *data_url)
+{
+ ssize_t size;
+ struct lttcomm_session_msg lsm;
+ struct lttng_uri *uris = NULL;
+
+ if (handle == NULL || (control_url == NULL && data_url == NULL)) {
+ return -1;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+
+ lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
+
+ copy_string(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ copy_lttng_domain(&lsm.domain, &handle->domain);
+
+ size = parse_str_urls_to_uri(control_url, data_url, &uris);
+ if (size < 0) {
+ return LTTCOMM_INVALID;
+ }
+
+ lsm.u.uri.size = size;
+
+ return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
+ NULL);
+}
+
+/*
+ * Enable consumer for a session and domain.
+ *
+ * Return 0 on success, else a negative value.
+ */
+int lttng_enable_consumer(struct lttng_handle *handle)
+{
+ struct lttcomm_session_msg lsm;
+
+ if (handle == NULL) {
+ return -1;
+ }
+
+ lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
+
+ copy_string(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ copy_lttng_domain(&lsm.domain, &handle->domain);
+
+ return ask_sessiond(&lsm, NULL);
+}
+
+/*
+ * Disable consumer for a session and domain.
+ *
+ * Return 0 on success, else a negative value.
+ */
+int lttng_disable_consumer(struct lttng_handle *handle)
+{
+ struct lttcomm_session_msg lsm;
+
+ if (handle == NULL) {
+ return -1;
+ }
+
+ lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
+
+ copy_string(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+ copy_lttng_domain(&lsm.domain, &handle->domain);
+
+ return ask_sessiond(&lsm, NULL);
+}
+
+/*
+ * Set health socket path by putting it in the global health_sock_path
+ * variable.
+ *
+ * Returns 0 on success or assert(0) on ENOMEM.
+ */
+static int set_health_socket_path(void)
+{
+ int ret;
+ int in_tgroup = 0; /* In tracing group */
+ uid_t uid;
+ const char *home;
+
+ uid = getuid();
+
+ if (uid != 0) {
+ /* Are we in the tracing group ? */
+ in_tgroup = check_tracing_group(tracing_group);
+ }
+
+ if ((uid == 0) || in_tgroup) {
+ copy_string(health_sock_path, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK,
+ sizeof(health_sock_path));
+ }
+
+ if (uid != 0) {
+ /*
+ * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
+ * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
+ */
+ home = getenv("HOME");
+ if (home == NULL) {
+ /* Fallback in /tmp .. */
+ home = "/tmp";
+ }
+
+ ret = snprintf(health_sock_path, sizeof(health_sock_path),
+ DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
+ if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
+ /* ENOMEM at this point... just kill the control lib. */
+ assert(0);
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Check session daemon health for a specific health component.
+ *
+ * Return 0 if health is OK or else 1 if BAD. A return value of -1 indicate
+ * that the control library was not able to connect to the session daemon
+ * health socket.
+ *
+ * Any other positive value is an lttcomm error which can be translate with
+ * lttng_strerror().
+ */
+int lttng_health_check(enum lttng_health_component c)
+{
+ int sock, ret;
+ struct lttcomm_health_msg msg;
+ struct lttcomm_health_data reply;
+
+ /* Connect to the sesssion daemon */
+ sock = lttcomm_connect_unix_sock(health_sock_path);
+ if (sock < 0) {
+ ret = -1;
+ goto error;
+ }
+
+ msg.cmd = LTTNG_HEALTH_CHECK;
+ msg.component = c;
+
+ ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
+ if (ret < 0) {
+ goto close_error;
+ }
+
+ ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
+ if (ret < 0) {
+ goto close_error;
+ }
+
+ ret = reply.ret_code;
+
+close_error:
+ close(sock);
+
+error:
+ return ret;
+}
+
+/*
+ * This is an extension of create session that is ONLY and SHOULD only be used
+ * by the lttng command line program. It exists to avoid using URI parsing in
+ * the lttng client.
+ *
+ * We need the date and time for the trace path subdirectory for the case where
+ * the user does NOT define one using either -o or -U. Using the normal
+ * lttng_create_session API call, we have no clue on the session daemon side if
+ * the URL was generated automatically by the client or define by the user.
+ *
+ * So this function "wrapper" is hidden from the public API, takes the datetime
+ * string and appends it if necessary to the URI subdirectory before sending it
+ * to the session daemon.
+ *
+ * With this extra function, the lttng_create_session call behavior is not
+ * changed and the timestamp is appended to the URI on the session daemon side
+ * if necessary.
+ */
+int _lttng_create_session_ext(const char *name, const char *url,
+ const char *datetime)
+{
+ int ret;
+ ssize_t size;
+ struct lttcomm_session_msg lsm;
+ struct lttng_uri *uris = NULL;
+
+ if (name == NULL || datetime == NULL) {
+ return -1;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+
+ lsm.cmd_type = LTTNG_CREATE_SESSION;
+ if (!strncmp(name, DEFAULT_SESSION_NAME, strlen(DEFAULT_SESSION_NAME))) {
+ ret = snprintf(lsm.session.name, sizeof(lsm.session.name), "%s-%s",
+ name, datetime);
+ if (ret < 0) {
+ PERROR("snprintf session name datetime");
+ return -1;
+ }
+ } else {
+ copy_string(lsm.session.name, name, sizeof(lsm.session.name));
+ }
+
+ /* There should never be a data URL */
+ size = parse_str_urls_to_uri(url, NULL, &uris);
+ if (size < 0) {
+ return LTTCOMM_INVALID;
+ }
+
+ lsm.u.uri.size = size;
+
+ if (uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
+ ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s", name,
+ datetime);
+ if (ret < 0) {
+ PERROR("snprintf uri subdir");
+ return -1;
+ }
+ }
+
+ return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
+ NULL);
+}
+