From owner-svn-src-head@freebsd.org Sat Sep 5 00:06:03 2015 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8C4009CB909; Sat, 5 Sep 2015 00:06:03 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7ACC6A60; Sat, 5 Sep 2015 00:06:03 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id t85063NG031957; Sat, 5 Sep 2015 00:06:03 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id t85062c3031950; Sat, 5 Sep 2015 00:06:02 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201509050006.t85062c3031950@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Sat, 5 Sep 2015 00:06:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r287473 - in head/usr.sbin: . sesutil X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Sep 2015 00:06:03 -0000 Author: bapt Date: Sat Sep 5 00:06:01 2015 New Revision: 287473 URL: https://svnweb.freebsd.org/changeset/base/287473 Log: Add a new sesutil(8) utility This is an utility for managing SCSI Enclosure Services (SES) device. For now only one command is supported "locate" which will change the test of the external LED associated to a given disk. Usage if the following: sesutil locate disk [on|off] Disk can be a device name: "da12" or a special keyword: "all". Reviewed by: mav MFC after: 1 month Relnotes: yes Sponsored by: gandi.net Differential Revision: https://reviews.freebsd.org/D3544 Added: head/usr.sbin/sesutil/ head/usr.sbin/sesutil/Makefile (contents, props changed) head/usr.sbin/sesutil/sesutil.8 (contents, props changed) head/usr.sbin/sesutil/sesutil.c (contents, props changed) Modified: head/usr.sbin/Makefile Modified: head/usr.sbin/Makefile ============================================================================== --- head/usr.sbin/Makefile Fri Sep 4 20:15:20 2015 (r287472) +++ head/usr.sbin/Makefile Sat Sep 5 00:06:01 2015 (r287473) @@ -74,6 +74,7 @@ SUBDIR= adduser \ rtprio \ service \ services_mkdb \ + sesutil \ setfib \ setfmac \ setpmac \ Added: head/usr.sbin/sesutil/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/sesutil/Makefile Sat Sep 5 00:06:01 2015 (r287473) @@ -0,0 +1,6 @@ +# $FreeBSD$ + +PROG= sesutil +MAN= sesutil.8 + +.include Added: head/usr.sbin/sesutil/sesutil.8 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/sesutil/sesutil.8 Sat Sep 5 00:06:01 2015 (r287473) @@ -0,0 +1,73 @@ +.\" Copyright (c) 2015 Baptiste Daroussin +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd September 1, 2015 +.Dt SESUTIL 8 +.Os +.Sh NAME +.Nm sesutil +.Nd Utility for managing SCSI Enclosure Services (SES) device +.Sh SYNOPSIS +.Nm +.Cm locate Ar disk Bq on|off +.Sh DESCRIPTION +The +.Nm +utility can be used to modify various parameter on SCSI Enclosure Services +(SES) device. +.Pp +List of supported commands: +.Bl -tag -width indent +.It Cm locate Ar disk Bq on|off +Change the state of the external LED associated with +.Ar disk . +.Ar disk +can be the device name of the disk, like +.Cm da12 , +or +.Cm all . +to indicate all disks attached to SES controllers. +.El +.Sh EXAMPLES +Turn off all external LEDs: +.Pp +.Dl Nm Cm locate all off +.Pp +Turn on the external LED of drive +.Pa da15 : +.Pp +.Dl Nm Cm locate da15 on +.Sh SEE ALSO +.Xr ses 4 +.Sh HISTORY +The +.Nm +utility first appeared in +.Fx 11.0 . +.Sh AUTHORS +The +.Nm utility was written by +.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org . Added: head/usr.sbin/sesutil/sesutil.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/sesutil/sesutil.c Sat Sep 5 00:06:01 2015 (r287473) @@ -0,0 +1,225 @@ +/*- + * Copyright (c) 2015 Baptiste Daroussin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static int locate(int argc, char **argv); + +static struct command { + const char *name; + const char *desc; + int (*exec)(int argc, char **argv); +} cmds[] = { + { "locate", "Change the state of the external LED associated with a" + " disk", locate} , +}; + +static const int nbcmds = nitems(cmds); + +static void +do_locate(int fd, unsigned int idx, bool onoff) +{ + encioc_elm_status_t o; + + o.elm_idx = idx; + if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) { + close(fd); + err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); + } + o.cstat[0] |= 0x80; + if (onoff) + o.cstat[2] |= 0x02; + else + o.cstat[2] &= 0xfd; + + if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) { + close(fd); + err(EXIT_FAILURE, "ENCIOC_SETELMSTAT"); + } +} + +static bool +disk_match(const char *devnames, const char *disk, size_t len) +{ + const char *devname; + + devname = devnames; + while ((devname = strstr(devname, disk)) != NULL) { + if (devname[len] == '\0' || devname[len] == ',') + return (true); + devname++; + } + return (false); +} + +static int +locate(int argc, char **argv) +{ + encioc_elm_devnames_t objdn; + encioc_element_t *objp; + glob_t g; + char *disk; + size_t len, i; + int fd, nobj, j; + bool all = false; + bool locate; + + if (argc != 2) { + errx(EXIT_FAILURE, "usage: %s locate [disk] [on|off]", + getprogname()); + } + + disk = argv[0]; + + if (strcmp(argv[1], "on") == 0) { + locate = true; + } else if (strcmp(argv[1], "off") == 0) { + locate = false; + } else { + errx(EXIT_FAILURE, "usage: %s locate [disk] [on|off]", + getprogname()); + } + + if (strcmp(disk, "all") == 0) { + all = true; + } + len = strlen(disk); + + /* Get the list of ses devices */ + if (glob("/dev/ses[0-9]*", 0, NULL, &g) == GLOB_NOMATCH) { + globfree(&g); + errx(EXIT_FAILURE, "No SES devices found"); + } + for (i = 0; i < g.gl_pathc; i++) { + /* ensure we only got numbers after ses */ + if (strspn(g.gl_pathv[i] + 8, "0123456789") != + strlen(g.gl_pathv[i] + 8)) + continue; + if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { + if (errno == EACCES) + err(EXIT_FAILURE, "enable to access SES device"); + break; + } + + if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) + err(EXIT_FAILURE, "ENCIOC_GETNELM"); + + objp = calloc(nobj, sizeof(encioc_element_t)); + if (objp == NULL) + err(EXIT_FAILURE, "calloc()"); + + if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) + err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); + + for (j = 0; j < nobj; j++) { + memset(&objdn, 0, sizeof(objdn)); + objdn.elm_idx = objp[j].elm_idx; + objdn.elm_names_size = 128; + objdn.elm_devnames = calloc(128, sizeof(char)); + if (objdn.elm_devnames == NULL) + err(EXIT_FAILURE, "calloc()"); + if (ioctl(fd, ENCIOC_GETELMDEVNAMES, + (caddr_t) &objdn) <0) + continue; + if (objdn.elm_names_len > 0) { + if (all) { + do_locate(fd, objdn.elm_idx, locate); + continue; + } + if (disk_match(objdn.elm_devnames, disk, len)) { + do_locate(fd, objdn.elm_idx, locate); + break; + } + } + } + close(fd); + i++; + } + globfree(&g); + + return (EXIT_SUCCESS); +} + +static void +usage(FILE *out) +{ + int i; + + fprintf(out, "Usage: %s [command] [options]\n", getprogname()); + fprintf(out, "Commands supported:\n"); + for (i = 0; i < nbcmds; i++) + fprintf(out, "\t%-15s%s\n", cmds[i].name, cmds[i].desc); +} + +int +main(int argc, char **argv) +{ + int i; + struct command *cmd = NULL; + + if (argc < 2) { + warnx("Missing command"); + usage(stderr); + return (EXIT_FAILURE); + } + + for (i = 0; i < nbcmds; i++) { + if (strcmp(argv[1], cmds[i].name) == 0) { + cmd = &cmds[i]; + break; + } + } + + if (cmd == NULL) { + warnx("unknown command %s", argv[1]); + usage(stderr); + return (EXIT_FAILURE); + } + + argc-=2; + argv+=2; + + return (cmd->exec(argc, argv)); +}