+ /* 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;