Script 'mail_helper' called by obssrc Hello community, here is the log from the commit of package ledmon for openSUSE:Factory checked in at 2022-07-19 17:19:41 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/ledmon (Old) and /work/SRC/openSUSE:Factory/.ledmon.new.1523 (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "ledmon" Tue Jul 19 17:19:41 2022 rev:23 rq:990038 version:0.96 Changes: -------- --- /work/SRC/openSUSE:Factory/ledmon/ledmon.changes 2021-10-16 22:47:52.508703796 +0200 +++ /work/SRC/openSUSE:Factory/.ledmon.new.1523/ledmon.changes 2022-07-19 17:19:42.228393987 +0200 @@ -1,0 +2,14 @@ +Mon Jul 18 15:09:00 UTC 2022 - Heming Zhao <[email protected]> + +- Update to version 0.96: (PED-85) + * Manual updates, clarify --listed-only option + * Fix cache indexing of ATA port + * Fixes in regard to macros + * Fix memory leak in amd_ipmi.c + * Fix NULL pointer dereferences in sysfs.c + * Make messages appear in service log immediately +- add patches after 0.96: + + 0001-Ledctl-slots-management-94.patch + + 0002-add-Dell-15G-servers.patch + +------------------------------------------------------------------- Old: ---- ledmon-0.95.tar.gz New: ---- 0001-Ledctl-slots-management-94.patch 0002-add-Dell-15G-servers.patch ledmon-0.96.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ ledmon.spec ++++++ --- /var/tmp/diff_new_pack.MgQz50/_old 2022-07-19 17:19:42.676394584 +0200 +++ /var/tmp/diff_new_pack.MgQz50/_new 2022-07-19 17:19:42.680394590 +0200 @@ -1,7 +1,7 @@ # # spec file for package ledmon # -# Copyright (c) 2021 SUSE LLC +# Copyright (c) 2022 SUSE LLC # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -17,7 +17,7 @@ Name: ledmon -Version: 0.95 +Version: 0.96 Release: 0 Summary: Enclosure LED Utilities License: GPL-2.0-only @@ -25,6 +25,8 @@ URL: https://github.com/intel/ledmon/ Source0: https://github.com/intel/ledmon/archive/%{version}.tar.gz#/%{name}-%{version}.tar.gz Patch0: harden_ledmon.service.patch +Patch1: 0001-Ledctl-slots-management-94.patch +Patch2: 0002-add-Dell-15G-servers.patch BuildRequires: autoconf BuildRequires: automake BuildRequires: libsgutils-devel ++++++ 0001-Ledctl-slots-management-94.patch ++++++ ++++ 1366 lines (skipped) ++++++ 0002-add-Dell-15G-servers.patch ++++++ >From 7274b21797d1ad76a2f4ccd2e3f70279e50512b4 Mon Sep 17 00:00:00 2001 From: prabhakar <[email protected]> Date: Tue, 5 Jul 2022 08:16:18 -0400 Subject: [PATCH 2/2] add Dell 15G servers --- src/dellssd.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/dellssd.c b/src/dellssd.c index 5a7ac5f4a629..fce3110d62d5 100644 --- a/src/dellssd.c +++ b/src/dellssd.c @@ -93,6 +93,9 @@ enum { DELL_13G_MODULAR = 0x21, DELL_14G_MONOLITHIC = 0x30, DELL_14G_MODULAR = 0x31, + DELL_15G_MONOLITHIC = 0x40, + DELL_15G_MODULAR = 0x41, + }; int get_dell_server_type() @@ -125,6 +128,9 @@ int get_dell_server_type() case DELL_13G_MODULAR: case DELL_14G_MONOLITHIC: case DELL_14G_MODULAR: + case DELL_15G_MONOLITHIC: + case DELL_15G_MODULAR: + gen = rdata[10]; return gen; default: @@ -166,6 +172,9 @@ static int ipmi_setled(int b, int d, int f, int state) break; case DELL_14G_MONOLITHIC: case DELL_14G_MODULAR: + case DELL_15G_MONOLITHIC: + case DELL_15G_MODULAR: + data[1] = DELL_OEM_STORAGE_GETDRVMAP_14G; break; } @@ -206,6 +215,9 @@ static int ipmi_setled(int b, int d, int f, int state) break; case DELL_14G_MONOLITHIC: case DELL_14G_MODULAR: + case DELL_15G_MONOLITHIC: + case DELL_15G_MODULAR: + data[1] = DELL_OEM_STORAGE_SETDRVSTATUS_14G; break; } -- 2.34.1 ++++++ ledmon-0.95.tar.gz -> ledmon-0.96.tar.gz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/CHANGELOG.md new/ledmon-0.96/CHANGELOG.md --- old/ledmon-0.95/CHANGELOG.md 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/CHANGELOG.md 2022-05-31 16:17:48.000000000 +0200 @@ -1,3 +1,17 @@ +### v0.96 / 2022-05-26 + +[Commit list](https://github.com/intel/ledmon/compare/v0.95...v0.96) + +Bug fixes + +* Manual updates, clarify --listed-only option +* Fix cache indexing of ATA port +* Fixes in regard to macros +* Fix memory leak in amd_ipmi.c +* Fix NULL pointer dereferences in sysfs.c +* Make messages appear in service log immediately +* Other minor fixes + ### v0.95 / 2021-01-15 [Commit list](https://github.com/intel/ledmon/compare/v0.94...v0.95) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/Makefile.am new/ledmon-0.96/Makefile.am --- old/ledmon-0.95/Makefile.am 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/Makefile.am 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ # # Intel(R) Enclosure LED Utilities -# Copyright (C) 2009-2021 Intel Corporation. +# Copyright (C) 2009-2022 Intel Corporation. # # This program is free software; you can redistribute it and/or modify it # under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/README.md new/ledmon-0.96/README.md --- old/ledmon-0.95/README.md 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/README.md 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ -# This package contains the Enclosure LED Utilities, version 0.95 +# This package contains the Enclosure LED Utilities, version 0.96 -Copyright (C) 2009-2021 Intel Corporation. +Copyright (C) 2009-2022 Intel Corporation. All files in this package can be freely distributed and used according to the terms of the GNU General Public License, version 2. diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/configure.ac new/ledmon-0.96/configure.ac --- old/ledmon-0.95/configure.ac 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/configure.ac 2022-05-31 16:17:48.000000000 +0200 @@ -1,8 +1,8 @@ AC_PREREQ([2.69]) -AC_INIT([ledmon], [0.95]) +AC_INIT([ledmon], [0.96]) AC_CONFIG_MACRO_DIR([m4]) -AC_SUBST([PACKAGE_DATE], "January 2021") +AC_SUBST([PACKAGE_DATE], "May 2022") AM_INIT_AUTOMAKE([-Wall -Werror foreign]) # Checks for programs. diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/doc/Makefile.am new/ledmon-0.96/doc/Makefile.am --- old/ledmon-0.95/doc/Makefile.am 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/doc/Makefile.am 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ # # Intel(R) Enclosure LED Utilities -# Copyright (C) 2009-2021 Intel Corporation. +# Copyright (C) 2009-2022 Intel Corporation. # # This program is free software; you can redistribute it and/or modify it # under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/doc/ledctl.pod new/ledmon-0.96/doc/ledctl.pod --- old/ledmon-0.95/doc/ledctl.pod 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/doc/ledctl.pod 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ # # Intel(R) Enclosure LED Utilities -# Copyright (C) 2009-2021 Intel Corporation. +# Copyright (C) 2009-2022 Intel Corporation. # # This program is free software; you can redistribute it and/or modify it # under the terms and conditions of the GNU General Public License, @@ -297,12 +297,9 @@ section below for details. A device is a path to file in /dev directory or in /sys/block directory. -It may identify a block device, a RAID device or a container device. -In case of a RAID device or a container device a state will be set for all -block devices associated, respectively. The LEDs of devices listed in I<list_of_devices> are set to the given -pattern I<pattern_name> and all other LEDs are turned off (unless --listed-only +pattern I<pattern_name> and all other LEDs, on all devices, are turned off (unless --listed-only option is given). =head1 OPTIONS @@ -354,33 +351,44 @@ =head1 EXAMPLES -The following example illustrates how to locate a single block device. +The following example illustrates how to set I<locate> on a single +block device. Note that all remaining LEDs, on all devices, will be turned off. ledctl locate=/dev/sda -The following example illustrates how to turn Locate LED off for the same block device. +The following example illustrates how to set I<locate_off> on a single +block device. - ledctl locate_off=/dev/sda + ledctl --listed-only locate_off=/dev/sda -The following example illustrates how to locate disks of a RAID device and -how to set rebuild pattern for two block devices at the same time. This example -uses both formats of device list. +The following example illustrates how to set I<off> on the given devices. It +uses second format of device list. - ledctl locate=/dev/md127 rebuild={ /sys/block/sd[a-b] } + ledctl --listed-only off={ /dev/sda /dev/sdb } -The following example illustrates how to turn Status LED and Failure LED off for -the given device(s). +The following example illustrates how to set I<locate> and I<rebuild> on +different devices at the same time. It uses the second format of device list. - ledctl off={ /dev/sda /dev/sdb } + ledctl --listed-only locate={ /dev/sdb } rebuild={ /sys/block/sdc } -The following example illustrates how to locate a three block devices. This -example uses the first format of device list. +The following example illustrates how to I<locate> on three block devices. It +uses the first format of device list. - ledctl locate=/dev/sda,/dev/sdb,/dev/sdc + ledctl --listed-only locate=/dev/sda,/dev/sdb,/dev/sdc + +The following example illustrates how to set I<locate> and I<rebuild> on +different devices at the same time. It uses the first format of device list. + + ledctl --listed-only locate=/dev/sdb rebuild=/sys/block/sdc + +The following example illustrates how to set I<locate> and I<rebuild> on +different devices at the same time. It uses the both formats of device list. + + ledctl --listed-only locate={ /dev/sdb } rebuild=/sys/block/sdc =head1 LICENSE -Copyright (c) 2009-2021 Intel Corporation. +Copyright (c) 2009-2022 Intel Corporation. This program is distributed under the terms of the GNU General Public License as published by the Free Software Foundation. See the built-in help for diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/doc/ledmon.conf.pod new/ledmon-0.96/doc/ledmon.conf.pod --- old/ledmon-0.95/doc/ledmon.conf.pod 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/doc/ledmon.conf.pod 2022-05-31 16:17:48.000000000 +0200 @@ -45,6 +45,7 @@ B<BLACKLIST> - Ledmon will exclude scanning controllers listed on blacklist. When whitelist is also set in config file, the blacklist will be ignored. The controllers should be separated by comma (B<,>) character. +The B<XBD Extended Regular Expressions> are supported. B<BLINK_ON_INIT> - Related with RAID Initialization (resync), Verify (check) and Verify and Fix (repair) processes. If value is set to true - status LEDs of @@ -82,6 +83,7 @@ B<WHITELIST> - Ledmon will limit changing LED state to controllers listed on whitelist. If any whitelist is set, only devices from list will be scanned by ledmon. The controllers should be separated by comma (B<,>) character. +The B<XBD Extended Regular Expressions> are supported. =head1 EXAMPLES diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/doc/ledmon.pod new/ledmon-0.96/doc/ledmon.pod --- old/ledmon-0.95/doc/ledmon.pod 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/doc/ledmon.pod 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ # # Intel(R) Enclosure LED Utilities -# Copyright (C) 2009-2021 Intel Corporation. +# Copyright (C) 2009-2022 Intel Corporation. # # This program is free software; you can redistribute it and/or modify it # under the terms and conditions of the GNU General Public License, @@ -183,7 +183,7 @@ =head1 LICENSE -Copyright (c) 2009-2021 Intel Corporation. +Copyright (c) 2009-2022 Intel Corporation. This program is distributed under the terms of the GNU General Public License as published by the Free Software Foundation. See the build-in help for details diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/Makefile.am new/ledmon-0.96/src/Makefile.am --- old/ledmon-0.95/src/Makefile.am 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/Makefile.am 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ # # Intel(R) Enclosure LED Utilities -# Copyright (C) 2009-2021 Intel Corporation. +# Copyright (C) 2009-2022 Intel Corporation. # # This program is free software; you can redistribute it and/or modify it # under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/ahci.c new/ledmon-0.96/src/ahci.c --- old/ledmon-0.95/src/ahci.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/ahci.c 2022-05-31 16:17:48.000000000 +0200 @@ -87,7 +87,7 @@ if ((ibpi < IBPI_PATTERN_NORMAL) || (ibpi > IBPI_PATTERN_LOCATE_OFF)) __set_errno_and_return(ERANGE); - sprintf(temp, "%u", ibpi2sgpio[ibpi]); + snprintf(temp, WRITE_BUFFER_SIZE, "%u", ibpi2sgpio[ibpi]); snprintf(path, sizeof(path), "%s/em_message", sysfs_path); @@ -102,28 +102,30 @@ char *ahci_get_port_path(const char *path) { - char *p; - char tmp[PATH_MAX]; + char *target_p, *host_p; + size_t host_length, length_to_target; char *buf; - size_t buf_size; - p = strstr(path, "/target"); - if (p == NULL) + host_p = strstr(path, "/host"); + if (host_p == NULL) return NULL; - if (sizeof(tmp) <= (p - path)) + target_p = strstr(host_p, "/target"); + if (target_p == NULL) return NULL; - strncpy(tmp, path, p - path); - tmp[p - path] = '\0'; - p = strrchr(tmp, PATH_DELIM); - if (p == NULL) + + length_to_target = target_p - path; + host_length = target_p - host_p; + + if (host_length + length_to_target + strlen(SCSI_HOST) > PATH_MAX - 1) return NULL; - buf_size = strlen(tmp) + strlen(p) + strlen(SCSI_HOST) + 1; - buf = malloc(buf_size); + buf = calloc(PATH_MAX, sizeof(char)); if (buf == NULL) return NULL; - snprintf(buf, buf_size, "%s%s%s", tmp, SCSI_HOST, p); + strncpy(buf, path, length_to_target); + strncat(buf, SCSI_HOST, strlen(SCSI_HOST) + 1); + strncat(buf, host_p, host_length); return buf; } diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/amd.c new/ledmon-0.96/src/amd.c --- old/ledmon-0.95/src/amd.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/amd.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * AMD LED control - * Copyright (C) 2021, Advanced Micro Devices, Inc. + * Copyright (C) 2022, Advanced Micro Devices, Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/amd.h new/ledmon-0.96/src/amd.h --- old/ledmon-0.95/src/amd.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/amd.h 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * AMD LED control - * Copyright (C) 2021, Advanced Micro Devices, Inc. + * Copyright (C) 2022, Advanced Micro Devices, Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/amd_ipmi.c new/ledmon-0.96/src/amd_ipmi.c --- old/ledmon-0.95/src/amd_ipmi.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/amd_ipmi.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * AMD IPMI LED control - * Copyright (C) 2021, Advanced Micro Devices, Inc. + * Copyright (C) 2022, Advanced Micro Devices, Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -52,6 +52,17 @@ [IBPI_PATTERN_HOTSPARE] = 0x47, }; +#define MG9098_CHIP_ID_REG 0x63 + +#define AMD_IPMI_NETFN 0x06 +#define AMD_IPMI_CMD 0x52 + +#define AMD_ETHANOL_X_CHANNEL 0x0d +#define AMD_DAYTONA_X_CHANNEL 0x17 + +#define AMD_BASE_SLAVE_ADDR 0xc0 +#define AMD_NVME_SLAVE_ADDR 0xc4 + /* The path we are given should be similar to * /sys/devices/pci0000:e0/0000:e0:03.3/0000:e3:00.0 * ^^^^^^^^^^ @@ -95,7 +106,8 @@ char *dname = strrchr(dir_path, '/'); dname++; - port = strtol(dname, NULL, 0); + if (str_toi(&port, dname, NULL, 0) != 0) + return -1; break; } } @@ -148,7 +160,8 @@ /* skip past 'ata' to get the ata port number */ t += 3; - port = strtoul(t, NULL, 10); + if (str_toi(&port, t, NULL, 10) != 0) + return -1; return port; } @@ -204,10 +217,10 @@ switch (amd_ipmi_platform) { case AMD_PLATFORM_ETHANOL_X: - drive->channel = 0xd; + drive->channel = AMD_ETHANOL_X_CHANNEL; break; case AMD_PLATFORM_DAYTONA_X: - drive->channel = 0x17; + drive->channel = AMD_DAYTONA_X_CHANNEL; break; default: rc = -1; @@ -224,27 +237,27 @@ switch (amd_ipmi_platform) { case AMD_PLATFORM_ETHANOL_X: - drive->slave_addr = 0xc0; + drive->slave_addr = AMD_BASE_SLAVE_ADDR; break; case AMD_PLATFORM_DAYTONA_X: if (drive->dev == AMD_NO_DEVICE) { /* Assume base slave address, we may not be able * to retrieve a valid amd_drive yet. */ - drive->slave_addr = 0xc0; + drive->slave_addr = AMD_BASE_SLAVE_ADDR; } else if (drive->dev == AMD_NVME_DEVICE) { /* On DaytonaX systems only drive bays 19 - 24 * support NVMe devices so use the slave address * for the corresponding MG9098 chip. */ - drive->slave_addr = 0xc4; + drive->slave_addr = AMD_NVME_SLAVE_ADDR; } else { if (drive->port <= 8) - drive->slave_addr = 0xc0; + drive->slave_addr = AMD_BASE_SLAVE_ADDR; else if (drive->port > 8 && drive->port < 17) - drive->slave_addr = 0xc2; + drive->slave_addr = AMD_BASE_SLAVE_ADDR + 2; else - drive->slave_addr = 0xc4; + drive->slave_addr = AMD_NVME_SLAVE_ADDR; } break; @@ -285,8 +298,8 @@ log_debug(REG_FMT_2, "channel", cmd_data[0], "slave addr", cmd_data[1]); log_debug(REG_FMT_2, "len", cmd_data[2], "register", cmd_data[3]); - rc = ipmicmd(BMC_SA, 0x0, 0x6, 0x52, 4, &cmd_data, 1, &data_sz, - &status); + rc = ipmicmd(BMC_SA, 0x0, AMD_IPMI_NETFN, AMD_IPMI_CMD, 4, &cmd_data, + 1, &data_sz, &status); if (rc) { log_error("Could not determine current register %x setting\n", reg); @@ -310,8 +323,8 @@ log_debug(REG_FMT_2, "len", cmd_data[2], "register", cmd_data[3]); log_debug(REG_FMT_1, "status", cmd_data[4]); - rc = ipmicmd(BMC_SA, 0x0, 0x6, 0x52, 5, &cmd_data, 1, &data_sz, - &status); + rc = ipmicmd(BMC_SA, 0x0, AMD_IPMI_NETFN, AMD_IPMI_CMD, 5, &cmd_data, + 1, &data_sz, &status); if (rc) { log_error("Could not enable register %x\n", reg); return rc; @@ -368,19 +381,20 @@ cmd_data[0] = drive.channel; cmd_data[1] = drive.slave_addr; cmd_data[2] = 0x1; - cmd_data[3] = 0x63; + cmd_data[3] = MG9098_CHIP_ID_REG; status = 0; - rc = ipmicmd(BMC_SA, 0x0, 0x6, 0x52, 4, &cmd_data, 1, - &data_sz, &status); + rc = ipmicmd(BMC_SA, 0x0, AMD_IPMI_NETFN, AMD_IPMI_CMD, 4, &cmd_data, + 1, &data_sz, &status); if (rc) { log_error("Can't determine MG9098 Status for AMD platform\n"); return 0; } + /* Status return of 98 indicates MG9098 backplane */ if (status != 98) { - log_error("Platform %s does not have a MG9098 controller\n"); + log_error("Platform does not have a MG9098 controller\n"); return 0; } @@ -423,20 +437,16 @@ char *_amd_ipmi_get_path(const char *cntrl_path, const char *sysfs_path) { - char *p, *t; + char *t; /* For NVMe devices we can just dup the path sysfs path */ - p = strstr(cntrl_path, "nvme"); - if (p) + if (strstr(cntrl_path, "nvme")) return strdup(sysfs_path); - /* For SATA devices we need everything up to 'ataXX/' in the path */ - p = strdup(cntrl_path); - if (!p) - return NULL; - - /* Find the beginning of the ataXX piece of the path */ - t = strstr(p, "ata"); + /* + * For SATA devices we need everything up to 'ataXX/' in the path + */ + t = strstr(cntrl_path, "ata"); if (!t) return NULL; @@ -447,8 +457,6 @@ if (!t) return NULL; - *++t = '\0'; - - return p; + return strndup(cntrl_path, (t - cntrl_path) + 1); } diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/amd_sgpio.c new/ledmon-0.96/src/amd_sgpio.c --- old/ledmon-0.95/src/amd_sgpio.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/amd_sgpio.c 2022-05-31 16:17:48.000000000 +0200 @@ -275,7 +275,7 @@ * cache_entry[n] => drive (4*n) to drive (4*n + 3) */ - index = (drive->ata_port / 4); + index = ((drive->ata_port - 1) / 4); return &sgpio_cache[index]; } @@ -569,7 +569,8 @@ /* skip past 'ata' to get the ata port number */ a += 3; - drive->ata_port = strtoul(a, NULL, 10); + if (str_toi(&drive->ata_port, a, NULL, 10) != 0) + return -1; found = _find_file_path(ata_dir, "port_no", path, PATH_MAX); if (!found) { diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/block.c new/ledmon-0.96/src/block.c --- old/ledmon-0.95/src/block.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/block.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -190,7 +190,7 @@ list_for_each(cntrl_list, cntrl) { if (strncmp(cntrl->sysfs_path, path, - strlen(cntrl->sysfs_path)) == 0) { + strnlen(cntrl->sysfs_path, PATH_MAX)) == 0) { if (cntrl->cntrl_type == CNTRL_TYPE_NPEM) return cntrl; non_npem_cntrl = cntrl; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/cntrl.c new/ledmon-0.96/src/cntrl.c --- old/ledmon-0.95/src/cntrl.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/cntrl.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -119,10 +119,12 @@ char tmp[PATH_MAX]; char *t; + memset(&tmp, 0, sizeof(tmp)); + if (!_is_nvme_cntrl(path)) return 0; - sprintf(tmp, "%s", path); + strncpy(tmp, path, PATH_MAX - 1); t = strrchr(tmp, '/'); if (!t) return 0; @@ -435,7 +437,7 @@ em_enabled = 0; } if (em_enabled) { - device = malloc(sizeof(struct cntrl_device)); + device = calloc(1, sizeof(struct cntrl_device)); if (device) { if (type == CNTRL_TYPE_SCSI) { device->isci_present = _is_isci_cntrl(path); @@ -445,7 +447,7 @@ device->hosts = NULL; } device->cntrl_type = type; - device->sysfs_path = str_dup(path); + strncpy(device->sysfs_path, path, PATH_MAX - 1); } } else { log_error @@ -463,7 +465,6 @@ void cntrl_device_fini(struct cntrl_device *device) { if (device) { - free(device->sysfs_path); free_hosts(device->hosts); free(device); } diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/cntrl.h new/ledmon-0.96/src/cntrl.h --- old/ledmon-0.95/src/cntrl.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/cntrl.h 2022-05-31 16:17:48.000000000 +0200 @@ -20,6 +20,8 @@ #ifndef _CNTRL_H_INCLUDED_ #define _CNTRL_H_INCLUDED_ +#include <limits.h> + /** * This enumeration type lists all supported storage controller types. */ @@ -42,7 +44,7 @@ /** * Path to the device in sysfs tree. */ - char *sysfs_path; + char sysfs_path[PATH_MAX]; /** * Type of storage controller device. diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/config_file.c new/ledmon-0.96/src/config_file.c --- old/ledmon-0.95/src/config_file.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/config_file.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,7 +1,7 @@ /* * Intel(R) Enclosure LED Utilities * - * Copyright (C) 2017-2021 Intel Corporation. + * Copyright (C) 2017-2022 Intel Corporation. * Copyright (C) 2009 Karel Zak <[email protected]> * * SPDX-License-Identifier: GPL-2.0 @@ -143,8 +143,8 @@ if (!strncmp(s, "INTERVAL=", 9)) { s += 9; if (*s) { - if (sscanf(s, "%d", &conf.scan_interval) != 1 || - conf.scan_interval < LEDMON_MIN_SLEEP_INTERVAL) + if (str_toi(&conf.scan_interval, s, NULL, 10) != 0 || + conf.scan_interval < LEDMON_MIN_SLEEP_INTERVAL) conf.scan_interval = LEDMON_MIN_SLEEP_INTERVAL; } } else if (!strncmp(s, "LOG_LEVEL=", 10)) { @@ -205,14 +205,14 @@ if (!filename || (filename && access(filename, F_OK) < 0)) { if (filename) - fprintf(stdout, "%s: does not exist, using global config file\n", + fprintf(stderr, "%s: does not exist, using global config file\n", filename); filename = LEDMON_DEF_CONF_FILE; } f = fopen(filename, "re"); if (!f) { - fprintf(stdout, "%s: does not exist, using built-in defaults\n", + fprintf(stderr, "%s: does not exist, using built-in defaults\n", filename); } else { while (!feof(f)) { @@ -228,7 +228,7 @@ if (!list_is_empty(&conf.cntrls_whitelist) && !list_is_empty(&conf.cntrls_blacklist)) - fprintf(stdout, "Both whitelist and blacklist are specified - ignoring blacklist."); + fprintf(stderr, "Both whitelist and blacklist are specified - ignoring blacklist."); return STATUS_SUCCESS; } @@ -240,11 +240,8 @@ memset(buf, 0, sizeof(buf)); list_for_each(list, elem) { - if (elem) { - int curr = strlen(buf); - - snprintf(buf + curr, sizeof(buf) - curr, "%s,", elem); - } + int curr = strlen(buf); + snprintf(buf + curr, sizeof(buf) - curr, "%s,", elem); } return str_dup(buf); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/enclosure.c new/ledmon-0.96/src/enclosure.c --- old/ledmon-0.95/src/enclosure.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/enclosure.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -110,7 +110,7 @@ */ struct enclosure_device *enclosure_device_init(const char *path) { - char temp[PATH_MAX]; + char temp[PATH_MAX] = "\0"; struct enclosure_device *enclosure; int ret; int fd; @@ -124,7 +124,7 @@ goto out; } - enclosure->sysfs_path = str_dup(temp); + memccpy(enclosure->sysfs_path, temp, '\0', PATH_MAX - 1); enclosure->sas_address = _get_sas_address(temp); enclosure->dev_path = _get_dev_sg(temp); @@ -157,7 +157,6 @@ { if (enclosure) { free(enclosure->slots); - free(enclosure->sysfs_path); free(enclosure->dev_path); free(enclosure); } diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/enclosure.h new/ledmon-0.96/src/enclosure.h --- old/ledmon-0.95/src/enclosure.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/enclosure.h 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -21,6 +21,7 @@ #define _ENCLOSURE_H_INCLUDED_ #include <stdint.h> +#include <limits.h> #include "ses.h" @@ -35,7 +36,7 @@ * Path to an enclosure device in sysfs tree. This is controller base * canonical path. */ - char *sysfs_path; + char sysfs_path[PATH_MAX]; /** * SAS address as identifier of an enclosure. diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/ledctl.c new/ledmon-0.96/src/ledctl.c --- old/ledmon-0.95/src/ledctl.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/ledctl.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -96,7 +96,7 @@ * information about the version of ledctl utility. */ static char *ledctl_version = "Intel(R) Enclosure LED Control Application %s %s\n" - "Copyright (C) 2009-2021 Intel Corporation.\n"; + "Copyright (C) 2009-2022 Intel Corporation.\n"; /** * Internal variable of monitor service. It is used to help parse command line @@ -441,7 +441,7 @@ if (strstr(temp, "/dev/") != NULL) { if (stat(temp, &st) < 0) return STATUS_STAT_ERROR; - sprintf(temp, "/sys/dev/block/%u:%u", major(st.st_rdev), + snprintf(temp, PATH_MAX, "/sys/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)); if ((realpath(temp, path) == NULL) && (errno != ENOTDIR)) return STATUS_INVALID_PATH; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/ledmon.c new/ledmon-0.96/src/ledmon.c --- old/ledmon-0.95/src/ledmon.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/ledmon.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -119,7 +119,7 @@ * information about the version of monitor service. */ static char *ledmon_version = "Intel(R) Enclosure LED Monitor Service %s %s\n" - "Copyright (C) 2009-2021 Intel Corporation.\n"; + "Copyright (C) 2009-2022 Intel Corporation.\n"; /** * Internal variable of monitor service. It is used to help parse command line @@ -292,9 +292,7 @@ */ static status_t _set_sleep_interval(const char *optarg) { - errno = 0; - conf.scan_interval = strtol(optarg, NULL, 10); - if (errno != 0) { + if (str_toi(&conf.scan_interval, optarg, NULL, 10) != 0) { log_error("Cannot parse sleep interval"); return STATUS_CMDLINE_ERROR; } @@ -842,7 +840,10 @@ char *elem; list_for_each(&dir, elem) { - int fd = (int)strtol(basename(elem), NULL, 10); + int fd; + + if (str_toi(&fd, basename(elem), NULL, 10) != 0) + continue; if (fd != get_log_fd()) close(fd); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/list.c new/ledmon-0.96/src/list.c --- old/ledmon-0.95/src/list.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/list.c 2022-05-31 16:17:48.000000000 +0200 @@ -59,6 +59,11 @@ struct node *new; struct node **x; + if (!item) { + log_error("Failed to insert item into list. NULL cannot be inserted."); + exit(1); + } + new = malloc(sizeof(struct node)); if (!new) { log_error("Failed to allocate memory for list node."); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/list.h new/ledmon-0.96/src/list.h --- old/ledmon-0.95/src/list.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/list.h 2022-05-31 16:17:48.000000000 +0200 @@ -48,7 +48,7 @@ #define __list_for_each(__list, __item, __start_fn, __iter_fn) \ for (struct node *__node = __start_fn(__list); \ - __node && ((__item = __node->item) || (!__node->item)); \ + __node && ((__item = __node->item) || (!__item)); \ __node = __iter_fn(__node)) #define list_for_each(__list, __item) \ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/npem.c new/ledmon-0.96/src/npem.c --- old/ledmon-0.95/src/npem.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/npem.c 2022-05-31 16:17:48.000000000 +0200 @@ -84,11 +84,16 @@ { unsigned int domain, bus, dev, fn; char *p = strrchr(path, '/'); + int ret = 0; if (!p) return NULL; - if (sscanf(p + 1, "%x:%x:%x.%x", &domain, &bus, &dev, &fn) != 4) + ret += str_toui(&domain, p + 1, &p, 16); + ret += str_toui(&bus, p + 1, &p, 16); + ret += str_toui(&dev, p + 1, &p, 16); + ret += str_toui(&fn, p + 1, &p, 16); + if (ret != 0) return NULL; return pci_get_dev(pacc, domain, bus, dev, fn); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/pidfile.c new/ledmon-0.96/src/pidfile.c --- old/ledmon-0.95/src/pidfile.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/pidfile.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -56,7 +56,7 @@ close(fd); return STATUS_FILE_LOCK_ERROR; } - sprintf(buf, "%d\n", getpid()); + snprintf(buf, PATH_MAX, "%d\n", getpid()); count = write(fd, buf, strlen(buf)); close(fd); return (count < 0) ? STATUS_FILE_WRITE_ERROR : STATUS_SUCCESS; @@ -99,7 +99,8 @@ p = buf_read(path); if (p == NULL) return STATUS_INVALID_PATH; - tp = strtol(p, NULL, 10); + if (str_toi(&tp, p, NULL, 10) != 0) + return STATUS_DATA_ERROR; if (pid) *pid = tp; free(p); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/scsi.c new/ledmon-0.96/src/scsi.c --- old/ledmon-0.95/src/scsi.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/scsi.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -157,7 +157,7 @@ { char *host; char host_path[PATH_MAX] = { 0 }; - size_t ctrl_path_len = strlen(ctrl_path); + size_t ctrl_path_len = strnlen(ctrl_path, PATH_MAX); if (strncmp(path, ctrl_path, ctrl_path_len) != 0) return NULL; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/scsi.h new/ledmon-0.96/src/scsi.h --- old/ledmon-0.95/src/scsi.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/scsi.h 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/ses.c new/ledmon-0.96/src/ses.c --- old/ledmon-0.95/src/ses.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/ses.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/ses.h new/ledmon-0.96/src/ses.h --- old/ledmon-0.95/src/ses.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/ses.h 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/slave.c new/ledmon-0.96/src/slave.c --- old/ledmon-0.95/src/slave.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/slave.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -75,17 +75,21 @@ /** */ -static unsigned int _get_slot(const char *path) +static int _get_slot(const char *path, unsigned int *dest) { - unsigned int result = -1; - + int ret = 1; + unsigned int n; char *p = get_text(path, "slot"); + if (p) { if (strcmp(p, "none") != 0) - result = strtol(p, NULL, 10); + if (str_toui(&n, p, NULL, 10) == 0) { + *dest = n; + ret = 0; + } free(p); } - return result; + return ret; } /** @@ -130,12 +134,14 @@ block = _get_block(path, block_list); if (block) { device = malloc(sizeof(struct slave_device)); - if (device) { + if (device && _get_slot(path, &device->slot) == 0) { device->raid = NULL; device->state = _get_state(path); - device->slot = _get_slot(path); device->errors = _get_errors(path); device->block = block; + } else { + free(device); + device = NULL; } } return device; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/smp.c new/ledmon-0.96/src/smp.c --- old/ledmon-0.95/src/smp.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/smp.c 2022-05-31 16:17:48.000000000 +0200 @@ -553,7 +553,7 @@ { char *path2 = NULL; char *c; - int host, port = 0; + int port = 0; struct dirent *de; DIR *d; @@ -604,8 +604,11 @@ /* Need link called "phy-XX:Y * Y is real phy we need. * This can also be found - * in phy_identifier file */ - if (sscanf(de->d_name, "phy-%d:%d", &host, &port) != 2) + * in phy_identifier file + */ + char *s = strstr(de->d_name, ":") + 1; + + if (str_toi(&port, s, NULL, 10) != 0) continue; break; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/smp.h new/ledmon-0.96/src/smp.h --- old/ledmon-0.95/src/smp.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/smp.h 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2011-2021 Intel Corporation. + * Copyright (C) 2011-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/sysfs.c new/ledmon-0.96/src/sysfs.c --- old/ledmon-0.95/src/sysfs.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/sysfs.c 2022-05-31 16:17:48.000000000 +0200 @@ -240,6 +240,10 @@ struct slave_device *device; char *t = strrchr(path, '/'); + + if (!t) + return; + if (strncmp(t + 1, "dev-", 4) == 0) { device = slave_device_init(path, &sysfs_block_list); if (device) { @@ -355,6 +359,10 @@ static void _check_raid(const char *path) { char *t = strrchr(path, '/'); + + if (!t) + return; + if (strncmp(t + 1, "md", 2) == 0) _raid_add(path); } @@ -645,9 +653,8 @@ struct enclosure_device *device; list_for_each(&enclo_list, device) { - if ((device->sysfs_path != NULL) && - (strncmp(device->sysfs_path, path, strlen(path)) == 0)) - return 1; + if (strncmp(device->sysfs_path, path, strnlen(path, PATH_MAX)) == 0) + return 1; } return 0; } diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/utils.c new/ledmon-0.96/src/utils.c --- old/ledmon-0.95/src/utils.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/utils.c 2022-05-31 16:17:48.000000000 +0200 @@ -1,6 +1,6 @@ /* * Intel(R) Enclosure LED Utilities - * Copyright (C) 2009-2021 Intel Corporation. + * Copyright (C) 2009-2022 Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -34,6 +34,7 @@ #include <sys/types.h> #include <time.h> #include <unistd.h> +#include <assert.h> #if _HAVE_DMALLOC_H #include <dmalloc.h> @@ -92,34 +93,27 @@ return defval; } -/* - * Function returns 64-bit unsigned integer value read from a text file. See - * utils.h for details. - */ uint64_t get_uint64(const char *path, uint64_t defval, const char *name) { char *p = get_text(path, name); - uint64_t retval = defval; - if (p) { - if (sscanf(p, "%" SCNx64, &defval) == 1) - retval = defval; - free(p); - } - return retval; + if (!p) + return defval; + + str_toul(&defval, p, NULL, 16); + free(p); + return defval; } -/* - * Function returns integer value read from a text file. - * See utils.h for details. - */ int get_int(const char *path, int defval, const char *name) { char *p = get_text(path, name); - if (p) { - defval = strtol(p, NULL, 10); - free(p); - } + + if (!p) + return defval; + + str_toi(&defval, p, NULL, 10); + free(p); return defval; } @@ -181,11 +175,13 @@ if (path == NULL) __set_errno_and_return(EINVAL); - if ((buf == NULL) || (strlen(buf) == 0)) + + if (buf == NULL || strnlen(buf, WRITE_BUFFER_SIZE) == 0) __set_errno_and_return(ENODATA); + fd = open(path, O_WRONLY); if (fd >= 0) { - size = write(fd, buf, strlen(buf)); + size = write(fd, buf, strnlen(buf, WRITE_BUFFER_SIZE)); close(fd); } return size; @@ -235,8 +231,8 @@ t = strchr(p, ':'); if (t) { *(t++) = '\0'; - did->major = strtol(p, NULL, 10); - did->minor = strtol(t, NULL, 10); + str_toi(&did->major, p, NULL, 10); + str_toi(&did->minor, t, NULL, 10); } free(p); } @@ -359,6 +355,71 @@ return ret; } +static int _str_to_num(const char *strptr, char **endptr, int base, unsigned long *n, int is_signed) +{ + char *c; + int sign_occured = 0; + + assert(n); + if (!strptr) + return 1; + errno = 0; + if (is_signed) + *n = (unsigned long)strtol(strptr, &c, base); + else { + while ((*strptr == '-' || *strptr == '+' || isspace(*strptr)) && + *strptr != '\0' && !sign_occured) { + if (*strptr == '-' || *strptr == '+') + sign_occured = 1; + strptr++; + } + *n = strtoul(strptr, &c, base); + } + if (errno != 0 || strptr == c) + return 1; + if (endptr) + *endptr = c; + return 0; +} + +static int _str_to_num_signed(const char *strptr, char **endptr, int base, unsigned long *n) +{ + return _str_to_num(strptr, endptr, base, n, 1); +} + +static int _str_to_num_unsigned(const char *strptr, char **endptr, int base, unsigned long *n) +{ + return _str_to_num(strptr, endptr, base, n, 0); +} + +static inline int value_in_range_signed(long value, long min, long max) +{ + return (value >= min && value <= max); +} + +static inline int value_in_range_unsigned(unsigned long value, unsigned long min, unsigned long max) +{ + return (value >= min && value <= max); +} + +#define _DECL_STR_TO_NUM(name, sign, type, min, max) \ +int name(sign type * dest, const char *strptr, char **endptr, int base) \ +{ \ + unsigned long n; \ + if ((!dest && !endptr) || _str_to_num_##sign(strptr, endptr, base, &n)) \ + return 1; \ + if (!value_in_range_##sign(n, min, max)) \ + return 1; \ + if (dest)\ + *dest = (sign type)n; \ + return 0; \ +} + +_DECL_STR_TO_NUM(str_tol, signed, long, LONG_MIN, LONG_MAX) +_DECL_STR_TO_NUM(str_toul, unsigned, long, 0, ULONG_MAX) +_DECL_STR_TO_NUM(str_toi, signed, int, INT_MIN, INT_MAX) +_DECL_STR_TO_NUM(str_toui, unsigned, int, 0, UINT_MAX) + char *get_path_hostN(const char *path) { char *c = NULL, *s = NULL, *p = str_dup(path); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/utils.h new/ledmon-0.96/src/utils.h --- old/ledmon-0.95/src/utils.h 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/utils.h 2022-05-31 16:17:48.000000000 +0200 @@ -308,6 +308,71 @@ char *str_dup(const char *src); /** + * @brief Converts string to long integer. + * + * This function works similar to strtol. + * Writes to destination only if successfully read the number. + * Destination can be NULL if endptr is set. + * + * @param[in] dest Pointer to destination. + * @param[in] strptr Pointer to source string. + * @param[in] endptr Pointer to the next character in string after parsed value. + * @param[in] base Base of the numerical value in string. + * + * @return 0 if operation was successful, otherwise 1. + */ +int str_tol(signed long *dest, const char *strptr, char **endptr, int base); + +/** + * @brief Converts string to unsigned long integer. + * + * This function works similar to strtoul. Skips + and - characters. + * Writes to destination only if successfully read the number. + * Destination can be NULL if endptr is set. + * + * @param[in] dest Pointer to destination. + * @param[in] strptr Pointer to source string. + * @param[in] endptr Pointer to the next character in string after parsed value. + * @param[in] base Base of the numerical value in string. + * + * @return 0 if operation was successful, otherwise 1. + */ +int str_toul(unsigned long *dest, const char *strptr, char **endptr, int base); + +/** + * @brief Converts string to integer. + * + * This function works similar to strtol, but for int. + * Writes to destination only if successfully read the number. + * Destination can be NULL if endptr is set. + * + * @param[in] dest Pointer to destination. + * @param[in] strptr Pointer to source string. + * @param[in] endptr Pointer to the next character in string after parsed value. + * @param[in] base Base of the numerical value in string. + * + * @return 0 if operation was successful, otherwise 1. + */ +int str_toi(signed int *dest, const char *strptr, char **endptr, int base); + +/** + * @brief Converts string to unsigned integer. + * + * This function works similar to strtoul, but for unsigned int. + * Skips + and - characters. + * Writes to destination only if successfully read the number. + * Destination can be NULL if endptr is set. + * + * @param[in] dest Pointer to destination. + * @param[in] strptr Pointer to source string. + * @param[in] endptr Pointer to the next character in string after parsed value. + * @param[in] base Base of the numerical value in string. + * + * @return 0 if operation was successful, otherwise 1. + */ +int str_toui(unsigned int *dest, const char *strptr, char **endptr, int base); + +/** */ char *truncate_path_component_rev(const char *path, int index); diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/ledmon-0.95/src/vmdssd.c new/ledmon-0.96/src/vmdssd.c --- old/ledmon-0.95/src/vmdssd.c 2021-01-15 16:51:29.000000000 +0100 +++ new/ledmon-0.96/src/vmdssd.c 2022-05-31 16:17:48.000000000 +0200 @@ -151,7 +151,7 @@ get_ctrl(ibpi, &val); snprintf(buf, WRITE_BUFFER_SIZE, "%u", val); snprintf(attention_path, PATH_MAX, "%s/attention", slot->sysfs_path); - if (buf_write(attention_path, buf) != (ssize_t) strlen(buf)) { + if (buf_write(attention_path, buf) != (ssize_t) strnlen(buf, WRITE_BUFFER_SIZE)) { log_error("%s write error: %d\n", slot->sysfs_path, errno); return -1; }
