+
+ /* send signature */
+ len = ustcomm_send_unix_sock(sock, signature, signature_len);
+ if (len > 0 && len != signature_len) {
+ 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);
+ }
+
+ 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;
+ }
+ }
+}
+
+/*
+ * 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,
+ 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(&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;
+ }
+