|
Packit Service |
db8df9 |
/*
|
|
Packit Service |
db8df9 |
* Intel(R) Enclosure LED Utilities
|
|
Packit Service |
db8df9 |
* Copyright (C) 2009-2019 Intel Corporation.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
* This program is free software; you can redistribute it and/or modify it
|
|
Packit Service |
db8df9 |
* under the terms and conditions of the GNU General Public License,
|
|
Packit Service |
db8df9 |
* version 2, as published by the Free Software Foundation.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
Packit Service |
db8df9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
Packit Service |
db8df9 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
Packit Service |
db8df9 |
* more details.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
* You should have received a copy of the GNU General Public License along with
|
|
Packit Service |
db8df9 |
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
Packit Service |
db8df9 |
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
*/
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
#include <dirent.h>
|
|
Packit Service |
db8df9 |
#include <limits.h>
|
|
Packit Service |
db8df9 |
#include <stdio.h>
|
|
Packit Service |
db8df9 |
#include <stdlib.h>
|
|
Packit Service |
db8df9 |
#include <string.h>
|
|
Packit Service |
db8df9 |
#include <unistd.h>
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
#if _HAVE_DMALLOC_H
|
|
Packit Service |
db8df9 |
#include <dmalloc.h>
|
|
Packit Service |
db8df9 |
#endif
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
#include "config.h"
|
|
Packit Service |
db8df9 |
#include "enclosure.h"
|
|
Packit Service |
db8df9 |
#include "utils.h"
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
/**
|
|
Packit Service |
db8df9 |
* @brief Gets SAS address of an enclosure device.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
* This is internal function of enclosure module. The function reads a
|
|
Packit Service |
db8df9 |
* SAS address of an enclosure from sysfs attribute.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
* @param[in] path Path to enclosure device in sysfs tree.
|
|
Packit Service |
db8df9 |
*
|
|
Packit Service |
db8df9 |
* @return SAS address of an enclosure if successful, otherwise 0.
|
|
Packit Service |
db8df9 |
*/
|
|
Packit Service |
db8df9 |
#define SAS_DEVICE "/sas_device"
|
|
Packit Service |
db8df9 |
static uint64_t _get_sas_address(const char *path)
|
|
Packit Service |
db8df9 |
{
|
|
Packit Service |
db8df9 |
char *tmp = str_dup(path);
|
|
Packit Service |
db8df9 |
char buf[PATH_MAX];
|
|
Packit Service |
db8df9 |
char *p, *s;
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
p = strstr(tmp, "/expander");
|
|
Packit Service |
db8df9 |
if (p == NULL)
|
|
Packit Service |
db8df9 |
goto out;
|
|
Packit Service |
db8df9 |
s = strchr(p + 1, PATH_DELIM);
|
|
Packit Service |
db8df9 |
if (s == NULL)
|
|
Packit Service |
db8df9 |
goto out;
|
|
Packit Service |
db8df9 |
*s = '\0';
|
|
Packit Service |
db8df9 |
snprintf(buf, sizeof(buf), "%s%s%s", tmp, SAS_DEVICE, p);
|
|
Packit Service |
db8df9 |
free(tmp);
|
|
Packit Service |
db8df9 |
return get_uint64(buf, 0, "sas_address");
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
out:
|
|
Packit Service |
db8df9 |
free(tmp);
|
|
Packit Service |
db8df9 |
return 0;
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
#define SCSI_GEN "device/scsi_generic"
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
static char *_get_dev_sg(const char *encl_path)
|
|
Packit Service |
db8df9 |
{
|
|
Packit Service |
db8df9 |
char *ret = NULL;
|
|
Packit Service |
db8df9 |
DIR *d;
|
|
Packit Service |
db8df9 |
struct dirent *de;
|
|
Packit Service |
db8df9 |
size_t sg_path_size = strlen(encl_path) + strlen(SCSI_GEN) + 2;
|
|
Packit Service |
db8df9 |
char *sg_path = malloc(sg_path_size);
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
if (!sg_path)
|
|
Packit Service |
db8df9 |
return NULL;
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
snprintf(sg_path, sg_path_size, "%s/%s", encl_path, SCSI_GEN);
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
/* /sys/class/enclosure/X/device/scsi_generic path is expected. */
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
d = opendir(sg_path);
|
|
Packit Service |
db8df9 |
free(sg_path);
|
|
Packit Service |
db8df9 |
if (!d)
|
|
Packit Service |
db8df9 |
return NULL;
|
|
Packit Service |
db8df9 |
while ((de = readdir(d))) {
|
|
Packit Service |
db8df9 |
if ((strcmp(de->d_name, ".") == 0) ||
|
|
Packit Service |
db8df9 |
(strcmp(de->d_name, "..") == 0))
|
|
Packit Service |
db8df9 |
continue;
|
|
Packit Service |
db8df9 |
break;
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
if (de) {
|
|
Packit Service |
db8df9 |
size_t size = strlen("/dev/") + strlen(de->d_name) + 1;
|
|
Packit Service |
db8df9 |
ret = malloc(size);
|
|
Packit Service |
db8df9 |
if (ret)
|
|
Packit Service |
db8df9 |
snprintf(ret, size, "/dev/%s", de->d_name);
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
closedir(d);
|
|
Packit Service |
db8df9 |
return ret;
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
/*
|
|
Packit Service |
db8df9 |
* Allocates memory for enclosure device structure and initializes fields of
|
|
Packit Service |
db8df9 |
* the structure.
|
|
Packit Service |
db8df9 |
*/
|
|
Packit Service |
db8df9 |
struct enclosure_device *enclosure_device_init(const char *path)
|
|
Packit Service |
db8df9 |
{
|
|
Packit Service |
db8df9 |
char temp[PATH_MAX];
|
|
Packit Service |
db8df9 |
struct enclosure_device *result = NULL;
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
if (realpath(path, temp)) {
|
|
Packit Service |
db8df9 |
result = calloc(1, sizeof(struct enclosure_device));
|
|
Packit Service |
db8df9 |
if (result == NULL)
|
|
Packit Service |
db8df9 |
return NULL;
|
|
Packit Service |
db8df9 |
result->sysfs_path = str_dup(temp);
|
|
Packit Service |
db8df9 |
result->sas_address = _get_sas_address(temp);
|
|
Packit Service |
db8df9 |
result->dev_path = _get_dev_sg(temp);
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
return result;
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
|
|
Packit Service |
db8df9 |
/*
|
|
Packit Service |
db8df9 |
* The function returns memory allocated for fields of enclosure structure to
|
|
Packit Service |
db8df9 |
* the system.
|
|
Packit Service |
db8df9 |
*/
|
|
Packit Service |
db8df9 |
void enclosure_device_fini(struct enclosure_device *enclosure)
|
|
Packit Service |
db8df9 |
{
|
|
Packit Service |
db8df9 |
if (enclosure) {
|
|
Packit Service |
db8df9 |
free(enclosure->sysfs_path);
|
|
Packit Service |
db8df9 |
free(enclosure->dev_path);
|
|
Packit Service |
db8df9 |
free(enclosure);
|
|
Packit Service |
db8df9 |
}
|
|
Packit Service |
db8df9 |
}
|