+ if (total_bytes_read < max_bytes)
+ buf[total_bytes_read] = '\0';
+ else
+ buf[max_bytes - 1] = '\0';
+
+ if (total_bytes_read > INT_MAX)
+ goto end;
+ ret = (int) total_bytes_read;
+end:
+ if (fd >= 0 && close(fd) < 0)
+ PERROR("close");
+ return ret;
+}
+
+/*
+ * Get the number of CPUs from the possible cpu mask.
+ *
+ * pmask: the mask to parse.
+ * len: the len of the mask excluding '\0'.
+ *
+ * Returns the number of possible CPUs from the mask or 0 on error.
+ */
+int get_num_possible_cpus_from_mask(const char *pmask, size_t len)
+{
+ ssize_t i;
+ unsigned long cpu_index;
+ char *endptr;
+
+ /* We need at least one char to read */
+ if (len < 1)
+ goto error;
+
+ /* Start from the end to read the last CPU index. */
+ for (i = len - 1; i > 0; i--) {
+ /* Break when we hit the first separator. */
+ if ((pmask[i] == ',') || (pmask[i] == '-')) {
+ i++;
+ break;
+ }
+ }
+
+ cpu_index = strtoul(&pmask[i], &endptr, 10);
+
+ /*
+ * If we read a CPU index, increment it by one to return a number of
+ * CPUs.
+ */
+ if ((&pmask[i] != endptr) && (cpu_index < INT_MAX))
+ return (int) cpu_index + 1;
+
+error:
+ return 0;
+}
+
+static void _get_num_possible_cpus(void)
+{
+ int ret;
+ char buf[LTTNG_UST_CPUMASK_SIZE];
+
+ /* Get the possible cpu mask from sysfs, fallback to sysconf. */
+ ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE);
+ if (ret <= 0)
+ goto fallback;
+
+ /* Parse the possible cpu mask, on failure fallback to sysconf. */
+ ret = get_num_possible_cpus_from_mask((char *) &buf, ret);
+ if (ret > 0)
+ goto end;
+
+fallback:
+ /* Fallback to sysconf. */
+ ret = get_num_possible_cpus_fallback();
+
+end:
+ /* If all methods failed, don't store the value. */
+ if (ret < 1)