Blame src/pci_slot.c

Packit 7e09eb
/*
Packit 7e09eb
 * Intel(R) Enclosure LED Utilities
Packit 7e09eb
 * Copyright (C) 2016-2018 Intel Corporation.
Packit 7e09eb
 *
Packit 7e09eb
 * This program is free software; you can redistribute it and/or modify it
Packit 7e09eb
 * under the terms and conditions of the GNU General Public License,
Packit 7e09eb
 * version 2, as published by the Free Software Foundation.
Packit 7e09eb
 *
Packit 7e09eb
 * This program is distributed in the hope it will be useful, but WITHOUT
Packit 7e09eb
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 7e09eb
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
Packit 7e09eb
 * more details.
Packit 7e09eb
 *
Packit 7e09eb
 * You should have received a copy of the GNU General Public License along with
Packit 7e09eb
 * this program; if not, write to the Free Software Foundation, Inc.,
Packit 7e09eb
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit 7e09eb
 *
Packit 7e09eb
 */
Packit 7e09eb
Packit 7e09eb
#include <limits.h>
Packit 7e09eb
#include <stdint.h>
Packit 7e09eb
#include <stdlib.h>
Packit 7e09eb
#include <string.h>
Packit 7e09eb
#include <unistd.h>
Packit 7e09eb
Packit 7e09eb
#if _HAVE_DMALLOC_H
Packit 7e09eb
#include <dmalloc.h>
Packit 7e09eb
#endif
Packit 7e09eb
Packit 7e09eb
#include "config.h"
Packit 7e09eb
#include "pci_slot.h"
Packit 7e09eb
#include "utils.h"
Packit 7e09eb
Packit 7e09eb
/*
Packit 7e09eb
 * Allocates memory for PCI hotplug slot structure and initializes fields of
Packit 7e09eb
 * the structure.
Packit 7e09eb
 */
Packit 7e09eb
struct pci_slot *pci_slot_init(const char *path)
Packit 7e09eb
{
Packit 7e09eb
	struct pci_slot *result = NULL;
Packit 7e09eb
Packit 7e09eb
	result = malloc(sizeof(struct pci_slot));
Packit 7e09eb
	if (result == NULL)
Packit 7e09eb
		return NULL;
Packit 7e09eb
	result->sysfs_path = str_dup(path);
Packit 7e09eb
	result->address = get_text(path, "address");
Packit 7e09eb
	result->attention = get_int(path, -1, "attention");
Packit 7e09eb
Packit 7e09eb
	if (result->attention == -1) {
Packit 7e09eb
		pci_slot_fini(result);
Packit 7e09eb
		return NULL;
Packit 7e09eb
	}
Packit 7e09eb
Packit 7e09eb
	return result;
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
/*
Packit 7e09eb
 * The function returns memory allocated for fields of hotplug slot structure
Packit 7e09eb
 * to the system.
Packit 7e09eb
 */
Packit 7e09eb
void pci_slot_fini(struct pci_slot *slot)
Packit 7e09eb
{
Packit 7e09eb
	if (slot) {
Packit 7e09eb
		free(slot->sysfs_path);
Packit 7e09eb
		free(slot->address);
Packit 7e09eb
		free(slot);
Packit 7e09eb
	}
Packit 7e09eb
}