+ return (const struct process_attr_tracker *)
+ _kernel_get_process_attr_tracker(session, process_attr);
+}
+
+enum lttng_error_code kernel_process_attr_tracker_set_tracking_policy(
+ struct ltt_kernel_session *session,
+ enum lttng_process_attr process_attr,
+ enum lttng_tracking_policy policy)
+{
+ int ret;
+ enum lttng_error_code ret_code = LTTNG_OK;
+ struct process_attr_tracker *tracker =
+ _kernel_get_process_attr_tracker(session, process_attr);
+ enum lttng_tracking_policy previous_policy;
+
+ if (!tracker) {
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ previous_policy = process_attr_tracker_get_tracking_policy(tracker);
+ ret = process_attr_tracker_set_tracking_policy(tracker, policy);
+ if (ret) {
+ ret_code = LTTNG_ERR_UNK;
+ goto end;
+ }
+
+ if (previous_policy == policy) {
+ goto end;
+ }
+
+ switch (policy) {
+ case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
+ if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
+ /*
+ * Maintain a special case for the process ID process
+ * attribute tracker as it was the only supported
+ * attribute prior to 2.12.
+ */
+ ret = kernctl_track_pid(session->fd, -1);
+ } else {
+ ret = kernctl_track_id(session->fd, process_attr, -1);
+ }
+ break;
+ case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
+ case LTTNG_TRACKING_POLICY_INCLUDE_SET:
+ /* fall-through. */
+ if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
+ /*
+ * Maintain a special case for the process ID process
+ * attribute tracker as it was the only supported
+ * attribute prior to 2.12.
+ */
+ ret = kernctl_untrack_pid(session->fd, -1);
+ } else {
+ ret = kernctl_untrack_id(session->fd, process_attr, -1);
+ }
+ break;
+ default:
+ abort();
+ }
+ /* kern-ctl error handling */
+ switch (-ret) {
+ case 0:
+ ret_code = LTTNG_OK;
+ break;
+ case EINVAL:
+ ret_code = LTTNG_ERR_INVALID;
+ break;
+ case ENOMEM:
+ ret_code = LTTNG_ERR_NOMEM;
+ break;
+ case EEXIST:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
+ break;
+ default:
+ ret_code = LTTNG_ERR_UNK;
+ break;
+ }
+end:
+ return ret_code;
+}
+
+enum lttng_error_code kernel_process_attr_tracker_inclusion_set_add_value(
+ struct ltt_kernel_session *session,
+ enum lttng_process_attr process_attr,
+ const struct process_attr_value *value)
+{
+ int ret, integral_value;
+ enum lttng_error_code ret_code;
+ struct process_attr_tracker *tracker;
+ enum process_attr_tracker_status status;
+
+ /*
+ * Convert process attribute tracker value to the integral
+ * representation required by the kern-ctl API.
+ */
+ switch (process_attr) {
+ case LTTNG_PROCESS_ATTR_PROCESS_ID:
+ case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
+ integral_value = (int) value->value.pid;
+ break;
+ case LTTNG_PROCESS_ATTR_USER_ID:
+ case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
+ if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
+ uid_t uid;
+
+ ret_code = utils_user_id_from_name(
+ value->value.user_name, &uid);
+ if (ret_code != LTTNG_OK) {
+ goto end;
+ }
+ integral_value = (int) uid;
+ } else {
+ integral_value = (int) value->value.uid;
+ }
+ break;
+ case LTTNG_PROCESS_ATTR_GROUP_ID:
+ case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
+ if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
+ gid_t gid;
+
+ ret_code = utils_group_id_from_name(
+ value->value.group_name, &gid);
+ if (ret_code != LTTNG_OK) {
+ goto end;
+ }
+ integral_value = (int) gid;
+ } else {
+ integral_value = (int) value->value.gid;
+ }
+ break;
+ default:
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ tracker = _kernel_get_process_attr_tracker(session, process_attr);
+ if (!tracker) {
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ status = process_attr_tracker_inclusion_set_add_value(tracker, value);
+ if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
+ switch (status) {
+ case PROCESS_ATTR_TRACKER_STATUS_EXISTS:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
+ break;
+ case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
+ break;
+ case PROCESS_ATTR_TRACKER_STATUS_ERROR:
+ default:
+ ret_code = LTTNG_ERR_UNK;
+ break;
+ }
+ goto end;
+ }
+
+ DBG("Kernel track %s %d for session id %" PRIu64,
+ lttng_process_attr_to_string(process_attr),
+ integral_value, session->id);
+ if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
+ /*
+ * Maintain a special case for the process ID process attribute
+ * tracker as it was the only supported attribute prior to 2.12.
+ */
+ ret = kernctl_track_pid(session->fd, integral_value);
+ } else {
+ ret = kernctl_track_id(
+ session->fd, process_attr, integral_value);
+ }
+ if (ret == 0) {
+ ret_code = LTTNG_OK;
+ goto end;
+ }
+
+ kernel_wait_quiescent();
+
+ /* kern-ctl error handling */
+ switch (-ret) {
+ case 0:
+ ret_code = LTTNG_OK;
+ break;
+ case EINVAL:
+ ret_code = LTTNG_ERR_INVALID;
+ break;
+ case ENOMEM:
+ ret_code = LTTNG_ERR_NOMEM;
+ break;
+ case EEXIST:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
+ break;
+ default:
+ ret_code = LTTNG_ERR_UNK;
+ break;
+ }
+
+ /* Attempt to remove the value from the tracker. */
+ status = process_attr_tracker_inclusion_set_remove_value(
+ tracker, value);
+ if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
+ ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
+ lttng_process_attr_to_string(process_attr),
+ integral_value);
+ }
+end:
+ return ret_code;
+}
+
+enum lttng_error_code kernel_process_attr_tracker_inclusion_set_remove_value(
+ struct ltt_kernel_session *session,
+ enum lttng_process_attr process_attr,
+ const struct process_attr_value *value)
+{
+ int ret, integral_value;
+ enum lttng_error_code ret_code;
+ struct process_attr_tracker *tracker;
+ enum process_attr_tracker_status status;
+
+ /*
+ * Convert process attribute tracker value to the integral
+ * representation required by the kern-ctl API.
+ */
+ switch (process_attr) {
+ case LTTNG_PROCESS_ATTR_PROCESS_ID:
+ case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
+ integral_value = (int) value->value.pid;
+ break;
+ case LTTNG_PROCESS_ATTR_USER_ID:
+ case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
+ if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
+ uid_t uid;
+
+ ret_code = utils_user_id_from_name(
+ value->value.user_name, &uid);
+ if (ret_code != LTTNG_OK) {
+ goto end;
+ }
+ integral_value = (int) uid;
+ } else {
+ integral_value = (int) value->value.uid;
+ }
+ break;
+ case LTTNG_PROCESS_ATTR_GROUP_ID:
+ case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
+ if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
+ gid_t gid;
+
+ ret_code = utils_group_id_from_name(
+ value->value.group_name, &gid);
+ if (ret_code != LTTNG_OK) {
+ goto end;
+ }
+ integral_value = (int) gid;
+ } else {
+ integral_value = (int) value->value.gid;
+ }
+ break;
+ default:
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ tracker = _kernel_get_process_attr_tracker(session, process_attr);
+ if (!tracker) {
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ status = process_attr_tracker_inclusion_set_remove_value(
+ tracker, value);
+ if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
+ switch (status) {
+ case PROCESS_ATTR_TRACKER_STATUS_MISSING:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
+ break;
+ case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
+ break;
+ case PROCESS_ATTR_TRACKER_STATUS_ERROR:
+ default:
+ ret_code = LTTNG_ERR_UNK;
+ break;
+ }
+ goto end;
+ }
+
+ DBG("Kernel track %s %d for session id %" PRIu64,
+ lttng_process_attr_to_string(process_attr),
+ integral_value, session->id);
+ if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
+ /*
+ * Maintain a special case for the process ID process attribute
+ * tracker as it was the only supported attribute prior to 2.12.
+ */
+ ret = kernctl_untrack_pid(session->fd, integral_value);
+ } else {
+ ret = kernctl_untrack_id(
+ session->fd, process_attr, integral_value);
+ }
+ if (ret == 0) {
+ ret_code = LTTNG_OK;
+ goto end;
+ }
+ kernel_wait_quiescent();
+
+ /* kern-ctl error handling */
+ switch (-ret) {
+ case 0:
+ ret_code = LTTNG_OK;
+ break;
+ case EINVAL:
+ ret_code = LTTNG_ERR_INVALID;
+ break;
+ case ENOMEM:
+ ret_code = LTTNG_ERR_NOMEM;
+ break;
+ case ENOENT:
+ ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
+ break;
+ default:
+ ret_code = LTTNG_ERR_UNK;
+ break;
+ }
+
+ /* Attempt to add the value to the tracker. */
+ status = process_attr_tracker_inclusion_set_add_value(
+ tracker, value);
+ if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
+ ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
+ lttng_process_attr_to_string(process_attr),
+ integral_value);
+ }
+end:
+ return ret_code;