4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <sys/types.h>
35 #include <dbus/dbus.h>
36 #include <hal/libhal.h>
44 #include "misc_scsi.h"
50 int keep_disc_open
= 0;
51 int requested_speed
= 0;
54 char *image_file
= NULL
;
55 char *blanking_type
= NULL
;
56 int audio_type
= AUDIO_TYPE_NONE
;
57 int extract_track_no
= 0;
58 char *extract_file
= NULL
;
59 char *alt_tmp_dir
= NULL
;
60 char *copy_src
= NULL
;
69 cd_device
*target
= NULL
; /* Default target device */
70 static char *tgtdev
= NULL
;
71 int device_type
= CD_RW
; /* Default to CD/RW */
72 int write_mode
= TAO_MODE
; /* Default to track at once */
77 err_msg(gettext("USAGE:\n"));
78 err_msg(gettext("\tcdrw -i [ -vSCO ] [ -d device ] [ -p speed ]"));
79 err_msg(gettext(" [ image-file ]\n"));
80 err_msg(gettext("\tcdrw -a [ -vSCO ] [ -d device ] [ -p speed ]"));
81 err_msg(gettext(" [ -T audio-type ] audio-file1 audio-file2 ...\n"));
82 err_msg(gettext("\tcdrw -x [ -v ] [ -d device ] [ -T audio-type ]"));
83 err_msg(gettext(" track-number audio-file\n"));
84 err_msg(gettext("\tcdrw -c [ -SC ] [ -d device ] [ -p speed ]"));
85 err_msg(gettext(" [ -m tmp-dir ] [ -s src-device ]\n"));
87 gettext("\tcdrw -b [ -v ] [ -d device ] all | session | fast\n"));
88 err_msg(gettext("\tcdrw -M [ -v ] [ -d device ]\n"));
89 err_msg(gettext("\tcdrw -L [ -v ] [ -d device ]\n"));
90 err_msg(gettext("\tcdrw -l [ -v ]\n"));
91 err_msg(gettext("\tcdrw -h\n"));
97 check_invalid_option(options
*specified
, char *opstr
)
102 set_options_mask(&c_op
, opstr
);
103 if ((ret
= compare_options_mask(&c_op
, specified
)) != 0) {
105 gettext("Option %c is not defined for this operation.\n"),
114 LibHalContext
*ctx
= NULL
;
115 DBusConnection
*con
= NULL
;
119 /* Initialize the dbus error states */
120 dbus_error_init(&error
);
122 if ((con
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
)) == NULL
) {
125 state
= DBUS_CONNECTION
;
127 /* Allocate a new hal context to work with the dbus */
128 if ((ctx
= libhal_ctx_new()) == NULL
)
132 /* Pair up the context with the connection */
133 if (!libhal_ctx_set_dbus_connection(ctx
, con
))
137 /* If libhal_ctx_init fails hald is not present */
138 if (!libhal_ctx_init(ctx
, &error
)) {
141 state
= HAL_INITIALIZED
;
145 if (dbus_error_is_set(&error
))
146 dbus_error_free(&error
);
147 detach_from_hald(ctx
, state
);
153 detach_from_hald(LibHalContext
*ctx
, hal_state_t state
)
156 DBusConnection
*con
= libhal_ctx_get_dbus_connection(ctx
);
158 dbus_error_init(&error
);
161 case HAL_INITIALIZED
:
162 if (libhal_ctx_shutdown(ctx
, &error
) == FALSE
)
163 if (dbus_error_is_set(&error
))
164 dbus_error_free(&error
);
167 (void) libhal_ctx_free(ctx
);
168 dbus_connection_unref(con
);
171 (void) libhal_ctx_free(ctx
);
173 case DBUS_CONNECTION
:
180 * This function returns one if hald is running and
181 * zero if hald is not running
188 if ((ctx
= attach_to_hald()) == NULL
)
191 detach_from_hald(ctx
, HAL_INITIALIZED
);
196 setup_target(int flag
)
200 if (tgtdev
!= NULL
) {
201 devpath
= (char *)my_zalloc(PATH_MAX
);
202 if (lookup_device(tgtdev
, devpath
)) {
203 target
= get_device(tgtdev
, devpath
);
206 if (target
== NULL
) {
211 return (scan_for_cd_device(flag
, &target
));
215 main(int argc
, char **argv
)
219 options specified_ops
;
220 int aflag
, iflag
, Mflag
, Lflag
, lflag
, bflag
, xflag
;
223 (void) setlocale(LC_ALL
, "");
225 #if !defined(TEXT_DOMAIN)
226 #define TEXT_DOMAIN "SYS_TEST"
230 (void) textdomain(TEXT_DOMAIN
);
235 if (check_auth(ruid
) != 1) {
237 "Authorization failed, Cannot access disks.\n"));
241 if ((cur_uid
== 0) && (ruid
!= 0)) {
242 priv_change_needed
= 1;
246 vol_running
= hald_running();
250 set_options_mask(&specified_ops
, "");
251 iflag
= Mflag
= Lflag
= lflag
= bflag
= aflag
= xflag
= cflag
= 0;
253 while ((c
= getopt(argc
, argv
, "abcCd:hiLlm:MOp:s:ST:vVx")) != EOF
) {
254 add_option(&specified_ops
, c
);
270 * cdrw now attempts to use the stated medium capacity
271 * by default, so this option no longer has any effect.
272 * It remains in the interface for backwards
273 * compatibility only.
280 print_usage(); /* will not return */
295 alt_tmp_dir
= optarg
;
305 requested_speed
= atoi(optarg
);
314 audio_type
= get_audio_type(optarg
);
315 if (audio_type
== -1) {
316 err_msg(gettext("Unknown audio type %s\n"),
326 * more verbose. this will print out debug comments
339 if (operations
== 0) {
340 err_msg(gettext("No operation specified.\n"));
343 if (operations
!= 1) {
344 err_msg(gettext("More than one operation specified.\n"));
349 check_invalid_option(&specified_ops
, "lhvV");
354 * we'll allow the user to specify the source device (-s) when
358 if (xflag
&& copy_src
)
362 * This will scan for all CD devices when xflag or Mflag
363 * (extract audio, list toc) commands are used, providing
364 * no CD-RW devices are found. Since these commands can
365 * be used without a CD writer.
368 if (xflag
|| Mflag
) {
369 ret
= setup_target(SCAN_ALL_CDS
);
371 ret
= setup_target(SCAN_WRITERS
);
376 if (tgtdev
!= NULL
) {
378 "Cannot find device %s.\n"), tgtdev
);
384 "No CD writers found or no media in the drive.\n"));
388 "Volume manager is not running.\n"));
390 "Please start volume manager or run cdrw as root to access all devices.\n"));
393 "No CD writers found.\n"));
398 } else if (ret
!= 1) {
399 err_msg(gettext("More than one CD device found.\n"));
400 err_msg(gettext("Specify one using -d option.\n"));
402 "Or use -l option to list all the CD devices found\n"));
405 (void) check_device(target
, CHECK_TYPE_NOT_CDROM
|EXIT_IF_CHECK_FAILED
);
407 if (check_device(target
, CHECK_NO_MEDIA
) == 0) {
409 for (retry
= 0; retry
< 5; retry
++) {
410 if (check_device(target
, CHECK_DEVICE_NOT_READY
) == 0)
417 check_invalid_option(&specified_ops
, "ahvSCOdpTV");
418 if (optind
== argc
) {
419 err_msg(gettext("No audio files specified.\n"));
422 write_audio(argv
, optind
, argc
);
425 check_invalid_option(&specified_ops
, "MhvdV");
429 check_invalid_option(&specified_ops
, "ihvSCOdpV");
430 if (optind
== (argc
- 1)) {
431 image_file
= argv
[optind
];
436 err_msg(gettext("Command line parsing error.\n"));
437 err_msg(gettext("Only one image-file can be specified.\n"));
441 check_invalid_option(&specified_ops
, "bhvdV");
442 if (optind
!= (argc
- 1)) {
443 err_msg(gettext("Command line parsing error.\n"));
446 blanking_type
= argv
[argc
- 1];
450 check_invalid_option(&specified_ops
, "xhpvdsTV");
451 if (optind
!= (argc
- 2)) {
452 err_msg(gettext("Command line parsing error.\n"));
455 extract_track_no
= atoi(argv
[argc
- 2]);
456 extract_file
= argv
[argc
- 1];
460 check_invalid_option(&specified_ops
, "chvSCdpmsV");
465 * Open a closed disk, we do this by erasing the track tail
466 * and then re-finalizing with an open leadout.
469 check_invalid_option(&specified_ops
, "LvdV");
470 (void) check_device(target
, CHECK_NO_MEDIA
|
471 CHECK_DEVICE_NOT_READY
| EXIT_IF_CHECK_FAILED
);
473 /* no need to erase blank media */
474 if (!check_device(target
, CHECK_MEDIA_IS_NOT_BLANK
))
477 blanking_type
= "leadout";
480 write_init(TRACK_MODE_DATA
);
481 (void) close_track(target
->d_fd
, 0, 1, 1);
482 (void) finalize(target
);
483 (void) printf(gettext("done.\n"));