xref: /titanic_51/usr/src/cmd/cdrw/main.c (revision b32163968bf7a125da3260bd894bf61aa6f013dd)
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	use_media_stated_capacity = 0;
517c478bd9Sstevel@tonic-gate int	keep_disc_open = 0;
527c478bd9Sstevel@tonic-gate int	requested_speed = 0;
537c478bd9Sstevel@tonic-gate int	simulation = 0;
547c478bd9Sstevel@tonic-gate int	verbose = 0;
557c478bd9Sstevel@tonic-gate char	*image_file = NULL;
567c478bd9Sstevel@tonic-gate char	*blanking_type = NULL;
577c478bd9Sstevel@tonic-gate int	audio_type = AUDIO_TYPE_NONE;
587c478bd9Sstevel@tonic-gate int	extract_track_no = 0;
597c478bd9Sstevel@tonic-gate char	*extract_file = NULL;
607c478bd9Sstevel@tonic-gate char	*alt_tmp_dir = NULL;
617c478bd9Sstevel@tonic-gate char	*copy_src = NULL;
627c478bd9Sstevel@tonic-gate int	vol_running = 0;
637c478bd9Sstevel@tonic-gate int	cflag = 0;
647c478bd9Sstevel@tonic-gate int	tflag = 0;
657c478bd9Sstevel@tonic-gate uid_t	ruid, cur_uid;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * global variables
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate cd_device *target = NULL;		/* Default target device */
717c478bd9Sstevel@tonic-gate static char *tgtdev = NULL;
727c478bd9Sstevel@tonic-gate int device_type = CD_RW;		/* Default to CD/RW */
737c478bd9Sstevel@tonic-gate int write_mode = TAO_MODE;		/* Default to track at once */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static void
767c478bd9Sstevel@tonic-gate print_usage(void)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	err_msg(gettext("USAGE:\n"));
797c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -i [ -vSCO ] [ -d device ] [ -p speed ]"));
807c478bd9Sstevel@tonic-gate 	err_msg(gettext(" [ image-file ]\n"));
817c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -a [ -vSCO ] [ -d device ] [ -p speed ]"));
827c478bd9Sstevel@tonic-gate 	err_msg(gettext(" [ -T audio-type ] audio-file1 audio-file2 ...\n"));
837c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -x [ -v ] [ -d device ] [ -T audio-type ]"));
847c478bd9Sstevel@tonic-gate 	err_msg(gettext(" track-number audio-file\n"));
85*b3216396Sanbui 	err_msg(gettext("\tcdrw -c [ -SC ] [ -d device ] [ -p speed ]"));
867c478bd9Sstevel@tonic-gate 	err_msg(gettext(" [ -m tmp-dir ] [ -s src-device ]\n"));
877c478bd9Sstevel@tonic-gate 	err_msg(
887c478bd9Sstevel@tonic-gate 	    gettext("\tcdrw -b [ -v ] [ -d device ] all | session | fast\n"));
897c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -M [ -v ] [ -d device ]\n"));
90*b3216396Sanbui 	err_msg(gettext("\tcdrw -L [ -v ] [ -d device ]\n"));
917c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -l [ -v ]\n"));
927c478bd9Sstevel@tonic-gate 	err_msg(gettext("\tcdrw -h\n"));
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	exit(2);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static void
987c478bd9Sstevel@tonic-gate check_invalid_option(options *specified, char *opstr)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	options c_op;
1017c478bd9Sstevel@tonic-gate 	int ret;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	set_options_mask(&c_op, opstr);
1047c478bd9Sstevel@tonic-gate 	if ((ret = compare_options_mask(&c_op, specified)) != 0) {
1057c478bd9Sstevel@tonic-gate 		err_msg(
1067c478bd9Sstevel@tonic-gate 		    gettext("Option %c is not defined for this operation.\n"),
1077c478bd9Sstevel@tonic-gate 		    (char)ret);
1087c478bd9Sstevel@tonic-gate 		print_usage();
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
112b0e06311Szk194757 LibHalContext *
113b0e06311Szk194757 attach_to_hald(void)
114b0e06311Szk194757 {
115b0e06311Szk194757 	LibHalContext *ctx = NULL;
116b0e06311Szk194757 	DBusConnection *con = NULL;
117b0e06311Szk194757 	DBusError error;
118b0e06311Szk194757 	hal_state_t state;
119b0e06311Szk194757 
120b0e06311Szk194757 	/* Initialize the dbus error states */
121b0e06311Szk194757 	dbus_error_init(&error);
122b0e06311Szk194757 
123b0e06311Szk194757 	if ((con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL) {
124b0e06311Szk194757 		return (NULL);
125b0e06311Szk194757 	}
126b0e06311Szk194757 	state = DBUS_CONNECTION;
127b0e06311Szk194757 
128b0e06311Szk194757 	/* Allocate a new hal context to work with the dbus */
129b0e06311Szk194757 	if ((ctx = libhal_ctx_new()) == NULL)
130b0e06311Szk194757 		return (NULL);
131b0e06311Szk194757 	state = HAL_CONTEXT;
132b0e06311Szk194757 
133b0e06311Szk194757 	/* Pair up the context with the connection */
134b0e06311Szk194757 	if (!libhal_ctx_set_dbus_connection(ctx, con))
135b0e06311Szk194757 		goto fail;
136b0e06311Szk194757 	state = HAL_PAIRED;
137b0e06311Szk194757 
138b0e06311Szk194757 	/* If libhal_ctx_init fails hald is not present */
139b0e06311Szk194757 	if (!libhal_ctx_init(ctx, &error)) {
140b0e06311Szk194757 		goto fail;
141b0e06311Szk194757 	}
142b0e06311Szk194757 	state = HAL_INITIALIZED;
143b0e06311Szk194757 
144b0e06311Szk194757 	return (ctx);
145b0e06311Szk194757 fail:
146b0e06311Szk194757 	if (dbus_error_is_set(&error))
147b0e06311Szk194757 		dbus_error_free(&error);
148b0e06311Szk194757 	detach_from_hald(ctx, state);
149b0e06311Szk194757 	return (NULL);
150b0e06311Szk194757 
151b0e06311Szk194757 }
152b0e06311Szk194757 
153b0e06311Szk194757 void
154b0e06311Szk194757 detach_from_hald(LibHalContext *ctx, hal_state_t state)
155b0e06311Szk194757 {
156b0e06311Szk194757 	DBusError error;
157b0e06311Szk194757 	DBusConnection *con = libhal_ctx_get_dbus_connection(ctx);
158b0e06311Szk194757 
159b0e06311Szk194757 	dbus_error_init(&error);
160b0e06311Szk194757 
161b0e06311Szk194757 	switch (state) {
162b0e06311Szk194757 	case HAL_INITIALIZED:
163b0e06311Szk194757 		if (libhal_ctx_shutdown(ctx, &error) == FALSE)
164b0e06311Szk194757 			if (dbus_error_is_set(&error))
165b0e06311Szk194757 				dbus_error_free(&error);
166b0e06311Szk194757 	/*FALLTHROUGH*/
167b0e06311Szk194757 	case HAL_PAIRED:
168b0e06311Szk194757 		(void) libhal_ctx_free(ctx);
169b0e06311Szk194757 		dbus_connection_unref(con);
170b0e06311Szk194757 		break;
171b0e06311Szk194757 	case HAL_CONTEXT:
172b0e06311Szk194757 		(void) libhal_ctx_free(ctx);
173b0e06311Szk194757 		break;
174b0e06311Szk194757 	case DBUS_CONNECTION:
175b0e06311Szk194757 	default:
176b0e06311Szk194757 		break;
177b0e06311Szk194757 	}
178b0e06311Szk194757 }
179b0e06311Szk194757 
180b0e06311Szk194757 /*
181b0e06311Szk194757  * This function returns one if hald is running and
182b0e06311Szk194757  * zero if hald is not running
183b0e06311Szk194757  */
184b0e06311Szk194757 int
185b0e06311Szk194757 hald_running(void)
186b0e06311Szk194757 {
187b0e06311Szk194757 	LibHalContext *ctx;
188b0e06311Szk194757 
189b0e06311Szk194757 	if ((ctx = attach_to_hald()) == NULL)
190b0e06311Szk194757 		return (0);
191b0e06311Szk194757 
192b0e06311Szk194757 	detach_from_hald(ctx, HAL_INITIALIZED);
193b0e06311Szk194757 	return (1);
194b0e06311Szk194757 }
195b0e06311Szk194757 
1967c478bd9Sstevel@tonic-gate int
1977c478bd9Sstevel@tonic-gate setup_target(int flag)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	char *devpath;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (tgtdev != NULL) {
2027c478bd9Sstevel@tonic-gate 		devpath = (char *)my_zalloc(PATH_MAX);
2037c478bd9Sstevel@tonic-gate 		if (lookup_device(tgtdev, devpath)) {
2047c478bd9Sstevel@tonic-gate 			target = get_device(tgtdev, devpath);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 		free(devpath);
2077c478bd9Sstevel@tonic-gate 		if (target == NULL) {
2087c478bd9Sstevel@tonic-gate 			return (0);
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 		return (1);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	return (scan_for_cd_device(flag, &target));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2150c83a891Snakanon int
2167c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	int		c;
2197c478bd9Sstevel@tonic-gate 	int		operations;
2207c478bd9Sstevel@tonic-gate 	options		specified_ops;
2217c478bd9Sstevel@tonic-gate 	int		aflag, iflag, Mflag, Lflag, lflag, bflag, xflag;
2227c478bd9Sstevel@tonic-gate 	int		ret;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2277c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2287c478bd9Sstevel@tonic-gate #endif
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	ruid = getuid();
2347c478bd9Sstevel@tonic-gate 	cur_uid = geteuid();
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (check_auth(ruid) != 1)  {
2377c478bd9Sstevel@tonic-gate 		err_msg(gettext(
2387c478bd9Sstevel@tonic-gate 		    "Authorization failed, Cannot access disks.\n"));
2397c478bd9Sstevel@tonic-gate 		exit(1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if ((cur_uid == 0) && (ruid != 0)) {
2437c478bd9Sstevel@tonic-gate 		priv_change_needed = 1;
2447c478bd9Sstevel@tonic-gate 		lower_priv();
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
247b0e06311Szk194757 	vol_running = hald_running();
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	tgtdev = NULL;
2507c478bd9Sstevel@tonic-gate 	operations = 0;
2517c478bd9Sstevel@tonic-gate 	set_options_mask(&specified_ops, "");
2527c478bd9Sstevel@tonic-gate 	iflag = Mflag = Lflag = lflag = bflag = aflag = xflag = cflag = 0;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "abcCd:hiLlm:MOp:s:ST:vVx")) != EOF) {
2557c478bd9Sstevel@tonic-gate 		add_option(&specified_ops, c);
2567c478bd9Sstevel@tonic-gate 		switch (c) {
2577c478bd9Sstevel@tonic-gate 		case 'a':
2587c478bd9Sstevel@tonic-gate 			aflag = 1;
2597c478bd9Sstevel@tonic-gate 			operations++;
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		case 'b':
2627c478bd9Sstevel@tonic-gate 			bflag = 1;
2637c478bd9Sstevel@tonic-gate 			operations++;
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 		case 'c':
2667c478bd9Sstevel@tonic-gate 			cflag = 1;
2677c478bd9Sstevel@tonic-gate 			operations++;
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 		case 'C':
2707c478bd9Sstevel@tonic-gate 			use_media_stated_capacity = 1;
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 		case 'd':
2737c478bd9Sstevel@tonic-gate 			tgtdev = optarg;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 		case 'h':
2767c478bd9Sstevel@tonic-gate 			print_usage(); /* will not return */
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		case 'i':
2797c478bd9Sstevel@tonic-gate 			iflag = 1;
2807c478bd9Sstevel@tonic-gate 			operations++;
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		case 'L':
2837c478bd9Sstevel@tonic-gate 			Lflag = 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 'm':
2917c478bd9Sstevel@tonic-gate 			alt_tmp_dir = optarg;
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 		case 'M':
2947c478bd9Sstevel@tonic-gate 			Mflag = 1;
2957c478bd9Sstevel@tonic-gate 			operations++;
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 		case 'O':
2987c478bd9Sstevel@tonic-gate 			keep_disc_open = 1;
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 		case 'p':
3017c478bd9Sstevel@tonic-gate 			requested_speed = atoi(optarg);
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 		case 's':
3047c478bd9Sstevel@tonic-gate 			copy_src = optarg;
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 		case 'S':
3077c478bd9Sstevel@tonic-gate 			simulation++;
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 		case 'T':
3107c478bd9Sstevel@tonic-gate 			audio_type = get_audio_type(optarg);
3117c478bd9Sstevel@tonic-gate 			if (audio_type == -1) {
3127c478bd9Sstevel@tonic-gate 				err_msg(gettext("Unknown audio type %s\n"),
3137c478bd9Sstevel@tonic-gate 				    optarg);
3147c478bd9Sstevel@tonic-gate 				exit(1);
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 		case 'v':
3187c478bd9Sstevel@tonic-gate 			verbose++;
3197c478bd9Sstevel@tonic-gate 			break;
3207c478bd9Sstevel@tonic-gate 		case 'V':
3217c478bd9Sstevel@tonic-gate 			/*
3227c478bd9Sstevel@tonic-gate 			 * more verbose. this will print out debug comments
3237c478bd9Sstevel@tonic-gate 			 */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			debug++;
3267c478bd9Sstevel@tonic-gate 			break;
3277c478bd9Sstevel@tonic-gate 		case 'x':
3287c478bd9Sstevel@tonic-gate 			xflag++;
3297c478bd9Sstevel@tonic-gate 			operations++;
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 		default:
3327c478bd9Sstevel@tonic-gate 			print_usage();
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	if (operations == 0) {
3367c478bd9Sstevel@tonic-gate 		err_msg(gettext("No operation specified.\n"));
3377c478bd9Sstevel@tonic-gate 		exit(1);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	if (operations != 1) {
3407c478bd9Sstevel@tonic-gate 		err_msg(gettext("More than one operation specified.\n"));
3417c478bd9Sstevel@tonic-gate 		exit(1);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (lflag) {
3457c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "lhvV");
3467c478bd9Sstevel@tonic-gate 		list();
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * we'll allow the user to specify the source device (-s) when
3517c478bd9Sstevel@tonic-gate 	 *  extracting audio.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (xflag && copy_src)
3557c478bd9Sstevel@tonic-gate 		tgtdev = copy_src;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * This will scan for all CD devices when xflag or Mflag
3597c478bd9Sstevel@tonic-gate 	 * (extract audio, list toc) commands are used, providing
3607c478bd9Sstevel@tonic-gate 	 * no CD-RW devices are found. Since these commands can
3617c478bd9Sstevel@tonic-gate 	 * be used without a CD writer.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (xflag || Mflag) {
3657c478bd9Sstevel@tonic-gate 		ret = setup_target(SCAN_ALL_CDS);
3667c478bd9Sstevel@tonic-gate 	} else {
3677c478bd9Sstevel@tonic-gate 		ret = setup_target(SCAN_WRITERS);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (ret == 0) {
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		if (tgtdev != NULL) {
3737c478bd9Sstevel@tonic-gate 			err_msg(gettext(
3747c478bd9Sstevel@tonic-gate 			    "Cannot find device %s.\n"), tgtdev);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		if (vol_running) {
3797c478bd9Sstevel@tonic-gate 			err_msg(gettext(
3807c478bd9Sstevel@tonic-gate 			    "No CD writers found or no media in the drive.\n"));
3817c478bd9Sstevel@tonic-gate 		} else {
3827c478bd9Sstevel@tonic-gate 			if (cur_uid != 0) {
3837c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3847c478bd9Sstevel@tonic-gate 				    "Volume manager is not running.\n"));
3857c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3867c478bd9Sstevel@tonic-gate "Please start volume manager or run cdrw as root to access all devices.\n"));
3877c478bd9Sstevel@tonic-gate 			} else {
3887c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3897c478bd9Sstevel@tonic-gate 				    "No CD writers found.\n"));
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		exit(1);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	} else if (ret != 1) {
3957c478bd9Sstevel@tonic-gate 		err_msg(gettext("More than one CD device found.\n"));
3967c478bd9Sstevel@tonic-gate 		err_msg(gettext("Specify one using -d option.\n"));
3977c478bd9Sstevel@tonic-gate 		err_msg(gettext(
3987c478bd9Sstevel@tonic-gate 		    "Or use -l option to list all the CD devices found\n"));
3997c478bd9Sstevel@tonic-gate 		exit(1);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	(void) check_device(target, CHECK_TYPE_NOT_CDROM|EXIT_IF_CHECK_FAILED);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (check_device(target, CHECK_NO_MEDIA) == 0) {
4047c478bd9Sstevel@tonic-gate 		int retry;
4057c478bd9Sstevel@tonic-gate 		for (retry = 0; retry < 5; retry++) {
4067c478bd9Sstevel@tonic-gate 			if (check_device(target, CHECK_DEVICE_NOT_READY) == 0)
4077c478bd9Sstevel@tonic-gate 				break;
4087c478bd9Sstevel@tonic-gate 			(void) sleep(3);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (aflag) {
4137c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "ahvSCOdpTV");
4147c478bd9Sstevel@tonic-gate 		if (optind == argc) {
4157c478bd9Sstevel@tonic-gate 			err_msg(gettext("No audio files specified.\n"));
4167c478bd9Sstevel@tonic-gate 			exit(1);
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 		write_audio(argv, optind, argc);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	if (Mflag) {
4217c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "MhvdV");
4227c478bd9Sstevel@tonic-gate 		info();
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	if (iflag) {
4257c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "ihvSCOdpV");
4267c478bd9Sstevel@tonic-gate 		if (optind == (argc - 1)) {
4277c478bd9Sstevel@tonic-gate 			image_file = argv[optind];
4287c478bd9Sstevel@tonic-gate 			write_image();
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		if (optind == argc)
4317c478bd9Sstevel@tonic-gate 			write_image();
4327c478bd9Sstevel@tonic-gate 		err_msg(gettext("Command line parsing error.\n"));
4337c478bd9Sstevel@tonic-gate 		err_msg(gettext("Only one image-file can be specified.\n"));
4347c478bd9Sstevel@tonic-gate 		exit(1);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	if (bflag) {
4377c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "bhvdV");
4387c478bd9Sstevel@tonic-gate 		if (optind != (argc - 1)) {
4397c478bd9Sstevel@tonic-gate 			err_msg(gettext("Command line parsing error.\n"));
4407c478bd9Sstevel@tonic-gate 			print_usage();
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		blanking_type = argv[argc - 1];
4437c478bd9Sstevel@tonic-gate 		blank();
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	if (xflag) {
4467c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "xhpvdsTV");
4477c478bd9Sstevel@tonic-gate 		if (optind != (argc - 2)) {
4487c478bd9Sstevel@tonic-gate 			err_msg(gettext("Command line parsing error.\n"));
4497c478bd9Sstevel@tonic-gate 			print_usage();
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		extract_track_no = atoi(argv[argc - 2]);
4527c478bd9Sstevel@tonic-gate 		extract_file = argv[argc - 1];
4537c478bd9Sstevel@tonic-gate 		extract_audio();
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	if (cflag) {
4567c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "chvSCdpmsV");
4577c478bd9Sstevel@tonic-gate 		copy_cd();
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * Open a closed disk, we do this by erasing the track tail
4627c478bd9Sstevel@tonic-gate 	 * and then re-finalizing with an open leadout.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	if (Lflag) {
4657c478bd9Sstevel@tonic-gate 		check_invalid_option(&specified_ops, "LvdV");
4667c478bd9Sstevel@tonic-gate 		(void) check_device(target, CHECK_NO_MEDIA |
4677c478bd9Sstevel@tonic-gate 		    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		/* no need to erase blank media */
4707c478bd9Sstevel@tonic-gate 		if (!check_device(target, CHECK_MEDIA_IS_NOT_BLANK))
4717c478bd9Sstevel@tonic-gate 			exit(0);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		blanking_type = "leadout";
4747c478bd9Sstevel@tonic-gate 		blank();
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		write_init(TRACK_MODE_DATA);
4777c478bd9Sstevel@tonic-gate 		(void) close_track(target->d_fd, 0, 1, 1);
4787c478bd9Sstevel@tonic-gate 		(void) finalize(target);
4797c478bd9Sstevel@tonic-gate 		(void) printf(gettext("done.\n"));
4807c478bd9Sstevel@tonic-gate 		exit(0);
4817c478bd9Sstevel@tonic-gate 	}
4820c83a891Snakanon 	return (0);
4837c478bd9Sstevel@tonic-gate }
484