+
+ /* send signature */
+ len = ustcomm_send_unix_sock(sock, signature, signature_len);
+ if (len > 0 && len != signature_len) {
+ ret = -EIO;
+ goto error_fields;
+ }
+ if (len < 0) {
+ ret = len;
+ goto error_fields;
+ }
+
+ /* send fields */
+ if (fields_len > 0) {
+ len = ustcomm_send_unix_sock(sock, fields, fields_len);
+ if (len > 0 && len != fields_len) {
+ ret = -EIO;
+ goto error_fields;
+ }
+ if (len < 0) {
+ ret = len;
+ goto error_fields;
+ }
+ }
+ free(fields);
+
+ if (model_emf_uri_len) {
+ /* send model_emf_uri */
+ len = ustcomm_send_unix_sock(sock, model_emf_uri,
+ model_emf_uri_len);
+ if (len > 0 && len != model_emf_uri_len) {
+ return -EIO;
+ }
+ if (len < 0) {
+ return len;
+ }
+ }
+
+ /* receive reply */
+ len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
+ switch (len) {
+ case 0: /* orderly shutdown */
+ return -EPIPE;
+ case sizeof(reply):
+ if (reply.header.notify_cmd != msg.header.notify_cmd) {
+ ERR("Unexpected result message command "
+ "expected: %u vs received: %u\n",
+ msg.header.notify_cmd, reply.header.notify_cmd);
+ return -EINVAL;
+ }
+ if (reply.r.ret_code > 0)
+ return -EINVAL;
+ if (reply.r.ret_code < 0)
+ return reply.r.ret_code;
+ *id = reply.r.event_id;
+ DBG("Sent register event notification for name \"%s\": ret_code %d, event_id %u\n",
+ event_name, reply.r.ret_code, reply.r.event_id);
+ return 0;
+ default:
+ if (len < 0) {
+ /* Transport level error */
+ if (errno == EPIPE || errno == ECONNRESET)
+ len = -errno;
+ return len;
+ } else {
+ ERR("incorrect message size: %zd\n", len);
+ return len;
+ }
+ }
+ /* Unreached. */
+
+ /* Error path only. */
+error_fields:
+ free(fields);
+ return ret;
+}
+
+/*
+ * Returns 0 on success, negative error value on error.
+ * Returns -EPIPE or -ECONNRESET if other end has hung up.
+ */
+int ustcomm_register_enum(int sock,
+ int session_objd, /* session descriptor */
+ const char *enum_name, /* enum name (input) */
+ size_t nr_entries, /* entries */
+ const struct lttng_enum_entry *lttng_entries,
+ uint64_t *id)
+{
+ ssize_t len;
+ struct {
+ struct ustcomm_notify_hdr header;
+ struct ustcomm_notify_enum_msg m;
+ } msg;
+ struct {
+ struct ustcomm_notify_hdr header;
+ struct ustcomm_notify_enum_reply r;
+ } reply;
+ size_t entries_len;
+ struct ustctl_enum_entry *entries = NULL;
+ int ret;
+
+ memset(&msg, 0, sizeof(msg));
+ msg.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM;
+ msg.m.session_objd = session_objd;
+ strncpy(msg.m.enum_name, enum_name, LTTNG_UST_SYM_NAME_LEN);
+ msg.m.enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
+
+ /* Calculate entries len, serialize entries. */
+ if (nr_entries > 0) {
+ ret = serialize_entries(&entries,
+ nr_entries, lttng_entries);
+ if (ret)
+ return ret;
+ }
+
+ entries_len = sizeof(*entries) * nr_entries;
+ msg.m.entries_len = entries_len;
+
+ len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
+ if (len > 0 && len != sizeof(msg)) {
+ ret = -EIO;
+ goto error_entries;
+ }
+ if (len < 0) {
+ ret = len;
+ goto error_entries;
+ }
+
+ /* send entries */
+ if (entries_len > 0) {
+ len = ustcomm_send_unix_sock(sock, entries, entries_len);
+ if (len > 0 && len != entries_len) {
+ ret = -EIO;
+ goto error_entries;
+ }
+ if (len < 0) {
+ ret = len;
+ goto error_entries;
+ }
+ }
+ free(entries);
+ entries = NULL;
+
+ /* receive reply */
+ len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
+ switch (len) {
+ case 0: /* orderly shutdown */
+ return -EPIPE;
+ case sizeof(reply):
+ if (reply.header.notify_cmd != msg.header.notify_cmd) {
+ ERR("Unexpected result message command "
+ "expected: %u vs received: %u\n",
+ msg.header.notify_cmd, reply.header.notify_cmd);
+ return -EINVAL;
+ }
+ if (reply.r.ret_code > 0)
+ return -EINVAL;
+ if (reply.r.ret_code < 0)
+ return reply.r.ret_code;
+ *id = reply.r.enum_id;
+ DBG("Sent register enum notification for name \"%s\": ret_code %d\n",
+ enum_name, reply.r.ret_code);
+ return 0;
+ default:
+ if (len < 0) {
+ /* Transport level error */
+ if (errno == EPIPE || errno == ECONNRESET)
+ len = -errno;
+ return len;
+ } else {
+ ERR("incorrect message size: %zd\n", len);
+ return len;
+ }
+ }
+ return ret;
+
+error_entries:
+ free(entries);
+ return ret;
+}
+
+/*
+ * Returns 0 on success, negative error value on error.
+ * Returns -EPIPE or -ECONNRESET if other end has hung up.
+ */
+int ustcomm_register_channel(int sock,
+ struct lttng_session *session,
+ int session_objd, /* session descriptor */
+ int channel_objd, /* channel descriptor */
+ size_t nr_ctx_fields,
+ const struct lttng_ctx_field *ctx_fields,
+ uint32_t *chan_id, /* channel id (output) */
+ int *header_type) /* header type (output) */
+{
+ ssize_t len;
+ struct {
+ struct ustcomm_notify_hdr header;
+ struct ustcomm_notify_channel_msg m;
+ } msg;
+ struct {
+ struct ustcomm_notify_hdr header;
+ struct ustcomm_notify_channel_reply r;
+ } reply;
+ size_t fields_len;
+ struct ustctl_field *fields = NULL;
+ int ret;
+ size_t nr_write_fields = 0;
+
+ memset(&msg, 0, sizeof(msg));
+ msg.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL;
+ msg.m.session_objd = session_objd;
+ msg.m.channel_objd = channel_objd;
+
+ /* Calculate fields len, serialize fields. */
+ if (nr_ctx_fields > 0) {
+ ret = serialize_ctx_fields(session, &nr_write_fields, &fields,
+ nr_ctx_fields, ctx_fields);
+ if (ret)
+ return ret;
+ }
+
+ fields_len = sizeof(*fields) * nr_write_fields;
+ msg.m.ctx_fields_len = fields_len;
+ len = ustcomm_send_unix_sock(sock, &msg, sizeof(msg));
+ if (len > 0 && len != sizeof(msg)) {
+ free(fields);
+ return -EIO;
+ }
+ if (len < 0) {
+ free(fields);
+ return len;
+ }
+
+ /* send fields */
+ if (fields_len > 0) {
+ len = ustcomm_send_unix_sock(sock, fields, fields_len);
+ free(fields);
+ if (len > 0 && len != fields_len) {
+ return -EIO;
+ }
+ if (len < 0) {
+ return len;
+ }
+ } else {
+ free(fields);
+ }
+
+ len = ustcomm_recv_unix_sock(sock, &reply, sizeof(reply));
+ switch (len) {
+ case 0: /* orderly shutdown */
+ return -EPIPE;
+ case sizeof(reply):
+ if (reply.header.notify_cmd != msg.header.notify_cmd) {
+ ERR("Unexpected result message command "
+ "expected: %u vs received: %u\n",
+ msg.header.notify_cmd, reply.header.notify_cmd);
+ return -EINVAL;
+ }
+ if (reply.r.ret_code > 0)
+ return -EINVAL;
+ if (reply.r.ret_code < 0)
+ return reply.r.ret_code;
+ *chan_id = reply.r.chan_id;
+ switch (reply.r.header_type) {
+ case 1:
+ case 2:
+ *header_type = reply.r.header_type;
+ break;
+ default:
+ ERR("Unexpected channel header type %u\n",
+ reply.r.header_type);
+ return -EINVAL;
+ }
+ DBG("Sent register channel notification: chan_id %d, header_type %d\n",
+ reply.r.chan_id, reply.r.header_type);
+ return 0;
+ default:
+ if (len < 0) {
+ /* Transport level error */
+ if (errno == EPIPE || errno == ECONNRESET)
+ len = -errno;
+ return len;
+ } else {
+ ERR("incorrect message size: %zd\n", len);
+ return len;
+ }
+ }
+}
+
+/*
+ * Set socket reciving timeout.
+ */
+int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
+{
+ int ret;
+ struct timeval tv;
+
+ tv.tv_sec = msec / 1000;
+ tv.tv_usec = (msec * 1000 % 1000000);
+
+ ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+ if (ret < 0) {
+ PERROR("setsockopt SO_RCVTIMEO");
+ ret = -errno;
+ }
+
+ return ret;
+}
+
+/*
+ * Set socket sending timeout.
+ */
+int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
+{
+ int ret;
+ struct timeval tv;
+
+ tv.tv_sec = msec / 1000;
+ tv.tv_usec = (msec * 1000) % 1000000;
+
+ ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+ if (ret < 0) {
+ PERROR("setsockopt SO_SNDTIMEO");
+ ret = -errno;
+ }
+