X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Futils%2Fliblustreapi_util.c;h=f0f199108297dad0d1ce930f5911d857947b6816;hb=2f8d7b4679de3fa467040aa61733f262714e39c9;hp=329f0e439856a5698aa74a0fbb11aec3d8283902;hpb=31e7f22cdc7c77ac7b48ba6a258ca9611063320a;p=fs%2Flustre-release.git diff --git a/lustre/utils/liblustreapi_util.c b/lustre/utils/liblustreapi_util.c index 329f0e4..f0f1991 100644 --- a/lustre/utils/liblustreapi_util.c +++ b/lustre/utils/liblustreapi_util.c @@ -5,6 +5,8 @@ * * (C) Copyright (c) 2015, Cray Inc, all rights reserved. * + * Copyright (c) 2016, 2017, Intel Corporation. + * * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser General Public License * LGPL version 2.1 or (at your discretion) any later version. @@ -26,26 +28,37 @@ * Author: Frank Zago */ +#include +#include +#include +#include +#include #include +#include #include -#include #include #include #include -#include #include +#include +#include /* only until LUSTRE_VERSION_CODE is gone */ +#include "lustreapi_internal.h" /* * Indicate whether the liblustreapi_init() constructor below has run or not. * - * This can be used by external programs to ensure if the initialization + * This can be used by external programs to ensure that the initialization * mechanism has actually worked. */ bool liblustreapi_initialized; - -/* - * Initializes the library +/** + * Initialize the library once at startup. + * + * Initializes the random number generator (random()). Get + * data from different places in case one of them fails. This + * is enough to get reasonably random numbers, but is not + * strong enough to be used for cryptography. */ static __attribute__ ((constructor)) void liblustreapi_init(void) { @@ -53,10 +66,6 @@ static __attribute__ ((constructor)) void liblustreapi_init(void) struct timeval tv; int fd; - /* Initializes the random number generator (random()). Get - * data from different places in case one of them fails. This - * is enough to get reasonably random numbers, but is not - * strong enough to be used for cryptography. */ seed = syscall(SYS_gettid); if (gettimeofday(&tv, NULL) == 0) { @@ -67,12 +76,107 @@ static __attribute__ ((constructor)) void liblustreapi_init(void) fd = open("/dev/urandom", O_RDONLY | O_NOFOLLOW); if (fd >= 0) { unsigned int rnumber; + ssize_t ret; - (void)read(fd, &rnumber, sizeof(rnumber)); - seed ^= rnumber; + ret = read(fd, &rnumber, sizeof(rnumber)); + seed ^= rnumber ^ ret; close(fd); } srandom(seed); liblustreapi_initialized = true; } + +/** + * Return the release version for the Lustre modules, e.g. 2.6.92. + * + * The "version" file in /proc currently returns only the line: + * lustre: 2.8.52 + * + * but in the past it also returned more lines that should be ignored: + * kernel: patchless_client + * build: v2_6_92_0-gadb3ee4-2.6.32-431.29.2.el6_lustre.g36cd22b.x86_64 + * + * \param version[in,out] buffer to store build version string + * \param version_size[in] size of \a version + * + * \retval 0 on success + * \retval -1 on failure, errno set + */ +int llapi_get_version_string(char *version, unsigned int version_size) +{ + char buffer[4096]; + char *ptr; + int rc; + + if (version == NULL || version_size == 0) { + errno = EINVAL; + return -1; + } + + rc = get_lustre_param_value(NULL, NULL, FILTER_BY_NONE, buffer, + "version", sizeof(buffer)); + if (rc < 0) { + errno = -rc; + return -1; + } + + ptr = strstr(buffer, "lustre:"); + if (ptr) { + ptr += strlen("lustre:"); + while (*ptr == ' ' || *ptr == '\t') + ptr++; + } else { + ptr = buffer; + } + llapi_chomp_string(ptr); + + if (ptr[0] == '\0') { + errno = ENODATA; + return -1; + } + + if (snprintf(version, version_size, "%s", ptr) >= version_size) { + errno = EOVERFLOW; + return -1; + } + return 0; +} + +#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0) +/** + * Return the build version of the Lustre code. + * + * The **version argument is pointless, so llapi_get_version_string() is + * better to use in the future, but give users a few versions to fix * it. + * + * \param buffer[in] temporary buffer to hold version string + * \param buffer_size[in] length of the \a buffer + * \param version[out] pointer to the start of build version string + * + * \retval 0 on success + * \retval -ve errno on failure + */ +int llapi_get_version(char *buffer, int buffer_size, char **version) +{ + int rc; +#if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 8, 53, 0) + static bool printed; + if (!printed) { + fprintf(stderr, + "%s deprecated, use llapi_get_version_string()\n", + __func__); + printed = true; + } +#endif + + rc = llapi_get_version_string(buffer, buffer_size); + /* keep old return style for this legacy function */ + if (rc == -1) + rc = -errno; + else + *version = buffer; + + return rc; +} +#endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0) */