Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/devad2/devad2_devctl.cc
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | native \ No newline at end of property |
svn:keywords | null | FreeBSD=%H \ No newline at end of property |
svn:mime-type | null | text/plain \ No newline at end of property |
/*- | |||||
* Copyright (c) 2014 Spectra Logic Corporation | |||||
* 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, | |||||
* without modification. | |||||
* 2. Redistributions in binary form must reproduce at minimum a disclaimer | |||||
* substantially similar to the "NO WARRANTY" disclaimer below | |||||
* ("Disclaimer") and any redistribution must be conditioned upon | |||||
* including a substantially similar Disclaimer requirement for further | |||||
* binary redistribution. | |||||
* | |||||
* NO WARRANTY | |||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR | |||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |||||
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES. | |||||
* | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
#include <sys/param.h> | |||||
#include <sys/queue.h> | |||||
#include <syslog.h> | |||||
#include <stdio.h> | |||||
#include <fcntl.h> | |||||
#include <err.h> | |||||
#include <cam/cam.h> | |||||
#include <cam/cam_debug.h> | |||||
#include <cam/cam_ccb.h> | |||||
#include <camlib.h> | |||||
#include <list> | |||||
#include <map> | |||||
#include <string> | |||||
#include <devdctl/guid.h> | |||||
#include <devdctl/event.h> | |||||
#include <devdctl/event_factory.h> | |||||
#include <devdctl/consumer.h> | |||||
#include "devad2.h" | |||||
#include "devad2_devctl.h" | |||||
/*============================ Namespace Control =============================*/ | |||||
using DevdCtl::EventFactory; | |||||
using std::string; | |||||
/*------------------------------- EventConsumer ------------------------------*/ | |||||
//- EventConsumer Static Private Data ------------------------------------------ | |||||
EventConsumer* EventConsumer::s_theEventConsumer; | |||||
EventFactory::Record EventConsumer::s_registryEntries[] = | |||||
{ | |||||
{ DevdCtl::Event::NOTIFY, "IFNET", &Event::Builder }, | |||||
{ DevdCtl::Event::NOTIFY, "CAM", &Event::Builder }, | |||||
{ DevdCtl::Event::NOTIFY, "DEVFS", &DevfsEvent::Builder } | |||||
}; | |||||
struct devad_softc *EventConsumer::s_softc; | |||||
//- EventConsumer Static Public Methods ---------------------------------------- | |||||
int | |||||
EventConsumer::Init(struct devad_softc *softc) | |||||
{ | |||||
s_theEventConsumer = new EventConsumer(); | |||||
s_softc = softc; | |||||
return (0); | |||||
} | |||||
//- EventConsumer Private Methods ---------------------------------------------- | |||||
EventConsumer::EventConsumer() | |||||
: Consumer(/*default*/&Event::Builder, | |||||
s_registryEntries, | |||||
nitems(s_registryEntries)) | |||||
{ | |||||
if (s_theEventConsumer != NULL) | |||||
errx(1, "Multiple HpdEventConsumer instances created. Exiting"); | |||||
s_theEventConsumer = this; | |||||
} | |||||
EventConsumer::~EventConsumer() | |||||
{ | |||||
} | |||||
/*---------------------------------- Event -----------------------------------*/ | |||||
//- Event Static Public Methods ------------------------------------------------ | |||||
DevdCtl::Event * | |||||
Event::Builder(DevdCtl::Event::Type type, DevdCtl::NVPairMap &nvpairs, | |||||
const string &eventString) | |||||
{ | |||||
return (new Event(type, nvpairs, eventString)); | |||||
} | |||||
//- Event Virtual Public Methods ----------------------------------------------- | |||||
bool | |||||
Event::Process() const | |||||
{ | |||||
return (DevdCtl::Event::Process()); | |||||
} | |||||
//- Event Protected Methods ---------------------------------------------------- | |||||
Event::Event(Event::Type type, DevdCtl::NVPairMap &nvpairs, | |||||
const string &eventString) | |||||
: DevdCtl::Event(type, nvpairs, eventString) | |||||
{ | |||||
} | |||||
/*---------------------------------- DevfsEvent ---------------------------*/ | |||||
//- DevfsEvent Static Private Data ------------------------------------------ | |||||
struct devad_softc *DevfsEvent::s_softc; | |||||
DevdCtl::Event * | |||||
DevfsEvent::Builder(DevdCtl::Event::Type type, DevdCtl::NVPairMap &nvpairs, | |||||
const string &eventString) | |||||
{ | |||||
return (new DevfsEvent(type, nvpairs, eventString, | |||||
EventConsumer::GetSoftc())); | |||||
} | |||||
//- DevfsEvent Protected Methods -------------------------------------------- | |||||
DevfsEvent::DevfsEvent(Event::Type type, DevdCtl::NVPairMap &nvpairs, | |||||
const string &eventString, struct devad_softc *softc) | |||||
: DevdCtl::DevfsEvent(type, nvpairs, eventString) | |||||
{ | |||||
s_softc = softc; | |||||
} | |||||
//- DevfsEvent Virtual Public Methods --------------------------------------- | |||||
bool | |||||
DevfsEvent::Process() const | |||||
{ | |||||
std::string device_name; | |||||
char tmpstr[32]; | |||||
int unit_number, is_add = 0; | |||||
int retval; | |||||
if (!IsDiskDev()) | |||||
return (false); | |||||
if (!IsWholeDev()) | |||||
return (false); | |||||
device_name = Value("cdev"); | |||||
if (device_name.empty()) | |||||
return (false); | |||||
retval = cam_get_device(device_name.c_str(), tmpstr, | |||||
sizeof(tmpstr), &unit_number); | |||||
if (retval != 0) | |||||
return (false); | |||||
if (strcmp(tmpstr, "da") != 0) | |||||
return (false); | |||||
if (Value("type") == "CREATE") { | |||||
is_add = 1; | |||||
} else if (Value("type") == "DESTROY") { | |||||
is_add = 0; | |||||
} else { | |||||
return (false); | |||||
} | |||||
fprintf(stdout, "%s %s%u\n", (is_add) ? "Adding" : "Removing", | |||||
tmpstr, unit_number); | |||||
if (is_add) | |||||
retval = devad_disk_probe_device(s_softc, tmpstr, unit_number); | |||||
else | |||||
retval = devad_disk_remove_device(s_softc, tmpstr, unit_number); | |||||
if (retval == 0) | |||||
return (true); | |||||
else | |||||
return (false); | |||||
} |