xref: /titanic_44/usr/src/cmd/cdrw/main.c (revision 4df0f9a378789b084a55c575805f07ef00d35fe5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5b0e06311Szk194757  * Common Development and Distribution License (the "License").
6b0e06311Szk194757  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b0e06311Szk194757  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
35b0e06311Szk194757 #include <dbus/dbus.h>
36b0e06311Szk194757 #include <hal/libhal.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "msgs.h"
397c478bd9Sstevel@tonic-gate #include "device.h"
407c478bd9Sstevel@tonic-gate #include "util.h"
417c478bd9Sstevel@tonic-gate #include "main.h"
427c478bd9Sstevel@tonic-gate #include "options.h"
437c478bd9Sstevel@tonic-gate #include "mmc.h"
447c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * global flags
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate int	debug = 0;
507c478bd9Sstevel@tonic-gate int	keep_disc_open = 0;
517c478bd9Sstevel@tonic-gate int	requested_speed = 0;
527c478bd9Sstevel@tonic-gate int	simulation = 0;
537c478bd9Sstevel@tonic-gate int	verbose = 0;
547c478bd9Sstevel@tonic-gate char	*image_file = NULL;
557c478bd9Sstevel@tonic-gate char	*blanking_type = NULL;
567c478bd9Sstevel@tonic-gate int	audio_type = AUDIO_TYPE_NONE;
577c478bd9Sstevel@tonic-gate int	extract_track_no = 0;
587c478bd9Sstevel@tonic-gate char	*extract_file = NULL;
597c478bd9Sstevel@tonic-gate char	*alt_tmp_dir = NULL;
607c478bd9Sstevel@tonic-gate char	*copy_src = NULL;
617c478bd9Sstevel@tonic-gate int	vol_running = 0;
627c478bd9Sstevel@tonic-gate int	cflag = 0;
637c478bd9Sstevel@tonic-gate int	tflag = 0;
647c478bd9Sstevel@tonic-gate uid_t	ruid, cur_uid;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * global variables
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate cd_device *target = NULL;		/* Default target device */
707c478bd9Sstevel@tonic-gate static char *tgtdev = NULL;
717c478bd9Sstevel@tonic-gate int device_type = CD_RW;		/* Default to CD/RW */
727c478bd9Sstevel@tonic-gate int write_mode = TAO_MODE;		/* Default to track at once */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static void
print_usage(void)757c478bd9Sstevel@tonic-gate print_usage(void)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	err_msg(gettext("USAGE:\n"));
787c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -i [ -vSCO ] [ -d device ] [ -p speed ]"));
797c478bd9Sstevel@tonic-gate 	err_msg(gettext(" [ image-file ]\n"));
807c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -a [ -vSCO ] [ -d device ] [ -p speed ]"));
817c478bd9Sstevel@tonic-gate 	err_msg(gettext(" [ -T audio-type ] audio-file1 audio-file2 ...\n"));
827c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -x [ -v ] [ -d device ] [ -T audio-type ]"));
837c478bd9Sstevel@tonic-gate 	err_msg(gettext(" track-number audio-file\n"));
84b3216396Sanbui 	err_msg(gettext("\tcdrw -c [ -SC ] [ -d device ] [ -p speed ]"));
857c478bd9Sstevel@tonic-gate 	err_msg(gettext(" [ -m tmp-dir ] [ -s src-device ]\n"));
867c478bd9Sstevel@tonic-gate 	err_msg(
877c478bd9Sstevel@tonic-gate 	    gettext("\tcdrw -b [ -v ] [ -d device ] all | session | fast\n"));
887c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -M [ -v ] [ -d device ]\n"));
89b3216396Sanbui 	err_msg(gettext("\tcdrw -L [ -v ] [ -d device ]\n"));
907c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -l [ -v ]\n"));
917c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -h\n"));
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	exit(2);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void
check_invalid_option(options * specified,char * opstr)977c478bd9Sstevel@tonic-gate check_invalid_option(options *specified, char *opstr)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	options c_op;
1007c478bd9Sstevel@tonic-gate 	int ret;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	set_options_mask(&c_op, opstr);
1037c478bd9Sstevel@tonic-gate 	if ((ret = compare_options_mask(&c_op, specified)) != 0) {
1047c478bd9Sstevel@tonic-gate 		err_msg(
1057c478bd9Sstevel@tonic-gate 		    gettext("Option %c is not defined for this operation.\n"),
1067c478bd9Sstevel@tonic-gate 		    (char)ret);
1077c478bd9Sstevel@tonic-gate 		print_usage();
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
111b0e06311Szk194757 LibHalContext *
attach_to_hald(void)112b0e06311Szk194757 attach_to_hald(void)
113b0e06311Szk194757 {
114b0e06311Szk194757 	LibHalContext *ctx = NULL;
115b0e06311Szk194757 	DBusConnection *con = NULL;
116b0e06311Szk194757 	DBusError error;
117b0e06311Szk194757 	hal_state_t state;
118b0e06311Szk194757 
119b0e06311Szk194757 	/* Initialize the dbus error states */
120b0e06311Szk194757 	dbus_error_init(&error);
121b0e06311Szk194757 
122b0e06311Szk194757 	if ((con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL) {
123b0e06311Szk194757 		return (NULL);
124b0e06311Szk194757 	}
125b0e06311Szk194757 	state = DBUS_CONNECTION;
126b0e06311Szk194757 
127b0e06311Szk194757 	/* Allocate a new hal context to work with the dbus */
128b0e06311Szk194757 	if ((ctx = libhal_ctx_new()) == NULL)
129b0e06311Szk194757 		return (NULL);
130b0e06311Szk194757 	state = HAL_CONTEXT;
131b0e06311Szk194757 
132b0e06311Szk194757 	/* Pair up the context with the connection */
133b0e06311Szk194757 	if (!libhal_ctx_set_dbus_connection(ctx, con))
134b0e06311Szk194757 		goto fail;
135b0e06311Szk194757 	state = HAL_PAIRED;
136b0e06311Szk194757 
137b0e06311Szk194757 	/* If libhal_ctx_init fails hald is not present */
138b0e06311Szk194757 	if (!libhal_ctx_init(ctx, &error)) {
139b0e06311Szk194757 		goto fail;
140b0e06311Szk194757 	}
141b0e06311Szk194757 	state = HAL_INITIALIZED;
142b0e06311Szk194757 
143b0e06311Szk194757 	return (ctx);
144b0e06311Szk194757 fail:
145b0e06311Szk194757 	if (dbus_error_is_set(&error))
146b0e06311Szk194757 		dbus_error_free(&error);
147b0e06311Szk194757 	detach_from_hald(ctx, state);
148b0e06311Szk194757 	return (NULL);
149b0e06311Szk194757 
150b0e06311Szk194757 }
151b0e06311Szk194757 
152b0e06311Szk194757 void
detach_from_hald(LibHalContext * ctx,hal_state_t state)153b0e06311Szk194757 detach_from_hald(LibHalContext *ctx, hal_state_t state)
154b0e06311Szk194757 {
155b0e06311Szk194757 	DBusError error;
156b0e06311Szk194757 	DBusConnection *con = libhal_ctx_get_dbus_connection(ctx);
157b0e06311Szk194757 
158b0e06311Szk194757 	dbus_error_init(&error);
159b0e06311Szk194757 
160b0e06311Szk194757 	switch (state) {
161b0e06311Szk194757 	case HAL_INITIALIZED:
162b0e06311Szk194757 		if (libhal_ctx_shutdown(ctx, &error) == FALSE)
163b0e06311Szk194757 			if (dbus_error_is_set(&error))
164b0e06311Szk194757 				dbus_error_free(&error);
165b0e06311Szk194757 	/*FALLTHROUGH*/
166b0e06311Szk194757 	case HAL_PAIRED:
167b0e06311Szk194757 		(void) libhal_ctx_free(ctx);
168b0e06311Szk194757 		dbus_connection_unref(con);
169b0e06311Szk194757 		break;
170b0e06311Szk194757 	case HAL_CONTEXT:
171b0e06311Szk194757 		(void) libhal_ctx_free(ctx);
172b0e06311Szk194757 		break;
173b0e06311Szk194757 	case DBUS_CONNECTION:
174b0e06311Szk194757 	default:
175b0e06311Szk194757 		break;
176b0e06311Szk194757 	}
177b0e06311Szk194757 }
178b0e06311Szk194757 
179b0e06311Szk194757 /*
180b0e06311Szk194757  * This function returns one if hald is running and
181b0e06311Szk194757  * zero if hald is not running
182b0e06311Szk194757  */
183b0e06311Szk194757 int
hald_running(void)184b0e06311Szk194757 hald_running(void)
185b0e06311Szk194757 {
186b0e06311Szk194757 	LibHalContext *ctx;
187b0e06311Szk194757 
188b0e06311Szk194757 	if ((ctx = attach_to_hald()) == NULL)
189b0e06311Szk194757 		return (0);
190b0e06311Szk194757 
191b0e06311Szk194757 	detach_from_hald(ctx, HAL_INITIALIZED);
192b0e06311Szk194757 	return (1);
193b0e06311Szk194757 }
194b0e06311Szk194757 
1957c478bd9Sstevel@tonic-gate int
setup_target(int flag)1967c478bd9Sstevel@tonic-gate setup_target(int flag)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	char *devpath;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (tgtdev != NULL) {
2017c478bd9Sstevel@tonic-gate 		devpath = (char *)my_zalloc(PATH_MAX);
2027c478bd9Sstevel@tonic-gate 		if (lookup_device(tgtdev, devpath)) {
2037c478bd9Sstevel@tonic-gate 			target = get_device(tgtdev, devpath);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		free(devpath);
2067c478bd9Sstevel@tonic-gate 		if (target == NULL) {
2077c478bd9Sstevel@tonic-gate 			return (0);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 		return (1);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	return (scan_for_cd_device(flag, &target));
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2140c83a891Snakanon int
main(int argc,char ** argv)2157c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	int		c;
2187c478bd9Sstevel@tonic-gate 	int		operations;
2197c478bd9Sstevel@tonic-gate 	options		specified_ops;
2207c478bd9Sstevel@tonic-gate 	int		aflag, iflag, Mflag, Lflag, lflag, bflag, xflag;
2217c478bd9Sstevel@tonic-gate 	int		ret;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2267c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2277c478bd9Sstevel@tonic-gate #endif
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	ruid = getuid();
2337c478bd9Sstevel@tonic-gate 	cur_uid = geteuid();
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (check_auth(ruid) != 1)  {
2367c478bd9Sstevel@tonic-gate 		err_msg(gettext(
2377c478bd9Sstevel@tonic-gate 		    "Authorization failed, Cannot access disks.\n"));
2387c478bd9Sstevel@tonic-gate 		exit(1);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if ((cur_uid == 0) && (ruid != 0)) {
2427c478bd9Sstevel@tonic-gate 		priv_change_needed = 1;
2437c478bd9Sstevel@tonic-gate 		lower_priv();
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
246b0e06311Szk194757 	vol_running = hald_running();
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	tgtdev = NULL;
2497c478bd9Sstevel@tonic-gate 	operations = 0;
2507c478bd9Sstevel@tonic-gate 	set_options_mask(&specified_ops, "");
2517c478bd9Sstevel@tonic-gate 	iflag = Mflag = Lflag = lflag = bflag = aflag = xflag = cflag = 0;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "abcCd:hiLlm:MOp:s:ST:vVx")) != EOF) {
2547c478bd9Sstevel@tonic-gate 		add_option(&specified_ops, c);
2557c478bd9Sstevel@tonic-gate 		switch (c) {
2567c478bd9Sstevel@tonic-gate 		case 'a':
2577c478bd9Sstevel@tonic-gate 			aflag = 1;
2587c478bd9Sstevel@tonic-gate 			operations++;
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 		case 'b':
2617c478bd9Sstevel@tonic-gate 			bflag = 1;
2627c478bd9Sstevel@tonic-gate 			operations++;
2637c478bd9Sstevel@tonic-gate 			break;
2647c478bd9Sstevel@tonic-gate 		case 'c':
2657c478bd9Sstevel@tonic-gate 			cflag = 1;
2667c478bd9Sstevel@tonic-gate 			operations++;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		case 'C':
269*4df0f9a3Sec158148 			/*
270*4df0f9a3Sec158148 			 * cdrw now attempts to use the stated medium capacity
271*4df0f9a3Sec158148 			 * by default, so this option no longer has any effect.
272*4df0f9a3Sec158148 			 * It remains in the interface for backwards
273*4df0f9a3Sec158148 			 * compatibility only.
274*4df0f9a3Sec158148 			 */
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 		case 'd':
2777c478bd9Sstevel@tonic-gate 			tgtdev = optarg;
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case 'h':
2807c478bd9Sstevel@tonic-gate 			print_usage(); /* will not return */
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		case 'i':
2837c478bd9Sstevel@tonic-gate 			iflag = 1;
2847c478bd9Sstevel@tonic-gate 			operations++;
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 		case 'L':
2877c478bd9Sstevel@tonic-gate 			Lflag = 1;
2887c478bd9Sstevel@tonic-gate 			operations++;
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		case 'l':
2917c478bd9Sstevel@tonic-gate 			lflag = 1;
2927c478bd9Sstevel@tonic-gate 			operations++;
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 		case 'm':
2957c478bd9Sstevel@tonic-gate 			alt_tmp_dir = optarg;
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 		case 'M':
2987c478bd9Sstevel@tonic-gate 			Mflag = 1;
2997c478bd9Sstevel@tonic-gate 			operations++;
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 		case 'O':
3027c478bd9Sstevel@tonic-gate 			keep_disc_open = 1;
3037c478bd9Sstevel@tonic-gate 			break;
3047c478bd9Sstevel@tonic-gate 		case 'p':
3057c478bd9Sstevel@tonic-gate 			requested_speed = atoi(optarg);
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 		case 's':
3087c478bd9Sstevel@tonic-gate 			copy_src = optarg;
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 		case 'S':
3117c478bd9Sstevel@tonic-gate 			simulation++;
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 		case 'T':
3147c478bd9Sstevel@tonic-gate 			audio_type = get_audio_type(optarg);
3157c478bd9Sstevel@tonic-gate 			if (audio_type == -1) {
3167c478bd9Sstevel@tonic-gate 				err_msg(gettext("Unknown audio type %s\n"),
3177c478bd9Sstevel@tonic-gate 				    optarg);
3187c478bd9Sstevel@tonic-gate 				exit(1);
3197c478bd9Sstevel@tonic-gate 			}
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		case 'v':
3227c478bd9Sstevel@tonic-gate 			verbose++;
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 		case 'V':
3257c478bd9Sstevel@tonic-gate 			/*
3267c478bd9Sstevel@tonic-gate 			 * more verbose. this will print out debug comments
3277c478bd9Sstevel@tonic-gate 			 */
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 			debug++;
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 		case 'x':
3327c478bd9Sstevel@tonic-gate 			xflag++;
3337c478bd9Sstevel@tonic-gate 			operations++;
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 		default:
3367c478bd9Sstevel@tonic-gate 			print_usage();
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	if (operations == 0) {
3407c478bd9Sstevel@tonic-gate 		err_msg(gettext("No operation specified.\n"));
3417c478bd9Sstevel@tonic-gate 		exit(1);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	if (operations != 1) {
3447c478bd9Sstevel@tonic-gate 		err_msg(gettext("More than one operation specified.\n"));
3457c478bd9Sstevel@tonic-gate 		exit(1);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (lflag) {
3497c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "lhvV");
3507c478bd9Sstevel@tonic-gate 		list();
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/*
3547c478bd9Sstevel@tonic-gate 	 * we'll allow the user to specify the source device (-s) when
3557c478bd9Sstevel@tonic-gate 	 *  extracting audio.
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (xflag && copy_src)
3597c478bd9Sstevel@tonic-gate 		tgtdev = copy_src;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * This will scan for all CD devices when xflag or Mflag
3637c478bd9Sstevel@tonic-gate 	 * (extract audio, list toc) commands are used, providing
3647c478bd9Sstevel@tonic-gate 	 * no CD-RW devices are found. Since these commands can
3657c478bd9Sstevel@tonic-gate 	 * be used without a CD writer.
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (xflag || Mflag) {
3697c478bd9Sstevel@tonic-gate 		ret = setup_target(SCAN_ALL_CDS);
3707c478bd9Sstevel@tonic-gate 	} else {
3717c478bd9Sstevel@tonic-gate 		ret = setup_target(SCAN_WRITERS);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (ret == 0) {
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		if (tgtdev != NULL) {
3777c478bd9Sstevel@tonic-gate 			err_msg(gettext(
3787c478bd9Sstevel@tonic-gate 			    "Cannot find device %s.\n"), tgtdev);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		if (vol_running) {
3837c478bd9Sstevel@tonic-gate 			err_msg(gettext(
3847c478bd9Sstevel@tonic-gate 			    "No CD writers found or no media in the drive.\n"));
3857c478bd9Sstevel@tonic-gate 		} else {
3867c478bd9Sstevel@tonic-gate 			if (cur_uid != 0) {
3877c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3887c478bd9Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
3897c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3907c478bd9Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
3917c478bd9Sstevel@tonic-gate 			} else {
3927c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3937c478bd9Sstevel@tonic-gate 				    "No CD writers found.\n"));
3947c478bd9Sstevel@tonic-gate 			}
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 		exit(1);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	} else if (ret != 1) {
3997c478bd9Sstevel@tonic-gate 		err_msg(gettext("More than one CD device found.\n"));
4007c478bd9Sstevel@tonic-gate 		err_msg(gettext("Specify one using -d option.\n"));
4017c478bd9Sstevel@tonic-gate 		err_msg(gettext(
4027c478bd9Sstevel@tonic-gate 		    "Or use -l option to list all the CD devices found\n"));
4037c478bd9Sstevel@tonic-gate 		exit(1);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 	(void) check_device(target, CHECK_TYPE_NOT_CDROM|EXIT_IF_CHECK_FAILED);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (check_device(target, CHECK_NO_MEDIA) == 0) {
4087c478bd9Sstevel@tonic-gate 		int retry;
4097c478bd9Sstevel@tonic-gate 		for (retry = 0; retry < 5; retry++) {
4107c478bd9Sstevel@tonic-gate 			if (check_device(target, CHECK_DEVICE_NOT_READY) == 0)
4117c478bd9Sstevel@tonic-gate 				break;
4127c478bd9Sstevel@tonic-gate 			(void) sleep(3);
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (aflag) {
4177c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "ahvSCOdpTV");
4187c478bd9Sstevel@tonic-gate 		if (optind == argc) {
4197c478bd9Sstevel@tonic-gate 			err_msg(gettext("No audio files specified.\n"));
4207c478bd9Sstevel@tonic-gate 			exit(1);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		write_audio(argv, optind, argc);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	if (Mflag) {
4257c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "MhvdV");
4267c478bd9Sstevel@tonic-gate 		info();
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 	if (iflag) {
4297c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "ihvSCOdpV");
4307c478bd9Sstevel@tonic-gate 		if (optind == (argc - 1)) {
4317c478bd9Sstevel@tonic-gate 			image_file = argv[optind];
4327c478bd9Sstevel@tonic-gate 			write_image();
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		if (optind == argc)
4357c478bd9Sstevel@tonic-gate 			write_image();
4367c478bd9Sstevel@tonic-gate 		err_msg(gettext("Command line parsing error.\n"));
4377c478bd9Sstevel@tonic-gate 		err_msg(gettext("Only one image-file can be specified.\n"));
4387c478bd9Sstevel@tonic-gate 		exit(1);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	if (bflag) {
4417c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "bhvdV");
4427c478bd9Sstevel@tonic-gate 		if (optind != (argc - 1)) {
4437c478bd9Sstevel@tonic-gate 			err_msg(gettext("Command line parsing error.\n"));
4447c478bd9Sstevel@tonic-gate 			print_usage();
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		blanking_type = argv[argc - 1];
4477c478bd9Sstevel@tonic-gate 		blank();
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	if (xflag) {
4507c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "xhpvdsTV");
4517c478bd9Sstevel@tonic-gate 		if (optind != (argc - 2)) {
4527c478bd9Sstevel@tonic-gate 			err_msg(gettext("Command line parsing error.\n"));
4537c478bd9Sstevel@tonic-gate 			print_usage();
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 		extract_track_no = atoi(argv[argc - 2]);
4567c478bd9Sstevel@tonic-gate 		extract_file = argv[argc - 1];
4577c478bd9Sstevel@tonic-gate 		extract_audio();
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	if (cflag) {
4607c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "chvSCdpmsV");
4617c478bd9Sstevel@tonic-gate 		copy_cd();
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Open a closed disk, we do this by erasing the track tail
4667c478bd9Sstevel@tonic-gate 	 * and then re-finalizing with an open leadout.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	if (Lflag) {
4697c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "LvdV");
4707c478bd9Sstevel@tonic-gate 		(void) check_device(target, CHECK_NO_MEDIA |
4717c478bd9Sstevel@tonic-gate 		    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		/* no need to erase blank media */
4747c478bd9Sstevel@tonic-gate 		if (!check_device(target, CHECK_MEDIA_IS_NOT_BLANK))
4757c478bd9Sstevel@tonic-gate 			exit(0);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		blanking_type = "leadout";
4787c478bd9Sstevel@tonic-gate 		blank();
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		write_init(TRACK_MODE_DATA);
4817c478bd9Sstevel@tonic-gate 		(void) close_track(target->d_fd, 0, 1, 1);
4827c478bd9Sstevel@tonic-gate 		(void) finalize(target);
4837c478bd9Sstevel@tonic-gate 		(void) printf(gettext("done.\n"));
4847c478bd9Sstevel@tonic-gate 		exit(0);
4857c478bd9Sstevel@tonic-gate 	}
4860c83a891Snakanon 	return (0);
4877c478bd9Sstevel@tonic-gate }
488