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