Merge illumos-gate
[unleashed.git] / usr / src / cmd / rmvolmgr / rmm_common.c
blob9eabde32f645aa27e149222b298c8fb406cfc388
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <stdarg.h>
31 #include <fcntl.h>
32 #include <libintl.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <ctype.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/mnttab.h>
41 #include <dbus/dbus.h>
42 #include <dbus/dbus-glib.h>
43 #include <dbus/dbus-glib-lowlevel.h>
44 #include <libhal.h>
45 #include <libhal-storage.h>
47 #include "rmm_common.h"
49 #define RMM_PRINT_DEVICE_WIDTH 20
51 extern int rmm_debug;
53 static const char *action_strings[] = {
54 "eject",
55 "mount",
56 "remount",
57 "unmount",
58 "clear_mounts",
59 "closetray"
63 LibHalContext *
64 rmm_hal_init(LibHalDeviceAdded devadd_cb, LibHalDeviceRemoved devrem_cb,
65 LibHalDevicePropertyModified propmod_cb, LibHalDeviceCondition cond_cb,
66 DBusError *error, rmm_error_t *rmm_error)
68 DBusConnection *dbus_conn;
69 LibHalContext *ctx;
70 char **devices;
71 int nr;
73 dbus_error_init(error);
76 * setup D-Bus connection
78 if (!(dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, error))) {
79 dprintf("cannot get system bus: %s\n", rmm_strerror(error, -1));
80 *rmm_error = RMM_EDBUS_CONNECT;
81 return (NULL);
83 rmm_dbus_error_free(error);
85 dbus_connection_setup_with_g_main(dbus_conn, NULL);
86 dbus_connection_set_exit_on_disconnect(dbus_conn, B_TRUE);
88 if ((ctx = libhal_ctx_new()) == NULL) {
89 dprintf("libhal_ctx_new failed");
90 *rmm_error = RMM_EHAL_CONNECT;
91 return (NULL);
94 libhal_ctx_set_dbus_connection(ctx, dbus_conn);
97 * register callbacks
99 if (devadd_cb != NULL) {
100 libhal_ctx_set_device_added(ctx, devadd_cb);
102 if (devrem_cb != NULL) {
103 libhal_ctx_set_device_removed(ctx, devrem_cb);
105 if (propmod_cb != NULL) {
106 libhal_ctx_set_device_property_modified(ctx, propmod_cb);
107 if (!libhal_device_property_watch_all(ctx, error)) {
108 dprintf("property_watch_all failed %s",
109 rmm_strerror(error, -1));
110 libhal_ctx_free(ctx);
111 *rmm_error = RMM_EHAL_CONNECT;
112 return (NULL);
115 if (cond_cb != NULL) {
116 libhal_ctx_set_device_condition(ctx, cond_cb);
119 if (!libhal_ctx_init(ctx, error)) {
120 dprintf("libhal_ctx_init failed: %s", rmm_strerror(error, -1));
121 libhal_ctx_free(ctx);
122 *rmm_error = RMM_EHAL_CONNECT;
123 return (NULL);
125 rmm_dbus_error_free(error);
128 * The above functions do not guarantee that HAL is actually running.
129 * Check by invoking a method.
131 if (!(devices = libhal_get_all_devices(ctx, &nr, error))) {
132 dprintf("HAL is not running: %s", rmm_strerror(error, -1));
133 libhal_ctx_shutdown(ctx, NULL);
134 libhal_ctx_free(ctx);
135 *rmm_error = RMM_EHAL_CONNECT;
136 return (NULL);
137 } else {
138 rmm_dbus_error_free(error);
139 libhal_free_string_array(devices);
142 return (ctx);
146 void
147 rmm_hal_fini(LibHalContext *hal_ctx)
149 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
151 (void) dbus_connection_unref(dbus_conn);
152 (void) libhal_ctx_free(hal_ctx);
157 * find volume from any type of name, similar to the old media_findname()
158 * returns the LibHalDrive object and a list of LibHalVolume objects.
160 LibHalDrive *
161 rmm_hal_volume_find(LibHalContext *hal_ctx, const char *name, DBusError *error,
162 GSList **volumes)
164 LibHalDrive *drive;
165 char *p;
166 char lastc;
168 *volumes = NULL;
170 /* temporarily remove trailing slash */
171 p = (char *)name + strlen(name) - 1;
172 if (*p == '/') {
173 lastc = *p;
174 *p = '\0';
175 } else {
176 p = NULL;
179 if (name[0] == '/') {
180 if (((drive = rmm_hal_volume_findby(hal_ctx,
181 "info.udi", name, volumes)) != NULL) ||
182 ((drive = rmm_hal_volume_findby(hal_ctx,
183 "block.device", name, volumes)) != NULL) ||
184 ((drive = rmm_hal_volume_findby(hal_ctx,
185 "block.solaris.raw_device", name, volumes)) != NULL) ||
186 ((drive = rmm_hal_volume_findby(hal_ctx,
187 "volume.mount_point", name, volumes)) != NULL)) {
188 goto out;
189 } else {
190 goto out;
194 /* try volume label */
195 if ((drive = rmm_hal_volume_findby(hal_ctx,
196 "volume.label", name, volumes)) != NULL) {
197 goto out;
200 drive = rmm_hal_volume_findby_nickname(hal_ctx, name, volumes);
202 out:
203 if (p != NULL) {
204 *p = lastc;
206 return (drive);
210 * find default volume. Returns volume pointer and name in 'name'.
212 LibHalDrive *
213 rmm_hal_volume_find_default(LibHalContext *hal_ctx, DBusError *error,
214 const char **name_out, GSList **volumes)
216 LibHalDrive *drive;
217 static const char *names[] = { "floppy", "cdrom", "rmdisk" };
218 int i;
220 *volumes = NULL;
222 for (i = 0; i < NELEM(names); i++) {
223 if ((drive = rmm_hal_volume_findby_nickname(hal_ctx,
224 names[i], volumes)) != NULL) {
226 * Skip floppy if it has no media.
227 * XXX might want to actually check for media
228 * every time instead of relying on volcheck.
230 if ((strcmp(names[i], "floppy") != 0) ||
231 libhal_device_get_property_bool(hal_ctx,
232 libhal_drive_get_udi(drive),
233 "storage.removable.media_available", NULL)) {
234 *name_out = names[i];
235 break;
238 rmm_dbus_error_free(error);
241 return (drive);
245 * find volume by property=value
246 * returns the LibHalDrive object and a list of LibHalVolume objects.
247 * XXX add support for multiple properties, reduce D-Bus traffic
249 LibHalDrive *
250 rmm_hal_volume_findby(LibHalContext *hal_ctx, const char *property,
251 const char *value, GSList **volumes)
253 DBusError error;
254 LibHalDrive *drive = NULL;
255 LibHalVolume *v = NULL;
256 char **udis;
257 int num_udis;
258 int i;
259 int i_drive = -1;
261 *volumes = NULL;
263 dbus_error_init(&error);
265 /* get all devices with property=value */
266 if ((udis = libhal_manager_find_device_string_match(hal_ctx, property,
267 value, &num_udis, &error)) == NULL) {
268 rmm_dbus_error_free(&error);
269 return (NULL);
272 /* find volumes and drives among these devices */
273 for (i = 0; i < num_udis; i++) {
274 rmm_dbus_error_free(&error);
275 if (libhal_device_query_capability(hal_ctx, udis[i], "volume",
276 &error)) {
277 v = libhal_volume_from_udi(hal_ctx, udis[i]);
278 if (v != NULL) {
279 *volumes = g_slist_prepend(*volumes, v);
281 } else if ((*volumes == NULL) &&
282 libhal_device_query_capability(hal_ctx, udis[i], "storage",
283 &error)) {
284 i_drive = i;
288 if (*volumes != NULL) {
289 /* used prepend, preserve original order */
290 *volumes = g_slist_reverse(*volumes);
292 v = (LibHalVolume *)(*volumes)->data;
293 drive = libhal_drive_from_udi(hal_ctx,
294 libhal_volume_get_storage_device_udi(v));
295 if (drive == NULL) {
296 rmm_volumes_free (*volumes);
297 *volumes = NULL;
299 } else if (i_drive >= 0) {
300 drive = libhal_drive_from_udi(hal_ctx, udis[i_drive]);
303 libhal_free_string_array(udis);
304 rmm_dbus_error_free(&error);
306 return (drive);
309 static void
310 rmm_print_nicknames_one(LibHalDrive *d, LibHalVolume *v,
311 const char *device, char **drive_nicknames)
313 const char *volume_label = NULL;
314 const char *mount_point = NULL;
315 boolean_t comma;
316 int i;
318 (void) printf("%-*s ", RMM_PRINT_DEVICE_WIDTH, device);
319 comma = B_FALSE;
321 if (drive_nicknames != NULL) {
322 for (i = 0; drive_nicknames[i] != NULL; i++) {
323 (void) printf("%s%s", comma ? "," : "",
324 drive_nicknames[i]);
325 comma = B_TRUE;
329 if ((v != NULL) &&
330 ((volume_label = libhal_volume_get_label(v)) != NULL) &&
331 (strlen(volume_label) > 0)) {
332 (void) printf("%s%s", comma ? "," : "", volume_label);
333 comma = B_TRUE;
336 if ((v != NULL) &&
337 ((mount_point = libhal_volume_get_mount_point(v)) != NULL) &&
338 (strlen(mount_point) > 0)) {
339 (void) printf("%s%s", comma ? "," : "", mount_point);
340 comma = B_TRUE;
343 (void) printf("\n");
347 * print nicknames for each available volume
349 * print_mask:
350 * RMM_PRINT_MOUNTABLE print only mountable volumes
351 * RMM_PRINT_EJECTABLE print volume-less ejectable drives
353 void
354 rmm_print_volume_nicknames(LibHalContext *hal_ctx, DBusError *error,
355 int print_mask)
357 char **udis;
358 int num_udis;
359 GSList *volumes = NULL;
360 LibHalDrive *d, *d_tmp;
361 LibHalVolume *v;
362 const char *device;
363 char **nicknames;
364 int i;
365 GSList *j;
366 int nprinted;
368 dbus_error_init(error);
370 if ((udis = libhal_find_device_by_capability(hal_ctx, "storage",
371 &num_udis, error)) == NULL) {
372 rmm_dbus_error_free(error);
373 return;
376 for (i = 0; i < num_udis; i++) {
377 if ((d = libhal_drive_from_udi(hal_ctx, udis[i])) == NULL) {
378 continue;
381 /* find volumes belonging to this drive */
382 if ((d_tmp = rmm_hal_volume_findby(hal_ctx,
383 "block.storage_device", udis[i], &volumes)) != NULL) {
384 libhal_drive_free(d_tmp);
387 nicknames = libhal_device_get_property_strlist(hal_ctx,
388 udis[i], "storage.solaris.nicknames", NULL);
390 nprinted = 0;
391 for (j = volumes; j != NULL; j = g_slist_next(j)) {
392 v = (LibHalVolume *)(j->data);
394 if ((device = libhal_volume_get_device_file(v)) ==
395 NULL) {
396 continue;
398 if ((print_mask & RMM_PRINT_MOUNTABLE) &&
399 (libhal_volume_get_fsusage(v) !=
400 LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM)) {
401 continue;
404 rmm_print_nicknames_one(d, v, device, nicknames);
405 nprinted++;
408 if ((nprinted == 0) &&
409 (print_mask & RMM_PRINT_EJECTABLE) &&
410 libhal_drive_requires_eject(d) &&
411 ((device = libhal_drive_get_device_file(d)) != NULL)) {
412 rmm_print_nicknames_one(d, NULL, device, nicknames);
415 libhal_free_string_array(nicknames);
416 libhal_drive_free(d);
417 rmm_volumes_free(volumes);
418 volumes = NULL;
421 libhal_free_string_array(udis);
425 * find volume by nickname
426 * returns the LibHalDrive object and a list of LibHalVolume objects.
428 LibHalDrive *
429 rmm_hal_volume_findby_nickname(LibHalContext *hal_ctx, const char *name,
430 GSList **volumes)
432 DBusError error;
433 LibHalDrive *drive = NULL;
434 LibHalDrive *drive_tmp;
435 char **udis;
436 int num_udis;
437 char **nicknames;
438 int i, j;
440 *volumes = NULL;
442 dbus_error_init(&error);
444 if ((udis = libhal_find_device_by_capability(hal_ctx, "storage",
445 &num_udis, &error)) == NULL) {
446 rmm_dbus_error_free(&error);
447 return (NULL);
450 /* find a drive by nickname */
451 for (i = 0; (i < num_udis) && (drive == NULL); i++) {
452 if ((nicknames = libhal_device_get_property_strlist(hal_ctx,
453 udis[i], "storage.solaris.nicknames", &error)) == NULL) {
454 rmm_dbus_error_free(&error);
455 continue;
457 for (j = 0; (nicknames[j] != NULL) && (drive == NULL); j++) {
458 if (strcmp(nicknames[j], name) == 0) {
459 drive = libhal_drive_from_udi(hal_ctx, udis[i]);
462 libhal_free_string_array(nicknames);
464 libhal_free_string_array(udis);
466 if (drive != NULL) {
467 /* found the drive, now find its volumes */
468 if ((drive_tmp = rmm_hal_volume_findby(hal_ctx,
469 "block.storage_device", libhal_drive_get_udi(drive),
470 volumes)) != NULL) {
471 libhal_drive_free(drive_tmp);
475 rmm_dbus_error_free(&error);
477 return (drive);
480 void
481 rmm_volumes_free(GSList *volumes)
483 GSList *i;
485 for (i = volumes; i != NULL; i = g_slist_next(i)) {
486 libhal_volume_free((LibHalVolume *)(i->data));
488 g_slist_free(volumes);
492 * Call HAL's Mount() method on the given device
494 boolean_t
495 rmm_hal_mount(LibHalContext *hal_ctx, const char *udi,
496 char **opts, int num_opts, char *mountpoint, DBusError *error)
498 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
499 DBusMessage *dmesg, *reply;
500 char *fstype;
502 dprintf("mounting %s...\n", udi);
504 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal", udi,
505 "org.freedesktop.Hal.Device.Volume", "Mount"))) {
506 dprintf(
507 "mount failed for %s: cannot create dbus message\n", udi);
508 return (B_FALSE);
511 fstype = "";
512 if (mountpoint == NULL) {
513 mountpoint = "";
516 if (!dbus_message_append_args(dmesg, DBUS_TYPE_STRING, &mountpoint,
517 DBUS_TYPE_STRING, &fstype,
518 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &opts, num_opts,
519 DBUS_TYPE_INVALID)) {
520 dprintf("mount failed for %s: cannot append args\n", udi);
521 dbus_message_unref(dmesg);
522 return (B_FALSE);
525 dbus_error_init(error);
526 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
527 dmesg, RMM_MOUNT_TIMEOUT, error))) {
528 dprintf("mount failed for %s: %s\n", udi, error->message);
529 dbus_message_unref(dmesg);
530 return (B_FALSE);
533 dprintf("mounted %s\n", udi);
535 dbus_message_unref(dmesg);
536 dbus_message_unref(reply);
538 rmm_dbus_error_free(error);
540 return (B_TRUE);
545 * Call HAL's Unmount() method on the given device
547 boolean_t
548 rmm_hal_unmount(LibHalContext *hal_ctx, const char *udi, DBusError *error)
550 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
551 DBusMessage *dmesg, *reply;
552 char **opts = NULL;
554 dprintf("unmounting %s...\n", udi);
556 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal", udi,
557 "org.freedesktop.Hal.Device.Volume", "Unmount"))) {
558 dprintf(
559 "unmount failed %s: cannot create dbus message\n", udi);
560 return (B_FALSE);
563 if (!dbus_message_append_args(dmesg, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
564 &opts, 0, DBUS_TYPE_INVALID)) {
565 dprintf("unmount failed %s: cannot append args\n", udi);
566 dbus_message_unref(dmesg);
567 return (B_FALSE);
570 dbus_error_init(error);
571 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
572 dmesg, RMM_UNMOUNT_TIMEOUT, error))) {
573 dprintf("unmount failed for %s: %s\n", udi, error->message);
574 dbus_message_unref(dmesg);
575 return (B_FALSE);
578 dprintf("unmounted %s\n", udi);
580 dbus_message_unref(dmesg);
581 dbus_message_unref(reply);
583 rmm_dbus_error_free(error);
585 return (B_TRUE);
590 * Call HAL's Eject() method on the given device
592 boolean_t
593 rmm_hal_eject(LibHalContext *hal_ctx, const char *udi, DBusError *error)
595 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
596 DBusMessage *dmesg, *reply;
597 char **options = NULL;
598 uint_t num_options = 0;
600 dprintf("ejecting %s...\n", udi);
602 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal", udi,
603 "org.freedesktop.Hal.Device.Storage", "Eject"))) {
604 dprintf("eject %s: cannot create dbus message\n", udi);
605 return (B_FALSE);
608 if (!dbus_message_append_args(dmesg,
609 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &options, num_options,
610 DBUS_TYPE_INVALID)) {
611 dprintf("eject %s: cannot append args to dbus message ", udi);
612 dbus_message_unref(dmesg);
613 return (B_FALSE);
616 dbus_error_init(error);
617 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
618 dmesg, RMM_EJECT_TIMEOUT, error))) {
619 dprintf("eject %s: %s\n", udi, error->message);
620 dbus_message_unref(dmesg);
621 return (B_FALSE);
624 dprintf("ejected %s\n", udi);
626 dbus_message_unref(dmesg);
627 dbus_message_unref(reply);
629 rmm_dbus_error_free(error);
631 return (B_TRUE);
635 * Call HAL's CloseTray() method on the given device
637 boolean_t
638 rmm_hal_closetray(LibHalContext *hal_ctx, const char *udi, DBusError *error)
640 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
641 DBusMessage *dmesg, *reply;
642 char **options = NULL;
643 uint_t num_options = 0;
645 dprintf("closing tray %s...\n", udi);
647 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal", udi,
648 "org.freedesktop.Hal.Device.Storage", "CloseTray"))) {
649 dprintf(
650 "closetray failed for %s: cannot create dbus message\n",
651 udi);
652 return (B_FALSE);
655 if (!dbus_message_append_args(dmesg,
656 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &options, num_options,
657 DBUS_TYPE_INVALID)) {
658 dprintf("closetray %s: cannot append args to dbus message ",
659 udi);
660 dbus_message_unref(dmesg);
661 return (B_FALSE);
664 dbus_error_init(error);
665 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
666 dmesg, RMM_CLOSETRAY_TIMEOUT, error))) {
667 dprintf("closetray failed for %s: %s\n", udi, error->message);
668 dbus_message_unref(dmesg);
669 return (B_FALSE);
672 dprintf("closetray ok %s\n", udi);
674 dbus_message_unref(dmesg);
675 dbus_message_unref(reply);
677 rmm_dbus_error_free(error);
679 return (B_TRUE);
683 * Call HAL's Rescan() method on the given device
685 boolean_t
686 rmm_hal_rescan(LibHalContext *hal_ctx, const char *udi, DBusError *error)
688 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
689 DBusMessage *dmesg, *reply;
691 dprintf("rescanning %s...\n", udi);
693 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal", udi,
694 "org.freedesktop.Hal.Device", "Rescan"))) {
695 dprintf("rescan failed for %s: cannot create dbus message\n",
696 udi);
697 return (B_FALSE);
700 dbus_error_init(error);
701 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
702 dmesg, -1, error))) {
703 dprintf("rescan failed for %s: %s\n", udi, error->message);
704 dbus_message_unref(dmesg);
705 return (B_FALSE);
708 dprintf("rescan ok %s\n", udi);
710 dbus_message_unref(dmesg);
711 dbus_message_unref(reply);
713 rmm_dbus_error_free(error);
715 return (B_TRUE);
718 boolean_t
719 rmm_hal_claim_branch(LibHalContext *hal_ctx, const char *udi)
721 DBusError error;
722 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
723 DBusMessage *dmesg, *reply;
724 const char *claimed_by = "rmvolmgr";
726 dprintf("claiming branch %s...\n", udi);
728 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal",
729 "/org/freedesktop/Hal/Manager", "org.freedesktop.Hal.Manager",
730 "ClaimBranch"))) {
731 dprintf("cannot create dbus message\n");
732 return (B_FALSE);
735 if (!dbus_message_append_args(dmesg, DBUS_TYPE_STRING, &udi,
736 DBUS_TYPE_STRING, &claimed_by, DBUS_TYPE_INVALID)) {
737 dprintf("cannot append args to dbus message\n");
738 dbus_message_unref(dmesg);
739 return (B_FALSE);
742 dbus_error_init(&error);
743 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
744 dmesg, -1, &error))) {
745 dprintf("cannot send dbus message\n");
746 dbus_message_unref(dmesg);
747 rmm_dbus_error_free(&error);
748 return (B_FALSE);
751 dprintf("claim branch ok %s\n", udi);
753 dbus_message_unref(dmesg);
754 dbus_message_unref(reply);
756 return (B_TRUE);
759 boolean_t
760 rmm_hal_unclaim_branch(LibHalContext *hal_ctx, const char *udi)
762 DBusError error;
763 DBusConnection *dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
764 DBusMessage *dmesg, *reply;
765 const char *claimed_by = "rmvolmgr";
767 dprintf("unclaiming branch %s...\n", udi);
769 if (!(dmesg = dbus_message_new_method_call("org.freedesktop.Hal",
770 "/org/freedesktop/Hal/Manager", "org.freedesktop.Hal.Manager",
771 "UnclaimBranch"))) {
772 dprintf("cannot create dbus message\n");
773 return (B_FALSE);
776 if (!dbus_message_append_args(dmesg, DBUS_TYPE_STRING, &udi,
777 DBUS_TYPE_STRING, &claimed_by, DBUS_TYPE_INVALID)) {
778 dprintf("cannot append args to dbus message\n");
779 dbus_message_unref(dmesg);
780 return (B_FALSE);
783 dbus_error_init(&error);
784 if (!(reply = dbus_connection_send_with_reply_and_block(dbus_conn,
785 dmesg, -1, &error))) {
786 dprintf("cannot send dbus message\n");
787 dbus_message_unref(dmesg);
788 rmm_dbus_error_free(&error);
789 return (B_FALSE);
792 dprintf("unclaim branch ok %s\n", udi);
794 dbus_message_unref(dmesg);
795 dbus_message_unref(reply);
797 return (B_TRUE);
800 static boolean_t
801 rmm_action_one(LibHalContext *hal_ctx, const char *name, action_t action,
802 const char *dev, const char *udi, LibHalVolume *v,
803 char **opts, int num_opts, char *mountpoint)
805 char dev_str[MAXPATHLEN];
806 char *mountp;
807 DBusError error;
808 boolean_t ret = B_FALSE;
810 if (strcmp(name, dev) == 0) {
811 (void) snprintf(dev_str, sizeof (dev_str), name);
812 } else {
813 (void) snprintf(dev_str, sizeof (dev_str), "%s %s", name, dev);
816 dbus_error_init(&error);
818 switch (action) {
819 case EJECT:
820 ret = rmm_hal_eject(hal_ctx, udi, &error);
821 break;
822 case INSERT:
823 case REMOUNT:
824 if (libhal_volume_is_mounted(v)) {
825 goto done;
827 ret = rmm_hal_mount(hal_ctx, udi,
828 opts, num_opts, mountpoint, &error);
829 break;
830 case UNMOUNT:
831 if (!libhal_volume_is_mounted(v)) {
832 goto done;
834 ret = rmm_hal_unmount(hal_ctx, udi, &error);
835 break;
836 case CLOSETRAY:
837 ret = rmm_hal_closetray(hal_ctx, udi, &error);
838 break;
841 if (!ret) {
842 (void) fprintf(stderr, gettext("%s of %s failed: %s\n"),
843 action_strings[action], dev_str, rmm_strerror(&error, -1));
844 goto done;
847 switch (action) {
848 case EJECT:
849 (void) printf(gettext("%s ejected\n"), dev_str);
850 break;
851 case INSERT:
852 case REMOUNT:
853 mountp = rmm_get_mnttab_mount_point(dev);
854 if (mountp != NULL) {
855 (void) printf(gettext("%s mounted at %s\n"),
856 dev_str, mountp);
857 free(mountp);
859 break;
860 case UNMOUNT:
861 (void) printf(gettext("%s unmounted\n"), dev_str);
862 break;
863 case CLOSETRAY:
864 (void) printf(gettext("%s tray closed\n"), dev_str);
865 break;
868 done:
869 rmm_dbus_error_free(&error);
870 return (ret);
874 * top level action routine
876 * If non-null 'aa' is passed, it will be used, otherwise a local copy
877 * will be created.
879 boolean_t
880 rmm_action(LibHalContext *hal_ctx, const char *name, action_t action,
881 struct action_arg *aap, char **opts, int num_opts, char *mountpoint)
883 DBusError error;
884 GSList *volumes, *i;
885 LibHalDrive *d;
886 LibHalVolume *v;
887 const char *udi, *d_udi;
888 const char *dev, *d_dev;
889 struct action_arg aa_local;
890 boolean_t ret = B_FALSE;
892 dprintf("rmm_action %s %s\n", name, action_strings[action]);
894 if (aap == NULL) {
895 bzero(&aa_local, sizeof (aa_local));
896 aap = &aa_local;
899 dbus_error_init(&error);
901 /* find the drive and its volumes */
902 d = rmm_hal_volume_find(hal_ctx, name, &error, &volumes);
903 rmm_dbus_error_free(&error);
904 if (d == NULL) {
905 (void) fprintf(stderr, gettext("cannot find '%s'\n"), name);
906 return (B_FALSE);
908 d_udi = libhal_drive_get_udi(d);
909 d_dev = libhal_drive_get_device_file(d);
910 if ((d_udi == NULL) || (d_dev == NULL)) {
911 goto out;
915 * For those drives that do not require media eject,
916 * EJECT turns into UNMOUNT.
918 if ((action == EJECT) && !libhal_drive_requires_eject(d)) {
919 action = UNMOUNT;
922 /* per drive action */
923 if ((action == EJECT) || (action == CLOSETRAY)) {
924 ret = rmm_action_one(hal_ctx, name, action, d_dev, d_udi, NULL,
925 opts, num_opts, NULL);
927 if (!ret || (action == CLOSETRAY)) {
928 goto out;
932 /* per volume action */
933 for (i = volumes; i != NULL; i = g_slist_next(i)) {
934 v = (LibHalVolume *)i->data;
935 udi = libhal_volume_get_udi(v);
936 dev = libhal_volume_get_device_file(v);
938 if ((udi == NULL) || (dev == NULL)) {
939 continue;
941 if (aap == &aa_local) {
942 if (!rmm_volume_aa_from_prop(hal_ctx, udi, v, aap)) {
943 dprintf("rmm_volume_aa_from_prop failed %s\n",
944 udi);
945 continue;
948 aap->aa_action = action;
950 /* ejected above, just need postprocess */
951 if (action != EJECT) {
952 ret = rmm_action_one(hal_ctx, name, action, dev, udi, v,
953 opts, num_opts, mountpoint);
955 if (ret) {
956 (void) vold_postprocess(hal_ctx, udi, aap);
959 if (aap == &aa_local) {
960 rmm_volume_aa_free(aap);
964 out:
965 rmm_volumes_free(volumes);
966 libhal_drive_free(d);
968 return (ret);
973 * rescan by name
974 * if name is NULL, rescan all drives
976 boolean_t
977 rmm_rescan(LibHalContext *hal_ctx, const char *name, boolean_t query)
979 DBusError error;
980 GSList *volumes;
981 LibHalDrive *drive = NULL;
982 const char *drive_udi;
983 char **udis;
984 int num_udis;
985 char *nickname;
986 char **nicks = NULL;
987 boolean_t do_free_udis = FALSE;
988 int i;
989 boolean_t ret = B_FALSE;
991 dprintf("rmm_rescan %s\n", name != NULL ? name : "all");
993 dbus_error_init(&error);
995 if (name != NULL) {
996 if ((drive = rmm_hal_volume_find(hal_ctx, name, &error,
997 &volumes)) == NULL) {
998 rmm_dbus_error_free(&error);
999 (void) fprintf(stderr,
1000 gettext("cannot find '%s'\n"), name);
1001 return (B_FALSE);
1003 rmm_dbus_error_free(&error);
1004 g_slist_free(volumes);
1006 drive_udi = libhal_drive_get_udi(drive);
1007 udis = (char **)&drive_udi;
1008 num_udis = 1;
1009 } else {
1010 if ((udis = libhal_find_device_by_capability(hal_ctx,
1011 "storage", &num_udis, &error)) == NULL) {
1012 rmm_dbus_error_free(&error);
1013 return (B_TRUE);
1015 rmm_dbus_error_free(&error);
1016 do_free_udis = TRUE;
1019 for (i = 0; i < num_udis; i++) {
1020 if (name == NULL) {
1021 nicks = libhal_device_get_property_strlist(hal_ctx,
1022 udis[i], "storage.solaris.nicknames", NULL);
1023 if (nicks != NULL) {
1024 nickname = nicks[0];
1025 } else {
1026 nickname = "";
1029 if (!(ret = rmm_hal_rescan(hal_ctx, udis[i], &error))) {
1030 (void) fprintf(stderr,
1031 gettext("rescan of %s failed: %s\n"),
1032 name ? name : nickname,
1033 rmm_strerror(&error, -1));
1034 libhal_free_string_array(nicks);
1035 continue;
1037 if (query) {
1038 ret = libhal_device_get_property_bool(hal_ctx, udis[i],
1039 "storage.removable.media_available", NULL);
1040 if (ret) {
1041 printf(gettext("%s is available\n"),
1042 name ? name : nickname);
1043 } else {
1044 printf(gettext("%s is not available\n"),
1045 name ? name : nickname);
1048 libhal_free_string_array(nicks);
1051 if (drive != NULL) {
1052 libhal_drive_free(drive);
1054 if (do_free_udis) {
1055 libhal_free_string_array(udis);
1058 return (ret);
1063 * set action_arg from volume properties
1065 boolean_t
1066 rmm_volume_aa_from_prop(LibHalContext *hal_ctx, const char *udi_arg,
1067 LibHalVolume *volume_arg, struct action_arg *aap)
1069 LibHalVolume *volume = volume_arg;
1070 const char *udi = udi_arg;
1071 const char *drive_udi;
1072 char *volume_label;
1073 char *mountpoint;
1074 int len;
1075 int ret = B_FALSE;
1077 /* at least udi or volume must be supplied */
1078 if ((udi == NULL) && (volume == NULL)) {
1079 return (B_FALSE);
1081 if (volume == NULL) {
1082 if ((volume = libhal_volume_from_udi(hal_ctx, udi)) == NULL) {
1083 dprintf("cannot get volume %s\n", udi);
1084 goto out;
1087 if (udi == NULL) {
1088 if ((udi = libhal_volume_get_udi(volume)) == NULL) {
1089 dprintf("cannot get udi\n");
1090 goto out;
1093 drive_udi = libhal_volume_get_storage_device_udi(volume);
1095 if (!(aap->aa_symdev = libhal_device_get_property_string(hal_ctx,
1096 drive_udi, "storage.solaris.legacy.symdev", NULL))) {
1097 dprintf("property %s not found %s\n",
1098 "storage.solaris.legacy.symdev", drive_udi);
1099 goto out;
1101 if (!(aap->aa_media = libhal_device_get_property_string(hal_ctx,
1102 drive_udi, "storage.solaris.legacy.media_type", NULL))) {
1103 dprintf("property %s not found %s\n",
1104 "storage.solaris.legacy.media_type", drive_udi);
1105 goto out;
1108 /* name is derived from volume label */
1109 aap->aa_name = NULL;
1110 if ((volume_label = (char *)libhal_device_get_property_string(hal_ctx,
1111 udi, "volume.label", NULL)) != NULL) {
1112 if ((len = strlen(volume_label)) > 0) {
1113 aap->aa_name = rmm_vold_convert_volume_label(
1114 volume_label, len);
1115 if (strlen(aap->aa_name) == 0) {
1116 free(aap->aa_name);
1117 aap->aa_name = NULL;
1120 libhal_free_string(volume_label);
1122 /* if no label, then unnamed_<mediatype> */
1123 if (aap->aa_name == NULL) {
1124 aap->aa_name = (char *)calloc(1, sizeof ("unnamed_floppyNNNN"));
1125 if (aap->aa_name == NULL) {
1126 goto out;
1128 (void) snprintf(aap->aa_name, sizeof ("unnamed_floppyNNNN"),
1129 "unnamed_%s", aap->aa_media);
1132 if (!(aap->aa_path = libhal_device_get_property_string(hal_ctx, udi,
1133 "block.device", NULL))) {
1134 dprintf("property %s not found %s\n", "block.device", udi);
1135 goto out;
1137 if (!(aap->aa_rawpath = libhal_device_get_property_string(hal_ctx, udi,
1138 "block.solaris.raw_device", NULL))) {
1139 dprintf("property %s not found %s\n",
1140 "block.solaris.raw_device", udi);
1141 goto out;
1143 if (!(aap->aa_type = libhal_device_get_property_string(hal_ctx, udi,
1144 "volume.fstype", NULL))) {
1145 dprintf("property %s not found %s\n", "volume.fstype", udi);
1146 goto out;
1148 if (!libhal_device_get_property_bool(hal_ctx, udi,
1149 "volume.is_partition", NULL)) {
1150 aap->aa_partname = NULL;
1151 } else if (!(aap->aa_partname = libhal_device_get_property_string(
1152 hal_ctx, udi, "block.solaris.slice", NULL))) {
1153 dprintf("property %s not found %s\n",
1154 "block.solaris.slice", udi);
1155 goto out;
1157 if (!(mountpoint = libhal_device_get_property_string(hal_ctx, udi,
1158 "volume.mount_point", NULL))) {
1159 dprintf("property %s not found %s\n",
1160 "volume.mount_point", udi);
1161 goto out;
1164 * aa_mountpoint can be reallocated in rmm_volume_aa_update_mountpoint()
1165 * won't have to choose between free() or libhal_free_string() later on
1167 aap->aa_mountpoint = strdup(mountpoint);
1168 libhal_free_string(mountpoint);
1169 if (aap->aa_mountpoint == NULL) {
1170 dprintf("mountpoint is NULL %s\n", udi);
1171 goto out;
1174 ret = B_TRUE;
1176 out:
1177 if ((volume != NULL) && (volume != volume_arg)) {
1178 libhal_volume_free(volume);
1180 if (!ret) {
1181 rmm_volume_aa_free(aap);
1183 return (ret);
1186 /* ARGSUSED */
1187 void
1188 rmm_volume_aa_update_mountpoint(LibHalContext *hal_ctx, const char *udi,
1189 struct action_arg *aap)
1191 if (aap->aa_mountpoint != NULL) {
1192 free(aap->aa_mountpoint);
1194 aap->aa_mountpoint = rmm_get_mnttab_mount_point(aap->aa_path);
1197 void
1198 rmm_volume_aa_free(struct action_arg *aap)
1200 if (aap->aa_symdev != NULL) {
1201 libhal_free_string(aap->aa_symdev);
1202 aap->aa_symdev = NULL;
1204 if (aap->aa_name != NULL) {
1205 free(aap->aa_name);
1206 aap->aa_name = NULL;
1208 if (aap->aa_path != NULL) {
1209 libhal_free_string(aap->aa_path);
1210 aap->aa_path = NULL;
1212 if (aap->aa_rawpath != NULL) {
1213 libhal_free_string(aap->aa_rawpath);
1214 aap->aa_rawpath = NULL;
1216 if (aap->aa_type != NULL) {
1217 libhal_free_string(aap->aa_type);
1218 aap->aa_type = NULL;
1220 if (aap->aa_media != NULL) {
1221 libhal_free_string(aap->aa_media);
1222 aap->aa_media = NULL;
1224 if (aap->aa_partname != NULL) {
1225 libhal_free_string(aap->aa_partname);
1226 aap->aa_partname = NULL;
1228 if (aap->aa_mountpoint != NULL) {
1229 free(aap->aa_mountpoint);
1230 aap->aa_mountpoint = NULL;
1235 * get device's mount point from mnttab
1237 char *
1238 rmm_get_mnttab_mount_point(const char *special)
1240 char *mount_point = NULL;
1241 FILE *f;
1242 struct mnttab mnt;
1243 struct mnttab mpref = { NULL, NULL, NULL, NULL, NULL };
1245 if ((f = fopen(MNTTAB, "r")) != NULL) {
1246 mpref.mnt_special = (char *)special;
1247 if (getmntany(f, &mnt, &mpref) == 0) {
1248 mount_point = strdup(mnt.mnt_mountp);
1250 fclose(f);
1253 return (mount_point);
1258 * get human readable string from error values
1260 const char *
1261 rmm_strerror(DBusError *dbus_error, int rmm_error)
1263 const char *str;
1265 if ((dbus_error != NULL) && dbus_error_is_set(dbus_error)) {
1266 str = dbus_error->message;
1267 } else {
1268 switch (rmm_error) {
1269 case RMM_EOK:
1270 str = gettext("success");
1271 break;
1272 case RMM_EDBUS_CONNECT:
1273 str = gettext("cannot connect to D-Bus");
1274 break;
1275 case RMM_EHAL_CONNECT:
1276 str = gettext("cannot connect to HAL");
1277 break;
1278 default:
1279 str = gettext("undefined error");
1280 break;
1284 return (str);
1287 void
1288 rmm_dbus_error_free(DBusError *error)
1290 if (error != NULL && dbus_error_is_set(error)) {
1291 dbus_error_free(error);
1295 static int
1296 rmm_vold_isbadchar(int c)
1298 int ret_val = 0;
1301 switch (c) {
1302 case '/':
1303 case ';':
1304 case '|':
1305 ret_val = 1;
1306 break;
1307 default:
1308 if (iscntrl(c) || isspace(c)) {
1309 ret_val = 1;
1313 return (ret_val);
1316 char *
1317 rmm_vold_convert_volume_label(const char *name, size_t len)
1319 char buf[MAXNAMELEN+1];
1320 char *s = buf;
1321 int i;
1323 if (len > MAXNAMELEN) {
1324 len = MAXNAMELEN;
1327 for (i = 0; i < len; i++) {
1328 if (name[i] == '\0') {
1329 break;
1331 if (isgraph((int)name[i])) {
1332 if (isupper((int)name[i])) {
1333 *s++ = tolower((int)name[i]);
1334 } else if (rmm_vold_isbadchar((int)name[i])) {
1335 *s++ = '_';
1336 } else {
1337 *s++ = name[i];
1341 *s = '\0';
1342 s = strdup(buf);
1344 return (s);
1348 * swiped from mkdir.c
1351 makepath(char *dir, mode_t mode)
1353 int err;
1354 char *slash;
1357 if ((mkdir(dir, mode) == 0) || (errno == EEXIST)) {
1358 return (0);
1360 if (errno != ENOENT) {
1361 return (-1);
1363 if ((slash = strrchr(dir, '/')) == NULL) {
1364 return (-1);
1366 *slash = '\0';
1367 err = makepath(dir, mode);
1368 *slash++ = '/';
1370 if (err || (*slash == '\0')) {
1371 return (err);
1374 return (mkdir(dir, mode));
1378 void
1379 dprintf(const char *fmt, ...)
1382 va_list ap;
1383 const char *p;
1384 char msg[BUFSIZ];
1385 char *errmsg = strerror(errno);
1386 char *s;
1388 if (rmm_debug == 0) {
1389 return;
1392 (void) memset(msg, 0, BUFSIZ);
1394 /* scan for %m and replace with errno msg */
1395 s = &msg[strlen(msg)];
1396 p = fmt;
1398 while (*p != '\0') {
1399 if ((*p == '%') && (*(p+1) == 'm')) {
1400 (void) strcat(s, errmsg);
1401 p += 2;
1402 s += strlen(errmsg);
1403 continue;
1405 *s++ = *p++;
1407 *s = '\0'; /* don't forget the null byte */
1409 va_start(ap, fmt);
1410 (void) vfprintf(stderr, msg, ap);
1411 va_end(ap);