summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cdrw
diff options
context:
space:
mode:
authorzk194757 <none@none>2008-03-19 07:23:18 -0700
committerzk194757 <none@none>2008-03-19 07:23:18 -0700
commitb0e063115d0b1a4c7975d20f1c3c2792fe47bda8 (patch)
treee957485bb40bae97217aa71788eab063849ec5ff /usr/src/cmd/cdrw
parenta6e966d7f60513f2732bbc387f32f7d45b80f9bc (diff)
downloadillumos-joyent-b0e063115d0b1a4c7975d20f1c3c2792fe47bda8.tar.gz
6522921 cdrw needs to be made aware of hald
Diffstat (limited to 'usr/src/cmd/cdrw')
-rw-r--r--usr/src/cmd/cdrw/Makefile12
-rw-r--r--usr/src/cmd/cdrw/device.c232
-rw-r--r--usr/src/cmd/cdrw/main.c96
-rw-r--r--usr/src/cmd/cdrw/main.h22
4 files changed, 247 insertions, 115 deletions
diff --git a/usr/src/cmd/cdrw/Makefile b/usr/src/cmd/cdrw/Makefile
index b7705289b5..f78ad8dd93 100644
--- a/usr/src/cmd/cdrw/Makefile
+++ b/usr/src/cmd/cdrw/Makefile
@@ -2,9 +2,8 @@
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
-# Common Development and Distribution License, Version 1.0 only
-# (the "License"). You may not use this file except in compliance
-# with the License.
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
@@ -20,7 +19,7 @@
# CDDL HEADER END
#
#
-# Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I% %E% SMI"
@@ -33,12 +32,13 @@ OBJS= main.o mmc.o device.o transport.o util.o msgs.o misc_scsi.o dumpinfo.o \
write_audio.o dae.o copycd.o
include ../Makefile.cmd
+include ../hal/Makefile.hal
SRCS= $(OBJS:.o=.c)
-LDLIBS += -lvolmgt -lsecdb
+LDLIBS += -ldbus-1 -lhal -lsecdb
CFLAGS += $(CCVERBOSE)
-CPPFLAGS += -D_REENTRANT -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
+CPPFLAGS += -D_REENTRANT -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 $(HAL_DBUS_CPPFLAGS)
LINTFLAGS += -um
$(ROOTBIN)/cdrw := FILEMODE = 04755
diff --git a/usr/src/cmd/cdrw/device.c b/usr/src/cmd/cdrw/device.c
index 096ed76830..967b924443 100644
--- a/usr/src/cmd/cdrw/device.c
+++ b/usr/src/cmd/cdrw/device.c
@@ -19,7 +19,7 @@
* CDDL HEADER END
*/
/*
- * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
@@ -27,7 +27,6 @@
#include <sys/types.h>
#include <fcntl.h>
-#include <volmgt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/dkio.h>
@@ -37,6 +36,8 @@
#include <stdlib.h>
#include <libintl.h>
#include <limits.h>
+#include <dbus/dbus.h>
+#include <hal/libhal.h>
#include "transport.h"
#include "mmc.h"
@@ -299,6 +300,106 @@ fini_device(cd_device *dev)
free(dev);
}
+/*
+ * Given a /dev path resolve that path to a symbolic
+ * name such as cdrom0 if hald is running. If hald is
+ * not running, or does not have a symbolic name for the
+ * the specified /dev path return NULL.
+ */
+static char *
+hald_symname(char *path)
+{
+ LibHalContext *ctx = NULL;
+ DBusError error;
+
+ char **udi, *p = NULL;
+ int ndevs = 0, i;
+
+ /* Make sure hald is running */
+ if (vol_running == 0)
+ return (p);
+
+ dbus_error_init(&error);
+
+ if ((ctx = attach_to_hald()) == NULL)
+ return (p);
+
+ if ((udi = libhal_manager_find_device_string_match(ctx,
+ HAL_RDSK_PROP, path, &ndevs, &error)) == NULL)
+ goto done;
+
+ /* Look for the node that contains the valid (non-null) symdev */
+ for (i = 0; i < ndevs; i++) {
+ if ((p = libhal_device_get_property_string(ctx, udi[i],
+ HAL_SYMDEV_PROP, NULL)) != NULL)
+ break;
+ else
+ libhal_free_string(p);
+ }
+
+done:
+ if (udi != NULL)
+ libhal_free_string_array(udi);
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+ detach_from_hald(ctx, HAL_INITIALIZED);
+ return (p);
+}
+
+/*
+ * Given a name resolve that name to a raw device in the case
+ * that it is a symbolic name or just return what is given if
+ * we are given a /dev path or hald is not running.
+ */
+static char *
+hald_findname(char *symname)
+{
+ LibHalContext *ctx = NULL;
+ DBusError error;
+
+ char **udi, *path = NULL;
+ int ndevs = 0, i;
+
+ /* We already have a raw path just return that */
+ if (symname[0] == '/')
+ return (symname);
+
+ /* Get the raw device from the hal record */
+ if (vol_running != 0) {
+ dbus_error_init(&error);
+
+ if ((ctx = attach_to_hald()) == NULL)
+ return (path);
+
+ if ((udi = libhal_manager_find_device_string_match(ctx,
+ HAL_SYMDEV_PROP, symname, &ndevs,
+ &error)) == NULL)
+ goto done;
+
+ /*
+ * Loop over the returned UDIs to access the raw
+ * device path.
+ */
+ for (i = 0; i < ndevs; i++) {
+ if ((path = libhal_device_get_property_string(ctx,
+ udi[i], HAL_RDSK_PROP, NULL)) != NULL)
+ break;
+ else
+ libhal_free_string(path);
+ }
+
+done:
+ if (udi != NULL)
+ libhal_free_string_array(udi);
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+ detach_from_hald(ctx, HAL_INITIALIZED);
+ return (path);
+ } else {
+ return (NULL);
+ }
+}
+
static int
vol_name_to_dev_node(char *vname, char *found)
{
@@ -308,13 +409,13 @@ vol_name_to_dev_node(char *vname, char *found)
if (vname == NULL)
return (0);
- if (vol_running)
- (void) volmgt_check(vname);
- p1 = media_findname(vname);
+
+ p1 = hald_findname(vname);
+
if (p1 == NULL)
return (0);
if (stat(p1, &statbuf) < 0) {
- free(p1);
+ libhal_free_string(p1);
return (0);
}
if (S_ISDIR(statbuf.st_mode)) {
@@ -324,45 +425,17 @@ vol_name_to_dev_node(char *vname, char *found)
break;
}
if (i == 16) {
- free(p1);
+ libhal_free_string(p1);
return (0);
}
} else {
(void) strlcpy(found, p1, PATH_MAX);
}
- free(p1);
+ libhal_free_string(p1);
return (1);
}
/*
- * Searches for volume manager's equivalent char device for the
- * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
- */
-static int
-vol_lookup(char *supplied, char *found)
-{
- char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
- int i, ret;
-
- (void) strlcpy(tmpstr, supplied, PATH_MAX);
- if ((p = volmgt_symname(tmpstr)) == NULL) {
- if (strrchr(tmpstr, 's') == NULL)
- return (0);
- *((char *)(strrchr(tmpstr, 's') + 1)) = 0;
- for (i = 0; i < 16; i++) {
- (void) snprintf(tmpstr1, PATH_MAX, "%s%d", tmpstr, i);
- if ((p = volmgt_symname(tmpstr1)) != NULL)
- break;
- }
- if (p == NULL)
- return (0);
- }
- ret = vol_name_to_dev_node(p, found);
- free(p);
- return (ret);
-}
-
-/*
* Builds an open()able device path from a user supplied node which can be
* of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
* cdrom[n]
@@ -382,9 +455,19 @@ lookup_device(char *supplied, char *found)
(void) strlcpy(found, supplied, PATH_MAX);
return (1);
}
- if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
- return (vol_lookup(supplied, found));
- if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
+
+ /*
+ * Hal only allows access to a device when the user is
+ * on the console, therefore if hal is running and we can't
+ * open the /dev/rdsk or /dev/removable-media/rdsk device
+ * file we will return 0 marking this device as not avaiable.
+ */
+ if (fd < 0 && ((strncmp(supplied, "/dev/rdsk/", 10) == 0) ||
+ (strncmp(supplied, "/dev/removable-media/rdsk/", 26) == 0)))
+ return (0);
+
+ if ((strncmp(supplied, "/dev/dsk/", 9) == 0) ||
+ (strncmp(supplied, "/dev/removable-media/dsk/", 25) == 0)) {
(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
(char *)strrchr(supplied, '/'));
@@ -393,10 +476,9 @@ lookup_device(char *supplied, char *found)
(void) strlcpy(found, supplied, PATH_MAX);
return (1);
}
- if ((access(tmpstr, F_OK) == 0) && vol_running)
- return (vol_lookup(tmpstr, found));
- else
- return (0);
+
+ /* This device can't be opened mark it as unavailable. */
+ return (0);
}
if ((strncmp(supplied, "cdrom", 5) != 0) &&
(strlen(supplied) < 32)) {
@@ -410,8 +492,9 @@ lookup_device(char *supplied, char *found)
(void) strlcpy(found, tmpstr, PATH_MAX);
return (1);
}
- if ((access(tmpstr, F_OK) == 0) && vol_running)
- return (vol_lookup(tmpstr, found));
+
+ /* This device can't be opened mark it as unavailable. */
+ return (0);
}
return (vol_name_to_dev_node(supplied, found));
}
@@ -473,21 +556,13 @@ scan_for_cd_device(int mode, cd_device **found)
int writers_found = 0;
int header_printed = 0;
int is_writer;
- int retry = 0;
int total_devices_found;
- int total_devices_found_last_time = 0;
- int defer = 0;
-
-#define MAX_RETRIES_FOR_SCANNING 5
TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
if (mode) {
- if (vol_running)
- defer = 1;
(void) printf(gettext("Looking for CD devices...\n"));
}
-try_again:
dir = opendir("/dev/rdsk");
if (dir == NULL)
@@ -527,10 +602,10 @@ try_again:
*found = t_dev;
}
- if ((mode == SCAN_LISTDEVS) && (!defer)) {
+ if (mode == SCAN_LISTDEVS) {
char *sn;
- sn = volmgt_symname(sdev);
+ sn = hald_symname(sdev);
if (!header_printed) {
print_header();
header_printed = 1;
@@ -550,44 +625,6 @@ try_again:
(void) closedir(dir);
-
- /*
- * If volume manager is running we'll do a retry in case the
- * user has just inserted media, This should allow vold
- * enough time to create the device links. If we cannot
- * find any devices, or we find any new devices we'll retry
- * again up to MAX_RETRIES_FOR_SCANNING
- */
-
- retry++;
-
- if ((retry <= MAX_RETRIES_FOR_SCANNING) && vol_running) {
- if (defer || ((mode != SCAN_LISTDEVS) &&
- (writers_found == 0))) {
-
- if ((total_devices_found == 0) ||
- (total_devices_found !=
- total_devices_found_last_time)) {
-
- /* before we rescan remove the device found */
- if (total_devices_found != 0 && t_dev) {
- fini_device(t_dev);
- }
-
- total_devices_found_last_time =
- total_devices_found;
- (void) sleep(2);
- goto try_again;
- } else {
-
- /* Do the printing this time */
- defer = 0;
- goto try_again;
-
- }
-
- }
- }
if ((mode & SCAN_WRITERS) || writers_found)
return (writers_found);
else
@@ -743,7 +780,8 @@ list(void)
if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
if (vol_running) {
err_msg(gettext(
- "No CD writers found or no media in the drive.\n"));
+ "No CD writers found, no media in the drive "
+ "or not on the console.\n"));
} else {
if (cur_uid != 0) {
err_msg(gettext(
@@ -818,7 +856,7 @@ get_media_type(int fd)
break;
case 0x13: /* DVD-RW restricted overwrite */
- case 0x14: /* DVD-RW sequencial */
+ case 0x14: /* DVD-RW sequential */
if (debug)
(void) printf("DVD-RW found\n");
device_type = DVD_MINUS;
@@ -839,8 +877,8 @@ get_media_type(int fd)
default:
if (debug)
(void) printf(
- "unknown drive found\n type = 0x%x",
- cap[7]);
+ "unknown drive found\n type = 0x%x",
+ cap[7]);
/*
* Treat as CD_RW to avoid regression, may
* be a legacy drive.
diff --git a/usr/src/cmd/cdrw/main.c b/usr/src/cmd/cdrw/main.c
index b28b07064f..088450f2fc 100644
--- a/usr/src/cmd/cdrw/main.c
+++ b/usr/src/cmd/cdrw/main.c
@@ -2,9 +2,8 @@
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License"). You may not use this file except in compliance
- * with the License.
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
@@ -20,7 +19,7 @@
* CDDL HEADER END
*/
/*
- * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
@@ -33,7 +32,8 @@
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
-#include <volmgt.h>
+#include <dbus/dbus.h>
+#include <hal/libhal.h>
#include "msgs.h"
#include "device.h"
@@ -109,6 +109,90 @@ check_invalid_option(options *specified, char *opstr)
}
}
+LibHalContext *
+attach_to_hald(void)
+{
+ LibHalContext *ctx = NULL;
+ DBusConnection *con = NULL;
+ DBusError error;
+ hal_state_t state;
+
+ /* Initialize the dbus error states */
+ dbus_error_init(&error);
+
+ if ((con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL) {
+ return (NULL);
+ }
+ state = DBUS_CONNECTION;
+
+ /* Allocate a new hal context to work with the dbus */
+ if ((ctx = libhal_ctx_new()) == NULL)
+ return (NULL);
+ state = HAL_CONTEXT;
+
+ /* Pair up the context with the connection */
+ if (!libhal_ctx_set_dbus_connection(ctx, con))
+ goto fail;
+ state = HAL_PAIRED;
+
+ /* If libhal_ctx_init fails hald is not present */
+ if (!libhal_ctx_init(ctx, &error)) {
+ goto fail;
+ }
+ state = HAL_INITIALIZED;
+
+ return (ctx);
+fail:
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+ detach_from_hald(ctx, state);
+ return (NULL);
+
+}
+
+void
+detach_from_hald(LibHalContext *ctx, hal_state_t state)
+{
+ DBusError error;
+ DBusConnection *con = libhal_ctx_get_dbus_connection(ctx);
+
+ dbus_error_init(&error);
+
+ switch (state) {
+ case HAL_INITIALIZED:
+ if (libhal_ctx_shutdown(ctx, &error) == FALSE)
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+ /*FALLTHROUGH*/
+ case HAL_PAIRED:
+ (void) libhal_ctx_free(ctx);
+ dbus_connection_unref(con);
+ break;
+ case HAL_CONTEXT:
+ (void) libhal_ctx_free(ctx);
+ break;
+ case DBUS_CONNECTION:
+ default:
+ break;
+ }
+}
+
+/*
+ * This function returns one if hald is running and
+ * zero if hald is not running
+ */
+int
+hald_running(void)
+{
+ LibHalContext *ctx;
+
+ if ((ctx = attach_to_hald()) == NULL)
+ return (0);
+
+ detach_from_hald(ctx, HAL_INITIALIZED);
+ return (1);
+}
+
int
setup_target(int flag)
{
@@ -160,7 +244,7 @@ main(int argc, char **argv)
lower_priv();
}
- vol_running = volmgt_running();
+ vol_running = hald_running();
tgtdev = NULL;
operations = 0;
diff --git a/usr/src/cmd/cdrw/main.h b/usr/src/cmd/cdrw/main.h
index d35eb40d24..b6085788f1 100644
--- a/usr/src/cmd/cdrw/main.h
+++ b/usr/src/cmd/cdrw/main.h
@@ -2,9 +2,8 @@
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License"). You may not use this file except in compliance
- * with the License.
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
@@ -20,7 +19,7 @@
* CDDL HEADER END
*/
/*
- * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
@@ -34,6 +33,7 @@ extern "C" {
#endif
#include "device.h"
+#include <hal/libhal.h>
extern int debug;
@@ -56,15 +56,18 @@ extern uid_t ruid, cur_uid;
extern int device_type;
extern int write_mode;
+typedef enum {DBUS_CONNECTION, HAL_CONTEXT, HAL_PAIRED,
+ HAL_INITIALIZED} hal_state_t;
+
#define TAO_MODE 0
-#define DAO_MODE 1 /* not implimented for CD yet only DVD */
+#define DAO_MODE 1 /* not implemented for CD yet only DVD */
#define CD_RW 1 /* CD_RW/CD-R */
#define DVD_MINUS 2 /* DVD-RW/DVD-R */
/*
* DVD+RW is listed differently from DVD+R since DVD+RW requires
- * that we format the media priot to writing, this cannot be
+ * that we format the media prior to writing, this cannot be
* done for DVD+R since it is write once media, we treat the
* media as pre-formatted.
*/
@@ -77,8 +80,15 @@ extern int write_mode;
#define LEADOUT 5 /* erases the leadout of the media */
#define CLEAR 1 /* same as fast, used for fixing media */
+#define HAL_RDSK_PROP "block.solaris.raw_device"
+#define HAL_SYMDEV_PROP "storage.solaris.legacy.symdev"
+
int setup_target(int flag);
+int hald_running(void);
+LibHalContext *attach_to_hald(void);
+void detach_from_hald(LibHalContext *ctx, hal_state_t state);
+
void info(void);
void list(void);
void write_image(void);