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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/*
* 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 (c) 1995-1997 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* routines in this module are meant to be called by other libvolmgt
* routines only
*/
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <string.h>
#include <libintl.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/vol.h>
#ifdef DEBUG
#include <sys/varargs.h>
#endif
#include "volmgt_private.h"
/*
* We have been passed a path which (presumably) is a volume.
* We look through the directory until we find a name which is
* a character device.
*/
char *
getrawpart0(char *path)
{
DIR *dirp = NULL;
struct dirent64 *dp;
static char fname[MAXPATHLEN+1];
struct stat64 sb;
char *res;
int len;
/* open the directory */
if ((dirp = opendir(path)) == NULL) {
res = NULL;
goto dun;
}
/* get length of directory part */
len = strlen(path);
/* scan the directory */
while (dp = readdir64(dirp)) {
/* skip "." and ".." */
if (strcmp(dp->d_name, ".") == 0) {
continue;
}
if (strcmp(dp->d_name, "..") == 0) {
continue;
}
/* ensure we have room for this name */
if ((len + strlen(dp->d_name) + 1) > MAXPATHLEN) {
/* XXX: just give up? */
continue;
}
/* create a pathname for this device */
(void) concat_paths(fname, path, dp->d_name, NULL);
if (stat64(fname, &sb) < 0) {
continue; /* this shouldn't happen */
}
/* check for a char-spcl device */
if (S_ISCHR(sb.st_mode)) {
res = strdup(fname);
goto dun;
}
}
/* raw part not found */
res = NULL;
dun:
if (dirp != NULL) {
(void) closedir(dirp);
}
return (res);
}
/*
* fix the getfull{raw,blk}name problem for the fd and diskette case
*
* return value is malloc'ed, and must be free'd
*
* no match gets a malloc'ed null string
*/
char *
volmgt_getfullblkname(char *n)
{
extern char *getfullblkname(char *);
char *rval;
char namebuf[MAXPATHLEN+1];
char *s;
char c;
char *res;
/* try to get full block-spcl device name */
rval = getfullblkname(n);
if ((rval != NULL) && (*rval != NULLC)) {
/* found it */
res = rval;
goto dun;
}
/* we have a null-string result */
if (rval != NULL) {
/* free null string */
free(rval);
}
/* ok, so we either have a bad device or a floppy */
/* try the rfd# or rdiskette forms */
if (((s = strstr(n, "/rfd")) != NULL) ||
((s = strstr(n, "/rdiskette")) != NULL) ||
((s = strstr(n, "/rdsk/")) != NULL)) {
/*
* we do not have to check for room here, since we will
* be making the string one shorter
*/
c = *++s; /* save the first char */
*s = NULLC; /* replace it with a null */
(void) strcpy(namebuf, n); /* save first part of it */
*s++ = c; /* give the first char back */
(void) strcat(namebuf, s); /* copy the rest */
res = strdup(namebuf);
goto dun;
}
/* no match found */
res = strdup("");
dun:
return (res);
}
char *
volmgt_getfullrawname(char *n)
{
extern char *getfullrawname(char *);
char *rval;
char namebuf[MAXPATHLEN+1];
char *s;
char c;
char *res;
#ifdef DEBUG
denter("volmgt_getfullrawname(%s): entering\n", n);
#endif
/* try to get full char-spcl device name */
rval = getfullrawname(n);
if ((rval != NULL) && (*rval != NULLC)) {
/* found it */
res = rval;
goto dun;
}
/* we have a null-string result */
if (rval != NULL) {
/* free null string */
free(rval);
}
/* ok, so we either have a bad device or a floppy */
/* try the "fd", "diskette", and the "dsk" form */
if (((s = strstr(n, "/fd")) != NULL) ||
((s = strstr(n, "/diskette")) != NULL) ||
((s = strstr(n, "/dsk/")) != NULL)) {
/*
* ensure we have room to add one more char
*/
if (strlen(n) < (MAXPATHLEN - 1)) {
c = *++s; /* save the first char */
*s = NULLC; /* replace it with a null */
(void) strcpy(namebuf, n); /* save first part of str */
*s = c; /* put first charback */
(void) strcat(namebuf, "r"); /* insert an 'r' */
(void) strcat(namebuf, s); /* copy rest of str */
res = strdup(namebuf);
goto dun;
}
}
/* no match found */
res = strdup("");
dun:
#ifdef DEBUG
dexit("volmgt_getfullrawname: returning %s\n",
res ? res : "<null ptr>");
#endif
return (res);
}
/*
* volctl_name -- return name of volctl device
*/
const char *
volctl_name(void)
{
static char dev_name[] = "/dev/" VOLCTLNAME;
return (dev_name);
}
/*
* concat_paths -- create a pathname from two (or three) components
*
* truncate the result if it is too large
*
* assume that res has a defined length of MAXPATHLEN+1
*
* ("head" and "tail" are required, but "tail2" is optional)
*/
char *
concat_paths(char *res, char *head, char *tail, char *tail2)
{
int head_len = strlen(head);
int len_avail = MAXPATHLEN;
/* put in as much of the head as will fit */
(void) strncpy(res, head, len_avail);
len_avail -= head_len;
/* see if there is room to proceed */
if (len_avail > 0) {
char *cp = res + head_len;
/* there is room to append a slash */
*cp++ = '/';
len_avail--;
/* see if there is room to proceed */
if (len_avail > 0) {
int tail_len = strlen(tail);
/* there is room to append the tail */
(void) strncpy(cp, tail, len_avail);
cp += tail_len;
len_avail -= tail_len;
/* see if there is room to proceed */
if ((len_avail > 0) && (tail2 != NULL)) {
/* there is room to add tail2 (and need) */
(void) strncpy(cp, tail2, len_avail);
}
}
}
/* null terminate result (just in case) and return */
res[MAXPATHLEN] = NULLC;
return (res);
}
#ifdef DEBUG
/*
* debug print routines -- private to libvolmgt
*/
#define DEBUG_INDENT_SPACES " "
int debug_level = 0;
static void
derrprint(char *fmt, va_list ap)
{
int i;
int j;
char date_buf[256];
time_t t;
struct tm *tm;
(void) time(&t);
tm = localtime(&t);
(void) fprintf(stderr, "%02d/%02d/%02d %02d:%02d:%02d ",
tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100,
tm->tm_hour, tm->tm_min, tm->tm_sec);
for (i = 0; i < debug_level; i++) {
(void) fprintf(stderr, DEBUG_INDENT_SPACES);
}
(void) vfprintf(stderr, fmt, ap);
}
/*
* denter -- do a derrprint(), then increment debug level
*/
void
denter(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
derrprint(fmt, ap);
va_end(ap);
debug_level++;
}
/*
* dexit -- decrement debug level then do a derrprint()
*/
void
dexit(char *fmt, ...)
{
va_list ap;
if (--debug_level < 0) {
debug_level = 0;
}
va_start(ap, fmt);
derrprint(fmt, ap);
va_end(ap);
}
/*
* dprintf -- print debug info, indenting based on debug level
*/
void
dprintf(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
derrprint(fmt, ap);
va_end(ap);
}
#endif /* DEBUG */
|