1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include	<stdio.h>
29#include	<string.h>
30#include	<dirent.h>
31#include	<fcntl.h>
32#include	<string.h>
33#include	<errno.h>
34#include	<limits.h>
35#include	<unistd.h>
36#include	<sys/mkdev.h>
37#include	<volmgt.h>
38#include	<ctype.h>
39#include	<sys/types.h>
40#include	<sys/stat.h>
41#include	<sys/param.h>
42#include	"volmgt_private.h"
43
44/*
45 * arc approved interface
46 *	- can not be modified without approval from an arc
47 *
48 * committment level:
49 *	public
50 *
51 * description:
52 *	volmgt_running: check to see if volume management is running.
53 *
54 * arguments:
55 *	none.
56 *
57 * return value(s):
58 *	TRUE if volume management is running, FALSE if not.
59 *
60 * preconditions:
61 *	none.
62 */
63int
64volmgt_running(void)
65{
66	/* vold is dead */
67	return (FALSE);
68}
69
70
71/*
72 * arc approved interface
73 *	- can not be modified without approval from an arc
74 *
75 * committment level:
76 *	public
77 *
78 * description:
79 *	volmgt_inuse: check to see if volume management is currently
80 *	managing a particular device.
81 *
82 * arguments:
83 *	path - the name of the device in /dev.  For example,
84 *	  "/dev/rdiskette".
85 *
86 * return value(s):
87 *	TRUE if volume management is managing the device, FALSE if not.
88 *
89 * preconditions:
90 *	none.
91 */
92/* ARGSUSED */
93int
94volmgt_inuse(char *path)
95{
96	return (FALSE);
97}
98
99
100/*
101 * arc approved interface
102 *	- can not be modified without approval from an arc
103 *
104 * committment level:
105 *	public
106 *
107 * description:
108 *	volmgt_check: have volume management look at its devices to check
109 *	for media having arrived.  Since volume management can't
110 *	automatically check all types of devices, this function is provided
111 *	to allow applications to cause the check to happen automatically.
112 *
113 * arguments:
114 *	path - the name of the device in /dev.  For example,
115 *	  /dev/rdiskette.  If path is NULL, all "checkable" devices are
116 *	  checked.
117 *
118 * return value(s):
119 *	TRUE if media was found in the device, FALSE if not.
120 *
121 * preconditions:
122 *	volume management must be running.
123 */
124/* ARGSUSED */
125int
126volmgt_check(char *path)
127{
128	return (FALSE);
129}
130
131
132/*
133 * arc approved interface
134 *	- can not be modified without approval from an arc
135 *
136 * committment level:
137 *	public
138 *
139 * description:
140 *	volmgt_ownspath: check to see if the given path is contained in
141 *	the volume management name space.
142 *
143 * arguments:
144 *	path - string containing the path.
145 *
146 * return value(s):
147 *	TRUE if the path is owned by volume management, FALSE if not.
148 *	Will return FALSE if volume management isn't running.
149 *
150 * preconditions:
151 *	none.
152 */
153/* ARGSUSED */
154int
155volmgt_ownspath(char *path)
156{
157	return (FALSE);
158}
159
160
161/*
162 * arc approved interface
163 *	- can not be modified without approval from an arc
164 *
165 * committment level:
166 *	public
167 *
168 * description:
169 *	volmgt_root: return the root of where the volume management
170 *	name space is mounted.
171 *
172 * arguments:
173 *	none.
174 *
175 * return value(s):
176 *	Returns a pointer to a static string containing the path to the
177 *	volume management root (e.g. "/vol").
178 *	Will return NULL if volume management isn't running.
179 *
180 * preconditions:
181 *	none.
182 */
183const char *
184volmgt_root(void)
185{
186	static const char *vold_root = "/dev";
187
188	return (vold_root);
189}
190
191
192/*
193 * arc approved interface
194 *	- can not be modified without approval from an arc
195 *
196 * committment level:
197 *	public
198 *
199 * description:
200 *	volmgt_symname: Returns the volume management symbolic name
201 *	for a given device.  If an application wants to determine
202 *	what the symbolic name (e.g. "floppy0") for the /dev/rdiskette
203 *	device would be, this is the function to use.
204 *
205 * arguments:
206 *	path - a string containing the /dev device name.  For example,
207 *	"/dev/diskette" or "/dev/rdiskette".
208 *
209 *	Note: must be a block- or char-spcl device, and have a non-zero
210 *	st_rdev (real device) stat() value.
211 *
212 * return value(s):
213 *	pointer to a string containing the symbolic name.
214 *
215 *	NULL indicates that volume management isn't managing that device.
216 *
217 *	The string must be free(3)'d.
218 *
219 * preconditions:
220 *	none.
221 */
222/* ARGSUSED */
223char *
224volmgt_symname(char *path)
225{
226	return (NULL);
227}
228
229
230/*
231 * arc approved interface
232 *	- can not be modified without approval from an arc
233 *
234 * committment level:
235 *	public
236 *
237 * description:
238 *	volmgt_symdev: Returns the device given the volume management
239 *	symbolic name. If an application wants to determine
240 *	what the device associated with a particular symbolic name
241 *	might be, this is the function to use.
242 *
243 * arguments:
244 *	path - a string containing the symbolic device name.  For example,
245 *	"cdrom0" or "floppy0".
246 *
247 * return value(s):
248 *	pointer to a string containing the /dev name.
249 *
250 *	NULL indicates that volume management isn't managing that device.
251 *
252 *	The string must be free(3)'d.
253 *
254 * preconditions:
255 *	none.
256 */
257/* ARGSUSED */
258char *
259volmgt_symdev(char *symname)
260{
261	return (NULL);
262}
263
264
265/*
266 * arc approved interface
267 *	- can not be modified without approval from an arc
268 *
269 * committment level:
270 *	public
271 *
272 * description:
273 *	volmgt_feat_enabled: check to see if a volume management feature
274 *	is available
275 *
276 * arguments:
277 *	feat_str - a string containing the feature to be checked for
278 *
279 * return value(s):
280 *	return non-zero if the specified feature is available in
281 *	volume management, else return zero
282 *
283 * preconditions:
284 *	none.
285 */
286
287
288/*
289 * the following is a lit of the "feature" available in volmgt
290 *
291 * this list is meant to be updated when new features (that users may
292 * want to use) are added to volmgt
293 *
294 * note: feature strings added should be all lower case, and spaces are
295 * discouraged
296 *
297 * (see psarc/1995/138 for more info)
298 */
299static char	*volmgt_feat_list[] = {
300#ifdef	DIRECT_DEV_ACCESS_WORKING
301	"direct-dev-access",		/* access through /dev co-exists */
302#endif
303	"floppy-summit-interfaces",	/* volmgt_{acquire,release} */
304	NULL
305};
306
307
308int
309volmgt_feature_enabled(char *feat_str)
310{
311	return (0);
312}
313/*
314 * arc approved interface
315 *	- can not be modified without approval from an arc
316 *
317 * committment level:
318 *	uncommitted
319 *
320 * description:
321 *	volmgt_acquire: try to acquire the volmgt advisory device reservation
322 *	for a specific device.
323 *
324 * arguments:
325 *	dev - a device name to attempt reserving.  This string can be:
326 *		- a full path name to a device
327 *		- a symbolic device name (e.g. floppy0)
328 *
329 *	id  - a reservation string that hopefully describes the application
330 *		making this reservation.
331 *
332 *	pid - a pointer to a pid_t type.  If this argument is not NULL
333 *		and the requested device is already reserved, the process
334 *		id of the reservation owner will be returned in this
335 *		location.
336 *
337 *	ovr - an override indicator.  If set to non-zero, the caller requests
338 *		that this reservation be made unconditionally.
339 *
340 *	err - the address of a pointer to a string which is to receive the
341 *		id argument used when the current device was reserved.  This
342 *		is only used when the current reservation attempt fails due
343 *		to an already existing reservation for this device.
344 *
345 * return value(s):
346 *	A non-zero indicator if successful.
347 *
348 *	A zero indicator if unsuccessful.  If errno is EBUSY, then the err
349 *	argument will be set to point to the string that the process currently
350 *	holding the reservation supplied when reserving the device.  It is up
351 *	to the caller to release the storage occupied by the string via
352 *	free(3C) when no longer needed.
353 *
354 * preconditions:
355 *	none
356 */
357/* ARGSUSED */
358int
359volmgt_acquire(char *dev, char *id, int ovr, char **err, pid_t *pidp)
360{
361	return (0);
362}
363
364
365/*
366 * arc approved interface
367 *	- can not be modified without approval from an arc
368 *
369 * committment level:
370 *	uncommitted
371 *
372 * description:
373 *	volmgt_release: try to release the volmgt advisory device reservation
374 *	for a specific device.
375 *
376 * arguments:
377 *	dev - a device name to attempt reserving.  This string can be:
378 *		- a full path name to a device
379 *		- a symbolic device name (e.g. floppy0)
380 *
381 * return value(s):
382 *	A non-zero indicator if successful
383 *	A zero indicator if unsuccessful
384 *
385 * preconditions:
386 *	none
387 */
388int
389volmgt_release(char *dev)
390{
391	return (0);
392}
393
394
395/*
396 * returns the "value" of the attribute.
397 * If the attribute is boolean and is TRUE,
398 * "true" is returned.  If the boolean is
399 * FALSE, NULL is returned.  If the attribute
400 * doesn't exist, NULL is returned.  The pointer
401 * returned by media_getattr has been malloc'd and
402 * it is the callers responsibility to free it.
403 */
404/*
405 * arc approved interface
406 *	- can not be modified without approval from an arc
407 *
408 * committment level:
409 *	public
410 *
411 * description:
412 *	media_getattr: returns the value for an attribute for a piece of
413 * 	removable media.
414 *
415 * arguments:
416 *	path - Path to the media in /vol.  Can be the block or character
417 *		device.
418 *
419 *	attr - name of the attribute.
420 *
421 * return value(s):
422 *	returns NULL or a pointer to a string that contains the value for
423 * 	the requested attribute.
424 *
425 *	NULL can mean:
426 *	 - the media doesn't exist
427 *	 - there is no more space for malloc(3)
428 *	 - the attribute doesn't exist for the named media
429 *	 - the attribute is a boolean and is FALSE
430 *
431 *	the pointer to the string must be free'd with free(3).
432 *
433 * preconditions:
434 *	volume management (vold) must be running.
435 */
436/* ARGSUSED */
437char *
438media_getattr(char *vol_path, char *attr)
439{
440	return (NULL);
441}
442
443
444/*
445 * sets the attribute "attr" to value "value".
446 *
447 * If value == "" the flag is
448 * considered to be a TRUE boolean.
449 *
450 * If value == 0, it is considered to be a FALSE boolean.
451 * returns TRUE on success, FALSE on failure.
452 *
453 * Can fail for reasons of permission, or if you
454 * write a read-only attribute.
455 */
456
457/*
458 * arc approved interface
459 *	- can not be modified without approval from an arc
460 *
461 * committment level:
462 *	public
463 *
464 * description:
465 *	media_setattr: set an attribute for a piece of media to a
466 *	particular value.
467 *
468 * arguments:
469 *	path - Path to the media in /vol.  Can be the block or character
470 *		device.
471 *
472 *	attr - name of the attribute.
473 *
474 *	value - value of the attribute.  If value == "", the flag is
475 *		considered to be a boolean that is TRUE.  If value == 0, it
476 *		is considered to be a FALSE boolean.
477 *
478 * return value(s):
479 *	TRUE on success, FALSE for failure.
480 *
481 *	Can fail because:
482 *		- don't have permission to set the attribute because caller
483 *		  is not the owner of the media and attribute is a "system"
484 *		  attribute.
485 *
486 *		- don't have permission to set the attribute because the
487 *		  attribute is a "system" attribute and is read-only.
488 *
489 * preconditions:
490 *	volume management must be running.
491 */
492/* ARGSUSED */
493int
494media_setattr(char *vol_path, char *attr, char *value)
495{
496	return (FALSE);
497}
498
499
500/*
501 * Returns the "id" of a volume.  If the returned value
502 * & VOLID_TMP, the volume is temporary and this value
503 * cannot be relied upon across reboots.
504 */
505/*
506 * arc approved interface
507 *	- can not be modified without approval from an arc
508 *
509 * committment level:
510 *	public
511 *
512 * description:
513 *	media_getid: return the "id" of a piece of media.
514 *
515 * arguments:
516 *	path - Path to the media in /vol.  Can be the block or character
517 *		device.
518 * return value(s):
519 *	returns a u_longlong_t that is the "id" of the volume.
520 *
521 * preconditions:
522 *	volume management must be running.
523 */
524u_longlong_t
525media_getid(char *vol_path)
526{
527	return (0);
528}
529/*
530 * arc approved interface (pending)
531 *	- can not be modified without approval from an arc
532 *
533 * committment level:
534 *	public
535 *
536 * description:
537 *	media_findname: try to come up with the character device when
538 *	provided with a starting point.  This interface provides the
539 *	application programmer to provide "user friendly" names and
540 *	easily determine the "/vol" name.
541 *
542 * arguments:
543 *	start - a string describing a device.  This string can be:
544 *		- a full path name to a device (insures it's a
545 *		  character device by using getfullrawname()).
546 *		- a full path name to a volume management media name
547 *		  with partitions (will return the lowest numbered
548 *		  raw partition.
549 *		- the name of a piece of media (e.g. "fred").
550 *		- a symbolic device name (e.g. floppy0, cdrom0, etc)
551 *		- a name like "floppy" or "cdrom".  Will pick the lowest
552 *		  numbered device with media in it.
553 *
554 * return value(s):
555 *	A pointer to a string that contains the character device
556 *	most appropriate to the "start" argument.
557 *
558 *	NULL indicates that we were unable to find media based on "start".
559 *
560 *	The string must be free(3)'d.
561 *
562 * preconditions:
563 *	none.
564 */
565/* ARGSUSED */
566char *
567media_findname(char *start)
568{
569	/*
570	 * Eventually should implement using HAL interfaces.
571	 * In the short term however, return NULL for aliases,
572	 * and self for absolute pathnames.
573	 */
574	if (start[0] == '/') {
575		return (strdup(start));
576	} else {
577		return (NULL);
578	}
579}
580
581struct alias {
582	char	*alias;
583	char	*name;
584};
585
586/*
587 * "old" aliases -- used to be used when vold wasn't running
588 */
589static struct alias device_aliases[] = {
590	{ "fd", "/dev/rdiskette" },
591	{ "fd0", "/dev/rdiskette" },
592	{ "fd1", "/dev/rdiskette1" },
593	{ "diskette", "/dev/rdiskette" },
594	{ "diskette0", "/dev/rdiskette0" },
595	{ "diskette1", "/dev/rdiskette1" },
596	{ "rdiskette", "/dev/rdiskette" },
597	{ "rdiskette0", "/dev/rdiskette0" },
598	{ "rdiskette1", "/dev/rdiskette1" },
599	{ "floppy", "/dev/rdiskette" },
600	{ "floppy0", "/dev/rdiskette0" },
601	{ "floppy1", "/dev/rdiskette1" },
602	{ "cd", "cdrom0" },
603	{ "cd0", "cdrom0" },
604	{ "cd1", "cdrom1" },
605	{ NULL, NULL }
606};
607
608/*
609 * This is an ON Consolidation Private interface.
610 */
611/* ARGSUSED */
612char *
613_media_oldaliases(char *start)
614{
615	struct alias	*s;
616	char		*p;
617	char		*res = NULL;
618
619	for (s = device_aliases; s->alias != NULL; s++) {
620		if (strcmp(start, s->alias) == 0) {
621			res = strdup(s->name);
622			break;
623		}
624	}
625
626	return (res);
627}
628
629
630/*
631 * This is an ON Consolidation Private interface.
632 *
633 * Print out the aliases available to the program user.  Changes
634 * depending in whether volume management is running.
635 */
636void
637_media_printaliases(void)
638{
639}
640