summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cdrw/util.c
blob: 34db0b0478454028e47adeed8e06803a5c2507ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>
#include <time.h>
#include <pwd.h>
#include <auth_attr.h>
#include <auth_list.h>
#include <secdb.h>

#include "transport.h"
#include "util.h"
#include "mmc.h"
#include "msgs.h"
#include "misc_scsi.h"
#include "main.h"
#include "trackio.h"
#include "bstream.h"

char strbuf[81];
int priv_change_needed = 0;

void *
my_zalloc(size_t size)
{
	void *ret;

	ret = malloc(size);
	if (ret == NULL) {

		/* Lets wait a sec. and try again */
		if (errno == EAGAIN) {
			(void) sleep(1);
			ret = malloc(size);
		}

		if (ret == NULL) {
			(void) err_msg("%s\n", gettext(strerror(errno)));
			(void) err_msg(gettext(
			    "Memory allocation failure, Exiting...\n"));
			exit(1);
		}
	}
	(void) memset(ret, 0, size);
	return (ret);
}

/*
 * Prints a string after going back pos number of steps.
 * Mainly used to show %age complete.
 */
int
str_print(char *str, int pos)
{
	if ((pos > 0) && (pos < 80)) {
		(void) memset(strbuf, 8, pos);
		strbuf[pos] = 0;
		(void) printf(strbuf);
		(void) memset(strbuf, ' ', pos);
		strbuf[pos] = 0;
		(void) printf(strbuf);
		(void) memset(strbuf, 8, pos);
		strbuf[pos] = 0;
		(void) printf(strbuf);
	}

	(void) printf("%s", str);
	(void) fflush(stdout);
	return (strlen(str));
}

/*
 * dump the trackio_error struct.
 */
void
print_trackio_error(struct trackio_error *te)
{
	char *msg, *msg1;

	msg = gettext("System could not supply data at the required rate.\n");
	msg1 = gettext("Try using a lower speed for write\n");

	switch (te->err_type) {
	case TRACKIO_ERR_SYSTEM:
		err_msg(gettext("System error: %s\n"), strerror(te->te_errno));
		return;
	case TRACKIO_ERR_TRANSPORT:
		err_msg(gettext("Transport mechanism error:\n"));
		if (te->status == 2) {
			if ((te->key == 3) && (te->asc == 0x0c) &&
			    (te->ascq == 9)) {
				err_msg(msg);
				err_msg(msg1);
				return;
			}
			if (te->key == 3) {
				err_msg(gettext("Bad media.\n"));
				return;
			}
			if (debug) {
				err_msg("Sense key %x, asc/asq %x/%x\n",
				    te->key, te->asc, te->ascq);
			} else {
				err_msg(gettext("I/O error\n"));
			}
			return;
		}
		if (te->te_errno != 0)
			err_msg("%s\n", strerror(te->te_errno));
		return;
	case TRACKIO_ERR_USER_ABORT:
		err_msg(gettext("User abort.\n"));
		return;
	default:
		err_msg(gettext("Unknown error type.\n"));
		if (debug) {
			err_msg("Trackio err type %d\n", te->err_type);
		}
	}
}

char *
get_err_str(void)
{
	if (str_errno != 0)
		return (str_errno_to_string(str_errno));
	return (strerror(errno));
}

int
get_audio_type(char *ext)
{
	if ((strcasecmp(ext, "au") == 0) ||
	    (strcasecmp(ext, "sun") == 0))
		return (AUDIO_TYPE_SUN);
	if ((strcasecmp(ext, "wav") == 0) ||
	    (strcasecmp(ext, "riff") == 0))
		return (AUDIO_TYPE_WAV);
	if (strcasecmp(ext, "cda") == 0)
		return (AUDIO_TYPE_CDA);
	if (strcasecmp(ext, "aur") == 0)
		return (AUDIO_TYPE_AUR);

	return (-1);
}

/*
 * common routines for showing progress.
 */

int progress_pos;
static uint64_t last_total;
time_t tm;

void
init_progress(void)
{
	progress_pos = 0;
	last_total = 0;
	tm = time(NULL);
}

int
progress(int64_t arg, int64_t completed)
{
	char s[BUFSIZE];
	uint64_t total = (uint64_t)arg;

	if (completed == -1) {
		/* Got ^C. Add 2 to progress pos to compensate for ^ and C */
		progress_pos = str_print("(flushing ...)", progress_pos+2);
		return (0);
	}
	if (total == 0) {
		if (tm != time(NULL)) {
			tm = time(NULL);
			(void) snprintf(s, BUFSIZE,
			    gettext("%d bytes written"), completed);

			progress_pos = str_print(s, progress_pos);
		}
	} else {
		total = (((uint64_t)completed) * 100)/total;
		if (total == last_total)
			return (0);
		last_total = total;
		if (total > 100) {
			/* There is clearly a miscalculation somewhere */
			if (debug)
				(void) printf("\nWrote more than 100 %% !!\n");
			return (0);
		}
		if (total == 100) {
			/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
			(void) snprintf(s, BUFSIZE, gettext("done.\n"));
		} else {
			(void) snprintf(s, BUFSIZE, "%d %%", (uint_t)total);
		}
		progress_pos = str_print(s, progress_pos);
	}
	return (0);
}

void
raise_priv(void)
{
	if (priv_change_needed && (cur_uid != 0)) {
		if (seteuid(0) == 0)
			cur_uid = 0;
	}
}

void
lower_priv(void)
{
	if (priv_change_needed && (cur_uid == 0)) {
		if (seteuid(ruid) == 0)
			cur_uid = ruid;
	}
}

int
check_auth(uid_t uid)
{
	struct passwd *pw;


	pw = getpwuid(uid);

	if (pw == NULL) {
		/* fail if we cannot get password entry */
		return (0);
	}

	/*
	 * check in the RBAC authority files to see if
	 * the user has permission to use CDRW
	 */
	if (chkauthattr(CDRW_AUTH, pw->pw_name) != 1) {
		/* user is not in database, return failure */
		return (0);
	} else {
		return (1);
	}
}

/*
 * This will busy delay in ms milliseconds. Needed for cases
 * where 1 sec wait is too long. This is needed for some newer
 * drives which can empty the drive cache very quickly.
 */
void
ms_delay(uint_t ms)
{

	hrtime_t start, req;

	start = gethrtime();
	req = start + ((hrtime_t)ms * 1000000);

	while (gethrtime() < req)
		yield();
}