Import of pkgsrc-2013Q2

This commit is contained in:
2013-09-26 17:14:40 +02:00
commit 785076ae39
74991 changed files with 4380255 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
#!@RCD_SCRIPTS_SHELL@
#
# $NetBSD: hal.sh,v 1.2 2008/11/22 16:16:26 jmcneill Exp $
#
# PROVIDE: hal
# REQUIRE: DAEMON dbus
# KEYWORD: shutdown
. /etc/rc.subr
name="hal"
rcvar=$name
command="@PREFIX@/sbin/hald"
start_precmd=hal_prestart
hal_prestart() {
dir="@VARBASE@/run/hald"
if @TEST@ ! -d $dir; then
@MKDIR@ $dir
@CHMOD@ 0755 $dir
@CHOWN@ @HAL_USER@:@HAL_GROUP@ $dir
fi
@CHMOD@ 0755 "@VARBASE@/cache"
}
load_rc_config $name
run_rc_command "$1"
@@ -0,0 +1,27 @@
SUBDIRS = probing addons .
AM_CPPFLAGS = \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-DPACKAGE_LOCALSTATEDIR=\""$(localstatedir)"\" \
-I$(top_srcdir) -I.. \
@GLIB_CFLAGS@ @DBUS_CFLAGS@ @POLKIT_CFLAGS@ @VOLUME_ID_CFLAGS@
if HALD_COMPILE_NETBSD
noinst_LTLIBRARIES = libhald_netbsd.la
endif
libhald_netbsd_la_SOURCES = \
osspec.c drvctl.c envsys.c vfsstat.c \
devinfo.c devinfo_misc.c devinfo_audio.c devinfo_video.c \
devinfo_mass.c \
hotplug.c hal-file-monitor.c \
devinfo_optical.c
# devinfo_scsipi.c
# devinfo_usb.c \
# devinfo_pci.c
libhald_netbsd_la_LDFLAGS = -lprop @VOLUME_ID_LIBS@
@@ -0,0 +1,17 @@
AM_CPPFLAGS = \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-DPACKAGE_LOCALSTATEDIR=\""$(localstatedir)"\" \
-I$(top_srcdir) -I$(top_srcdir)/hald -I$(top_srcdir)/libhal -I$(top_srcdir)/libhal-storage \
@GLIB_CFLAGS@ @DBUS_CFLAGS@
if HALD_COMPILE_NETBSD
libexec_PROGRAMS = hald-addon-storage
endif
hald_addon_storage_SOURCES = addon-storage.c ../../logger.c
hald_addon_storage_LDADD = $(top_builddir)/libhal/libhal.la
@@ -0,0 +1,516 @@
/***************************************************************************
*
* addon-storage.c : watch removable media state changes
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/dkio.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipi_cd.h>
#include <sys/scsiio.h>
#include <libhal.h>
#include <logger.h>
#define SLEEP_PERIOD 5
#define _EXPOSE_MMC
#include <sys/cdio.h>
enum discstate {
DISC_INSERTED,
DISC_EJECTED,
DISC_NONE,
DISC_UNKNOWN
};
static enum discstate
scsi_test_unit_ready (int fd)
{
struct scsi_test_unit_ready tur;
scsireq_t req;
memset(&tur, 0, sizeof(tur));
tur.opcode = SCSI_TEST_UNIT_READY;
memset(&req, 0, sizeof(req));
memcpy(req.cmd, &tur, sizeof(tur));
req.cmdlen = sizeof(tur);
req.databuf = NULL;
req.datalen = 0;
req.timeout = 10000;
req.flags = SCCMD_READ;
req.senselen = SENSEBUFLEN;
if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
return DISC_UNKNOWN;
if (req.retsts == SCCMD_OK)
return DISC_INSERTED;
return DISC_EJECTED;
}
static void
my_dbus_error_free(DBusError *error)
{
if (dbus_error_is_set(error)) {
dbus_error_free(error);
}
}
static void
force_unmount (LibHalContext *ctx, const char *udi)
{
DBusError error;
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
char **options = NULL;
unsigned int num_options = 0;
DBusConnection *dbus_connection;
char *device_file;
dbus_error_init (&error);
dbus_connection = libhal_ctx_get_dbus_connection (ctx);
msg = dbus_message_new_method_call ("org.freedesktop.Hal", udi,
"org.freedesktop.Hal.Device.Volume",
"Unmount");
if (msg == NULL) {
HAL_DEBUG (("Could not create dbus message for %s", udi));
goto out;
}
options = calloc (1, sizeof (char *));
if (options == NULL) {
HAL_DEBUG (("Could not allocate options array"));
goto out;
}
device_file = libhal_device_get_property_string (ctx, udi, "block.device", &error);
if (device_file != NULL) {
libhal_free_string (device_file);
}
dbus_error_free (&error);
if (!dbus_message_append_args (msg,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &options, num_options,
DBUS_TYPE_INVALID)) {
HAL_DEBUG (("Could not append args to dbus message for %s", udi));
goto out;
}
if (!(reply = dbus_connection_send_with_reply_and_block (dbus_connection, msg, -1, &error))) {
HAL_DEBUG (("Unmount failed for %s: %s : %s\n", udi, error.name, error.message));
goto out;
}
if (dbus_error_is_set (&error)) {
HAL_DEBUG (("Unmount failed for %s\n%s : %s\n", udi, error.name, error.message));
goto out;
}
HAL_DEBUG (("Succesfully unmounted udi '%s'", udi));
out:
dbus_error_free (&error);
if (options != NULL)
free (options);
if (msg != NULL)
dbus_message_unref (msg);
if (reply != NULL)
dbus_message_unref (reply);
}
static void
unmount_childs (LibHalContext *ctx, const char *udi)
{
DBusError error;
int num_volumes;
char **volumes;
dbus_error_init (&error);
/* need to force unmount all partitions */
if ((volumes = libhal_manager_find_device_string_match (
ctx, "block.storage_device", udi, &num_volumes, &error)) != NULL) {
dbus_error_free (&error);
int i;
for (i = 0; i < num_volumes; i++) {
char *vol_udi;
vol_udi = volumes[i];
if (libhal_device_get_property_bool (ctx, vol_udi, "block.is_volume", &error)) {
dbus_error_free (&error);
if (libhal_device_get_property_bool (ctx, vol_udi, "volume.is_mounted", &error)) {
dbus_error_free (&error);
HAL_DEBUG (("Forcing unmount of child '%s'", vol_udi));
force_unmount (ctx, vol_udi);
}
}
}
libhal_free_string_array (volumes);
}
my_dbus_error_free (&error);
}
/** Check if a filesystem on a special device file is mounted
*
* @param device_file Special device file, e.g. /dev/cdrom
* @return TRUE iff there is a filesystem system mounted
* on the special device file
*/
static dbus_bool_t
is_mounted (const char *device_file)
{
int count;
struct statvfs *statvfs;
count = getmntinfo(&statvfs, ST_WAIT);
while (count-- > 0) {
if (strcmp(statvfs->f_mntfromname, device_file) == 0)
return TRUE;
}
return FALSE;
}
static void
close_device (int *fd)
{
if (*fd > 0) {
close (*fd);
*fd = -1;
}
}
/* XXX why not share this further */
static char const *
get_profile_typestring(int profile)
{
switch (profile) {
case 0x00 : return "unknown"; // "Unknown[0] profile";
case 0x01 : return "unknown"; // "Non removeable disc";
case 0x02 : return "unknown"; // "Removable disc";
case 0x03 : return "mo"; // Magneto Optical with sector erase";
case 0x04 : return "mo"; // "Magneto Optical write once";
case 0x05 : return "mo"; // "Advance Storage Magneto Optical";
case 0x08 : return "cd_rom"; // "CD-ROM"; no writing
case 0x09 : return "cd_r"; // "CD-R recordable";
case 0x0a : return "cd_rw"; // "CD-RW rewritable";
case 0x10 : return "dvd_rom"; // "DVD-ROM"; no writing
case 0x11 : return "dvd_r"; // "DVD-R sequential";
case 0x12 : return "dvd_ram"; // "DVD-RAM rewritable";
case 0x13 : return "dvd_rw"; // "DVD-RW restricted overwrite";
case 0x14 : return "dvd_rw"; // "DVD-RW sequential";
case 0x15 : return "dvd_r"; // "DVD-R dual layer sequential";
case 0x16 : return "dvd_r"; // "DVD-R dual layer jump";
case 0x17 : return "dvd_rw"; // "DVD-RW dual layer";
case 0x18 : return "unknown"; // "DVD-Download disc"; UNKNOWN
case 0x1a : return "dvd_plus_rw"; // "DVD+RW rewritable";
case 0x1b : return "dvd_plus_r"; // "DVD+R recordable";
case 0x20 : return "ddcd_rom"; // "DDCD readonly (retracted)"; no writing
case 0x21 : return "ddcd_r"; // "DDCD-R recordable (retracted)"; OK?
case 0x22 : return "ddcd_rw"; // "DDCD-RW rewritable (retracted)"; OK?
case 0x2a : return "dvd_plus_rw_dl";//"DVD+RW double layer";
case 0x2b : return "dvd_plus_r_dl";// "DVD+R double layer";
case 0x40 : return "bd_rom"; // "BD-ROM";
case 0x41 : return "bd_r"; // "BD-R Sequential Recording (SRM)";
case 0x42 : return "bd_r"; // "BD-R Random Recording (RRM)";
case 0x43 : return "bd_re"; // "BD-RE rewritable";
case 0x50 : return "hddvd_rom"; // "HD DVD-ROM (retracted)";
case 0x51 : return "hddvd_r"; // "HD DVD-R (retracted)";
case 0x52 : return "hddvd_ram"; // "HD DVD-RAM (retracted)";
case 0x53 : return "hddvd_rw"; // "HD DVD-RW (retracted)";
case 0x58 : return "hddvd_r_dl"; // "HD DVD-R dual layer (retracted)";
case 0x5a : return "hddvd_rw_dl"; // "HD DVD-RW dual layer (retracted)";
}
/* reserved */
return "unknown";
}
static void
set_volume_properties(int fd, LibHalContext *ctx, char *udi, int state)
{
struct mmc_discinfo di;
struct mmc_trackinfo ti;
struct volume_id *vid;
DBusError error;
uint64_t capacity;
char const *disc_type;
char *disc_fstype, *disc_label;
int has_audio, has_data, is_vcd, is_svcd, is_videodvd, is_appendable, is_blank, is_rewritable;
int tracknr, err;
disc_fstype = "";
disc_label = "";
disc_type = "unknown";
has_audio = has_data = is_vcd = is_svcd = is_videodvd = is_appendable = is_blank = is_rewritable = 0;
capacity = 0;
dbus_error_init (&error);
if (state == DISC_INSERTED) {
/* fetch new values */
memset(&di, 0, sizeof(struct mmc_discinfo));
err = ioctl(fd, MMCGETDISCINFO, &di);
if (!err) {
disc_type = get_profile_typestring(di.mmc_profile);
is_rewritable = di.mmc_cur & MMC_CAP_REWRITABLE;
is_blank = (di.disc_state == MMC_STATE_EMPTY);
is_appendable = (di.disc_state != MMC_STATE_FULL);
/* can't check is_videodvd, is_svcd, is_vcd (yet); use volume lib */
disc_fstype = "cd9660";
disc_label = "label";
vid = NULL;
#if 0
vid = volume_id_open_fd (fd);
if (vid) {
if (volume_id_probe_all (vid, 0, psize) == 0) {
hal_device_property_set_string (d, "volume.label", vid->label);
hal_device_property_set_string (d, "volume.partition.label", vid->label);
hal_device_property_set_string (d, "volume.uuid", vid->uuid);
hal_device_property_set_string (d, "volume.partition.uuid", vid->uuid);
}
volume_id_close (vid);
}
#endif
for (tracknr = di.first_track; tracknr <= di.last_track_last_session; tracknr++) {
memset(&ti, 0, sizeof(struct mmc_trackinfo));
ti.tracknr = tracknr;
err = ioctl(fd, MMCGETTRACKINFO, &ti);
if (err)
break;
if (!(ti.flags & MMC_TRACKINFO_BLANK)) {
if (ti.flags & MMC_TRACKINFO_DATA)
has_data = TRUE;
if (ti.flags & MMC_TRACKINFO_AUDIO)
has_audio = TRUE;
}
capacity += (ti.track_size + ti.free_blocks) * di.sector_size;
}
}
}
/* add volume properties (ignoring dbus errors) */
libhal_device_set_property_bool (ctx, udi, "volume.ignore", FALSE, &error); /* make visible */
libhal_device_set_property_bool (ctx, udi, "volume.is_mounted", FALSE, &error); /* XXX fixme XXX */
libhal_device_set_property_bool (ctx, udi, "volume.is_mounted_readonly", FALSE, &error); /* XXX fixme XXX */
libhal_device_set_property_string (ctx, udi, "volume.fsusage", "filesystem", &error);
libhal_device_set_property_string (ctx, udi, "volume.fstype", disc_fstype, &error);
libhal_device_set_property_string (ctx, udi, "volume.label", disc_label, &error);
libhal_device_set_property_string (ctx, udi, "volume.uuid", "", &error);
libhal_device_set_property_uint64 (ctx, udi, "volume.size", capacity, &error);
/* add volume.disc properties (ignoring dbus errors) */
libhal_device_set_property_bool (ctx, udi, "volume.disc.has_audio", has_audio, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.has_data", has_data, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.is_vcd", is_vcd, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.is_svcd", is_svcd, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.is_videodvd", is_videodvd, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.is_appendable", is_appendable, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.is_blank", is_blank, &error);
libhal_device_set_property_bool (ctx, udi, "volume.disc.is_rewritable", is_rewritable, &error);
libhal_device_set_property_string (ctx, udi, "volume.disc.type", disc_type, &error);
libhal_device_set_property_uint64 (ctx, udi, "volume.disc.capacity", capacity, &error);
my_dbus_error_free (&error);
}
static void
update_disc_volume_properties(int fd, LibHalContext *ctx, const char *udi, int state)
{
DBusError error;
char **volumes;
char *vol_udi;
int num_volumes, i;
dbus_error_init (&error);
/* update volume children */
if ((volumes = libhal_manager_find_device_string_match (
ctx, "info.parent", udi, &num_volumes, &error)) != NULL) {
dbus_error_free (&error);
for (i = 0; i < num_volumes; i++) {
vol_udi = volumes[i];
if (libhal_device_get_property_bool (ctx, vol_udi, "block.is_volume", &error)) {
dbus_error_free (&error);
HAL_DEBUG(("Updating child %s of %s\n", udi, vol_udi));
set_volume_properties(fd, ctx, vol_udi, state);
#if 0
if (libhal_device_get_property_bool (ctx, vol_udi, "volume.is_mounted", &error)) {
dbus_error_free (&error);
HAL_DEBUG (("Forcing unmount of child '%s'", vol_udi));
force_unmount (ctx, vol_udi);
}
#endif
}
}
libhal_free_string_array (volumes);
}
my_dbus_error_free (&error);
}
int
main (int argc, char *argv[])
{
char *udi;
char *device_file, *raw_device_file;
LibHalContext *ctx = NULL;
DBusError error;
char *bus;
char *drive_type;
int state, last_state;
char *support_media_changed_str;
int support_media_changed;
int fd = -1;
if ((udi = getenv ("UDI")) == NULL)
goto out;
if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL)
goto out;
if ((raw_device_file = getenv ("HAL_PROP_BLOCK_NETBSD_RAW_DEVICE")) == NULL)
goto out;
if ((bus = getenv ("HAL_PROP_STORAGE_BUS")) == NULL)
goto out;
if ((drive_type = getenv ("HAL_PROP_STORAGE_DRIVE_TYPE")) == NULL)
goto out;
setup_logger ();
support_media_changed_str = getenv ("HAL_PROP_STORAGE_CDROM_SUPPORT_MEDIA_CHANGED");
if (support_media_changed_str != NULL && strcmp (support_media_changed_str, "true") == 0)
support_media_changed = TRUE;
else
support_media_changed = FALSE;
dbus_error_init (&error);
if ((ctx = libhal_ctx_init_direct (&error)) == NULL) {
goto out;
}
my_dbus_error_free (&error);
if (!libhal_device_addon_is_ready (ctx, udi, &error)) {
goto out;
}
my_dbus_error_free (&error);
printf ("Doing addon-storage for %s (bus %s) (drive_type %s) (udi %s)\n", device_file, bus, drive_type, udi);
last_state = state = DISC_NONE;
/* Linux version of this addon attempts to re-open the device O_EXCL
* every 2 seconds, trying to figure out if some other app,
* like a cd burner, is using the device. Aside from questionable
* value of this (apps should use HAL's locked property or/and
* Solaris in_use facility), but also frequent opens/closes
* keeps media constantly spun up. All this needs more thought.
*/
for (;;) {
sleep (SLEEP_PERIOD);
if (is_mounted (device_file)) {
close_device (&fd);
} else if ((fd < 0) && ((fd = open (raw_device_file, O_RDONLY | O_NONBLOCK)) < 0)) {
HAL_DEBUG (("open failed for %s: %s", raw_device_file, strerror (errno)));
} else {
/* Check if a disc is in the drive */
state = scsi_test_unit_ready (fd);
if (state == last_state) {
HAL_DEBUG (("state has not changed %d %s", state, device_file));
/* TODO check if eject button was pressed */
/* see linux addons/addon-storage.c */
continue;
} else {
HAL_DEBUG (("new state %d %s", state, device_file));
}
switch (state) {
case DISC_EJECTED:
HAL_DEBUG (("Media removal detected on %s", device_file));
last_state = state;
libhal_device_set_property_bool (ctx, udi, "storage.removable.media_available", FALSE, &error);
my_dbus_error_free (&error);
/* attempt to unmount all childs */
unmount_childs (ctx, udi);
update_disc_volume_properties(fd, ctx, udi, state);
/* could have a fs on the main block device; do a rescan to remove it */
libhal_device_rescan (ctx, udi, &error);
my_dbus_error_free (&error);
break;
case DISC_INSERTED:
HAL_DEBUG (("Media insertion detected on %s", device_file));
last_state = state;
libhal_device_set_property_bool (ctx, udi, "storage.removable.media_available", TRUE, &error);
my_dbus_error_free (&error);
update_disc_volume_properties(fd, ctx, udi, state);
/* could have a fs on the main block device; do a rescan to add it */
libhal_device_rescan (ctx, udi, &error);
my_dbus_error_free (&error);
break;
case DISC_UNKNOWN:
default:
HAL_DEBUG (("Device gone detected on %s", device_file));
last_state = state;
unmount_childs (ctx, udi);
close_device (&fd);
goto out;
}
}
}
out:
if (ctx != NULL) {
my_dbus_error_free (&error);
libhal_ctx_shutdown (ctx, &error);
libhal_ctx_free (ctx);
}
return 0;
}
+418
View File
@@ -0,0 +1,418 @@
/***************************************************************************
*
* devinfo.c : main file for drvctl-based device enumeration
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <sys/drvctlio.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../hald_runner.h"
#include "osspec_netbsd.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_audio.h"
#include "devinfo_video.h"
#include "devinfo_pci.h"
#include "devinfo_storage.h"
#include "devinfo_mass.h"
#include "devinfo_usb.h"
#include "devinfo_misc.h"
#include "devinfo_cpu.h"
#include "devinfo_optical.h"
/* #include "devinfo_scsipi.h" */
#include "drvctl.h"
void devinfo_add_subtree(HalDevice *parent, const char *devnode, gboolean is_root);
void
devinfo_add(HalDevice *parent, gchar *name)
{
devinfo_add_subtree (parent, name, TRUE);
}
void
devinfo_add_subtree(HalDevice *parent, const char *devnode, gboolean is_root)
{
HalDevice *d;
struct devlistargs laa;
int i;
HAL_INFO (("add_subtree: %s", devnode));
if (parent == NULL) {
parent = devinfo_add_node (NULL, devnode);
}
if (drvctl_list (devnode, &laa) == -1) {
HAL_INFO (("devinfo_add_subtree: drvctl_list failed"));
return;
}
for (i = 0; i < laa.l_children; i++) {
d = devinfo_add_node (parent, laa.l_childname[i]);
if (d)
devinfo_add_subtree (d, laa.l_childname[i], FALSE);
}
if (laa.l_childname)
free(laa.l_childname);
}
void
devinfo_set_default_properties (HalDevice *d, HalDevice *parent, const char *devnode, char *devfs_path)
{
char *driver_name, *s;
const char *s1;
char udi[HAL_PATH_MAX];
if (parent != NULL) {
char *pdevice = hal_device_property_get_string (parent, "netbsd.device");
if (pdevice) {
gchar *path;
if (strcmp (pdevice, "mainbus0") == 0)
pdevice = "computer";
path = g_strdup_printf ("/org/freedesktop/Hal/devices/%s", pdevice);
hal_device_property_set_string (d, "info.parent", path);
g_free (path);
}
} else {
gchar pdevnode[512];
char *pdevice = pdevnode;
if (drvctl_find_parent (devnode, pdevnode) == TRUE) {
gchar *path;
if (strcmp (pdevnode, "mainbus0") == 0)
pdevice = "computer";
path = g_strdup_printf ("/org/freedesktop/Hal/devices/%s", pdevice);
hal_device_property_set_string (d, "info.parent", path);
g_free (path);
} else
hal_device_property_set_string (d, "info.parent", "/org/freedesktop/Hal/devices/local");
}
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/%s", devnode);
hal_device_set_udi (d, udi);
#if notyet
if (di_prop_lookup_strings (DDI_DEV_T_ANY, node, "model", &s) > 0) {
hal_device_property_set_string (d, "info.product", s);
} else {
hal_device_property_set_string (d, "info.product", di_node_name (node));
}
#else
hal_device_property_set_string (d, "info.product", devnode);
#endif
hal_device_property_set_string (d, "netbsd.device", devnode);
#if notyet
if ((driver_name = di_driver_name (node)) != NULL) {
hal_device_property_set_string (d, "info.solaris.driver",
driver_name);
}
/* inherit parent's claim attributes */
if (hal_device_property_get_bool (parent, "info.claimed")) {
s1 = hal_device_property_get_string (parent, "info.claimed.service");
if (s1 != NULL) {
hal_device_property_set_bool (d, "info.claimed", TRUE);
hal_device_property_set_string (d, "info.claimed.service", s1);
}
}
#endif
}
/* device handlers, ordered specific to generic */
static DevinfoDevHandler *devinfo_handlers[] = {
&devinfo_computer_handler,
&devinfo_cpu_handler,
&devinfo_optical_handler,
#if notyet
&devinfo_scsipi_handler,
&devinfo_ide_handler,
&devinfo_scsi_handler,
&devinfo_floppy_handler,
&devinfo_usb_handler,
&devinfo_pci_handler,
&devinfo_lofi_handler,
#endif
&devinfo_audio_handler,
&devinfo_audio_mixer_handler,
&devinfo_audio_dsp_handler,
&devinfo_video_handler,
&devinfo_mass_handler,
&devinfo_default_handler,
NULL
};
HalDevice *
devinfo_add_node(HalDevice *parent, const char *devnode)
{
HalDevice *d = NULL;
char *devfs_path;
char *device_type = NULL;
DevinfoDevHandler *handler;
int i;
devfs_path = g_strdup_printf ("%s", devnode);
#if notyet
(void) di_prop_lookup_strings (DDI_DEV_T_ANY, node, "device_type",
&device_type);
#else
device_type = "unknown";
#endif
for (i = 0; (d == NULL) && (devinfo_handlers[i] != NULL); i++) {
handler = devinfo_handlers[i];
d = handler->add (parent, devnode, devfs_path, device_type);
}
g_free(devfs_path);
HAL_INFO (("add_node: %s", d ? hal_device_get_udi (d) : "none"));
return (d);
}
void
devinfo_hotplug_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler, int action, int front)
{
HotplugEvent *hotplug_event;
hotplug_event = g_new0 (HotplugEvent, 1);
hotplug_event->action = action;
hotplug_event->type = HOTPLUG_EVENT_DEVFS;
hotplug_event->d = d;
strlcpy (hotplug_event->un.devfs.devfs_path, devfs_path,
sizeof (hotplug_event->un.devfs.devfs_path));
hotplug_event->un.devfs.handler = handler;
hotplug_event_enqueue (hotplug_event, front);
}
void
devinfo_add_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler)
{
devinfo_hotplug_enqueue (d, devfs_path, handler, HOTPLUG_ACTION_ADD, 0);
}
void
devinfo_add_enqueue_at_front(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler)
{
devinfo_hotplug_enqueue (d, devfs_path, handler, HOTPLUG_ACTION_ADD, 1);
}
void
devinfo_remove_enqueue(gchar *devfs_path, DevinfoDevHandler *handler)
{
devinfo_hotplug_enqueue (NULL, devfs_path, handler, HOTPLUG_ACTION_REMOVE, 0);
}
void
devinfo_callouts_add_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
/* Move from temporary to global device store */
hal_device_store_remove (hald_get_tdl (), d);
hal_device_store_add (hald_get_gdl (), d);
hotplug_event_end (end_token);
}
void
devinfo_callouts_probing_done (HalDevice *d, guint32 exit_type, gint return_code, char **error, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
/* Discard device if probing reports failure */
if (exit_type != HALD_RUN_SUCCESS || (return_code != 0)) {
HAL_INFO (("Probing for %s failed %d", hal_device_get_udi (d), return_code));
hal_device_store_remove (hald_get_tdl (), d);
g_object_unref (d);
hotplug_event_end (end_token);
return;
}
/* Merge properties from .fdi files */
di_search_and_merge (d, DEVICE_INFO_TYPE_INFORMATION);
di_search_and_merge (d, DEVICE_INFO_TYPE_POLICY);
hal_util_callout_device_add (d, devinfo_callouts_add_done, end_token, NULL);
}
void
devinfo_callouts_preprobing_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
DevinfoDevHandler *handler = (DevinfoDevHandler *) userdata2;
void (*probing_done) (HalDevice *, guint32, gint, char **, gpointer, gpointer);
const gchar *prober;
int prober_timeout;
if (hal_device_property_get_bool (d, "info.ignore")) {
HAL_INFO (("Preprobing merged info.ignore==TRUE"));
/* Leave device with info.ignore==TRUE so we won't pick up children */
hal_device_property_remove (d, "info.category");
hal_device_property_remove (d, "info.capabilities");
hal_device_store_remove (hald_get_tdl (), d);
hal_device_store_add (hald_get_gdl (), d);
hotplug_event_end (end_token);
return;
}
if (handler != NULL && handler->get_prober != NULL) {
prober = handler->get_prober (d, &prober_timeout);
} else {
prober = NULL;
}
if (handler->probing_done != NULL) {
probing_done = handler->probing_done;
} else {
probing_done = devinfo_callouts_probing_done;
}
if (prober != NULL) {
/* probe the device */
HAL_INFO(("Probing udi=%s", hal_device_get_udi (d)));
hald_runner_run (d,
prober, NULL,
prober_timeout,
probing_done,
(gpointer) end_token, (gpointer) handler);
} else {
probing_done (d, 0, 0, NULL, userdata1, userdata2);
}
}
/* This is the beginning of hotplug even handling */
void
hotplug_event_begin_add_devinfo (HalDevice *d, HalDevice *parent, DevinfoDevHandler *handler, void *end_token)
{
HAL_INFO(("Preprobing udi=%s", hal_device_get_udi (d)));
if (parent != NULL && hal_device_property_get_bool (parent, "info.ignore")) {
HAL_INFO (("Ignoring device since parent has info.ignore==TRUE"));
hotplug_event_end (end_token);
return;
}
/* add to TDL so preprobing callouts and prober can access it */
hal_device_store_add (hald_get_tdl (), d);
#if 0
/* Process preprobe fdi files */
di_search_and_merge (d, DEVICE_INFO_TYPE_PREPROBE);
#endif
/* Run preprobe callouts */
hal_util_callout_device_preprobe (d, devinfo_callouts_preprobing_done, end_token, handler);
}
void
devinfo_remove (gchar *devfs_path)
{
devinfo_remove_enqueue ((gchar *)devfs_path, NULL);
}
/* generate hotplug event for each device in this branch */
void
devinfo_remove_branch (gchar *devnode, HalDevice *d)
{
GSList *i;
GSList *children;
HalDevice *child;
char *child_devfs_path;
if (d == NULL) {
d = hal_device_store_match_key_value_string (hald_get_gdl (),
"netbsd.device", devnode);
if (d == NULL)
return;
}
HAL_INFO (("remove_branch: %s %s\n", devnode, hal_device_get_udi (d)));
/* first remove children */
children = hal_device_store_match_multiple_key_value_string (hald_get_gdl(),
"info.parent", hal_device_get_udi (d));
for (i = children; i != NULL; i = g_slist_next (i)) {
child = HAL_DEVICE (i->data);
HAL_INFO (("remove_branch: child %s\n", hal_device_get_udi (child)));
devinfo_remove_branch ((gchar *)hal_device_property_get_string (child, "netbsd.device"), child);
}
g_slist_free (children);
HAL_INFO (("remove_branch: done with children"));
/* then remove self */
HAL_INFO (("remove_branch: queueing %s", devnode));
devinfo_remove_enqueue (devnode, NULL);
}
void
devinfo_callouts_remove_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
HAL_INFO (("Remove callouts completed udi=%s", hal_device_get_udi (d)));
if (!hal_device_store_remove (hald_get_gdl (), d)) {
HAL_WARNING (("Error removing device"));
}
g_object_unref (d);
hotplug_event_end (end_token);
}
void
hotplug_event_begin_remove_devinfo (HalDevice *d, gchar *devfs_path, void *end_token)
{
#if notyet
if (hal_device_has_capability (d, "volume")) {
devinfo_volume_hotplug_begin_remove (d, devfs_path, end_token);
} else {
hal_util_callout_device_remove (d, devinfo_callouts_remove_done, end_token, NULL);
}
#else
#warning hotplug_event_begin_remove_devinfo TODO
hal_util_callout_device_remove (d, devinfo_callouts_remove_done, end_token, NULL);
#endif
}
gboolean
devinfo_device_rescan (HalDevice *d)
{
#if notyet
if (hal_device_has_capability (d, "block")) {
return (devinfo_storage_device_rescan (d));
} else {
return (FALSE);
}
#else
#warning devinfo_device_rescan TODO
return FALSE;
#endif
}
+72
View File
@@ -0,0 +1,72 @@
/***************************************************************************
*
* devinfo.h : definitions for libdevinfo-based device enumeration
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_H
#define DEVINFO_H
#include <glib.h>
#include <prop/proplib.h>
#include "../hald.h"
#include "../device_info.h"
typedef struct DevinfoDevHandler_s
{
HalDevice *(*add) (HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
/* yet unused */
void (*remove) (char *devfs_path);
void (*hotplug_begin_add) (HalDevice *d, HalDevice *parent, struct DevinfoDevHandler_s *handler, void *end_token);
void (*hotplug_begin_remove) (HalDevice *d, struct DevinfoDevHandler_s *handler, void *end_token);
void (*probing_done) (HalDevice *d, guint32 exit_type, gint return_code, char **error, gpointer userdata1, gpointer userdata2);
const gchar *(*get_prober) (HalDevice *d, int *timeout);
} DevinfoDevHandler;
#define PROP_INT(d, node, v, diprop, halprop) \
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, diprop, &(v)) > 0) { \
hal_device_property_set_int (d, halprop, *(v)); \
}
#define PROP_STR(d, node, v, diprop, halprop) \
if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, diprop, &(v)) > 0) { \
hal_device_property_set_string (d, halprop, v); \
}
#define PROP_BOOL(d, node, v, diprop, halprop) \
hal_device_property_set_bool (d, halprop, \
(di_prop_lookup_ints(DDI_DEV_T_ANY, node, diprop, &(v)) >= 0));
#define NELEM(a) (sizeof (a) / sizeof (*(a)))
void devinfo_add (HalDevice *parent, gchar *path);
HalDevice *devinfo_add_node(HalDevice *parent, const char *devnode);
void devinfo_set_default_properties (HalDevice *d, HalDevice *parent, const char *devnode, char *devfs_path);
void devinfo_callouts_preprobing_done (HalDevice *d, gpointer userdata1, gpointer userdata2);
void devinfo_callouts_probing_done (HalDevice *d, guint32 exit_type, gint return_code, char **error,
gpointer userdata1, gpointer userdata2);
void devinfo_callouts_add_done (HalDevice *d, gpointer userdata1, gpointer userdata2);
void devinfo_callouts_remove_done (HalDevice *d, gpointer userdata1, gpointer userdata2);
void hotplug_event_begin_add_devinfo (HalDevice *d, HalDevice *parent, DevinfoDevHandler *handler, void *end_token);
void devinfo_remove (gchar *path);
void devinfo_remove_branch (gchar *path, HalDevice *d);
void hotplug_event_begin_remove_devinfo (HalDevice *d, gchar *devfs_path, void *end_token);
void devinfo_hotplug_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler, int action, int front);
void devinfo_add_enqueue(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler);
void devinfo_add_enqueue_at_front(HalDevice *d, gchar *devfs_path, DevinfoDevHandler *handler);
void devinfo_remove_enqueue(gchar *devfs_path, DevinfoDevHandler *handler);
gboolean devinfo_device_rescan (HalDevice *d);
#endif /* DEVINFO_H */
@@ -0,0 +1,44 @@
/***************************************************************************
*
* devinfo_acpi.h : definitions for acpi devices
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_ACPI_H
#define DEVINFO_ACPI_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_acpi_handler;
extern DevinfoDevHandler devinfo_power_button_handler;
#define MINOR_SHIFT 8
#define MINOR2TYPE(minor) ((minor) >> MINOR_SHIFT)
/* Battery device types */
enum batt_type {
BATT_TYPE_UNKNOWN = -1,
BATT_TYPE_CBAT,
BATT_TYPE_AC,
BATT_TYPE_SBAT
};
HalDevice *devinfo_battery_add_major(HalDevice *parent, const char *devnode,
char *devfs_path, char *device_type, gboolean rescan, HalDevice *battery_d);
void devinfo_acpi_add_minor(HalDevice *parent, const char *devnode,
char *minor_path, dev_t dev);
void devinfo_battery_remove_minor(char *parent_devfs_path, gchar *udi);
void devinfo_battery_rescan(char *parent_devfs_path, gchar *udi);
const gchar *devinfo_acpi_get_prober(HalDevice *d, int *timeout);
void devinfo_power_button_event(void);
void devinfo_brightness_hotkeys_event(char *subclass);
void devinfo_lid_event(char *subclass, gchar *udi);
gboolean devinfo_lid_rescan(HalDevice *d);
#endif /* DEVINFO_ACPI_H */
@@ -0,0 +1,233 @@
/* $NetBSD: devinfo_audio.c,v 1.2 2008/11/27 18:00:44 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/audioio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <paths.h>
#include <unistd.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_audio.h"
#include "drvctl.h"
HalDevice *devinfo_audio_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
HalDevice *devinfo_audio_mixer_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
HalDevice *devinfo_audio_dsp_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
DevinfoDevHandler devinfo_audio_handler = {
devinfo_audio_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
DevinfoDevHandler devinfo_audio_mixer_handler = {
devinfo_audio_mixer_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
DevinfoDevHandler devinfo_audio_dsp_handler = {
devinfo_audio_dsp_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
HalDevice *
devinfo_audio_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d = NULL, *mixer, *dsp;
prop_dictionary_t dict;
const char *driver, *parent_udi;
char *childnode;
int16_t unit;
char *audioctl;
struct audio_device audiodev;
int fd;
if (drvctl_find_device (devnode, &dict) == FALSE || dict == NULL)
return NULL;
if (prop_dictionary_get_int16 (dict, "device-unit", &unit) == false ||
prop_dictionary_get_cstring_nocopy (dict, "device-driver", &driver) == false) {
prop_object_release (dict);
return NULL;
}
if (strcmp (driver, "audio") != 0) {
prop_object_release (dict);
return NULL;
}
audioctl = g_strdup_printf (_PATH_AUDIOCTL "%d", unit);
fd = open (audioctl, O_RDONLY);
if (fd < 0) {
HAL_WARNING (("couldn't open %s: %s", audioctl, strerror(errno)));
goto done;
}
if (ioctl (fd, AUDIO_GETDEV, &audiodev) == -1) {
HAL_WARNING (("couldn't query %s: %s", audioctl, strerror(errno)));
goto done;
}
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devfs_path);
hal_device_add_capability (d, "sound");
hal_device_property_set_string (d, "info.category", "sound");
hal_device_property_set_string (d, "info.subsystem", "sound");
hal_device_property_set_int (d, "sound.card", unit);
hal_device_property_set_string (d, "sound.card_id", audiodev.name);
hal_device_property_set_string (d, "netbsd.sound.hardware", audiodev.config);
parent_udi = hal_device_property_get_string (parent, "info.udi");
if (parent_udi)
hal_device_property_set_string (d, "sound.originating_device", parent_udi);
devinfo_add_enqueue (d, devfs_path, &devinfo_audio_handler);
childnode = g_strdup_printf ("oss_mixer_%d", unit);
devinfo_add_node (d, childnode);
g_free (childnode);
childnode = g_strdup_printf ("oss_dsp_%d", unit);
devinfo_add_node (d, childnode);
g_free (childnode);
done:
if (dict)
prop_object_release (dict);
if (audioctl)
g_free (audioctl);
if (fd >= 0)
close (fd);
return d;
}
HalDevice *
devinfo_audio_mixer_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d = NULL;
char *device_file, *parent_udi, *card_id, *device_id;
int16_t unit;
if (strstr (devnode, "oss_mixer_") != devnode)
return NULL;
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devfs_path);
hal_device_add_capability (d, "oss");
hal_device_property_set_string (d, "info.category", "oss");
hal_device_property_set_string (d, "info.subsystem", "sound");
card_id = hal_device_property_get_string (parent, "sound.card_id");
hal_device_property_set_string (d, "oss.card_id", card_id);
unit = hal_device_property_get_int (parent, "sound.card");
hal_device_property_set_int (d, "oss.card", unit);
hal_device_property_set_int (d, "oss.device", unit + 16);
device_id = hal_device_property_get_string (parent, "netbsd.sound.hardware");
hal_device_property_set_string (d, "oss.device_id", device_id);
hal_device_property_set_string (d, "oss.type", "mixer");
device_file = g_strdup_printf (_PATH_MIXER "%d", unit);
hal_device_property_set_string (d, "oss.device_file", device_file);
g_free (device_file);
parent_udi = hal_device_property_get_string (parent, "info.udi");
if (parent_udi)
hal_device_property_set_string (d, "sound.originating_device", parent_udi);
devinfo_add_enqueue (d, devfs_path, &devinfo_audio_mixer_handler);
return d;
}
HalDevice *
devinfo_audio_dsp_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d = NULL;
char *device_file, *parent_udi, *card_id, *device_id;
int16_t unit;
if (strstr (devnode, "oss_dsp_") != devnode)
return NULL;
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devfs_path);
hal_device_add_capability (d, "oss");
hal_device_property_set_string (d, "info.category", "oss");
hal_device_property_set_string (d, "info.subsystem", "sound");
card_id = hal_device_property_get_string (parent, "sound.card_id");
hal_device_property_set_string (d, "oss.card_id", card_id);
unit = hal_device_property_get_int (parent, "sound.card");
hal_device_property_set_int (d, "oss.card", unit);
hal_device_property_set_int (d, "oss.device", unit);
device_id = hal_device_property_get_string (parent, "netbsd.sound.hardware");
hal_device_property_set_string (d, "oss.device_id", device_id);
hal_device_property_set_string (d, "oss.type", "pcm");
device_file = g_strdup_printf (_PATH_SOUND "%d", unit);
hal_device_property_set_string (d, "oss.device_file", device_file);
g_free (device_file);
parent_udi = hal_device_property_get_string (parent, "info.udi");
if (parent_udi)
hal_device_property_set_string (d, "sound.originating_device", parent_udi);
devinfo_add_enqueue (d, devfs_path, &devinfo_audio_dsp_handler);
return d;
}
@@ -0,0 +1,38 @@
/* $NetBSD: devinfo_audio.h,v 1.1 2008/11/27 16:07:14 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifndef DEVINFO_AUDIO_H
#define DEVINFO_AUDIO_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_audio_handler;
extern DevinfoDevHandler devinfo_audio_mixer_handler;
extern DevinfoDevHandler devinfo_audio_dsp_handler;
#endif /* DEVINFO_AUDIO_H */
@@ -0,0 +1,19 @@
/***************************************************************************
*
* devinfo_cpu.h : definition for cpu devices
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_CPU_H
#define DEVINFO_CPU_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_cpu_handler;
#endif /* DEVINFO_CPU_H */
@@ -0,0 +1,416 @@
/* $NetBSD: devinfo_mass.c,v 1.4 2010/12/26 20:59:31 markd Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/disklabel.h>
#include <sys/bootblock.h>
#include <sys/ioctl.h>
#include <sys/scsiio.h>
#include <dev/scsipi/scsipi_all.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <paths.h>
#include <unistd.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_mass.h"
#include "drvctl.h"
#include <libvolume_id.h>
HalDevice *devinfo_mass_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
HalDevice *devinfo_mass_disklabel_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
/* XXX from devinfo_optical */
extern bool scsi_command(int, void *, size_t, void *, size_t, int, int);
DevinfoDevHandler devinfo_mass_handler = {
devinfo_mass_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
DevinfoDevHandler devinfo_mass_disklabel_handler = {
devinfo_mass_disklabel_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
static char *
rtrim_copy(char *src, int len)
{
char *dst, *p;
if (len == 0) {
len = strlen(src);
}
if ((dst = calloc(1, len + 1)) != NULL) {
strncpy(dst, src, len);
p = dst + len - 1;
while ((p >= dst) && (isspace((int)*p))) {
*p-- = '\0';
}
}
return (dst);
}
static const char *
devinfo_mass_get_fstype(uint8_t fstype)
{
switch (fstype) {
case FS_BSDFFS:
return "ffs";
case FS_MSDOS:
return "vfat";
case FS_EX2FS:
return "ext2";
case FS_NTFS:
return "ntfs";
case FS_APPLEUFS:
return "ffs";
default:
return NULL;
}
}
static uint8_t
devinfo_mass_get_mbrtype(uint8_t fstype)
{
switch (fstype) {
case FS_BSDFFS:
return MBR_PTYPE_NETBSD;
case FS_MSDOS:
return MBR_PTYPE_FAT32; /* XXX */
case FS_EX2FS:
return MBR_PTYPE_LNXEXT2;
case FS_NTFS:
return MBR_PTYPE_NTFS;
case FS_APPLEUFS:
return MBR_PTYPE_APPLE_UFS;
default:
return MBR_PTYPE_UNUSED;
}
}
HalDevice *
devinfo_mass_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d = NULL, *gparent = NULL;
prop_dictionary_t dict;
struct disklabel label;
struct stat st;
const char *driver;
char *rdevpath, *devpath;
char *childnode;
char *parent_devnode, *gparent_devnode = NULL;
char *gparent_udi;
int16_t unit;
int i, fd;
struct scsipi_inquiry_data inqbuf;
struct scsipi_inquiry cmd;
bool scsiinq_status;
char *storage_model = NULL, *storage_vendor = NULL;
if (drvctl_find_device (devnode, &dict) == FALSE || dict == NULL)
return NULL;
if (prop_dictionary_get_int16 (dict, "device-unit", &unit) == false ||
prop_dictionary_get_cstring_nocopy (dict, "device-driver", &driver) == false) {
prop_object_release (dict);
return NULL;
}
if (strcmp (driver, "wd") != 0 && strcmp (driver, "sd") != 0 &&
strcmp (driver, "ld") != 0) {
prop_object_release (dict);
return NULL;
}
sleep (1);
devpath = g_strdup_printf ("/dev/%s%c", devnode, RAW_PART + 'a');
rdevpath = g_strdup_printf ("/dev/r%s%c", devnode, RAW_PART + 'a');
HAL_INFO ((" going to open %s", rdevpath));
fd = open (rdevpath, O_RDONLY);
if (fd < 0) {
HAL_WARNING (("couldn't open %s: %s", rdevpath, strerror (errno)));
g_free (rdevpath);
g_free (devpath);
return NULL;
}
HAL_INFO ((" going to DIOCGDINFO %s", rdevpath));
if (ioctl (fd, DIOCGDINFO, &label) == -1) {
HAL_WARNING (("DIOCGDINFO failed on %s: %s", rdevpath, strerror (errno)));
g_free (rdevpath);
g_free (devpath);
close (fd);
return NULL;
}
if (strcmp (driver, "sd") == 0) {
memset(&cmd, 0, sizeof (cmd));
memset(&inqbuf, 0, sizeof (inqbuf));
cmd.opcode = INQUIRY;
cmd.length = sizeof (inqbuf);
scsiinq_status = scsi_command (fd, &cmd, sizeof (cmd), &inqbuf, sizeof (inqbuf), 10000, SCCMD_READ);
} else
scsiinq_status = false;
close (fd);
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devfs_path);
hal_device_add_capability (d, "block");
hal_device_property_set_string (d, "info.subsystem", "block");
hal_device_property_set_string (d, "block.device", devpath);
if (stat (devpath, &st) == 0) {
hal_device_property_set_int (d, "block.major", major (st.st_rdev));
hal_device_property_set_int (d, "block.minor", minor (st.st_rdev));
}
hal_device_property_set_bool (d, "block.is_volume", FALSE);
hal_device_property_set_bool (d, "block.no_partitions", FALSE);
hal_device_property_set_bool (d, "block.have_scanned", TRUE);
hal_device_add_capability (d, "storage");
hal_device_property_set_string (d, "info.category", "storage");
parent_devnode = hal_device_property_get_string (parent, "netbsd.device");
gparent_udi = hal_device_property_get_string (parent, "info.parent");
if (gparent_udi) {
gparent = hal_device_store_find (hald_get_gdl (), gparent_udi);
if (gparent)
gparent_devnode = hal_device_property_get_string (gparent, "netbsd.device");
}
if (gparent_devnode && strstr (gparent_devnode, "umass") == gparent_devnode)
hal_device_property_set_string (d, "storage.bus", "usb");
else if (strstr (parent_devnode, "atabus") == parent_devnode)
hal_device_property_set_string (d, "storage.bus", "ide");
else
hal_device_property_set_string (d, "storage.bus", "scsi");
hal_device_property_set_string (d, "storage.device_type", "disk");
hal_device_property_set_bool (d, "storage.removable", label.d_flags & D_REMOVABLE ? TRUE : FALSE);
if (label.d_flags & D_REMOVABLE) {
hal_device_property_set_bool (d, "storage.removable.media_available", TRUE);
hal_device_property_set_uint64 (d, "storage.removable.media_size",
(uint64_t)label.d_secsize * (uint64_t)label.d_secperunit);
hal_device_property_set_uint64 (d, "storage.size", 0);
hal_device_property_set_bool (d, "storage.hotpluggable", TRUE);
hal_device_property_set_bool (d, "storage.automount_enabled_hint", TRUE);
} else {
hal_device_property_set_bool (d, "storage.removable.media_available", FALSE);
hal_device_property_set_uint64 (d, "storage.removable.media_size", 0);
hal_device_property_set_uint64 (d, "storage.size",
(uint64_t)label.d_secsize * (uint64_t)label.d_secperunit);
hal_device_property_set_bool (d, "storage.hotpluggable", FALSE);
hal_device_property_set_bool (d, "storage.automount_enabled_hint", FALSE);
}
hal_device_property_set_bool (d, "storage.no_partitions_hint", FALSE);
hal_device_property_set_bool (d, "storage.requires_eject", FALSE);
hal_device_property_set_bool (d, "storage.removable.support_async_notification", FALSE);
hal_device_property_set_string (d, "storage.partitioning_scheme", "mbr");
hal_device_property_set_string (d, "storage.originating_device",
hal_device_property_get_string (d, "info.udi"));
if (scsiinq_status == true) {
storage_model = rtrim_copy(inqbuf.product, sizeof (inqbuf.product));
storage_vendor = rtrim_copy(inqbuf.vendor, sizeof (inqbuf.vendor));
}
if (storage_model) {
hal_device_property_set_string (d, "storage.model", storage_model);
free (storage_model);
} else
hal_device_property_set_string (d, "storage.model", label.d_packname);
if (storage_vendor) {
hal_device_property_set_string (d, "storage.vendor", storage_vendor);
free (storage_vendor);
} else
hal_device_property_set_string (d, "storage.vendor", label.d_typename);
devinfo_add_enqueue (d, devfs_path, &devinfo_mass_handler);
for (i = 0; i < MAXPARTITIONS; i++) {
const char *fstype;
fstype = devinfo_mass_get_fstype(label.d_partitions[i].p_fstype);
if (fstype == NULL)
continue;
childnode = g_strdup_printf ("%s%c", devnode, 'a' + i);
HAL_INFO ((" adding %s on %s", childnode, rdevpath));
devinfo_mass_disklabel_add (d, childnode, childnode, childnode);
g_free (childnode);
}
HAL_INFO ((" returning"));
g_free (rdevpath);
g_free (devpath);
done:
prop_object_release (dict);
return d;
}
HalDevice *
devinfo_mass_disklabel_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d = NULL;
struct disklabel label;
struct partition *part;
struct stat st;
const char *driver;
char *devpath, *rdevpath, *partname;
char *childnode;
char unit;
struct volume_id *vid;
uint64_t psize, msize;
int i, fd;
partname = devnode;
unit = partname[strlen (partname) - 1] - 'a';
if (unit >= MAXPARTITIONS)
return NULL;
devpath = g_strdup_printf ("/dev/%s", partname);
rdevpath = g_strdup_printf ("/dev/r%s", partname);
fd = open (rdevpath, O_RDONLY);
if (fd < 0) {
HAL_WARNING (("couldn't open %s: %s", rdevpath, strerror (errno)));
g_free (rdevpath);
return NULL;
}
if (ioctl (fd, DIOCGDINFO, &label) == -1) {
HAL_WARNING (("DIOCGDINFO failed on %s: %s", rdevpath, strerror (errno)));
g_free (rdevpath);
close (fd);
return NULL;
}
part = &label.d_partitions[unit];
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devfs_path);
hal_device_add_capability (d, "block");
hal_device_property_set_string (d, "info.subsystem", "block");
hal_device_property_set_string (d, "info.category", "volume");
hal_device_property_set_string (d, "block.device", devpath);
hal_device_property_set_string (d, "block.storage_device",
hal_device_property_get_string (parent, "info.udi"));
if (stat (devpath, &st) == 0) {
hal_device_property_set_int (d, "block.major", major (st.st_rdev));
hal_device_property_set_int (d, "block.minor", minor (st.st_rdev));
}
hal_device_property_set_bool (d, "block.is_volume", TRUE);
hal_device_property_set_bool (d, "block.no_partitions", FALSE);
hal_device_property_set_bool (d, "block.have_scanned", TRUE);
hal_device_add_capability (d, "volume");
hal_device_property_set_bool (d, "volume.ignore", FALSE);
hal_device_property_set_bool (d, "volume.is_mounted", FALSE);
hal_device_property_set_bool (d, "volume.is_mounted_read_only", FALSE);
hal_device_property_set_string (d, "volume.mount_point", "");
hal_device_property_set_string (d, "volume.fsusage", "filesystem");
hal_device_property_set_string (d, "volume.fstype", devinfo_mass_get_fstype (part->p_fstype));
hal_device_property_set_bool (d, "volume.is_disc", FALSE);
hal_device_property_set_string (d, "volume.label", "");
hal_device_property_set_string (d, "volume.partition.label", "");
hal_device_property_set_string (d, "volume.uuid", "");
hal_device_property_set_string (d, "volume.partition.uuid", "");
psize = (uint64_t)part->p_size * (uint64_t)label.d_secsize;
msize = (uint64_t)label.d_secsize * (uint64_t)label.d_secperunit;
hal_device_property_set_uint64 (d, "volume.size", psize);
hal_device_property_set_int (d, "volume.block_size", label.d_secsize);
hal_device_property_set_uint64 (d, "volume.num_blocks", part->p_size);
hal_device_property_set_uint64 (d, "volume.partition.media_size", msize);
hal_device_property_set_bool (d, "volume.is_partition", TRUE);
hal_device_property_set_int (d, "volume.partition.number", unit);
hal_device_property_set_string (d, "volume.partition.scheme", "mbr");
vid = volume_id_open_fd (fd);
if (vid) {
if (volume_id_probe_all (vid, 0, psize) == 0) {
char *type;
hal_device_property_set_string (d, "volume.label", vid->label);
hal_device_property_set_string (d, "volume.partition.label", vid->label);
hal_device_property_set_string (d, "volume.uuid", vid->uuid);
hal_device_property_set_string (d, "volume.partition.uuid", vid->uuid);
if (volume_id_get_type (vid, &type) && type != NULL)
if (strcmp (type, "vfat") == 0) {
HAL_INFO (("%s disklabel reports %s but libvolume_id says it is "
"%s, assuming disklabel is incorrect",
devpath, devinfo_mass_get_fstype (part->p_fstype), type));
hal_device_property_set_string (d, "volume.fstype", type);
}
}
volume_id_close (vid);
}
devinfo_add_enqueue (d, devfs_path, &devinfo_mass_disklabel_handler);
close (fd);
g_free (rdevpath);
g_free (devpath);
return d;
}
@@ -0,0 +1,37 @@
/* $NetBSD: devinfo_mass.h,v 1.1 2008/12/01 02:02:33 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifndef DEVINFO_MASS_H
#define DEVINFO_MASS_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_mass_handler;
extern DevinfoDevHandler devinfo_mass_disklabel_handler;
#endif /* DEVINFO_MASS_H */
@@ -0,0 +1,168 @@
/***************************************************************************
*
* devinfo_misc : misc devices
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#include <sys/types.h>
#include <sys/sysctl.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/utsname.h>
#include <stdio.h>
#include <string.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "devinfo_misc.h"
#include "drvctl.h"
static HalDevice *devinfo_computer_add(HalDevice *, const char *, char *, char *);
static HalDevice *devinfo_cpu_add(HalDevice *, const char *, char *,char *);
static HalDevice *devinfo_default_add(HalDevice *, const char *, char *, char *);
DevinfoDevHandler devinfo_computer_handler = {
devinfo_computer_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
DevinfoDevHandler devinfo_cpu_handler = {
devinfo_cpu_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
DevinfoDevHandler devinfo_default_handler = {
devinfo_default_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
static HalDevice *
devinfo_computer_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d;
struct utsname un;
char acpi_supported_states[20];
size_t len = sizeof(acpi_supported_states);
if (strcmp (devnode, "mainbus0") != 0) {
return (NULL);
}
HAL_INFO (("devinfo_computer_add parent=%p devnode=%s devfs_path=%s device_type=%s",
parent, devnode, devfs_path, device_type));
d = hal_device_new ();
hal_device_property_set_string (d, "info.subsystem", "unknown");
hal_device_property_set_string (d, "info.product", "Computer");
hal_device_set_udi (d, "/org/freedesktop/Hal/devices/computer");
hal_device_property_set_string (d, "netbsd.device", devnode);
if (uname (&un) >= 0) {
hal_device_property_set_string (d, "system.kernel.name", un.sysname);
hal_device_property_set_string (d, "system.kernel.version", un.release);
hal_device_property_set_string (d, "system.kernel.machine", un.machine);
}
devinfo_add_enqueue (d, devfs_path, &devinfo_computer_handler);
if (sysctlbyname ("hw.acpi.supported_states", acpi_supported_states, &len, NULL, 0) == 0 ||
sysctlbyname ("hw.acpi.sleep.states", acpi_supported_states, &len, NULL, 0) == 0) {
hal_device_property_set_string (d, "power_management.type", "acpi");
if (strstr (acpi_supported_states, "S3") != NULL)
hal_device_property_set_bool (d, "power_management.can_suspend", TRUE);
else
hal_device_property_set_bool (d, "power_management.can_suspend", FALSE);
hal_device_property_set_bool (d, "power_management.can_hibernate", FALSE);
}
if (drvctl_find_device ("acpibat0", NULL) == TRUE)
hal_device_property_set_string (d, "system.formfactor", "laptop");
else
hal_device_property_set_string (d, "system.formfactor", "desktop"); /* XXX */
devinfo_add_enqueue (d, devnode, &devinfo_default_handler);
return d;
}
static HalDevice *
devinfo_cpu_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d;
if (strncmp(devnode, "cpu", 3) != 0) {
return (NULL);
}
HAL_INFO (("devinfo_cpu_add: parent=%p devnode=%s devfs_path=%s device_type=%s",
parent, devnode, devfs_path, device_type));
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devnode);
hal_device_add_capability (d, "processor");
devinfo_add_enqueue (d, devnode, &devinfo_cpu_handler);
return (d);
}
static void
devinfo_default_apply_quirks(HalDevice *d, const char *devnode)
{
/* acpiacad(4) */
if (strncmp (devnode, "acpiacad", 8) == 0) {
HAL_INFO (("%s: applying acpiacad quirks"));
hal_device_add_capability (d, "ac_adapter");
/* acpibat(4) */
} else if (strncmp (devnode, "acpibat", 7) == 0) {
HAL_INFO (("%s: applying acpibat quirks"));
hal_device_add_capability (d, "battery");
hal_device_property_set_string (d, "battery.type", "primary");
}
}
static HalDevice *
devinfo_default_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
char *driver_name;
const char *parent_path;
HalDevice *d;
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devnode);
devinfo_default_apply_quirks (d, devnode);
devinfo_add_enqueue (d, devnode, &devinfo_default_handler);
return (d);
}
@@ -0,0 +1,21 @@
/***************************************************************************
*
* devinfo_misc.h : definitions for misc devices
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_MISC_H
#define DEVINFO_MISC_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_computer_handler;
extern DevinfoDevHandler devinfo_keyboard_handler;
extern DevinfoDevHandler devinfo_default_handler;
#endif /* DEVINFO_MISC_H */
@@ -0,0 +1,485 @@
/*
* Copyright (c) 2008 Reinoud Zandijk
* 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 ``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 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.
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/scsiio.h>
#include <sys/disklabel.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipi_cd.h>
#include <string.h>
#include <machine/disklabel.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "devinfo_optical.h"
#include "devinfo.h"
#define DEVINFO_PROBE_STORAGE_TIMEOUT 60000
static HalDevice *devinfo_optical_add(HalDevice *, const char *, char *,char *);
static HalDevice *devinfo_optical_volume_add(HalDevice *, const char *, char *, char *);
static const gchar *
devinfo_storage_get_prober (HalDevice *d, int *timeout)
{
*timeout = DEVINFO_PROBE_STORAGE_TIMEOUT;
return "hald-probe-storage";
}
#if notyet
const gchar *
devinfo_volume_get_prober (HalDevice *d, int *timeout)
{
*timeout = DEVINFO_PROBE_VOLUME_TIMEOUT;
return "hald-probe-volume";
}
#endif
DevinfoDevHandler devinfo_optical_handler = {
devinfo_optical_add,
NULL, /* remove */
NULL, /* hotplug_begin_add */
NULL, /* hotplug_begin_remove */
NULL, /* probing_done */
NULL /* devinfo_storage_get_prober */ /* get_prober */
};
DevinfoDevHandler devinfo_optical_volume_handler = {
devinfo_optical_volume_add,
NULL, /* remove */
NULL, /* hotplug_begin_add */
NULL, /* hotplug_begin_remove */
NULL, /* probing_done */
NULL /* devinfo_storage_get_prober */ /* get_prober */
};
/* XXX i dont know how to link cdutils here XXX ! */
bool
scsi_command (int fd, void *cmd, size_t cmdlen, void *data, size_t datalen,
int timeout, int flags)
{
scsireq_t req;
memset(&req, 0, sizeof(req));
memcpy(req.cmd, cmd, cmdlen);
req.cmdlen = cmdlen;
req.databuf = data;
req.datalen = datalen;
req.timeout = timeout;
req.flags = flags;
req.senselen = SENSEBUFLEN;
if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
return false;
if (req.retsts == SCCMD_OK)
return true;
return false;
}
static void
optical_get_str(char *str, unsigned char *buf, int len)
{
char *pos;
int i;
pos = str;
for (i = 0; i < len; i++) {
if (isprint(buf[i]))
*pos++ = buf[i];
}
*pos = (char) 0;
pos--;
while (*pos == ' ')
*pos-- = (char) 0;
}
static void
devinfo_optical_identify_drive(HalDevice *d, char *devstr)
{
uint8_t cmd[12], buf[100];
char str[100];
int fd, ok;
fd = open(devstr, O_RDONLY, 0);
if(!fd)
return;
HAL_INFO(("devinfo_optical_identify_drive: SCSI call"));
cmd[0] = 0x12; /* INQUIRY */
cmd[1] = 0; /* basic inquiry */
cmd[2] = 0; /* no page or operation code */
cmd[3] = 0; /* reserved/MSB result */
cmd[4] = 96; /* all but vendor specific */
cmd[5] = 0; /* control */
ok = scsi_command(fd, cmd, 6, buf, 96, 30000, SCCMD_READ);
close(fd);
if (!ok)
return;
optical_get_str(str, buf+8, 8);
hal_device_property_set_string (d, "storage.vendor", str);
optical_get_str(str, buf+16, 16);
hal_device_property_set_string (d, "storage.model", str);
optical_get_str(str, buf+32, 4);
hal_device_property_set_string (d, "storage.firmware_version", str);
}
static char const *
get_profile_name(HalDevice *d, int profile)
{
switch (profile) {
case 0x00 : return NULL; // "Unknown[0] profile";
case 0x01 : return NULL; // "Non removeable disc";
case 0x02 : return NULL; // "Removable disc";
case 0x03 : return "mo"; // Magneto Optical with sector erase";
case 0x04 : return "mo"; // "Magneto Optical write once";
case 0x05 : return "mo"; // "Advance Storage Magneto Optical";
case 0x08 : return "cd"; // "CD-ROM"; no writing
case 0x09 : return "cdr"; // "CD-R recordable";
case 0x0a : return "cdrw"; // "CD-RW rewritable";
case 0x10 : return "dvd"; // "DVD-ROM"; no writing
case 0x11 : return "dvdr"; // "DVD-R sequential";
case 0x12 : return "dvdram"; // "DVD-RAM rewritable";
case 0x13 : return "dvdrw"; // "DVD-RW restricted overwrite";
case 0x14 : return "dvdrw"; // "DVD-RW sequential";
case 0x15 : return "dvdr"; // "DVD-R dual layer sequential";
case 0x16 : return "dvdr"; // "DVD-R dual layer jump";
case 0x17 : return "dvdrw"; // "DVD-RW dual layer";
case 0x18 : return NULL; // "DVD-Download disc"; UNKNOWN
case 0x1a : return "dvdplusrw"; // "DVD+RW rewritable";
case 0x1b : return "dvdplusr"; // "DVD+R recordable";
case 0x20 : return "ddcd"; // "DDCD readonly (retracted)"; no writing
case 0x21 : return "ddcdr"; // "DDCD-R recordable (retracted)"; OK?
case 0x22 : return "ddcdrw"; // "DDCD-RW rewritable (retracted)"; OK?
case 0x2a : return "dvdplusrwdl";//"DVD+RW double layer";
case 0x2b : return "dvdplusrdl";// "DVD+R double layer";
case 0x40 : return "bd"; // "BD-ROM";
case 0x41 : return "bdr"; // "BD-R Sequential Recording (SRM)";
case 0x42 : return "bdr"; // "BD-R Random Recording (RRM)";
case 0x43 : return "bdre"; // "BD-RE rewritable";
case 0x50 : return "hddvd"; // "HD DVD-ROM (retracted)";
case 0x51 : return "hddvdr"; // "HD DVD-R (retracted)";
case 0x52 : return "hddvdram"; // "HD DVD-RAM (retracted)";
case 0x53 : return "hddvdrw"; // "HD DVD-RW (retracted)";
case 0x58 : return "hddvdrdl"; // "HD DVD-R dual layer (retracted)";
case 0x5a : return "hddvdrwdl"; // "HD DVD-RW dual layer (retracted)";
}
/* reserved */
return NULL;
}
#define feat_tbl_len 300
#define max_profile 255
static void
devinfo_optical_disc_caps(int fd, HalDevice *d)
{
uint8_t cmd[12], buf[feat_tbl_len], *fpos, *pos;
char str[100];
uint8_t present[max_profile+1];
char const *name;
int feature, feature_cur, feature_len, req_feature;
int cnt, profile;
int mrw, mrw_w, ok;
/* set defaults */
for (profile = 0; profile <= max_profile; profile++) {
present[profile] = FALSE;
}
HAL_INFO(("devinfo_optical_disc_caps: get feature 0 SCSI call"));
req_feature = 0;
cmd[0] = 0x46; /* Get configuration */
cmd[1] = 0; /* RT=0 -> all independent of current setting */
cmd[2] = (req_feature) >> 8; /* MSB feature number */
cmd[3] = (req_feature) & 0xff; /* LSB feature number */
cmd[7] = (feat_tbl_len) >> 8; /* MSB buffersize */
cmd[8] = (feat_tbl_len) & 0xff; /* LSB buffersize */
cmd[9] = 0; /* control */
ok = scsi_command(fd, cmd, 10, buf, feat_tbl_len, 30000, SCCMD_READ);
if (!ok)
return;
HAL_INFO(("devinfo_optical_disc_caps: processing profiles\n"));
fpos = buf + 8;
feature = fpos[1] | (fpos[0] << 8);
feature_cur = (fpos[2] & 1);
feature_len = fpos[3];
if (feature != 0) {
HAL_INFO(("cdrom drive on crack, its not returning feature 0\n"));
return;
}
if ((feature_len & 3) != 0)
HAL_INFO(("*** drive returned bad feature length ***"));
/* process feature */
pos = &fpos[4];
for (cnt=0; cnt < feature_len; cnt += 4) {
profile = pos[1] | (pos[0] << 8);
if (profile <= max_profile) {
present[profile] = TRUE;
} else {
if (profile != 0xffff)
HAL_INFO(("devinfo_optical_disc_caps: bump max_profile!, "
"found profile %d", profile));
}
pos += 4;
}
/* set hal properties */
for (profile = 0; profile <= max_profile; profile++) {
/* process profile */
name = get_profile_name(d, profile);
if (name) {
strcpy(str, "storage.cdrom.");
strcat(str, name);
HAL_INFO(("Profile %d : %s : %s", profile, str,
present[profile] ? "true":"false"));
hal_device_property_set_bool(d, str, present[profile]);
}
}
/* set MRW support; feature 0x28, version 0/1 */
HAL_INFO(("devinfo_optical_disc_caps: get feature 0x28 SCSI call"));
req_feature = 0x28;
cmd[0] = 0x46; /* Get configuration */
cmd[1] = 0; /* RT=0 -> all independent of current setting */
cmd[2] = (req_feature) >> 8; /* MSB feature number */
cmd[3] = (req_feature) & 0xff; /* LSB feature number */
cmd[7] = (feat_tbl_len) >> 8; /* MSB buffersize */
cmd[8] = (feat_tbl_len) & 0xff; /* LSB buffersize */
cmd[9] = 0; /* control */
ok = scsi_command(fd, cmd, 10, buf, feat_tbl_len, 30000, SCCMD_READ);
if (!ok)
return;
feature = fpos[1] | (fpos[0] << 8);
feature_cur = (fpos[2] & 1);
feature_len = fpos[3];
if ((feature_len & 3) != 0)
HAL_INFO(("*** drive returned bad feature length ***"));
HAL_INFO(("devinfo_optical_disc_caps: processing MRW\n"));
/* process feature */
pos = &fpos[4];
mrw = FALSE;
mrw_w = FALSE;
if (feature == 0x28) {
mrw = TRUE;
if (pos[0] & 1)
mrw_w = TRUE;
}
hal_device_property_set_bool(d, "storage.cdrom.mrw", mrw);
hal_device_property_set_bool(d, "storage.cdrom.mrw_w", mrw_w);
/*
* XXX very rare to now have multi-session support; could be detected
* in the change bits of page 5, but set to true for now
*/
hal_device_property_set_bool(d, "storage.cdrom.support_multisession", TRUE);
/* XXX realy? */
hal_device_property_set_bool(d, "storage.cdrom.support_media_changed", TRUE);
/* media dependent; addon-storage will handle these */
//hal_device_property_set_int(d,"storage.cdrom.read_speed", 0);
//hal_device_property_set_int(d,"storage.cdrom.write_speed", 0);
//hal_device_property_set_string(d, "storage.cdrom.write_speeds", FALSE);
}
#undef max_profile
#undef feat_tbl_len
static HalDevice *
devinfo_optical_volume_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d;
struct stat devstat;
char *devstr;
int error;
HAL_INFO (("devinfo_optical_volume_add: parent=%p devnode=%s devfs_path=%s device_type=%s",
parent, devnode, devfs_path, device_type));
d = hal_device_new ();
devstr = malloc(strlen(devnode) + 10);
/* info */
devinfo_set_default_properties (d, parent, devnode, devnode);
hal_device_add_capability (d, "block");
hal_device_add_capability (d, "volume");
hal_device_add_capability (d, "volume.disc");
hal_device_property_set_string (d, "info.category", "volume");
/* block */
sprintf(devstr, "/dev/%s", devnode);
hal_device_property_set_string (d, "block.device", devstr);
error = stat(devstr, &devstat);
if (!error) {
hal_device_property_set_int (d, "block.major", major(devstat.st_rdev));
hal_device_property_set_int (d, "block.minor", minor(devstat.st_rdev));
}
sprintf(devstr, "/dev/r%s", devnode);
hal_device_property_set_string (d, "block.netbsd.raw_device", devstr);
hal_device_property_set_bool (d, "block.is_volume", TRUE);
hal_device_property_set_bool (d, "block.no_partitions", TRUE);
hal_device_property_set_bool (d, "block.have_scanned", TRUE);
hal_device_property_set_string (d, "block.storage_device",
hal_device_property_get_string (parent, "info.udi"));
/* volume */
hal_device_property_set_bool (d, "volume.ignore", FALSE);
hal_device_property_set_string(d, "volume.fsusage", "filesytem");
hal_device_property_set_string(d, "volume.fstype", "");
hal_device_property_set_string(d, "volume.label", "");
hal_device_property_set_bool (d, "volume.is_disc", TRUE);
hal_device_property_set_bool (d, "volume.is_partition", FALSE);
/* rest by addon */
/* volume.disc */
/* by addon */
devinfo_add_enqueue (d, devnode, &devinfo_optical_volume_handler);
return d;
}
static HalDevice *
devinfo_optical_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d;
struct stat devstat;
char *devstr;
char *parent_devnode;
int fd, error;
if (strncmp(devnode, "cd", 2) != 0) {
return (NULL);
}
HAL_INFO (("devinfo_optical_add: parent=%p devnode=%s devfs_path=%s device_type=%s",
parent, devnode, devfs_path, device_type));
d = hal_device_new ();
devstr = malloc(strlen(devnode) + 10);
/* info */
devinfo_set_default_properties (d, parent, devnode, devnode);
// hal_device_property_set_string (d, "netbsd.device", devfs_path);
hal_device_property_set_string (d, "info.subsystem", "block");
hal_device_add_capability (d, "block");
hal_device_add_capability (d, "storage");
hal_device_add_capability (d, "storage.cdrom");
/* block */
sprintf(devstr, "/dev/%s%c", devnode, 'a' + RAW_PART);
hal_device_property_set_string (d, "block.device", devstr);
error = stat(devstr, &devstat);
if (!error) {
hal_device_property_set_int (d, "block.major", major(devstat.st_rdev));
hal_device_property_set_int (d, "block.minor", minor(devstat.st_rdev));
}
sprintf(devstr, "/dev/r%s%c", devnode, 'a' + RAW_PART);
hal_device_property_set_string (d, "block.netbsd.raw_device", devstr);
hal_device_property_set_bool (d, "block.is_volume", FALSE);
hal_device_property_set_bool (d, "block.no_partitions", TRUE);
hal_device_property_set_bool (d, "block.have_scanned", TRUE);
/* storage */
parent_devnode = hal_device_property_get_string (parent, "netbsd.device");
if (strstr (parent_devnode, "umass") == parent_devnode)
hal_device_property_set_string (d, "storage.bus", "usb");
else if (strstr (parent_devnode, "atapi") == parent_devnode)
hal_device_property_set_string (d, "storage.bus", "ide");
else
hal_device_property_set_string (d, "storage.bus", "scsi");
hal_device_property_set_string (d, "storage.drive_type", "cdrom");
hal_device_property_set_bool (d, "storage.removable", TRUE);
/* "storage.removable.media_available" set by addon-storage */
/* "storage.removable.media_size" set by addon-storage */
hal_device_property_set_bool (d, "storage.removable.support_async_notification", FALSE);
hal_device_property_set_bool (d, "storage.requires_eject", TRUE);
hal_device_property_set_bool (d, "storage.hotpluggable", TRUE);
hal_device_property_set_bool (d, "storage.media_check_enabled", TRUE); /* XXX */
hal_device_property_set_bool (d, "storage.automount_enabled_hint", FALSE); /* multiple choice */
hal_device_property_set_bool (d, "storage.no_partitions_hint", TRUE);
hal_device_property_set_string (d, "storage.originating_device", ""); /* XXX */
fd = open(devstr, O_RDONLY, 0);
if (fd) {
/* get drive's vendor and model name */
devinfo_optical_identify_drive(d, devstr);
/* get CD specific `storage.cdrom' values */
devinfo_optical_disc_caps(fd, d);
}
devinfo_add_enqueue (d, devnode, &devinfo_optical_handler);
if (fd) {
/* create CD volume node */
sprintf(devstr, "%s%c", devnode, 'a' + RAW_PART);
devinfo_optical_volume_add(d, devstr, devfs_path, "volume");
}
close(fd);
return (d);
}
@@ -0,0 +1,20 @@
/***************************************************************************
*
* devinfo_optical.h : definition for optical (MMC compliant) media
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_OPTICAL_H
#define DEVINFO_OPTICAL_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_optical_handler;
#endif /* DEVINFO_OPTICAL_H */
@@ -0,0 +1,122 @@
/***************************************************************************
*
* devinfo_pci.c : PCI devices
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#pragma ident "@(#)devinfo_pci.c 1.2 06/10/13 SMI"
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <libdevinfo.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "devinfo_pci.h"
HalDevice *devinfo_pci_add (HalDevice *parent, di_node_t node, char *devfs_path, char *device_type);
DevinfoDevHandler devinfo_pci_handler = {
devinfo_pci_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
HalDevice *devinfo_pci_add (HalDevice *parent, di_node_t node, char *devfs_path, char *device_type)
{
HalDevice *d;
char *s;
int *i;
int vid, pid, svid, spid;
if ((device_type == NULL) ||
((strcmp (device_type, "pci") != 0) &&
(strcmp (device_type, "pci-ide") != 0))) {
if (parent == NULL) {
return (NULL);
} else {
s = (char *)hal_device_property_get_string (parent, "info.subsystem");
if ((s == NULL) || (strcmp (s, "pci") != 0)) {
return (NULL);
}
}
}
d = hal_device_new ();
devinfo_set_default_properties (d, parent, node, devfs_path);
hal_device_property_set_string (d, "info.subsystem", "pci");
vid = pid = svid = spid = 0;
if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "vendor-id", &i) > 0) {
vid = i[0];
}
if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "device-id", &i) > 0) {
pid = i[0];
}
if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "subsystem-vendor-id", &i) > 0) {
svid = i[0];
}
if (di_prop_lookup_ints (DDI_DEV_T_ANY, node, "subsystem-id", &i) > 0) {
spid = i[0];
}
hal_device_property_set_int (d, "pci.vendor_id", vid);
hal_device_property_set_int (d, "pci.product_id", pid);
hal_device_property_set_int (d, "pci.subsys_vendor_id", svid);
hal_device_property_set_int (d, "pci.subsys_product_id", spid);
{
char *vendor_name;
char *product_name;
char *subsys_vendor_name;
char *subsys_product_name;
ids_find_pci (hal_device_property_get_int (d, "pci.vendor_id"),
hal_device_property_get_int (d, "pci.product_id"),
hal_device_property_get_int (d, "pci.subsys_vendor_id"),
hal_device_property_get_int (d, "pci.subsys_product_id"),
&vendor_name, &product_name, &subsys_vendor_name,
&subsys_product_name);
if (vendor_name != NULL) {
hal_device_property_set_string (d, "pci.vendor", vendor_name);
hal_device_property_set_string (d, "info.vendor", vendor_name);
}
if (product_name != NULL) {
hal_device_property_set_string (d, "pci.product", product_name);
hal_device_property_set_string (d, "info.product", product_name);
}
if (subsys_vendor_name != NULL) {
hal_device_property_set_string (d, "pci.subsys_vendor",
subsys_vendor_name);
}
if (subsys_product_name != NULL) {
hal_device_property_set_string (d, "pci.subsys_product", subsys_product_name);
}
}
devinfo_add_enqueue (d, devfs_path, &devinfo_pci_handler);
return (d);
}
@@ -0,0 +1,21 @@
/***************************************************************************
*
* devinfo_pci.h : definitions for PCI devices
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#pragma ident "%Z%%M% %I% %E% SMI"
#ifndef DEVINFO_PCI_H
#define DEVINFO_PCI_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_pci_handler;
#endif /* DEVINFO_PCI_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,30 @@
/***************************************************************************
*
* devinfo_storage.h : definitions for storage devices
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_STORAGE_H
#define DEVINFO_STORAGE_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_ide_handler;
extern DevinfoDevHandler devinfo_scsi_handler;
extern DevinfoDevHandler devinfo_pcata_handler;
extern DevinfoDevHandler devinfo_floppy_handler;
extern DevinfoDevHandler devinfo_lofi_handler;
gboolean devinfo_storage_device_rescan (HalDevice *d);
HalDevice *devinfo_lofi_add_major(HalDevice *parent, const char *devinfo, char *devfs_path,
char *device_type, gboolean rescan, HalDevice *lofi_d);
void devinfo_lofi_remove_minor(char *parent_devfs_path, char *name);
void devinfo_storage_mnttab_event (HalDevice *hal_volume);
void devinfo_volume_hotplug_begin_remove (HalDevice *d, char *devfs_path, void *end_token);
#endif /* DEVINFO_STORAGE_H */
@@ -0,0 +1,229 @@
/***************************************************************************
*
* devinfo_usb.h : USB devices
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#pragma ident "@(#)devinfo_usb.c 1.2 06/10/13 SMI"
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <libdevinfo.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/stat.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_usb.h"
HalDevice *devinfo_usb_add(HalDevice *parent, di_node_t node, char *devfs_path, char *device_type);
static HalDevice *devinfo_usb_if_add(HalDevice *d, di_node_t node, gchar *devfs_path, int ifnum);
static HalDevice *devinfo_usb_scsa2usb_add(HalDevice *d, di_node_t node, gchar *devfs_path);
DevinfoDevHandler devinfo_usb_handler = {
devinfo_usb_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
HalDevice *
devinfo_usb_add(HalDevice *parent, di_node_t node, char *devfs_path, char *device_type)
{
HalDevice *d, *nd = NULL;
char *s;
int *i, *vid;
char *driver_name, *binding_name;
char if_devfs_path[HAL_PATH_MAX];
/*
* we distinguish USB devices by presence of "usb-vendor-id"
* property. should USB devices have "device_type"?
*/
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "usb-vendor-id", &vid) <= 0) {
return (NULL);
}
d = hal_device_new ();
devinfo_set_default_properties (d, parent, node, devfs_path);
hal_device_property_set_string (d, "info.subsystem", "usb_device");
PROP_STR(d, node, s, "usb-product-name", "info.product");
PROP_STR(d, node, s, "usb-product-name", "usb_device.product");
PROP_STR(d, node, s, "usb-vendor-name", "usb_device.vendor");
PROP_INT(d, node, i, "usb-vendor-id", "usb_device.vendor_id");
PROP_INT(d, node, i, "usb-product-id", "usb_device.product_id");
PROP_INT(d, node, i, "usb-revision-id", "usb_device.device_revision_bcd");
PROP_INT(d, node, i, "usb-release-id", "usb_device.version_bcd");
PROP_STR(d, node, s, "usb-serialno", "usb_device.serial");
/* class, subclass */
/* hal_device_property_set_int (d, "usb_device.device_class", 8); */
/* binding name tells us if driver is bound to interface or device */
if (((binding_name = di_binding_name(node)) != NULL) &&
(strncmp(binding_name, "usbif,", sizeof ("usbif,") - 1) == 0)) {
snprintf(if_devfs_path, sizeof (if_devfs_path), "%s:if%d", devfs_path, 0);
if ((nd = devinfo_usb_if_add(d, node, if_devfs_path, 0)) != NULL) {
d = nd;
nd = NULL;
devfs_path = if_devfs_path;
}
}
/* driver specific */
driver_name = di_driver_name (node);
if ((driver_name != NULL) && (strcmp (driver_name, "scsa2usb") == 0)) {
nd = devinfo_usb_scsa2usb_add (d, node, devfs_path);
} else {
devinfo_add_enqueue (d, devfs_path, &devinfo_usb_handler);
}
out:
if (nd != NULL) {
return (nd);
} else {
return (d);
}
}
static HalDevice *
devinfo_usb_if_add(HalDevice *parent, di_node_t node, gchar *devfs_path, int ifnum)
{
HalDevice *d = NULL;
char udi[HAL_PATH_MAX];
devinfo_add_enqueue (parent, devfs_path, &devinfo_usb_handler);
d = hal_device_new ();
devinfo_set_default_properties (d, parent, node, devfs_path);
hal_device_property_set_string (d, "info.subsystem", "usb");
hald_compute_udi (udi, sizeof (udi),
"%s_if%d", hal_device_get_udi (parent), ifnum);
hal_device_set_udi (d, udi);
hal_device_property_set_string (d, "info.product", "USB Device Interface");
/* copy parent's usb_device.* properties */
hal_device_merge_with_rewrite (d, parent, "usb.", "usb_device.");
return (d);
}
static int
walk_devlinks(di_devlink_t devlink, void *arg)
{
char **path = (char **)arg;
*path = strdup(di_devlink_path(devlink));
return (DI_WALK_TERMINATE);
}
static char *
get_devlink(di_devlink_handle_t devlink_hdl, char *path)
{
char *devlink = NULL;
(void) di_devlink_walk(devlink_hdl, NULL, path,
DI_PRIMARY_LINK, &devlink, walk_devlinks);
return (devlink);
}
static HalDevice *
devinfo_usb_scsa2usb_add(HalDevice *usbd, di_node_t node, gchar *devfs_path)
{
HalDevice *d = NULL;
di_devlink_handle_t devlink_hdl;
int major;
di_minor_t minor;
dev_t devt;
char *minor_path = NULL;
char *devlink = NULL;
char udi[HAL_PATH_MAX];
devinfo_add_enqueue (usbd, devfs_path, &devinfo_usb_handler);
if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
printf("di_devlink_init() failed\n");
return (NULL);
}
major = di_driver_major(node);
minor = DI_MINOR_NIL;
while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
devt = di_minor_devt(minor);
if (major != major(devt)) {
continue;
}
if ((minor_path = di_devfs_minor_path(minor)) == NULL) {
continue;
}
if (di_minor_type(minor) != DDM_MINOR) {
continue;
}
if (strcmp (di_minor_nodetype(minor),
"ddi_ctl:devctl:scsi") == 0) {
devlink = get_devlink(devlink_hdl, minor_path);
if (devlink == NULL) {
devlink = strdup("");
}
break;
}
di_devfs_path_free (minor_path);
minor_path = NULL;
}
di_devlink_fini (&devlink_hdl);
if (devlink == NULL) {
goto out;
}
d = hal_device_new ();
devinfo_set_default_properties (d, usbd, node, minor_path);
hal_device_property_set_string (d, "scsi_host.solaris.device", devlink);
hal_device_property_set_string (d, "info.category", "scsi_host");
hal_device_property_set_int (d, "scsi_host.host", 0);
hald_compute_udi (hald_get_gdl (), udi, sizeof (udi),
"%s/scsi_host%d", hal_device_get_udi (usbd),
hal_device_property_get_int (d, "scsi_host.host"));
hal_device_set_udi (d, udi);
hal_device_property_set_string (d, "info.product", "SCSI Host Adapter");
devinfo_add_enqueue (d, minor_path, &devinfo_usb_handler);
out:
if (devlink) {
free(devlink);
}
if (minor_path) {
di_devfs_path_free (minor_path);
}
return (d);
}
@@ -0,0 +1,23 @@
/***************************************************************************
*
* devinfo_usb.h : definitions for USB devices
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DEVINFO_USB_H
#define DEVINFO_USB_H
#include "devinfo.h"
#define bcd(a) ((((a) & 0xf000) >> 12) * 1000 + (((a) & 0xf00) >> 8) * 100 + (((a) & 0xf0) >> 4) * 10 + ((a) & 0xf))
extern DevinfoDevHandler devinfo_usb_handler;
HalDevice *devinfo_usb_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
#endif /* DEVINFO_USB_H */
@@ -0,0 +1,125 @@
/* $NetBSD: devinfo_video.c,v 1.1 2008/11/27 22:19:10 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/videoio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <paths.h>
#include <unistd.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_video.h"
#include "drvctl.h"
HalDevice *devinfo_video_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type);
DevinfoDevHandler devinfo_video_handler = {
devinfo_video_add,
NULL,
NULL,
NULL,
NULL,
NULL
};
HalDevice *
devinfo_video_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
HalDevice *d = NULL;
prop_dictionary_t dict;
const char *driver, *parent_udi;
char *videodev;
int16_t unit;
struct v4l2_capability caps;
int fd;
if (drvctl_find_device (devnode, &dict) == FALSE || dict == NULL)
return NULL;
if (prop_dictionary_get_int16 (dict, "device-unit", &unit) == false ||
prop_dictionary_get_cstring_nocopy (dict, "device-driver", &driver) == false) {
prop_object_release (dict);
return NULL;
}
prop_object_release (dict);
if (strcmp (driver, "video") != 0)
return NULL;
videodev = g_strdup_printf ("/dev/video%d", unit);
fd = open (videodev, O_RDONLY);
if (fd < 0) {
HAL_WARNING (("couldn't open %s: %s", videodev, strerror(errno)));
goto done;
}
if (ioctl (fd, VIDIOC_QUERYCAP, &caps) == -1) {
HAL_WARNING (("couldn't query %s: %s", videodev, strerror(errno)));
close (fd);
goto done;
}
close (fd);
d = hal_device_new ();
devinfo_set_default_properties (d, parent, devnode, devfs_path);
hal_device_add_capability (d, "video4linux");
if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
hal_device_add_capability (d, "video4linux.video_capture");
hal_device_property_set_string (d, "info.category", "video4linux");
hal_device_property_set_string (d, "info.subsystem", "video4linux");
hal_device_property_set_string (d, "video4linux.device", videodev);
hal_device_property_set_int (d, "video4linux.version", 2);
hal_device_property_set_string (d, "info.product", caps.card);
devinfo_add_enqueue (d, devfs_path, &devinfo_video_handler);
done:
if (videodev)
g_free (videodev);
return d;
}
@@ -0,0 +1,36 @@
/* $NetBSD: devinfo_video.h,v 1.1 2008/11/27 22:19:10 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifndef DEVINFO_VIDEO_H
#define DEVINFO_VIDEO_H
#include "devinfo.h"
extern DevinfoDevHandler devinfo_video_handler;
#endif /* DEVINFO_VIDEO_H */
+303
View File
@@ -0,0 +1,303 @@
/***************************************************************************
*
* drvctl.c : NetBSD drvctl events
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/drvctlio.h>
#include <sys/dkio.h>
#include <sys/stat.h>
#include <sys/drvctlio.h>
#include <glib.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "osspec_netbsd.h"
#include "hotplug.h"
#include "devinfo.h"
#include "devinfo_storage.h"
#include "drvctl.h"
static gboolean drvctl_iochannel_data(GIOChannel *, GIOCondition, gpointer);
static void drvctl_dev_add(gchar *);
static void drvctl_dev_remove(gchar *);
static void drvctl_dev_branch(gchar *);
static int drvctl_fd;
static GIOChannel *drvctl_iochannel;
gboolean
drvctl_init(void)
{
drvctl_fd = open (DRVCTLDEV, O_RDWR);
if (drvctl_fd == -1) {
HAL_INFO (("open(%s, O_RDWR) failed: %s", DRVCTLDEV, strerror(errno)));
return FALSE;
}
drvctl_iochannel = g_io_channel_unix_new (drvctl_fd);
if (drvctl_iochannel == NULL) {
HAL_INFO (("g_io_channel_unix_new failed"));
return FALSE;
}
g_io_add_watch (drvctl_iochannel, G_IO_IN, drvctl_iochannel_data, NULL);
return TRUE;
}
void
drvctl_fini(void)
{
HAL_INFO (("drvctl_fini"));
}
static gboolean
drvctl_iochannel_data (GIOChannel *source,
GIOCondition condition,
gpointer user_data)
{
prop_dictionary_t ev;
const char *event, *device;
int res;
HAL_INFO (("drvctl_iochannel_data"));
res = prop_dictionary_recv_ioctl (drvctl_fd, DRVGETEVENT, &ev);
if (res) {
HAL_WARNING (("DRVGETEVENT failed: %s", strerror(errno)));
return FALSE;
}
if (!prop_dictionary_get_cstring_nocopy (ev, "event", &event)) {
HAL_WARNING (("DRVGETEVENT missing \"event\" parameter"));
goto done;
}
if (!prop_dictionary_get_cstring_nocopy (ev, "device", &device)) {
HAL_WARNING (("DRVGETEVENT missing \"device\" parameter"));
goto done;
}
HAL_INFO (("DRVGETEVENT event=%s device=%s", event, device));
if (strcmp (event, "device-attach") == 0) {
drvctl_dev_add (device);
} else {
drvctl_dev_remove (device);
}
done:
prop_object_release(ev);
return TRUE;
}
static void
drvctl_dev_add(gchar *name)
{
HalDevice *parent, *d;
gchar pdevnode[512];
if (drvctl_find_parent (name, pdevnode) == FALSE) {
HAL_INFO (("dev_add: name=%s orphan", name));
parent = NULL;
} else {
parent = hal_device_store_match_key_value_string (
hald_get_gdl(), "netbsd.device", pdevnode);
if (parent == NULL)
HAL_INFO (("dev_add: name=%s but netbsd.device=%s not found", pdevnode));
}
d = devinfo_add_node (parent, name);
if (d == NULL)
HAL_WARNING (("dev_add: couldn't add %s node (parent=%p)", name, parent));
hotplug_event_process_queue ();
}
static void
drvctl_dev_remove(gchar *name)
{
HAL_INFO (("dev_remove: %s", name));
devinfo_remove_branch (name, NULL);
hotplug_event_process_queue ();
}
static void
drvctl_dev_branch(gchar *name)
{
HAL_INFO (("branch_remove: %s", name));
devinfo_remove_branch (name, NULL);
hotplug_event_process_queue ();
}
int
drvctl_list(const gchar *name, struct devlistargs *laa)
{
size_t children;
/* HAL_INFO (("drvctl_list: %s", name)); */
memset (laa, 0, sizeof (*laa));
strlcpy (laa->l_devname, name, sizeof (laa->l_devname));
if (ioctl (drvctl_fd, DRVLISTDEV, laa) == -1) {
HAL_INFO (("DRVLISTDEV/1 failed: %s", strerror(errno)));
return -1;
}
children = laa->l_children;
/* HAL_INFO (("%s: found %d children", name, children)); */
if (children == 0)
return -1;
laa->l_childname = malloc (children * sizeof (laa->l_childname[0]));
if (laa->l_childname == NULL) {
HAL_INFO (("drvctl_list couldn't allocate %d children: %s\n", children, strerror(errno)));
return -1;
}
if (ioctl (drvctl_fd, DRVLISTDEV, laa) == -1) {
HAL_INFO (("DRVLISTDEV/2 failed: %s", strerror(errno)));
return -1;
}
if (children != laa->l_children)
HAL_WARNING (("DRVLISTDEV/3 expected %d children, got %d", children, laa->l_childname));
}
gboolean
drvctl_find_device(const gchar *devnode, prop_dictionary_t *properties)
{
prop_dictionary_t command_dict;
prop_dictionary_t args_dict;
prop_dictionary_t results_dict;
int err;
command_dict = prop_dictionary_create ();
args_dict = prop_dictionary_create ();
prop_dictionary_set_cstring_nocopy (command_dict, "drvctl-command", "get-properties");
prop_dictionary_set_cstring_nocopy (args_dict, "device-name", devnode);
prop_dictionary_set (command_dict, "drvctl-arguments", args_dict);
prop_object_release (args_dict);
err = prop_dictionary_sendrecv_ioctl (command_dict, drvctl_fd,
DRVCTLCOMMAND, &results_dict);
prop_object_release (command_dict);
if (err)
return FALSE;
if (prop_dictionary_get_int8 (results_dict, "drvctl-error", &err) == false || err != 0) {
prop_object_release (results_dict);
return FALSE;
}
if (properties) {
prop_dictionary_t result_data;
result_data = prop_dictionary_get (results_dict, "drvctl-result-data");
if (result_data)
*properties = prop_dictionary_copy (result_data);
}
prop_object_release (results_dict);
return TRUE;
}
static gboolean
drvctl_find_device_with_child(const gchar *curnode, const gchar *devnode,
char *parent)
{
struct devlistargs laa;
u_int i;
if (drvctl_list (curnode, &laa) == -1)
return FALSE;
for (i = 0; i < laa.l_children; i++) {
if (strcmp (laa.l_childname[i], devnode) == 0) {
strlcpy(parent, curnode, 16);
free(laa.l_childname);
return TRUE;
}
if (drvctl_find_device_with_child (laa.l_childname[i], devnode, parent) == TRUE) {
free(laa.l_childname);
return TRUE;
}
}
if (laa.l_childname)
free(laa.l_childname);
/* HAL_INFO (("%s: couldn't find device with child %s", curnode, devnode)); */
return FALSE;
}
gboolean
drvctl_find_parent(const gchar *devnode, char *parent)
{
return drvctl_find_device_with_child("mainbus0", devnode, parent);
}
#if 0
static void
drvctl_lofi_add(gchar *devfs_path, gchar *name)
{
di_node_t node;
const char *parent_udi;
HalDevice *d, *parent;
HAL_INFO (("lofi_add: %s %s", name, devfs_path));
if ((d = hal_device_store_match_key_value_string (hald_get_gdl (),
"solaris.devfs_path", devfs_path)) == NULL) {
HAL_INFO (("device not found in GDL %s", devfs_path));
return;
}
parent_udi = hal_device_property_get_string (d, "info.parent");
if ((parent_udi == NULL) || (strlen(parent_udi) == 0)) {
HAL_INFO (("parent not found in GDL %s", parent_udi));
return;
}
if ((parent = hal_device_store_match_key_value_string (hald_get_gdl (),
"info.udi", parent_udi)) == NULL) {
HAL_INFO (("parent not found in GDL %s", parent_udi));
return;
}
if ((node = di_init (devfs_path, DINFOCPYALL)) == DI_NODE_NIL) {
HAL_INFO (("device not found in devinfo %s", devfs_path));
return;
}
HAL_INFO (("device %s parent %s", hal_device_get_udi (d), parent_udi));
devinfo_lofi_add_major (parent, node, devfs_path, NULL, TRUE, d);
di_fini (node);
hotplug_event_process_queue ();
}
static void
drvctl_lofi_remove(gchar *parent_devfs_path, gchar *name)
{
devinfo_lofi_remove_minor(parent_devfs_path, name);
hotplug_event_process_queue ();
}
#endif
+24
View File
@@ -0,0 +1,24 @@
/***************************************************************************
*
* drvctl.h : definitions for NetBSD drvctl events
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef DRVCTL_H
#define DRVCTL_H
#include <glib.h>
#include <sys/drvctlio.h>
gboolean drvctl_init(void);
void drvctl_fini(void);
int drvctl_list(const gchar *devnode, struct devlistargs *laa);
gboolean drvctl_find_parent(const gchar *devnode, char *parent);
gboolean drvctl_find_device(const gchar *devnode, prop_dictionary_t *properties);
#endif /* DRVCTL_H */
+249
View File
@@ -0,0 +1,249 @@
/* $NetBSD: envsys.c,v 1.6 2010/12/16 11:34:51 ahoka Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/envsys.h>
#include <prop/proplib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "devinfo.h"
#include "envsys.h"
#define _DEV_SYSMON "/dev/sysmon"
static int envsys_fd = -1;
static gboolean envsys_timeout(gpointer user_data);
static void envsys_handle_devnode(const char *devnode, prop_array_t properties);
static void envsys_acadapter_handler(HalDevice *d, prop_array_t properties);
static void envsys_battery_handler(HalDevice *d, prop_array_t properties);
enum battery_state {
CHARGING,
DISCHARGING,
NORMAL
};
static struct envsys_devmap {
const char *capability;
void (*handler)(HalDevice *d, prop_array_t properties);
} devmap[] = {
{ "ac_adapter", envsys_acadapter_handler },
{ "battery", envsys_battery_handler },
};
void
envsys_init(void)
{
envsys_fd = open (_DEV_SYSMON, O_RDONLY);
if (envsys_fd == -1) {
HAL_WARNING (("Couldn't open " _DEV_SYSMON ": %s", strerror(errno)));
return;
}
g_timeout_add (1000, envsys_timeout, NULL);
}
static gboolean
envsys_timeout(gpointer user_data)
{
prop_dictionary_t global_dict;
prop_array_t global_array;
prop_object_iterator_t iter;
prop_dictionary_keysym_t keysym;
int status;
status = prop_dictionary_recv_ioctl (envsys_fd, ENVSYS_GETDICTIONARY, &global_dict);
if (status) {
HAL_WARNING (("envsys_timeout: ENVSYS_GETDICTIONARY failed: %s", strerror(status)));
return FALSE;
}
global_array = prop_dictionary_all_keys (global_dict);
if (global_array == NULL) {
HAL_WARNING (("envsys_timeout: prop_dictionary_all_keys returned NULL"));
return FALSE;
}
iter = prop_array_iterator (global_array);
while ((keysym = (prop_dictionary_keysym_t)prop_object_iterator_next (iter)) != NULL) {
const char *devnode;
prop_object_t obj;
devnode = prop_dictionary_keysym_cstring_nocopy (keysym);
obj = prop_dictionary_get_keysym (global_dict, keysym);
if (obj == NULL || prop_object_type (obj) != PROP_TYPE_ARRAY)
continue;
envsys_handle_devnode (devnode, (prop_array_t)obj);
}
prop_object_iterator_release (iter);
prop_object_release (global_array);
prop_object_release (global_dict);
return TRUE;
}
static void
envsys_handle_devnode(const char *devnode, prop_array_t properties)
{
HalDevice *d;
unsigned int i;
d = hal_device_store_match_key_value_string (hald_get_gdl (), "netbsd.device", devnode);
if (d == NULL)
return;
for (i = 0; i < __arraycount (devmap); i++) {
if (!hal_device_has_capability (d, devmap[i].capability))
continue;
devmap[i].handler (d, properties);
}
return;
}
static void
envsys_acadapter_handler(HalDevice *d, prop_array_t properties)
{
prop_object_iterator_t iter;
prop_dictionary_t prop;
iter = prop_array_iterator (properties);
while ((prop = (prop_dictionary_t)prop_object_iterator_next (iter)) != NULL) {
const char *descr;
uint8_t connected;
if (prop_dictionary_get_cstring_nocopy (prop, "description", &descr) == false)
continue;
if (strcmp (descr, "connected") != 0)
continue;
if (prop_dictionary_get_uint8 (prop, "cur-value", &connected) == false)
continue;
hal_device_property_set_bool (d, "ac_adapter.present", connected);
}
prop_object_iterator_release (iter);
}
static void
envsys_battery_handler(HalDevice *d, prop_array_t properties)
{
prop_object_iterator_t iter;
prop_dictionary_t prop;
enum battery_state battstate = NORMAL;
device_property_atomic_update_begin ();
hal_device_property_set_bool (d, "battery.is_rechargeable", TRUE);
iter = prop_array_iterator (properties);
while ((prop = (prop_dictionary_t)prop_object_iterator_next (iter)) != NULL) {
const char *descr;
const char *valid;
int64_t intval;
if (prop_dictionary_get_cstring_nocopy (prop, "description", &descr) == false ||
prop_dictionary_get_int64 (prop, "cur-value", &intval) == false ||
prop_dictionary_get_cstring_nocopy (prop, "state", &valid) == false ||
strcmp (valid, "valid") != 0)
continue;
if (strcmp (descr, "present") == 0)
hal_device_property_set_bool (d, "battery.present", intval);
else if (strcmp (descr, "design cap") == 0)
hal_device_property_set_int (d, "battery.charge_level.design", intval);
else if (strcmp (descr, "last full cap") == 0)
hal_device_property_set_int (d, "battery.charge_level.last_full", intval);
else if (strcmp (descr, "charge") == 0) {
int64_t maxval;
const char *type;
if (prop_dictionary_get_cstring_nocopy (prop, "type", &type))
if (strcmp (type, "Ampere hour") == 0) {
/* HAL 0.5.12 spec discourages this, but what can we do about it? */
hal_device_property_set_string (d, "battery.charge_level.unit", "mAh");
}
hal_device_property_set_int (d, "battery.charge_level.current", intval);
if (prop_dictionary_get_int64 (prop, "max-value", &maxval) && maxval > 0)
hal_device_property_set_int (d, "battery.charge_level.percentage", intval * 100 / maxval);
else
hal_device_property_set_int (d, "battery.charge_level.percentage", 0);
}
else if (strcmp (descr, "charge rate") == 0) {
battstate = CHARGING;
hal_device_property_set_int (d, "battery.charge_level.rate", intval);
} else if (strcmp (descr, "discharge rate") == 0) {
battstate = DISCHARGING;
hal_device_property_set_int (d, "battery.charge_level.rate", intval);
}
}
switch (battstate) {
case NORMAL:
hal_device_property_set_bool (d, "battery.rechargeable.is_charging", FALSE);
hal_device_property_set_bool (d, "battery.rechargeable.is_discharging", FALSE);
hal_device_property_set_int (d, "battery.charge_level.rate", 0);
break;
case CHARGING:
hal_device_property_set_bool (d, "battery.rechargeable.is_charging", TRUE);
hal_device_property_set_bool (d, "battery.rechargeable.is_discharging", FALSE);
break;
case DISCHARGING:
hal_device_property_set_bool (d, "battery.rechargeable.is_charging", FALSE);
hal_device_property_set_bool (d, "battery.rechargeable.is_discharging", TRUE);
break;
}
device_property_atomic_update_end ();
prop_object_iterator_release (iter);
}
+36
View File
@@ -0,0 +1,36 @@
/* $NetBSD: envsys.h,v 1.1 2008/11/27 01:45:00 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifndef DEVINFO_ENVSYS_H
#define DEVINFO_ENVSYS_H
#include "devinfo.h"
void envsys_init(void);
#endif /* DEVINFO_ENVSYS_H */
@@ -0,0 +1,821 @@
/***************************************************************************
* CVSID: $Id: hal-file-monitor.c,v 1.1 2008/11/25 23:10:23 jmcneill Exp $
*
* hal-file-monitor.c: Kqueue-based file monitor
*
* Copyright (C) 2007 Joe Marcus Clarke <marcus@FreeBSD.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include "../hal-file-monitor.h"
#define HAL_FILE_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), HAL_TYPE_FILE_MONITOR, HalFileMonitorPrivate))
#define VN_NOTE_CHANGED (NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK)
#define VN_NOTE_DELETED (NOTE_DELETE | NOTE_REVOKE)
#define VN_NOTE_ALL (VN_NOTE_CHANGED | VN_NOTE_DELETED | NOTE_RENAME)
typedef struct
{
char *path;
int omask;
gboolean isdir;
HalFileMonitorNotifyFunc func;
gpointer udata;
GHashTable *dir_contents;
} FileKqueueData;
typedef struct
{
char *path;
time_t atime;
gboolean owner;
HalFileMonitorNotifyFunc func;
gpointer udata;
} FileAccessData;
struct HalFileMonitorPrivate
{
int kqueue_fd;
guint io_watch;
guint access_source;
gboolean initialized_kqueue;
GHashTable *fd_to_kdata;
GHashTable *fd_to_adata;
};
G_DEFINE_TYPE (HalFileMonitor, hal_file_monitor, G_TYPE_OBJECT)
static gpointer monitor_object = NULL;
static void hal_file_monitor_finalize (GObject *object);
static GHashTable *get_dir_contents (const char *path);
static GHashTable *diff_dir_contents (FileKqueueData *data, GSList **added, GSList **removed);
static int hal_mask_to_kmask (int mask);
static void monitor_release_kdata (HalFileMonitor *monitor, int fd, FileKqueueData *data);
static void monitor_release_adata (HalFileMonitor *monitor, int fd, FileAccessData *data);
static gboolean remove_kdata_foreach (gpointer fd, FileKqueueData *data, HalFileMonitor *monitor);
static gboolean remove_adata_foreach (gpointer fd, FileAccessData *data, HalFileMonitor *monitor);
static void hal_file_monitor_remove_kdata (HalFileMonitor *monitor, guint id);
static void hal_file_monitor_remove_adata (HalFileMonitor *monitor, guint id);
static void setup_monitor (HalFileMonitor *monitor);
static void close_monitor (HalFileMonitor *monitor);
static gboolean hal_file_access_monitor (gpointer data);
/*static char *fflags_to_str (int fflags);*/
static void emit_monitor_event (HalFileMonitor *monitor, HalFileMonitorEvent event, const char *path, HalFileMonitorNotifyFunc func, gpointer udata);
static gboolean handle_kqueue_event (GIOChannel *source, GIOCondition condition, gpointer udata);
GQuark
hal_file_monitor_error_quark (void)
{
static GQuark ret = 0;
if (ret == 0)
{
ret = g_quark_from_static_string ("hal_file_monitor_error");
}
return ret;
}
static int
hal_mask_to_kmask (int mask)
{
int kmask = 0;
if (mask & HAL_FILE_MONITOR_EVENT_CREATE)
{
kmask |= NOTE_WRITE | NOTE_LINK;
}
if (mask & HAL_FILE_MONITOR_EVENT_DELETE)
{
kmask |= NOTE_WRITE | NOTE_LINK | VN_NOTE_DELETED;
}
if (mask & HAL_FILE_MONITOR_EVENT_CHANGE)
{
kmask |= VN_NOTE_CHANGED;
}
return kmask;
}
guint
hal_file_monitor_add_notify (HalFileMonitor *monitor,
const char *path,
int mask,
HalFileMonitorNotifyFunc notify_func,
gpointer data)
{
struct kevent ev;
struct stat sb;
int fd;
int id = 0;
if (! monitor->priv->initialized_kqueue)
{
return id;
}
fd = open (path, O_RDONLY);
if (fd < 0)
{
return id;
}
if (fstat (fd, &sb) == -1)
{
close (fd);
return id;
}
if (mask & HAL_FILE_MONITOR_EVENT_ACCESS)
{
FileAccessData *adata;
adata = g_new0 (FileAccessData, 1);
adata->path = g_strdup (path);
adata->atime = sb.st_atime;
adata->func = notify_func;
adata->udata = data;
if (mask == HAL_FILE_MONITOR_EVENT_ACCESS)
{
/* We will close the file descriptor when we release the adata. */
adata->owner = TRUE;
}
g_hash_table_insert (monitor->priv->fd_to_adata,
GINT_TO_POINTER (fd),
adata);
id = fd;
}
if ((mask & HAL_FILE_MONITOR_EVENT_CREATE) ||
(mask & HAL_FILE_MONITOR_EVENT_DELETE) ||
(mask & HAL_FILE_MONITOR_EVENT_CHANGE))
{
FileKqueueData *kdata;
int kmask;
gboolean isdir;
kmask = hal_mask_to_kmask (mask);
isdir = (sb.st_mode & S_IFDIR) != 0;
if (! isdir && mask == HAL_FILE_MONITOR_EVENT_CREATE)
{
/* We can't monitor creation on a file. */
goto done;
}
kdata = g_new0 (FileKqueueData, 1);
kdata->path = g_strdup (path);
kdata->omask = mask;
kdata->isdir = isdir;
kdata->func = notify_func;
kdata->udata = data;
if (isdir)
{
kdata->dir_contents = get_dir_contents (path);
}
/*g_warning ("XXX: Adding event with mask %s", fflags_to_str (kmask));*/
EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
kmask, 0, monitor);
if (kevent (monitor->priv->kqueue_fd, &ev, 1, NULL, 0, NULL) < 0)
{
monitor_release_kdata (monitor, fd, kdata);
goto done;
}
g_hash_table_insert (monitor->priv->fd_to_kdata,
GINT_TO_POINTER (fd),
kdata);
id = fd;
}
done:
return id;
}
static void
hal_file_monitor_remove_kdata (HalFileMonitor *monitor,
guint id)
{
FileKqueueData *kdata;
kdata = (FileKqueueData *) g_hash_table_lookup (monitor->priv->fd_to_kdata, GINT_TO_POINTER (id));
if (kdata)
{
g_hash_table_remove (monitor->priv->fd_to_kdata, GINT_TO_POINTER (id));
monitor_release_kdata (monitor, id, kdata);
}
}
static void
hal_file_monitor_remove_adata (HalFileMonitor *monitor,
guint id)
{
FileAccessData *adata;
adata = (FileAccessData *) g_hash_table_lookup (monitor->priv->fd_to_adata, GINT_TO_POINTER (id));
if (adata)
{
g_hash_table_remove (monitor->priv->fd_to_adata, GINT_TO_POINTER (id));
monitor_release_adata (monitor, id, adata);
}
}
void
hal_file_monitor_remove_notify (HalFileMonitor *monitor,
guint id)
{
if (! monitor->priv->initialized_kqueue)
return;
hal_file_monitor_remove_kdata (monitor, id);
hal_file_monitor_remove_adata (monitor, id);
}
static void
monitor_release_kdata (HalFileMonitor *monitor,
int fd,
FileKqueueData *data)
{
g_free (data->path);
data->path = NULL;
if (data->dir_contents)
{
g_hash_table_remove_all (data->dir_contents);
g_hash_table_destroy (data->dir_contents);
}
data->dir_contents = NULL;
close (fd);
g_free (data);
}
static void
monitor_release_adata (HalFileMonitor *monitor,
int fd,
FileAccessData *data)
{
g_free (data->path);
data->path = NULL;
if (data->owner)
{
close (fd);
}
g_free (data);
}
static gboolean
remove_kdata_foreach (gpointer fd,
FileKqueueData *data,
HalFileMonitor *monitor)
{
monitor_release_kdata (monitor, GPOINTER_TO_INT (fd), data);
return TRUE;
}
static gboolean
remove_adata_foreach (gpointer fd,
FileAccessData *data,
HalFileMonitor *monitor)
{
monitor_release_adata (monitor, GPOINTER_TO_INT (fd), data);
return TRUE;
}
static void
close_monitor (HalFileMonitor *monitor)
{
if (! monitor->priv->initialized_kqueue)
{
return;
}
monitor->priv->initialized_kqueue = FALSE;
g_hash_table_foreach_remove (monitor->priv->fd_to_kdata,
(GHRFunc) remove_kdata_foreach,
monitor);
g_hash_table_foreach_remove (monitor->priv->fd_to_adata,
(GHRFunc) remove_adata_foreach,
monitor);
if (monitor->priv->io_watch)
{
g_source_remove (monitor->priv->io_watch);
}
monitor->priv->io_watch = 0;
if (monitor->priv->access_source)
{
g_source_remove (monitor->priv->access_source);
}
monitor->priv->access_source = 0;
if (monitor->priv->kqueue_fd > -1)
{
close (monitor->priv->kqueue_fd);
}
monitor->priv->kqueue_fd = -1;
}
static gboolean
hal_file_access_monitor (gpointer data)
{
HalFileMonitor *monitor;
GList *keys, *l;
g_return_val_if_fail (HAL_IS_FILE_MONITOR (data), FALSE);
monitor = HAL_FILE_MONITOR (data);
g_return_val_if_fail (monitor->priv != NULL, FALSE);
keys = g_hash_table_get_keys (monitor->priv->fd_to_adata);
for (l = keys; l != NULL; l = l->next)
{
FileAccessData *adata;
struct stat sb;
int fd;
fd = GPOINTER_TO_INT (l->data);
adata = g_hash_table_lookup (monitor->priv->fd_to_adata,
l->data);
if (! adata)
{
continue;
}
if (fstat (fd, &sb) == -1)
{
g_warning ("Failed to stat %s: %s", adata->path, g_strerror (errno));
hal_file_monitor_remove_adata (monitor, fd);
continue;
}
if (sb.st_atime != adata->atime)
{
adata->atime = sb.st_atime;
emit_monitor_event (monitor, HAL_FILE_MONITOR_EVENT_ACCESS, adata->path, adata->func, adata->udata);
}
}
return TRUE;
}
static void
setup_monitor (HalFileMonitor *monitor)
{
GIOChannel *io_channel;
int fd;
if (monitor->priv->initialized_kqueue)
{
return;
}
if ((fd = kqueue ()) < 0)
{
g_warning ("Failed to initialize kqueue: %s",
g_strerror (errno));
return;
}
monitor->priv->kqueue_fd = fd;
monitor->priv->fd_to_kdata = g_hash_table_new (g_direct_hash,
g_direct_equal);
monitor->priv->fd_to_adata = g_hash_table_new (g_direct_hash,
g_direct_equal);
io_channel = g_io_channel_unix_new (fd);
monitor->priv->io_watch = g_io_add_watch (io_channel,
G_IO_IN|G_IO_PRI,
(GIOFunc) handle_kqueue_event,
monitor);
g_io_channel_unref (io_channel);
monitor->priv->access_source = g_timeout_add (1000, (GSourceFunc) hal_file_access_monitor, monitor);
monitor->priv->initialized_kqueue = TRUE;
}
static void
hal_file_monitor_init (HalFileMonitor *monitor)
{
monitor->priv = HAL_FILE_MONITOR_GET_PRIVATE (monitor);
setup_monitor (monitor);
}
static void
hal_file_monitor_class_init (HalFileMonitorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = hal_file_monitor_finalize;
g_type_class_add_private (klass, sizeof (HalFileMonitorPrivate));
}
static void
hal_file_monitor_finalize (GObject *object)
{
HalFileMonitor *monitor;
g_return_if_fail (object != NULL);
g_return_if_fail (HAL_IS_FILE_MONITOR (object));
monitor = HAL_FILE_MONITOR (object);
g_return_if_fail (monitor->priv != NULL);
close_monitor (monitor);
g_hash_table_destroy (monitor->priv->fd_to_kdata);
g_hash_table_destroy (monitor->priv->fd_to_adata);
G_OBJECT_CLASS (hal_file_monitor_parent_class)->finalize (object);
}
HalFileMonitor *
hal_file_monitor_new (void)
{
if (monitor_object != NULL)
{
g_object_ref (monitor_object);
}
else
{
monitor_object = g_object_new (HAL_TYPE_FILE_MONITOR, NULL);
g_object_add_weak_pointer (monitor_object,
(gpointer *) &monitor_object);
}
return HAL_FILE_MONITOR (monitor_object);
}
/*
static char *
fflags_to_str (int fflags)
{
GString *out;
out = g_string_new (NULL);
if (fflags & NOTE_WRITE)
{
g_string_append (out, " WRITE ");
}
if (fflags & NOTE_EXTEND)
{
g_string_append (out, " EXTEND ");
}
if (fflags & NOTE_ATTRIB)
{
g_string_append (out, " ATTRIB ");
}
if (fflags & NOTE_LINK)
{
g_string_append (out, " LINK ");
}
if (fflags & NOTE_DELETE)
{
g_string_append (out, " DELETE ");
}
if (fflags & NOTE_REVOKE)
{
g_string_append (out, " REVOKE ");
}
if (fflags & NOTE_RENAME)
{
g_string_append (out, " RENAME ");
}
return g_string_free (out, FALSE);
}
*/
static void
emit_monitor_event (HalFileMonitor *monitor,
HalFileMonitorEvent event,
const char *path,
HalFileMonitorNotifyFunc func,
gpointer udata)
{
if (func)
{
func (monitor, event, path, udata);
}
}
static gboolean
handle_kqueue_event (GIOChannel *source,
GIOCondition condition,
gpointer udata)
{
struct kevent ev;
struct timespec timeout = { 0, 0 };
int nevents;
HalFileMonitor *monitor;
g_return_val_if_fail (HAL_IS_FILE_MONITOR (udata), FALSE);
monitor = HAL_FILE_MONITOR (udata);
g_return_val_if_fail (monitor->priv != NULL, FALSE);
g_return_val_if_fail (monitor->priv->kqueue_fd > -1, FALSE);
nevents = kevent (monitor->priv->kqueue_fd, NULL, 0, &ev, 1, &timeout);
if (nevents == 1)
{
int fd;
FileKqueueData *data;
fd = ev.ident;
data = g_hash_table_lookup (monitor->priv->fd_to_kdata,
GINT_TO_POINTER (fd));
if (! data)
{
/* The monitor may have been deleted. */
return TRUE;
}
if ((data->omask & HAL_FILE_MONITOR_EVENT_DELETE) &&
(ev.fflags & VN_NOTE_DELETED))
{
emit_monitor_event (monitor, HAL_FILE_MONITOR_EVENT_DELETE, data->path, data->func, data->udata);
hal_file_monitor_remove_kdata (monitor, fd);
return TRUE;
}
if ((data->omask & HAL_FILE_MONITOR_EVENT_CREATE) ||
(data->omask & HAL_FILE_MONITOR_EVENT_DELETE))
{
if (data->isdir)
{
GSList *added = NULL;
GSList *removed = NULL;
GSList *l;
GHashTable *table;
gboolean found_change = FALSE;
table = diff_dir_contents (data, &added, &removed);
if (data->omask & HAL_FILE_MONITOR_EVENT_CREATE)
{
for (l = added; l != NULL; l = l->next)
{
char *path;
path = g_build_filename (data->path, l->data, NULL);
emit_monitor_event (monitor, HAL_FILE_MONITOR_EVENT_CREATE, path, data->func, data->udata);
g_free (path);
}
}
if (data->omask & HAL_FILE_MONITOR_EVENT_DELETE)
{
for (l = removed; l != NULL; l = l->next)
{
char *path;
path = g_build_filename (data->path, l->data, NULL);
emit_monitor_event (monitor, HAL_FILE_MONITOR_EVENT_DELETE, path, data->func, data->udata);
g_free (path);
}
}
if (added || removed)
{
g_hash_table_remove_all (data->dir_contents);
g_hash_table_destroy (data->dir_contents);
data->dir_contents = table;
found_change = TRUE;
}
else if (table)
{
g_hash_table_remove_all (table);
g_hash_table_destroy (table);
}
else
{
hal_file_monitor_remove_kdata (monitor, fd);
}
if (added)
{
g_slist_foreach (added, (GFunc) g_free, NULL);
g_slist_free (added);
}
if (removed)
{
g_slist_foreach (removed, (GFunc) g_free, NULL);
g_slist_free (removed);
}
if (found_change)
{
return TRUE;
}
}
}
if (data->omask & HAL_FILE_MONITOR_EVENT_CHANGE)
{
emit_monitor_event (monitor, HAL_FILE_MONITOR_EVENT_CHANGE, data->path, data->func, data->udata);
}
}
else
{
g_warning ("Failed to read from kqueue: %s", g_strerror (errno));
return FALSE;
}
return TRUE;
}
static GHashTable *
diff_dir_contents (FileKqueueData *data,
GSList **added,
GSList **removed)
{
GDir *dir;
GError *err = NULL;
GHashTable *table = NULL;
dir = g_dir_open (data->path, 0, &err);
if (dir)
{
const char *fname;
table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
while ((fname = g_dir_read_name (dir)))
{
if (added)
{
if (! g_hash_table_lookup (data->dir_contents, fname))
{
*added = g_slist_prepend (*added, g_strdup (fname));
}
}
g_hash_table_insert (table, g_strdup (fname), GINT_TO_POINTER (TRUE));
}
g_dir_close (dir);
if (removed)
{
GList *keys;
GList *l;
keys = g_hash_table_get_keys (data->dir_contents);
for (l = keys; l != NULL; l = l->next)
{
if (! g_hash_table_lookup (table, l->data))
{
*removed = g_slist_prepend (*removed, g_strdup (l->data));
}
}
}
}
else
{
g_warning ("Failed to open directory: %s", err->message);
g_error_free (err);
}
return table;
}
static GHashTable *
get_dir_contents (const char *path)
{
GDir *dir;
GError *err = NULL;
GHashTable *table = NULL;
dir = g_dir_open (path, 0, &err);
if (dir)
{
const char *fname;
table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
while ((fname = g_dir_read_name (dir)))
{
g_hash_table_insert (table, g_strdup (fname), GINT_TO_POINTER (TRUE));
}
g_dir_close (dir);
}
else
{
g_warning ("Failed to open directory %s: %s\n", path, err->message);
g_error_free (err);
}
return table;
}
/*
static void
print_event (HalFileMonitor *monitor,
HalFileMonitorEvent event,
const char *path,
gpointer udata)
{
GString *ename;
ename = g_string_new (NULL);
if (event & HAL_FILE_MONITOR_EVENT_ACCESS)
{
g_string_append (ename, "ACCESS ");
}
if (event & HAL_FILE_MONITOR_EVENT_CREATE)
{
g_string_append (ename, "CREATE ");
}
if (event & HAL_FILE_MONITOR_EVENT_DELETE)
{
g_string_append (ename, "DELETE ");
}
if (event & HAL_FILE_MONITOR_EVENT_CHANGE)
{
g_string_append (ename, "CHANGE ");
}
printf("Received event for %s: %s\n", path, g_string_free (ename, FALSE));
}
int
main(void)
{
const char *path = "/tmp/kqueue.d";
guint watch;
GMainLoop *loop;
HalFileMonitor *monitor;
g_type_init ();
monitor = hal_file_monitor_new ();
hal_file_monitor_add_notify (monitor, path, HAL_FILE_MONITOR_EVENT_CREATE|HAL_FILE_MONITOR_EVENT_DELETE|HAL_FILE_MONITOR_EVENT_CHANGE,print_event, NULL);
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
return 0;
}
*/
+204
View File
@@ -0,0 +1,204 @@
/***************************************************************************
*
* hotplug.c : HAL-internal hotplug events
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#pragma ident "@(#)hotplug.c 1.2 06/10/13 SMI"
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../device_info.h"
#include "osspec_netbsd.h"
#include "hotplug.h"
#include "devinfo.h"
/** Queue of ordered hotplug events */
GQueue *hotplug_event_queue;
/** List of HotplugEvent objects we are currently processing */
GSList *hotplug_events_in_progress = NULL;
static void hotplug_event_begin (HotplugEvent *hotplug_event);
void
hotplug_event_end (void *end_token)
{
HotplugEvent *hotplug_event = (HotplugEvent *) end_token;
hotplug_events_in_progress = g_slist_remove (hotplug_events_in_progress, hotplug_event);
g_free (hotplug_event);
hotplug_event_process_queue ();
}
static void
hotplug_event_begin_devfs_add (HotplugEvent *hotplug_event, HalDevice *d)
{
HalDevice *parent;
const gchar *parent_udi;
void (*begin_add_func) (HalDevice *, HalDevice *, DevinfoDevHandler *, void *);
if (d != NULL) {
/* XXX */
HAL_ERROR (("devpath %s already present in store, ignore event", hotplug_event->un.devfs.devfs_path));
hotplug_event_end ((void *) hotplug_event);
return;
}
/* find parent */
parent_udi = hal_device_property_get_string (hotplug_event->d, "info.parent");
if (parent_udi == NULL || strlen(parent_udi) == 0) {
parent = NULL;
HAL_INFO (("no info.parent parameter found"));
} else {
parent = hal_device_store_match_key_value_string (hald_get_gdl (), "info.udi", parent_udi);
if (parent == NULL)
HAL_INFO (("no info.udi=%s parameter found", parent_udi));
}
if (parent == NULL) {
gchar pdevnode[512];
HAL_INFO (("couldn't look up %s parent by udi, trying netbsd.device", hotplug_event->un.devfs.devfs_path));
if (drvctl_find_parent (hotplug_event->un.devfs.devfs_path, pdevnode) == TRUE)
parent = hal_device_store_match_key_value_string (hald_get_gdl (), "netbsd.device", pdevnode);
}
/* only root node is allowed to be orphan */
if (parent == NULL) {
if (strcmp(hotplug_event->un.devfs.devfs_path, "mainbus0") != 0) {
HAL_ERROR (("Parent is NULL devfs_path=%s parent_udi=%s", hotplug_event->un.devfs.devfs_path, parent_udi ? parent_udi : "<null>"));
hotplug_event_end ((void *) hotplug_event);
return;
}
}
/* children of ignored parent should be ignored */
if (hal_device_property_get_bool (parent, "info.ignore")) {
HAL_INFO (("parent ignored %s", parent_udi));
hotplug_event_end ((void *) hotplug_event);
return;
}
/* custom or generic add function */
begin_add_func = hotplug_event->un.devfs.handler->hotplug_begin_add;
if (begin_add_func == NULL) {
begin_add_func = hotplug_event_begin_add_devinfo;
}
begin_add_func (hotplug_event->d,
parent,
hotplug_event->un.devfs.handler,
(void *) hotplug_event);
}
static void
hotplug_event_begin_devfs_remove (HotplugEvent *hotplug_event, HalDevice *d)
{
if (d == NULL) {
HAL_ERROR (("devpath %s not present in store, ignore event", hotplug_event->un.devfs.devfs_path));
hotplug_event_end ((void *) hotplug_event);
return;
}
HAL_INFO (("hotplug_event_begin_devfs_remove %s", hal_device_get_udi (d)));
hotplug_event_begin_remove_devinfo(d,
hotplug_event->un.devfs.devfs_path,
(void *) hotplug_event);
}
static void
hotplug_event_begin_devfs (HotplugEvent *hotplug_event)
{
HalDevice *d;
HAL_INFO (("hotplug_event_begin_devfs: %s", hotplug_event->un.devfs.devfs_path));
d = hal_device_store_match_key_value_string (hald_get_gdl (),
"netbsd.device",
hotplug_event->un.devfs.devfs_path);
if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
hotplug_event_begin_devfs_add (hotplug_event, d);
} else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
hotplug_event_begin_devfs_remove (hotplug_event, d);
} else {
hotplug_event_end ((void *) hotplug_event);
}
}
static void
hotplug_event_begin (HotplugEvent *hotplug_event)
{
switch (hotplug_event->type) {
case HOTPLUG_EVENT_DEVFS:
hotplug_event_begin_devfs (hotplug_event);
break;
default:
HAL_ERROR (("Unknown hotplug event type %d", hotplug_event->type));
hotplug_event_end ((void *) hotplug_event);
break;
}
}
void
hotplug_event_enqueue (HotplugEvent *hotplug_event, int front)
{
if (hotplug_event_queue == NULL)
hotplug_event_queue = g_queue_new ();
if (front) {
g_queue_push_head (hotplug_event_queue, hotplug_event);
} else {
g_queue_push_tail (hotplug_event_queue, hotplug_event);
}
}
void
hotplug_event_process_queue (void)
{
HotplugEvent *hotplug_event;
if (hotplug_events_in_progress == NULL &&
(hotplug_event_queue == NULL || g_queue_is_empty (hotplug_event_queue))) {
hotplug_queue_now_empty ();
goto out;
}
/* do not process events if some other event is in progress */
if (hotplug_events_in_progress != NULL && g_slist_length (hotplug_events_in_progress) > 0)
goto out;
hotplug_event = g_queue_pop_head (hotplug_event_queue);
if (hotplug_event == NULL)
goto out;
hotplug_events_in_progress = g_slist_append (hotplug_events_in_progress, hotplug_event);
hotplug_event_begin (hotplug_event);
out:
;
}
+58
View File
@@ -0,0 +1,58 @@
/***************************************************************************
*
* hotplug.h : definitions for HAL-internal hotplug events
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef HOTPLUG_H
#define HOTPLUG_H
#include <glib.h>
#include "../device.h"
#include "../util.h"
#include "devinfo.h"
typedef enum {
HOTPLUG_ACTION_ADD,
HOTPLUG_ACTION_REMOVE,
} HotplugActionType;
typedef enum {
HOTPLUG_EVENT_DEVFS = 0,
} HotplugEventType;
/** Data structure representing a hotplug event; also used for
* coldplugging.
*/
typedef struct
{
HotplugActionType action; /**< Whether the event is add or remove */
HotplugEventType type; /**< Type of hotplug event */
HalDevice *d;
union {
struct {
char devfs_path[HAL_PATH_MAX];
DevinfoDevHandler *handler;
} devfs;
} un;
} HotplugEvent;
void hotplug_event_enqueue (HotplugEvent *event, int front);
void hotplug_event_process_queue (void);
void hotplug_event_end (void *end_token);
void hotplug_queue_now_empty (void);
#endif /* HOTPLUG_H */
+183
View File
@@ -0,0 +1,183 @@
/***************************************************************************
*
* osspec.c : NetBSD HAL backend entry points
*
* Copyright 2008 Jared D. McNeill <jmcneill@NetBSD.org>
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "osspec_netbsd.h"
#include "hotplug.h"
#include "drvctl.h"
#include "envsys.h"
#include "devinfo.h"
#include "devinfo_storage.h"
static void mntinfo_event_init ();
static gboolean mntinfo_timeout (gpointer user_data);
HalFileMonitor *
osspec_get_file_monitor (void)
{
#warning Please implement
return NULL;
}
void
osspec_init (void)
{
ids_init ();
mntinfo_event_init ();
envsys_init ();
}
void
osspec_privileged_init (void)
{
drvctl_init ();
}
void
hotplug_queue_now_empty (void)
{
if (hald_is_initialising) {
osspec_probe_done ();
}
}
void
osspec_probe (void)
{
/* add entire device tree */
devinfo_add (NULL, "mainbus0");
/* start processing events */
hotplug_event_process_queue ();
}
gboolean
osspec_device_rescan (HalDevice *d)
{
return (devinfo_device_rescan (d));
}
gboolean
osspec_device_reprobe (HalDevice *d)
{
return FALSE;
}
DBusHandlerResult
osspec_filter_function (DBusConnection *connection, DBusMessage *message, void *user_data)
{
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/**
* hal_util_find_closest_ancestor:
* @devfs_path: Path into devfs, e.g. /pci@0,0/pci1025,57@10,2/storage@1
*
* Returns: Parent Hal Device Object or #NULL if there is none
*
* Find the closest ancestor by looking at devfs paths
*/
HalDevice *
hal_util_find_closest_ancestor (const gchar *devfs_path, gchar **ancestor_devfs_path, gchar **hotplug_devfs_path)
{
gchar buf[512];
gchar c;
HalDevice *parent;
parent = NULL;
if (drvctl_find_parent (devfs_path, buf) == FALSE)
return NULL;
HAL_INFO (("hal_util_find_closest_ancestor: devnode=%s parent=%s\n", devfs_path, buf));
parent = hal_device_store_match_key_value_string (hald_get_gdl (),
"netbsd.device",
buf);
if (parent != NULL) {
if (ancestor_devfs_path != NULL) {
*ancestor_devfs_path = g_strdup (buf);
}
if (hotplug_devfs_path != NULL) {
*hotplug_devfs_path = g_strdup (buf);
}
}
return parent;
}
char *
dsk_to_rdsk(char *dsk)
{
int len, pos;
char *p;
char *rdsk;
if ((len = strlen (dsk)) < sizeof ("/dev/AANA") - 1) {
return (strdup(""));
}
p = strstr (dsk, "/dev/");
if (p == NULL) {
return (strdup(""));
}
pos = (uintptr_t)p - (uintptr_t)dsk;
if ((rdsk = (char *)calloc (len + 2, 1)) != NULL) {
strncpy (rdsk, dsk, pos + 1);
rdsk[pos + 1] = 'r';
strcpy (rdsk + pos + 2, dsk + pos + 1);
}
return (rdsk);
}
/*
* Setup to watch mntinfo changes
*/
static void
mntinfo_event_init ()
{
g_timeout_add (1000, mntinfo_timeout, NULL);
}
static gboolean
mntinfo_timeout (gpointer user_data)
{
if (!hald_is_initialising)
vfsstat_event (NULL);
return TRUE;
}
void
osspec_refresh_mount_state_for_block_device (HalDevice *d)
{
vfsstat_event (d);
}
@@ -0,0 +1,21 @@
/***************************************************************************
*
* osspec_netbsd.h : definitions for NetBSD HAL backend
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef OSSPEC_NETBSD_H
#define OSSPEC_NETBSD_H
#include <glib.h>
void hotplug_queue_now_empty (void);
HalDevice *hal_util_find_closest_ancestor (const gchar *devfs_path, gchar **ancestor_devfs_path, gchar **hotplug_devfs_path);
char *dsk_to_rdsk(char *);
#endif /* OSSPEC_NETBSD_H */
@@ -0,0 +1,20 @@
AM_CPPFLAGS = \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-DPACKAGE_LOCALSTATEDIR=\""$(localstatedir)"\" \
-I$(top_srcdir) -I$(top_srcdir)/hald -I$(top_srcdir)/libhal -I$(top_srcdir)/libhal-storage \
@GLIB_CFLAGS@ @DBUS_CFLAGS@
if HALD_COMPILE_NETBSD
libexec_PROGRAMS = hald-probe-storage hald-probe-volume
endif
hald_probe_storage_SOURCES = probe-storage.c cdutils.c fsutils.c ../../logger.c
hald_probe_storage_LDADD = $(top_builddir)/libhal/libhal.la @GLIB_LIBS@ -lprop
hald_probe_volume_SOURCES = probe-volume.c cdutils.c fsutils.c ../../logger.c
hald_probe_volume_LDADD = $(top_builddir)/libhal/libhal.la @GLIB_LIBS@ -lprop
@@ -0,0 +1,379 @@
/***************************************************************************
*
* cdutils.h : CD/DVD utilities
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/scsiio.h>
#include <sys/disklabel.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipi_cd.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/dkio.h>
#include <libintl.h>
#include <logger.h>
#include "cdutils.h"
#define RQLEN 32
#define SENSE_KEY(rqbuf) (rqbuf[2]) /* scsi error category */
#define ASC(rqbuf) (rqbuf[12]) /* additional sense code */
#define ASCQ(rqbuf) (rqbuf[13]) /* ASC qualifier */
#define GET16(a) (((a)[0] << 8) | (a)[1])
#define GET32(a) (((a)[0] << 24) | ((a)[1] << 16) | ((a)[2] << 8) | (a)[3])
bool
scsi_command (int fd, void *cmd, size_t cmdlen, void *data, size_t datalen,
int timeout, int flags)
{
scsireq_t req;
memset(&req, 0, sizeof(req));
memcpy(req.cmd, cmd, cmdlen);
req.cmdlen = cmdlen;
req.databuf = data;
req.datalen = datalen;
req.timeout = timeout;
req.flags = flags;
req.senselen = SENSEBUFLEN;
if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
return false;
if (req.retsts == SCCMD_OK)
return true;
return false;
}
int
mode_sense(int fd, u_char pc, int dbd, int page_len, u_char *buffer)
{
struct scsi_mode_sense_10 sms10;
memset(&sms10, 0, sizeof(sms10));
sms10.opcode = SCSI_MODE_SENSE_10;
if (dbd)
sms10.byte2 = SMS_DBD; /* no block descriptors */
sms10.page = pc;
sms10.length[0] = (page_len >> 8) & 0xff;
sms10.length[1] = page_len & 0xff;
return (scsi_command(fd, &sms10, sizeof(sms10), buffer, page_len, 10000, SCCMD_READ) == true);
}
/*
* will get the mode page only i.e. will strip off the header.
*/
int
get_mode_page(int fd, int page_no, int pc, int buf_len, u_char *buffer, int *plen)
{
int ret;
u_char byte2;
u_char buf[256];
u_int header_len, page_len, copy_cnt;
byte2 = (u_char)(((pc << 6) & 0xC0) | (page_no & 0x3f));
/* Ask 254 bytes only to make our IDE driver happy */
if ((ret = mode_sense(fd, byte2, 1, 254, buf)) == 0) {
return (0);
}
header_len = 8 + GET16(&buf[6]);
page_len = buf[header_len + 1] + 2;
copy_cnt = ((int)page_len > buf_len) ? (u_int)buf_len : page_len;
(void) memcpy(buffer, &buf[header_len], copy_cnt);
if (plen) {
*plen = page_len;
}
return (1);
}
/* Get information about the Logical Unit's capabilities */
int
get_configuration(int fd, uint16_t feature, int bufsize, u_char *buf)
{
struct scsipi_get_configuration conf;
memset(&conf, 0, sizeof(conf));
conf.opcode = GET_CONFIGURATION;
conf.request_type = 0x2;
conf.start_at_feature[0] = (feature >> 8) & 0xff;
conf.start_at_feature[1] = feature & 0xff;
conf.data_len[0] = (bufsize >> 8) & 0xff;
conf.data_len[1] = bufsize & 0xff;
return (scsi_command(fd, &conf, sizeof(conf), buf, bufsize, 10000, SCCMD_READ) == true);
}
bool
get_current_profile(int fd, int *profile)
{
u_char smallbuf[4];
size_t buflen;
u_char *bufp;
int ret = false;
/* first determine amount of memory needed to hold all profiles */
if (get_configuration(fd, 0, 4, &smallbuf[0])) {
buflen = GET32(smallbuf) + 4;
bufp = (u_char *)malloc(buflen);
/* now get all profiles */
if (get_configuration(fd, 0, buflen, bufp)) {
*profile = GET16(&bufp[6]);
ret = true;
}
free(bufp);
}
return (ret);
}
void
walk_profiles(int fd, int (*f)(void *, int, bool), void *arg)
{
size_t i;
uint16_t profile, current_profile;
u_char smallbuf[4];
size_t buflen;
u_char *bufp;
int ret;
/* first determine amount of memory needed to hold all profiles */
if (get_configuration(fd, 0, 4, &smallbuf[0])) {
buflen = GET32(smallbuf) + 4;
bufp = (u_char *)malloc(buflen);
/* now get all profiles */
if (get_configuration(fd, 0, buflen, bufp)) {
current_profile = GET16(&bufp[6]);
for (i = 8 + 4; i < buflen; i += 4) {
profile = GET16(&bufp[i]);
ret = f(arg, profile, (profile == current_profile));
if (ret == CDUTIL_WALK_STOP) {
break;
}
}
}
free(bufp);
}
}
/* retrieve speed list from the Write Speed Performance Descriptor Blocks
*/
static void
get_write_speeds(u_char *page, int n, intlist_t **speeds, int *n_speeds, intlist_t **speeds_mem)
{
u_char *p = page + 2;
int i;
intlist_t **nextp;
intlist_t *current;
bool skip;
*n_speeds = 0;
*speeds = NULL;
*speeds_mem = (intlist_t *)calloc(n, sizeof (intlist_t));
if (*speeds_mem == NULL) {
return;
}
for (i = 0; i < n; i++, p += 4) {
current = &(*speeds_mem)[i];
current->val = GET16(p);
/* keep the list sorted */
skip = false;
for (nextp = speeds; *nextp != NULL; nextp = &((*nextp)->next)) {
if (current->val == (*nextp)->val) {
skip = true; /* skip duplicates */
break;
} else if (current->val > (*nextp)->val) {
break;
}
}
if (!skip) {
current->next = *nextp;
*nextp = current;
*n_speeds++;
}
}
}
void
get_read_write_speeds(int fd, int *read_speed, int *write_speed,
intlist_t **speeds, int *n_speeds, intlist_t **speeds_mem)
{
int page_len;
u_char p[254];
int n; /* number of write speed performance descriptor blocks */
*read_speed = *write_speed = 0;
*speeds = *speeds_mem = NULL;
if (!get_mode_page(fd, 0x2A, 0, sizeof (p), p, &page_len)) {
return;
}
if (page_len > 8) {
*read_speed = GET16(&p[8]);
}
if (page_len > 18) {
*write_speed = GET16(&p[18]);
}
if (page_len < 28) {
printf("MMC-2\n");
return;
} else {
printf("MMC-3\n");
}
*write_speed = GET16(&p[28]);
if (page_len < 30) {
return;
}
/* retrieve speed list */
n = GET16(&p[30]);
n = min(n, (sizeof (p) - 32) / 4);
get_write_speeds(&p[32], n, speeds, n_speeds, speeds_mem);
if (*speeds != NULL) {
*write_speed = max(*write_speed, (*speeds)[0].val);
}
}
bool
get_disc_info(int fd, disc_info_t *di)
{
struct scsipi_read_discinfo discinfo;
uint8_t buf[32];
int bufsize = sizeof (buf);
bzero(buf, bufsize);
memset(&discinfo, 0, sizeof(discinfo));
discinfo.opcode = READ_DISCINFO;
discinfo.data_len[0] = (bufsize >> 8) & 0xff;
discinfo.data_len[1] = bufsize & 0xff;
if (scsi_command(fd, &discinfo, sizeof(discinfo), buf, bufsize, 10000, SCCMD_READ) == false)
return false;
di->disc_status = buf[2] & 0x03;
di->erasable = buf[2] & 0x10;
if ((buf[21] != 0) && (buf[21] != 0xff)) {
di->capacity = ((buf[21] * 60) + buf[22]) * 75;
} else {
di->capacity = 0;
}
return true;
}
/*
* returns current/maximum format capacity in bytes
*/
bool
read_format_capacity(int fd, uint64_t *capacity)
{
char cdb[12];
uint8_t buf[32];
int bufsize = sizeof (buf);
uint32_t num_blocks;
uint32_t block_len;
memset(buf, 0, bufsize);
memset(cdb, 0, sizeof(cdb));
cdb[0] = 0x23; /* READ FORMAT CAPACITIES */
cdb[7] = (bufsize >> 8) & 0xff; /* allocation length */
cdb[8] = bufsize & 0xff;
if (scsi_command(fd, &cdb, sizeof(cdb), buf, bufsize, 10000, SCCMD_READ) == false)
return false;
num_blocks = (uint32_t)(buf[4] << 24) + (buf[5] << 16) + (buf[6] << 8) + buf[7];
block_len = (uint32_t)(buf[9] << 16) + (buf[10] << 8) + buf[11];
*capacity = (uint64_t)num_blocks * block_len;
return (true);
}
bool
get_media_info(int fd, uint64_t *capacity)
{
struct disklabel d;
int ret;
ret = ioctl(fd, DIOCGDINFO, &d);
if (ret == -1)
return false;
*capacity = d.d_secsize * d.d_secperunit;
return true;
}
/*
* given current profile, use the best method for determining
* disc capacity (in bytes)
*/
bool
get_disc_capacity_for_profile(int fd, int profile, uint64_t *capacity)
{
disc_info_t di;
bool ret = false;
switch (profile) {
case 0x08: /* CD-ROM */
case 0x10: /* DVD-ROM */
if (get_media_info(fd, capacity))
ret = true;
break;
default:
if (read_format_capacity(fd, capacity) && (*capacity > 0)) {
ret = true;
} else if (get_disc_info(fd, &di) && (di.capacity > 0)) {
if (get_media_info(fd, capacity))
ret = true;
}
}
return (ret);
}
bool
read_toc(int fd, int format, int trackno, int buflen, u_char *buf)
{
struct scsipi_read_toc toc;
memset(buf, 0, buflen);
toc.opcode = READ_TOC;
toc.resp_format = format & 0xf;
toc.from_track = trackno;
toc.data_len[0] = (buflen >> 8) & 0xff;
toc.data_len[1] = buflen & 0xff;
return scsi_command(fd, &toc, sizeof(toc), buf, buflen, 10000, SCCMD_READ);
}
@@ -0,0 +1,61 @@
/***************************************************************************
*
* cdutils.h : definitions for CD/DVD utilities
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef CDUTILS_H
#define CDUTILS_H
#include <sys/types.h>
#include <sys/dkio.h>
#include <sys/cdio.h>
#include <sys/scsiio.h>
#include <stdbool.h>
#include <stdint.h>
enum {
CDUTIL_WALK_CONTINUE,
CDUTIL_WALK_STOP
};
typedef struct intlist {
int val;
struct intlist *next;
} intlist_t;
typedef struct disc_info {
int disc_status;
int erasable;
uint64_t capacity;
} disc_info_t;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
bool scsi_command(int fd, void *cmd, size_t cmdlen, void *data,
size_t datalen, int timeout, int flags);
int mode_sense(int fd, u_char pc, int dbd, int page_len,
u_char *buffer);
int get_mode_page(int fd, int page_no, int pc, int buf_len,
u_char *buffer, int *plen);
int get_configuration(int fd, uint16_t feature, int bufsize,
u_char *buf);
bool get_current_profile(int fd, int *profile);
void walk_profiles(int fd, int (*f)(void *, int, bool), void *);
void get_read_write_speeds(int fd, int *read_speed, int *write_speed,
intlist_t **wspeeds, int *n_wspeeds, intlist_t **wspeeds_mem);
bool get_disc_info(int fd, disc_info_t *);
bool read_format_capacity(int fd, uint64_t *capacity);
bool get_media_info(int fd, uint64_t *capacity);
bool get_disc_capacity_for_profile(int fd, int profile,
uint64_t *capacity);
bool read_toc(int fd, int format, int trackno, int buflen,
u_char *buf);
#endif /* CDUTILS_H */
@@ -0,0 +1,217 @@
/***************************************************************************
*
* fsutils.c : filesystem utilities
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/scsiio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/dkio.h>
#include <libintl.h>
#include <sys/disklabel.h>
#include <sys/bootblock.h>
#include <libhal.h>
#include "fsutils.h"
/*
* Separates dos notation device spec into device and drive number
*/
bool
dos_to_dev(char *path, char **devpath, int *num)
{
char *p;
if ((p = strrchr(path, ':')) == NULL) {
return (false);
}
if ((*num = atoi(p + 1)) == 0) {
return (false);
}
p[0] = '\0';
*devpath = strdup(path);
p[0] = ':';
return (*devpath != NULL);
}
char *
get_slice_name (char *devlink)
{
char *part, *slice, *disk;
char *s = NULL;
char *p;
if ((p = strstr(devlink, "/lofi/")) != 0) {
return (p + sizeof ("/lofi/") - 1);
}
part = strrchr(devlink, 'p');
slice = strrchr(devlink, 's');
disk = strrchr(devlink, 'd');
if ((part != NULL) && (part > slice) && (part > disk)) {
s = part;
} else if ((slice != NULL) && (slice > disk)) {
s = slice;
} else {
s = disk;
}
if ((s != NULL) && isdigit(s[1])) {
return (s);
} else {
return ("");
}
}
bool
is_dos_drive(u_char type)
{
return ((type == 1) || (type == 4) || (type == 5) || (type == 6) ||
((type >= 8) && (type <= 0xf)));
}
bool
is_dos_extended(u_char id)
{
return MBR_IS_EXTENDED(id);
}
struct part_find_s {
int num;
int count;
int systid;
int r_systid;
int r_relsect;
int r_numsect;
};
enum { WALK_CONTINUE, WALK_TERMINATE };
/*
* Walk partition tables and invoke a callback for each.
*/
static void
walk_partitions(int fd, int startsec, int (*f)(void *, int, int, int),
void *arg)
{
uint32_t buf[1024/4];
int bufsize = 1024;
struct mbr_sector *msect = (struct mbr_sector *)&buf[0];
struct mbr_partition mpart[MBR_PART_COUNT];
int sec = startsec;
int lastsec = sec + 1;
int relsect;
int ext = 0;
int systid;
bool valid;
int i;
while (sec != lastsec) {
if (pread(fd, buf, bufsize, (off_t)sec * 512) != bufsize) {
break;
}
lastsec = sec;
if (le16toh(msect->mbr_magic) != MBR_MAGIC) {
break;
}
memcpy(mpart, msect->mbr_parts, MBR_PART_COUNT * sizeof (struct mbr_partition));
for (i = 0; i < MBR_PART_COUNT; i++) {
systid = mpart[i].mbrp_type;
relsect = sec + le32toh(mpart[i].mbrp_start);
if (systid == 0) {
continue;
}
valid = true;
if (is_dos_extended(systid) && (sec == lastsec)) {
sec = startsec + le32toh(mpart[i].mbrp_start);
if (ext++ == 0) {
relsect = startsec = sec;
} else {
valid = false;
}
}
if (valid && f(arg, mpart[i].mbrp_type, relsect,
le32toh(mpart[i].mbrp_size)) == WALK_TERMINATE) {
return;
}
}
}
}
static int
find_dos_drive_cb(void *arg, int systid, int relsect, int numsect)
{
struct part_find_s *p = arg;
if (is_dos_drive(systid)) {
if (++p->count == p->num) {
p->r_relsect = relsect;
p->r_numsect = numsect;
p->r_systid = systid;
return (WALK_TERMINATE);
}
}
return (WALK_CONTINUE);
}
/*
* Given a dos drive number, return its relative sector number,
* number of sectors in partition and the system id.
*/
bool
find_dos_drive(int fd, int num, int *relsect, int *numsect, int *systid)
{
struct part_find_s p = { 0, 0, 0, 0, 0, 0 };
p.num = num;
if (num > 0) {
walk_partitions(fd, 0, find_dos_drive_cb, &p);
if (p.count == num) {
*relsect = p.r_relsect;
*numsect = p.r_numsect;
*systid = p.r_systid;
return (true);
}
}
return (false);
}
static int
get_num_dos_drives_cb(void *arg, int systid, int relsect, int numsect)
{
if (is_dos_drive(systid)) {
(*(int *)arg)++;
}
return (WALK_CONTINUE);
}
int
get_num_dos_drives(int fd)
{
int count = 0;
walk_partitions(fd, 0, get_num_dos_drives_cb, &count);
return (count);
}
@@ -0,0 +1,25 @@
/***************************************************************************
*
* fsutils.h : definitions for filesystem utilities
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifndef FSUTILS_H
#define FSUTILS_H
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
bool dos_to_dev(char *path, char **devpath, int *num);
char *get_slice_name (char *devlink);
bool is_dos_drive(u_char id);
bool is_dos_extended(u_char id);
bool find_dos_drive(int fd, int num, int *relsect, int *numsect, int *systid);
#endif /* FSUTILS_H */
@@ -0,0 +1,414 @@
/***************************************************************************
*
* probe-storage.c : Probe for storage devices
*
* Copyright 2008 Jared D. McNeill <jmcneill@NetBSD.org>
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/fdio.h>
#include <sys/statvfs.h>
#include <sys/syslog.h>
#include <sys/scsiio.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <libhal.h>
#include <cdutils.h>
#include <fsutils.h>
#include <logger.h>
#if notyet
/** Check if a filesystem on a special device file is mounted
*
* @param device_file Special device file, e.g. /dev/cd0a
* @return TRUE iff there is a filesystem system mounted
* on the special device file
*/
static dbus_bool_t
is_mounted (const char *device_file)
{
int count;
struct statvfs *statvfs;
count = getmntinfo(&statvfs, ST_WAIT);
while (count-- > 0) {
if (strcmp(statvfs->f_mntfromname, device_file) == 0)
return TRUE;
}
return FALSE;
}
#endif
static int
get_cdrom_properties_walker (void *arg, int profile, bool is_current)
{
LibHalChangeSet *cs = (LibHalChangeSet *)arg;
switch (profile) {
case 0x09:
libhal_changeset_set_property_bool (cs, "storage.cdrom.cdr", TRUE);
break;
case 0x0a:
libhal_changeset_set_property_bool (cs, "storage.cdrom.cdrw", TRUE);
break;
case 0x10:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvd", TRUE);
break;
case 0x11:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdr", TRUE);
break;
case 0x12:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdram", TRUE);
break;
case 0x13:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdrw", TRUE);
break;
case 0x14:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdrw", TRUE);
break;
case 0x1a:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdplusrw", TRUE);
break;
case 0x1b:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdplusr", TRUE);
break;
case 0x2b:
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdplusrdl", TRUE);
break;
case 0x40:
libhal_changeset_set_property_bool (cs, "storage.cdrom.bd", TRUE);
break;
case 0x41:
case 0x42:
libhal_changeset_set_property_bool (cs, "storage.cdrom.bdr", TRUE);
break;
case 0x43:
libhal_changeset_set_property_bool (cs, "storage.cdrom.bdre", TRUE);
break;
case 0x50:
libhal_changeset_set_property_bool (cs, "storage.cdrom.hddvd", TRUE);
break;
case 0x51:
libhal_changeset_set_property_bool (cs, "storage.cdrom.hddvdr", TRUE);
break;
case 0x52:
libhal_changeset_set_property_bool (cs, "storage.cdrom.hddvdrw", TRUE);
break;
}
return CDUTIL_WALK_CONTINUE;
}
#define WSPLEN 64
static void
get_cdrom_properties (int fd, LibHalChangeSet *cs)
{
int read_speed, write_speed;
intlist_t *write_speeds, *write_speeds_mem, *sp;
int n_wspeeds;
char **wspeeds;
char *wspeeds_mem;
int i;
libhal_changeset_set_property_bool (cs, "storage.cdrom.cdr", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.cdrw", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvd", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdr", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdrw", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdram", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdplusr", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdplusrw", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.dvdplusrdl", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.bd", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.bdr", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.bdre", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.hddvd", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.hddvdr", FALSE);
libhal_changeset_set_property_bool (cs, "storage.cdrom.hddvdrw", FALSE);
walk_profiles(fd, get_cdrom_properties_walker, cs);
/* XXX */
libhal_changeset_set_property_bool (cs, "storage.cdrom.support_media_changed", TRUE);
get_read_write_speeds(fd, &read_speed, &write_speed, &write_speeds, &n_wspeeds, &write_speeds_mem);
libhal_changeset_set_property_int (cs, "storage.cdrom.read_speed", read_speed);
libhal_changeset_set_property_int (cs, "storage.cdrom.write_speed", write_speed);
if (n_wspeeds <= 0) {
wspeeds_mem = NULL;
libhal_changeset_set_property_strlist (cs, "storage.cdrom.write_speeds", (const char **)&wspeeds_mem);
return;
}
if ((wspeeds = (char **)calloc(n_wspeeds + 1, sizeof (char *))) == NULL) {
free (write_speeds_mem);
return;
}
if ((wspeeds_mem = (char *)calloc(n_wspeeds, WSPLEN)) == NULL) {
free (wspeeds);
free (write_speeds_mem);
return;
}
for (i = 0; i < n_wspeeds; i++) {
wspeeds[i] = &wspeeds_mem[i * WSPLEN];
}
for (sp = write_speeds, i = 0; sp != NULL; sp = sp->next, i++) {
snprintf (wspeeds[i], WSPLEN, "%d", sp->val);
}
libhal_changeset_set_property_strlist (cs, "storage.cdrom.write_speeds", (const char **)wspeeds);
free (wspeeds);
free (wspeeds_mem);
free (write_speeds_mem);
}
/*
* Return a copy of a string without trailing spaces. If 'len' is non-zero,
* it specifies max length, otherwise the string must be null-terminated.
*/
static char *
rtrim_copy(char *src, int len)
{
char *dst, *p;
if (len == 0) {
len = strlen(src);
}
if ((dst = calloc(1, len + 1)) != NULL) {
strncpy(dst, src, len);
p = dst + len - 1;
while ((p >= dst) && (isspace((int)*p))) {
*p-- = '\0';
}
}
return (dst);
}
static void
get_disk_properties (int fd, LibHalChangeSet *cs)
{
struct scsipi_inquiry_data inqbuf;
struct scsipi_inquiry cmd;
dbus_bool_t status;
char *s;
/* INQUIRY */
memset(&cmd, 0, sizeof(cmd));
memset(&inqbuf, 0, sizeof(inqbuf));
cmd.opcode = INQUIRY;
cmd.length = sizeof(inqbuf);
status = scsi_command(fd, &cmd, sizeof(cmd), &inqbuf, sizeof(inqbuf), 10000, SCCMD_READ);
if (status == false)
return;
if ((s = rtrim_copy(inqbuf.vendor, sizeof (inqbuf.vendor))) != NULL) {
libhal_changeset_set_property_string (cs, "storage.vendor", s);
free(s);
}
if ((s = rtrim_copy(inqbuf.product, sizeof (inqbuf.product))) != NULL) {
libhal_changeset_set_property_string (cs, "storage.model", s);
free(s);
}
if ((s = rtrim_copy(inqbuf.revision, sizeof (inqbuf.revision))) != NULL) {
libhal_changeset_set_property_string (cs, "storage.firmware_revision", s);
free(s);
}
if ((s = rtrim_copy((char *)inqbuf.vendor_specific, sizeof (inqbuf.vendor_specific))) != NULL) {
libhal_changeset_set_property_string (cs, "storage.serial", s);
free(s);
}
}
#if notyet
/*
* returns TRUE if diskette is inserted.
* also returns write protection status.
*/
static dbus_bool_t
check_floppy(int fd, dbus_bool_t *wprot)
{
int chg;
if ((ioctl(fd, FDGETCHANGE, &chg) == 0) && !(chg & FDGC_CURRENT)) {
*wprot = ((chg & FDGC_CURWPROT) != NULL);
return (TRUE);
} else {
return (FALSE);
}
}
#endif
int
main (int argc, char *argv[])
{
int ret = 1;
int fd = -1;
int rfd = -1;
char *udi;
char *device_file;
char *raw_device_file;
LibHalContext *ctx = NULL;
DBusError error;
char *bus;
char *drive_type;
dbus_bool_t is_cdrom;
dbus_bool_t is_floppy;
dbus_bool_t only_check_for_media;
int got_media = FALSE;
dbus_bool_t is_write_protected = FALSE;
dbus_bool_t is_mbr = FALSE;
dbus_bool_t is_smi = FALSE;
dbus_bool_t is_gpt = FALSE;
dbus_bool_t is_partitioned = FALSE;
int dos_cnt = 0;
const char *scheme = "";
LibHalChangeSet *cs = NULL;
HAL_INFO(("f0"));
if ((udi = getenv ("UDI")) == NULL)
goto out;
HAL_INFO(("f0-1"));
if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL)
goto out;
HAL_INFO(("f0-2"));
if ((raw_device_file = getenv ("HAL_PROP_BLOCK_NETBSD_RAW_DEVICE")) == NULL)
goto out;
HAL_INFO(("f0-3"));
if ((bus = getenv ("HAL_PROP_STORAGE_BUS")) == NULL)
goto out;
HAL_INFO(("f0-4"));
if ((drive_type = getenv ("HAL_PROP_STORAGE_DRIVE_TYPE")) == NULL)
goto out;
HAL_INFO(("f1"));
setup_logger ();
if (argc == 2 && strcmp (argv[1], "--only-check-for-media") == 0)
only_check_for_media = TRUE;
else
only_check_for_media = FALSE;
is_cdrom = (strcmp (drive_type, "cdrom") == 0);
is_floppy = (strcmp (drive_type, "floppy") == 0);
HAL_INFO(("f2"));
dbus_error_init (&error);
HAL_INFO(("f3"));
if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
goto out;
HAL_INFO(("f4"));
if ((cs = libhal_device_new_changeset (udi)) == NULL) {
HAL_DEBUG (("Cannot allocate changeset"));
goto out;
}
HAL_INFO(("f5"));
HAL_DEBUG (("Doing probe-storage for %s (bus %s) (drive_type %s) (udi=%s) (--only-check-for-media==%d)",
device_file, bus, drive_type, udi, only_check_for_media));
if ((rfd = open (raw_device_file, O_RDONLY | O_NONBLOCK)) < 0) {
HAL_DEBUG (("Cannot open %s: %s", raw_device_file, strerror (errno)));
goto out;
}
if (!only_check_for_media) {
if (strcmp (drive_type, "cdrom") == 0) {
get_cdrom_properties (rfd, cs);
} else if (strcmp (drive_type, "disk") == 0) {
get_disk_properties (rfd, cs);
}
}
ret = 0;
if (is_cdrom) {
uint64_t capacity;
HAL_DEBUG (("Checking for optical disc on %s", raw_device_file));
got_media = get_media_info(rfd, &capacity);
if (!got_media) {
goto out_cs;
}
/* XXX */
is_write_protected = TRUE;
} else if (is_floppy) {
#if 0
HAL_DEBUG (("Checking for floppy on %s", raw_device_file));
if (check_floppy(rfd, &is_write_protected)) {
got_media = TRUE;
}
#endif
/* don't look for partitions on floppy */
goto out_cs;
} else {
got_media = TRUE;
}
HAL_DEBUG (("Checking for partitions on %s", device_file));
if ((fd = open (device_file, O_RDONLY | O_NONBLOCK)) < 0) {
HAL_DEBUG (("Cannot open %s: %s", device_file, strerror (errno)));
goto out_cs;
}
scheme = "mbr";
dos_cnt = 1;
out_cs:
is_partitioned = is_mbr || is_smi || is_gpt;
libhal_changeset_set_property_bool (cs, "storage.no_partitions_hint", !is_partitioned);
libhal_changeset_set_property_bool (cs, "block.no_partitions", !is_partitioned);
libhal_changeset_set_property_string (cs, "storage.partitioning_scheme", scheme);
libhal_changeset_set_property_int (cs, "storage.netbsd.num_dos_partitions", dos_cnt);
/* XXX should only set for removable drives */
libhal_changeset_set_property_bool (cs, "storage.removable.media_available", got_media);
libhal_changeset_set_property_bool (cs, "storage.removable.netbsd.read_only", is_write_protected);
libhal_device_commit_changeset (ctx, cs, &error);
out:
if (cs != NULL) {
libhal_device_free_changeset (cs);
}
if (fd >= 0) {
close (fd);
}
if (rfd >= 0) {
close (rfd);
}
if (ctx != NULL) {
if (dbus_error_is_set(&error)) {
dbus_error_free (&error);
}
libhal_ctx_shutdown (ctx, &error);
libhal_ctx_free (ctx);
}
return ret;
}
@@ -0,0 +1,629 @@
/***************************************************************************
*
* probe-volume.c : probe volumes
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <sys/dkio.h>
#include <sys/cdio.h>
#include <sys/fdio.h>
#include <sys/disklabel.h>
#include <isofs/cd9660/iso.h>
#include <libhal.h>
#include <cdutils.h>
#include <fsutils.h>
#include <logger.h>
static void
my_dbus_error_free(DBusError *error)
{
if (dbus_error_is_set(error)) {
dbus_error_free(error);
}
}
/*
* Return a copy of a string without trailing spaces. If 'len' is non-zero,
* it specifies max length, otherwise the string must be null-terminated.
*/
static char *
rtrim_copy(char *src, int len)
{
char *dst, *p;
if (len == 0) {
len = strlen(src);
}
if ((dst = calloc(1, len + 1)) != NULL) {
strncpy(dst, src, len);
p = dst + len - 1;
while ((p >= dst) && (isspace(*p))) {
*p-- = '\0';
}
}
return (dst);
}
static void
set_fstyp_properties (LibHalContext *ctx, const char *udi, const char *fstype)
{
char buf[256];
DBusError error;
char *uuid = NULL;
char *label_orig = NULL;
char *label = NULL;
LibHalChangeSet *cs;
dbus_error_init (&error);
if ((cs = libhal_device_new_changeset (udi)) == NULL) {
return;
}
libhal_changeset_set_property_string (cs, "volume.fsusage", "filesystem");
libhal_changeset_set_property_string (cs, "volume.fstype", fstype);
#if notyet
/* label */
(void) nvlist_lookup_string(fsattr, "gen_volume_label", &label_orig);
#endif
if (label_orig != NULL) {
label = rtrim_copy(label_orig, 0);
}
if ((label != NULL) && (label[0] != '\0')) {
libhal_changeset_set_property_string (cs, "volume.label", label);
libhal_changeset_set_property_string (cs, "info.product", label);
} else {
libhal_changeset_set_property_string (cs, "volume.label", "");
snprintf (buf, sizeof (buf), "Volume (%s)", fstype);
libhal_changeset_set_property_string (cs, "info.product", buf);
}
if (label)
free(label);
#if notyet
/* uuid */
if (nvlist_lookup_string(fsattr, "gen_uuid", &uuid) == 0) {
libhal_changeset_set_property_string (cs, "volume.uuid", uuid);
} else {
libhal_changeset_set_property_string (cs, "volume.uuid", "");
}
#else
libhal_changeset_set_property_string (cs, "volume.uuid", "");
#endif
libhal_device_commit_changeset (ctx, cs, &error);
libhal_device_free_changeset (cs);
my_dbus_error_free (&error);
}
#define ISO_VOLDESC_SEC 16
#define IPE_FPESIZE 8
#define IDE_MAX_NAME_LEN (255 - ISO_DIRECTORY_RECORD_SIZE)
/*
* isofs/cd9660 contents detection: Video DVD, Video CD, etc.
*/
static void
isofs_contents(int fd, off_t probe_offset, LibHalContext *ctx, const char *udi)
{
size_t secsz = sizeof(struct iso_primary_descriptor);
struct iso_primary_descriptor ipdesc;
uint8_t buf[sizeof(struct iso_volume_descriptor)];
int ptbl_lbn, ptbl_size;
int off, reloff, readoff;
char *name;
int name_len;
int ipe_len;
DBusError error;
/*
* find 1st Primary Volume Descriptor
*/
readoff = probe_offset + ISO_VOLDESC_SEC * secsz;
if (pread (fd, &ipdesc, secsz, readoff) != secsz) {
return;
}
while (isonum_711 (ipdesc.type) != ISO_VD_PRIMARY) {
if (isonum_711 (ipdesc.type) == ISO_VD_END) {
return;
}
readoff += secsz;
if (pread (fd, buf, secsz, readoff) != secsz) {
return;
}
}
/*
* PVD contains size and offset of the LSB/MSB path table
*/
ptbl_size = isonum_733 (ipdesc.path_table_size);
ptbl_lbn = isonum_732 (ipdesc.type_m_path_table);
/*
* Look through path table entries
*/
readoff = probe_offset + ptbl_lbn * secsz;
if (pread (fd, buf, secsz, readoff) != secsz) {
return;
}
dbus_error_init (&error);
for (off = reloff = 0;
off < ptbl_size;
off += ipe_len, reloff += ipe_len) {
uint8_t *p;
/* load sectors on demand */
if (reloff >= secsz) {
readoff += secsz;
if (pread (fd, buf, secsz, readoff) != secsz) {
break;
}
reloff -= secsz;
}
p = buf + reloff;
name_len = isonum_711 (p[0]);
ipe_len = IPE_FPESIZE + name_len + (name_len % 2);
/* only interested in root directories */
if (isonum_711 (p[6]) != 1) {
continue;
}
if ((name_len < 2) || (name_len > IDE_MAX_NAME_LEN)) {
continue;
}
name = (char *)&p[8];
if (strncasecmp (name, "VIDEO_TS", min (8, name_len)) == 0) {
libhal_device_set_property_bool (ctx, udi,
"volume.disc.is_videodvd", TRUE, &error);
} else if (strncasecmp (name, "VCD", min (3, name_len)) == 0) {
libhal_device_set_property_bool (ctx, udi,
"volume.disc.is_vcd", TRUE, &error);
} else if (strncasecmp (name, "SVCD", min (4, name_len)) == 0) {
libhal_device_set_property_bool (ctx, udi,
"volume.disc.is_svcd", TRUE, &error);
}
}
my_dbus_error_free (&error);
}
static dbus_bool_t
probe_disc (int fd, LibHalContext *ctx, const char *udi, dbus_bool_t *should_probe_for_fs)
{
DBusError error;
disc_info_t di;
int profile;
dbus_bool_t has_audio, has_data, is_blank, is_appendable, is_rewritable;
char *disc_type = "cd_rom";
uint64_t capacity = 0;
int i;
LibHalChangeSet *cs;
dbus_error_init (&error);
if (get_disc_info (fd, &di)) {
is_blank = (di.disc_status == 0);
is_appendable = (di.disc_status == 1);
is_rewritable = (di.erasable != 0);
} else {
is_blank = is_appendable = is_rewritable = FALSE;
}
if (get_current_profile (fd, &profile)) {
switch (profile) {
case 0x08: /* CD-ROM */
disc_type = "cd_rom";
break;
case 0x09: /* CD-R */
disc_type = "cd_r";
break;
case 0x0A: /* CD-RW */
disc_type = "cd_rw";
is_rewritable = TRUE;
break;
case 0x10: /* DVD-ROM */
disc_type = "dvd_rom";
break;
case 0x11: /* DVD-R Sequential */
disc_type = "dvd_r";
break;
case 0x12: /* DVD-RAM */
disc_type = "dvd_ram";
is_rewritable = TRUE;
break;
case 0x13: /* DVD-RW Restricted Overwrite */
disc_type = "dvd_rw";
is_rewritable = TRUE;
break;
case 0x14: /* DVD-RW Sequential */
disc_type = "dvd_rw";
is_rewritable = TRUE;
break;
case 0x1A: /* DVD+RW */
disc_type = "dvd_plus_rw";
is_rewritable = TRUE;
break;
case 0x1B: /* DVD+R */
disc_type = "dvd_plus_r";
break;
case 0x2B: /* DVD+R Double Layer */
disc_type = "dvd_plus_r_dl";
break;
case 0x40: /* BD-ROM */
disc_type = "bd_rom";
break;
case 0x41: /* BD-R Sequential */
case 0x42: /* BD-R Random */
disc_type = "bd_r";
break;
case 0x43: /* BD-RE */
disc_type = "bd_re";
is_rewritable = TRUE;
break;
case 0x50: /* HD DVD-ROM */
disc_type = "hddvd_rom";
break;
case 0x51: /* HD DVD-R */
disc_type = "hddvd_r";
break;
case 0x52: /* HD DVD-Rewritable */
disc_type = "hddvd_rw";
is_rewritable = TRUE;
break;
}
(void) get_disc_capacity_for_profile(fd, profile, &capacity);
}
has_audio = has_data = FALSE;
if (!is_blank) {
u_char smalltoc[12];
size_t toc_size;
u_char *toc, *p;
/*
* XXX for some reason CDROMREADTOCENTRY fails on video DVDs,
* but extracting the toc directly works okay.
*/
if (!read_toc(fd, 0, 1, 4, smalltoc)) {
HAL_DEBUG(("read_toc failed"));
has_data = TRUE; /* probe for fs anyway */
} else {
toc_size = smalltoc[0] * 256 + smalltoc[1] + 2;
toc = (u_char *)calloc(1, toc_size);
if (toc == NULL || !read_toc(fd, 0, 1, toc_size, toc)) {
HAL_DEBUG (("read_toc again failed"));
} else {
for (p = &toc[4]; p < (toc + toc_size); p += 8) {
/* skip leadout */
if (p[2] == 0xAA) {
continue;
}
if (p[1] & 4) {
has_data = TRUE;
} else {
has_audio = TRUE;
}
}
}
free(toc);
}
}
if ((cs = libhal_device_new_changeset (udi)) == NULL) {
return (FALSE);
}
libhal_changeset_set_property_string (cs, "volume.disc.type", disc_type);
libhal_changeset_set_property_bool (cs, "volume.disc.is_blank", is_blank);
libhal_changeset_set_property_bool (cs, "volume.disc.has_audio", has_audio);
libhal_changeset_set_property_bool (cs, "volume.disc.has_data", has_data);
libhal_changeset_set_property_bool (cs, "volume.disc.is_appendable", is_appendable);
libhal_changeset_set_property_bool (cs, "volume.disc.is_rewritable", is_rewritable);
libhal_changeset_set_property_uint64 (cs, "volume.disc.capacity", capacity);
libhal_changeset_set_property_bool (cs, "volume.disc.is_videodvd", FALSE);
libhal_changeset_set_property_bool (cs, "volume.disc.is_vcd", FALSE);
libhal_changeset_set_property_bool (cs, "volume.disc.is_svcd", FALSE);
libhal_device_commit_changeset (ctx, cs, &error);
libhal_device_free_changeset (cs);
out:
*should_probe_for_fs = has_data;
my_dbus_error_free (&error);
return (TRUE);
}
int
main (int argc, char *argv[])
{
int fd, rfd;
int ret;
char *udi;
char *device_file, *raw_device_file;
char *devpath, *rdevpath;
bool is_dos;
int dos_num;
LibHalContext *ctx = NULL;
DBusError error;
DBusConnection *conn;
char *parent_udi;
char *storage_device;
char *is_disc_str;
int fdc;
dbus_bool_t is_disc = FALSE;
dbus_bool_t is_floppy = FALSE;
unsigned int block_size;
dbus_uint64_t vol_size;
dbus_bool_t should_probe_for_fs;
char *partition_scheme = NULL;
struct disklabel d;
dbus_uint64_t partition_start = 0;
int partition_number = 0;
int i, dos_cnt;
int systid, relsect, numsect;
off_t probe_offset = 0;
int num_volumes;
char **volumes;
dbus_uint64_t v_start;
const char *fstype;
fd = rfd = -1;
ret = 1;
if ((udi = getenv ("UDI")) == NULL) {
goto out;
}
if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) {
goto out;
}
if ((raw_device_file = getenv ("HAL_PROP_BLOCK_SOLARIS_RAW_DEVICE")) == NULL) {
goto out;
}
if (!dos_to_dev(device_file, &rdevpath, &dos_num)) {
rdevpath = raw_device_file;
}
if (!(is_dos = dos_to_dev(device_file, &devpath, &dos_num))) {
devpath = device_file;
}
if ((parent_udi = getenv ("HAL_PROP_INFO_PARENT")) == NULL) {
goto out;
}
if ((storage_device = getenv ("HAL_PROP_BLOCK_STORAGE_DEVICE")) == NULL) {
goto out;
}
is_disc_str = getenv ("HAL_PROP_VOLUME_IS_DISC");
if (is_disc_str != NULL && strcmp (is_disc_str, "true") == 0) {
is_disc = TRUE;
} else {
is_disc = FALSE;
}
setup_logger ();
dbus_error_init (&error);
if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
goto out;
HAL_DEBUG (("Doing probe-volume for %s\n", device_file));
fd = open (devpath, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
goto out;
}
rfd = open (rdevpath, O_RDONLY | O_NONBLOCK);
if (rfd < 0) {
goto out;
}
#if notyet
/* if it's a floppy with no media, bail out */
if (ioctl(rfd, FDGETCHANGE, &fdc) == 0) {
is_floppy = TRUE;
if (fdc & FDGC_CURRENT) {
goto out;
}
}
#endif
/* block size and total size */
if (ioctl(rfd, DIOCGDINFO, &d) != -1) {
block_size = d.d_secsize;
vol_size = d.d_secsize * d.d_secperunit;
} else {
block_size = 512;
vol_size = 0;
}
libhal_device_set_property_int (ctx, udi, "volume.block_size", block_size, &error);
my_dbus_error_free (&error);
libhal_device_set_property_uint64 (ctx, udi, "volume.size", vol_size, &error);
my_dbus_error_free (&error);
should_probe_for_fs = TRUE;
if (is_disc) {
if (!probe_disc (rfd, ctx, udi, &should_probe_for_fs)) {
HAL_DEBUG (("probe_disc failed, skipping fstyp"));
goto out;
}
/* XXX vol_probe_offset for multisession discs? */
}
if (!should_probe_for_fs) {
goto skip_fs;
}
/* don't support partitioned floppy */
if (is_floppy) {
goto skip_part;
}
/*
* first get partitioning info
*/
if (is_dos) {
/* for a dos drive find partition offset */
if (!find_dos_drive(fd, dos_num, &relsect, &numsect, &systid)) {
goto out;
}
partition_scheme = "mbr";
partition_start = (dbus_uint64_t)relsect * 512;
partition_number = dos_num;
probe_offset = (off_t)relsect * 512;
} else {
#if notyet
if ((partition_number = read_vtoc(rfd, &vtoc)) >= 0) {
if (!vtoc_one_slice_entire_disk(&vtoc)) {
partition_scheme = "smi";
if (partition_number < vtoc.v_nparts) {
if (vtoc.v_part[partition_number].p_size == 0) {
HAL_DEBUG (("zero size partition"));
}
partition_start = vtoc.v_part[partition_number].p_start * block_size;
}
}
} else if ((partition_number = efi_alloc_and_read(rfd, &gpt)) >= 0) {
partition_scheme = "gpt";
if (partition_number < gpt->efi_nparts) {
if (gpt->efi_parts[partition_number].p_size == 0) {
HAL_DEBUG (("zero size partition"));
}
partition_start = gpt->efi_parts[partition_number].p_start * block_size;
}
efi_free(gpt);
}
#endif
probe_offset = 0;
}
if (partition_scheme != NULL) {
libhal_device_set_property_string (ctx, udi, "volume.partition.scheme", partition_scheme, &error);
my_dbus_error_free (&error);
libhal_device_set_property_int (ctx, udi, "volume.partition.number", partition_number, &error);
my_dbus_error_free (&error);
libhal_device_set_property_uint64 (ctx, udi, "volume.partition.start", partition_start, &error);
my_dbus_error_free (&error);
libhal_device_set_property_bool (ctx, udi, "volume.is_partition", TRUE, &error);
my_dbus_error_free (&error);
} else {
libhal_device_set_property_bool (ctx, udi, "volume.is_partition", FALSE, &error);
my_dbus_error_free (&error);
}
/*
* ignore duplicate partitions
*/
if ((volumes = libhal_manager_find_device_string_match (
ctx, "block.storage_device", storage_device, &num_volumes, &error)) != NULL) {
my_dbus_error_free (&error);
for (i = 0; i < num_volumes; i++) {
if (strcmp (udi, volumes[i]) == 0) {
continue; /* skip self */
}
v_start = libhal_device_get_property_uint64 (ctx, volumes[i], "volume.partition.start", &error);
if (dbus_error_is_set(&error)) {
dbus_error_free(&error);
continue;
}
if (v_start == partition_start) {
HAL_DEBUG (("duplicate partition"));
goto out;
}
}
libhal_free_string_array (volumes);
}
skip_part:
#if notyet
/*
* now determine fs type
*/
if (fstyp_init(fd, probe_offset, NULL, &fstyp_handle) != 0) {
HAL_DEBUG (("fstyp_init failed"));
goto out;
}
if ((fstyp_ident(fstyp_handle, NULL, &fstype) != 0) ||
(fstyp_get_attr(fstyp_handle, &fsattr) != 0)) {
HAL_DEBUG (("fstyp ident or get_attr failed"));
/*
* XXX fstyp_udfs has a bug that it only works on raw,
* but we don't want to slow down the fast path above.
* Try raw for just udfs here until the bug is fixed.
*/
HAL_DEBUG (("trying udfs workaround"));
fstyp_fini(fstyp_handle);
if (fstyp_init(rfd, probe_offset, NULL, &fstyp_handle) != 0) {
goto out;
}
if ((fstyp_ident(fstyp_handle, "udfs", &fstype) != 0) ||
(fstyp_get_attr(fstyp_handle, &fsattr) != 0)) {
fstyp_fini(fstyp_handle);
goto out;
}
}
#else
fstype = "cd9660";
#endif
set_fstyp_properties (ctx, udi, fstype);
if (strcmp (fstype, "cd9660") == 0) {
isofs_contents (fd, probe_offset, ctx, udi);
}
#if notyet
fstyp_fini(fstyp_handle);
#endif
skip_fs:
ret = 0;
out:
if (fd >= 0)
close (fd);
if (rfd >= 0)
close (rfd);
if (ctx != NULL) {
my_dbus_error_free (&error);
libhal_ctx_shutdown (ctx, &error);
libhal_ctx_free (ctx);
}
return ret;
}
+157
View File
@@ -0,0 +1,157 @@
/* $NetBSD: vfsstat.c,v 1.1 2008/12/01 02:02:33 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/statvfs.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <paths.h>
#include <unistd.h>
#include "../osspec.h"
#include "../logger.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../hald_runner.h"
#include "../device_info.h"
#include "../util.h"
#include "../ids.h"
#include "hotplug.h"
#include "devinfo.h"
#include "vfsstat.h"
#include "drvctl.h"
static void
vfsstat_cleanup_mountpoint_cb(HalDevice *d, guint32 exit_type,
gint return_code, gchar **error, gpointer data1, gpointer data2)
{
char *mount_point = data1;
HAL_INFO (("cleaned up mount point %s", mount_point));
g_free (mount_point);
}
void
vfsstat_event(HalDevice *volume)
{
GSList *volumes, *v;
HalDevice *d;
struct statvfs *statvfs;
int nmounts, i;
if (volume == NULL)
volumes = hal_device_store_match_multiple_key_value_string (hald_get_gdl (), "info.category", "volume");
else
volumes = g_slist_append (NULL, volume);
if (volumes == NULL)
return;
nmounts = getvfsstat (NULL, 0, ST_WAIT);
if (nmounts <= 0)
return;
statvfs = calloc (nmounts, sizeof (*statvfs));
if (statvfs == NULL)
return;
nmounts = getvfsstat (statvfs, nmounts * sizeof (*statvfs), ST_WAIT);
if (nmounts <= 0)
goto done;
for (i = 0; i < nmounts; i++)
for (v = volumes; v; v = g_slist_next (v)) {
const char *devpath;
d = HAL_DEVICE (v->data);
devpath = hal_device_property_get_string (d, "block.device");
if (devpath == NULL || strlen (devpath) == 0)
continue;
if (strcmp (devpath, statvfs[i].f_mntfromname) != 0)
continue;
device_property_atomic_update_begin ();
hal_device_property_set_bool (d, "volume.is_mounted", TRUE);
hal_device_property_set_bool (d, "volume.is_mounted_read_only",
statvfs[i].f_flag & ST_RDONLY ? TRUE : FALSE);
hal_device_property_set_string (d, "volume.mount_point", statvfs[i].f_mntonname);
device_property_atomic_update_end ();
volumes = g_slist_delete_link (volumes, v);
}
for (v = volumes; v; v = g_slist_next (v)) {
const char *mount_path;
d = HAL_DEVICE (v->data);
mount_path = g_strdup (hal_device_property_get_string (d, "volume.mount_point"));
if (mount_path == NULL || strlen (mount_path) == 0) {
if (mount_path)
g_free (mount_path);
continue;
}
device_property_atomic_update_begin ();
hal_device_property_set_bool (d, "volume.is_mounted", FALSE);
hal_device_property_set_bool (d, "volume.is_mounted_read_only", FALSE);
hal_device_property_set_string (d, "volume.mount_point", "");
device_property_atomic_update_end ();
if (hal_util_is_mounted_by_hald (mount_path)) {
char *cleanup_stdin;
char *extra_env[2];
extra_env[0] = g_strdup_printf ("HALD_CLEANUP=%s", mount_path);
extra_env[1] = NULL;
cleanup_stdin = "\n";
hald_runner_run_method (d,
"hal-storage-cleanup-mountpoint",
extra_env,
cleanup_stdin, TRUE,
0,
vfsstat_cleanup_mountpoint_cb,
g_strdup (mount_path), NULL);
g_free (extra_env[0]);
}
g_free (mount_path);
}
g_slist_free (volumes);
done:
free (statvfs);
}
+36
View File
@@ -0,0 +1,36 @@
/* $NetBSD: vfsstat.h,v 1.1 2008/12/01 02:02:33 jmcneill Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#ifndef VFSSTAT_H
#define VFSSTAT_H
#include "devinfo.h"
void vfsstat_event(HalDevice *volume);
#endif /* VFSSTAT_H */
@@ -0,0 +1,40 @@
## Process this file with automake to produce Makefile.in
# $NetBSD: Makefile.am,v 1.2 2011/01/08 13:39:08 obache Exp $
AM_CPPFLAGS = \
-DPACKAGE_LIBEXEC_DIR=\""$(libexecdir)"\" \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALSTATEDIR=\""$(localstatedir)"\" \
-DPACKAGE_SCRIPT_DIR=\""$(libexecdir)/scripts"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-I$(top_srcdir) -I$(top_srcdir)/libhal -I$(top_srcdir)/libhal-storage \
@DBUS_CFLAGS@ @GLIB_CFLAGS@ @POLKIT_CFLAGS@
scriptdir = $(libexecdir)/scripts/netbsd
script_SCRIPTS = \
hal-system-power-suspend-netbsd \
hal-system-power-shutdown-netbsd \
hal-system-power-reboot-netbsd
EXTRA_DIST = $(script_SCRIPTS)
check:
for f in $(script_SCRIPTS); do \
echo -n "Validate bash syntax in $$f : "; \
bash -n $(srcdir)/$$f 2> bash.error;\
if test -s bash.error; then \
echo failed; \
cat bash.error; \
rm -f bash.error; \
exit 1; \
else \
echo ok; \
rm -f bash.error; \
fi; \
done;
clean-local :
rm -f *~
@@ -0,0 +1,19 @@
#!/bin/sh
# $NetBSD: hal-system-power-reboot-netbsd,v 1.1 2008/11/27 03:32:19 jmcneill Exp $
unsupported() {
echo "org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported" >&2
echo "No reboot command found" >&2
exit 1
}
#Try for common tools
if [ -x "/sbin/shutdown" ] ; then
/sbin/shutdown -r now
exit $?
elif [ -x "/usr/sbin/shutdown" ] ; then
/usr/sbin/shutdown -r now
exit $?
else
unsupported
fi
@@ -0,0 +1,19 @@
#!/bin/sh
# $NetBSD: hal-system-power-shutdown-netbsd,v 1.1 2008/11/27 03:32:19 jmcneill Exp $
unsupported() {
echo "org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported" >&2
echo "No shutdown command found" >&2
exit 1
}
#Try for common tools
if [ -x "/sbin/shutdown" ] ; then
/sbin/shutdown -p now
exit $?
elif [ -x "/usr/sbin/shutdown" ] ; then
/usr/sbin/shutdown -p now
exit $?
else
unsupported
fi
@@ -0,0 +1,29 @@
#!/bin/sh
# $NetBSD: hal-system-power-suspend-netbsd,v 1.1 2008/11/27 03:32:19 jmcneill Exp $
alarm_not_supported() {
echo org.freedesktop.Hal.Device.SystemPowerManagement.AlarmNotSupported >&2
echo Waking the system up is not supported >&2
exit 1
}
unsupported() {
echo org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported >&2
echo No suspend method found >&2
exit 1
}
read seconds_to_sleep
if [ -x /usr/sbin/apm ] ; then
if [ -e /var/run/apmdev ]; then
/usr/sbin/apm -z 2>/dev/null
else
/usr/sbin/apm -z -d 2>/dev/null
fi
RET=$?
else
unsupported
fi
exit $RET