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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*4077f1edSminht * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <time.h>
377c478bd9Sstevel@tonic-gate #include <pwd.h>
387c478bd9Sstevel@tonic-gate #include <auth_attr.h>
397c478bd9Sstevel@tonic-gate #include <auth_list.h>
407c478bd9Sstevel@tonic-gate #include <secdb.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include "transport.h"
437c478bd9Sstevel@tonic-gate #include "util.h"
447c478bd9Sstevel@tonic-gate #include "mmc.h"
457c478bd9Sstevel@tonic-gate #include "msgs.h"
467c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
477c478bd9Sstevel@tonic-gate #include "main.h"
487c478bd9Sstevel@tonic-gate #include "trackio.h"
497c478bd9Sstevel@tonic-gate #include "bstream.h"
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate char strbuf[81];
527c478bd9Sstevel@tonic-gate int priv_change_needed = 0;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate void *
my_zalloc(size_t size)557c478bd9Sstevel@tonic-gate my_zalloc(size_t size)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate void *ret;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate ret = malloc(size);
607c478bd9Sstevel@tonic-gate if (ret == NULL) {
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /* Lets wait a sec. and try again */
637c478bd9Sstevel@tonic-gate if (errno == EAGAIN) {
647c478bd9Sstevel@tonic-gate (void) sleep(1);
657c478bd9Sstevel@tonic-gate ret = malloc(size);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if (ret == NULL) {
697c478bd9Sstevel@tonic-gate (void) err_msg("%s\n", gettext(strerror(errno)));
707c478bd9Sstevel@tonic-gate (void) err_msg(gettext(
717c478bd9Sstevel@tonic-gate "Memory allocation failure, Exiting...\n"));
727c478bd9Sstevel@tonic-gate exit(1);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate (void) memset(ret, 0, size);
767c478bd9Sstevel@tonic-gate return (ret);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Prints a string after going back pos number of steps.
817c478bd9Sstevel@tonic-gate * Mainly used to show %age complete.
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate int
str_print(char * str,int pos)847c478bd9Sstevel@tonic-gate str_print(char *str, int pos)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate if ((pos > 0) && (pos < 80)) {
877c478bd9Sstevel@tonic-gate (void) memset(strbuf, 8, pos);
887c478bd9Sstevel@tonic-gate strbuf[pos] = 0;
897c478bd9Sstevel@tonic-gate (void) printf(strbuf);
907c478bd9Sstevel@tonic-gate (void) memset(strbuf, ' ', pos);
917c478bd9Sstevel@tonic-gate strbuf[pos] = 0;
927c478bd9Sstevel@tonic-gate (void) printf(strbuf);
937c478bd9Sstevel@tonic-gate (void) memset(strbuf, 8, pos);
947c478bd9Sstevel@tonic-gate strbuf[pos] = 0;
957c478bd9Sstevel@tonic-gate (void) printf(strbuf);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate (void) printf("%s", str);
997c478bd9Sstevel@tonic-gate (void) fflush(stdout);
1007c478bd9Sstevel@tonic-gate return (strlen(str));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * dump the trackio_error struct.
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate void
print_trackio_error(struct trackio_error * te)1077c478bd9Sstevel@tonic-gate print_trackio_error(struct trackio_error *te)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate char *msg, *msg1;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate msg = gettext("System could not supply data at the required rate.\n");
1127c478bd9Sstevel@tonic-gate msg1 = gettext("Try using a lower speed for write\n");
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate switch (te->err_type) {
1157c478bd9Sstevel@tonic-gate case TRACKIO_ERR_SYSTEM:
1167c478bd9Sstevel@tonic-gate err_msg(gettext("System error: %s\n"), strerror(te->te_errno));
1177c478bd9Sstevel@tonic-gate return;
1187c478bd9Sstevel@tonic-gate case TRACKIO_ERR_TRANSPORT:
1197c478bd9Sstevel@tonic-gate err_msg(gettext("Transport mechanism error:\n"));
1207c478bd9Sstevel@tonic-gate if (te->status == 2) {
1217c478bd9Sstevel@tonic-gate if ((te->key == 3) && (te->asc == 0x0c) &&
1227c478bd9Sstevel@tonic-gate (te->ascq == 9)) {
1237c478bd9Sstevel@tonic-gate err_msg(msg);
1247c478bd9Sstevel@tonic-gate err_msg(msg1);
1257c478bd9Sstevel@tonic-gate return;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate if (te->key == 3) {
1287c478bd9Sstevel@tonic-gate err_msg(gettext("Bad media.\n"));
1297c478bd9Sstevel@tonic-gate return;
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate if (debug) {
1327c478bd9Sstevel@tonic-gate err_msg("Sense key %x, asc/asq %x/%x\n",
1337c478bd9Sstevel@tonic-gate te->key, te->asc, te->ascq);
1347c478bd9Sstevel@tonic-gate } else {
1357c478bd9Sstevel@tonic-gate err_msg(gettext("I/O error\n"));
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate return;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate if (te->te_errno != 0)
1407c478bd9Sstevel@tonic-gate err_msg("%s\n", strerror(te->te_errno));
1417c478bd9Sstevel@tonic-gate return;
1427c478bd9Sstevel@tonic-gate case TRACKIO_ERR_USER_ABORT:
1437c478bd9Sstevel@tonic-gate err_msg(gettext("User abort.\n"));
1447c478bd9Sstevel@tonic-gate return;
1457c478bd9Sstevel@tonic-gate default:
1467c478bd9Sstevel@tonic-gate err_msg(gettext("Unknown error type.\n"));
1477c478bd9Sstevel@tonic-gate if (debug) {
1487c478bd9Sstevel@tonic-gate err_msg("Trackio err type %d\n", te->err_type);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate char *
get_err_str(void)1547c478bd9Sstevel@tonic-gate get_err_str(void)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate if (str_errno != 0)
1577c478bd9Sstevel@tonic-gate return (str_errno_to_string(str_errno));
1587c478bd9Sstevel@tonic-gate return (strerror(errno));
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate int
get_audio_type(char * ext)1627c478bd9Sstevel@tonic-gate get_audio_type(char *ext)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate if ((strcasecmp(ext, "au") == 0) ||
1657c478bd9Sstevel@tonic-gate (strcasecmp(ext, "sun") == 0))
1667c478bd9Sstevel@tonic-gate return (AUDIO_TYPE_SUN);
1677c478bd9Sstevel@tonic-gate if ((strcasecmp(ext, "wav") == 0) ||
1687c478bd9Sstevel@tonic-gate (strcasecmp(ext, "riff") == 0))
1697c478bd9Sstevel@tonic-gate return (AUDIO_TYPE_WAV);
1707c478bd9Sstevel@tonic-gate if (strcasecmp(ext, "cda") == 0)
1717c478bd9Sstevel@tonic-gate return (AUDIO_TYPE_CDA);
1727c478bd9Sstevel@tonic-gate if (strcasecmp(ext, "aur") == 0)
1737c478bd9Sstevel@tonic-gate return (AUDIO_TYPE_AUR);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate return (-1);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * common routines for showing progress.
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate int progress_pos;
1837c478bd9Sstevel@tonic-gate static uint64_t last_total;
1847c478bd9Sstevel@tonic-gate time_t tm;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate void
init_progress(void)1877c478bd9Sstevel@tonic-gate init_progress(void)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate progress_pos = 0;
1907c478bd9Sstevel@tonic-gate last_total = 0;
1917c478bd9Sstevel@tonic-gate tm = time(NULL);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate int
progress(int64_t arg,int64_t completed)1950c83a891Snakanon progress(int64_t arg, int64_t completed)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate char s[BUFSIZE];
198*4077f1edSminht uint64_t total = (uint64_t)arg;
199*4077f1edSminht
2007c478bd9Sstevel@tonic-gate if (completed == -1) {
2017c478bd9Sstevel@tonic-gate /* Got ^C. Add 2 to progress pos to compensate for ^ and C */
2027c478bd9Sstevel@tonic-gate progress_pos = str_print("(flushing ...)", progress_pos+2);
2037c478bd9Sstevel@tonic-gate return (0);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate if (total == 0) {
2067c478bd9Sstevel@tonic-gate if (tm != time(NULL)) {
2077c478bd9Sstevel@tonic-gate tm = time(NULL);
2087c478bd9Sstevel@tonic-gate (void) snprintf(s, BUFSIZE,
2097c478bd9Sstevel@tonic-gate gettext("%d bytes written"), completed);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate progress_pos = str_print(s, progress_pos);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate } else {
2147c478bd9Sstevel@tonic-gate total = (((uint64_t)completed) * 100)/total;
2157c478bd9Sstevel@tonic-gate if (total == last_total)
2167c478bd9Sstevel@tonic-gate return (0);
2177c478bd9Sstevel@tonic-gate last_total = total;
2187c478bd9Sstevel@tonic-gate if (total > 100) {
2197c478bd9Sstevel@tonic-gate /* There is clearly a miscalculation somewhere */
2207c478bd9Sstevel@tonic-gate if (debug)
2217c478bd9Sstevel@tonic-gate (void) printf("\nWrote more than 100 %% !!\n");
2227c478bd9Sstevel@tonic-gate return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate if (total == 100) {
2257c478bd9Sstevel@tonic-gate /* l10n_NOTE : 'done' as in "Writing track 1...done" */
2267c478bd9Sstevel@tonic-gate (void) snprintf(s, BUFSIZE, gettext("done.\n"));
2277c478bd9Sstevel@tonic-gate } else {
2287c478bd9Sstevel@tonic-gate (void) snprintf(s, BUFSIZE, "%d %%", (uint_t)total);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate progress_pos = str_print(s, progress_pos);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate return (0);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate void
raise_priv(void)2367c478bd9Sstevel@tonic-gate raise_priv(void)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate if (priv_change_needed && (cur_uid != 0)) {
2397c478bd9Sstevel@tonic-gate if (seteuid(0) == 0)
2407c478bd9Sstevel@tonic-gate cur_uid = 0;
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate void
lower_priv(void)2457c478bd9Sstevel@tonic-gate lower_priv(void)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate if (priv_change_needed && (cur_uid == 0)) {
2487c478bd9Sstevel@tonic-gate if (seteuid(ruid) == 0)
2497c478bd9Sstevel@tonic-gate cur_uid = ruid;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate int
check_auth(uid_t uid)2547c478bd9Sstevel@tonic-gate check_auth(uid_t uid)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate struct passwd *pw;
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate pw = getpwuid(uid);
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate if (pw == NULL) {
2627c478bd9Sstevel@tonic-gate /* fail if we cannot get password entry */
2637c478bd9Sstevel@tonic-gate return (0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * check in the RBAC authority files to see if
2687c478bd9Sstevel@tonic-gate * the user has permission to use CDRW
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate if (chkauthattr(CDRW_AUTH, pw->pw_name) != 1) {
2717c478bd9Sstevel@tonic-gate /* user is not in database, return failure */
2727c478bd9Sstevel@tonic-gate return (0);
2737c478bd9Sstevel@tonic-gate } else {
2747c478bd9Sstevel@tonic-gate return (1);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate * This will busy delay in ms milliseconds. Needed for cases
2807c478bd9Sstevel@tonic-gate * where 1 sec wait is too long. This is needed for some newer
2817c478bd9Sstevel@tonic-gate * drives which can empty the drive cache very quickly.
2827c478bd9Sstevel@tonic-gate */
2837c478bd9Sstevel@tonic-gate void
ms_delay(uint_t ms)2847c478bd9Sstevel@tonic-gate ms_delay(uint_t ms)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate hrtime_t start, req;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate start = gethrtime();
2907c478bd9Sstevel@tonic-gate req = start + ((hrtime_t)ms * 1000000);
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate while (gethrtime() < req)
2937c478bd9Sstevel@tonic-gate yield();
2947c478bd9Sstevel@tonic-gate }
295