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