Print this page
4845 rm(u)mount don't always print mount/unmount errors
   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 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.


  24  */
  25 
  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>
  40 
  41 #include <dbus/dbus.h>
  42 #include <dbus/dbus-glib.h>
  43 #include <dbus/dbus-glib-lowlevel.h>


 790         }
 791 
 792         dprintf("unclaim branch ok %s\n", udi);
 793 
 794         dbus_message_unref(dmesg);
 795         dbus_message_unref(reply);
 796 
 797         return (B_TRUE);
 798 }
 799 
 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)
 804 {
 805         char            dev_str[MAXPATHLEN];
 806         char            *mountp;
 807         DBusError       error;
 808         boolean_t       ret = B_FALSE;
 809 


 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);
 814         }
 815 
 816         dbus_error_init(&error);
 817 
 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;
 826                 }
 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;
 833                 }
 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;
 839         }
 840 
 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;
 845         }
 846 
 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);


 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);
 907         }
 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;
 912         }
 913 
 914         /*
 915          * For those drives that do not require media eject,
 916          * EJECT turns into UNMOUNT.
 917          */
 918         if ((action == EJECT) && !libhal_drive_requires_eject(d)) {
 919                 action = UNMOUNT;
 920         }
 921 












 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);
 926 
 927                 if (!ret || (action == CLOSETRAY)) {
 928                         goto out;
 929                 }
 930         }
 931 
 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);
 937 
 938                 if ((udi == NULL) || (dev == NULL)) {
 939                         continue;
 940                 }
 941                 if (aap == &aa_local) {


 945                                 continue;
 946                         }
 947                 }
 948                 aap->aa_action = action;
 949 
 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);
 954                 }
 955                 if (ret) {
 956                         (void) vold_postprocess(hal_ctx, udi, aap);
 957                 }
 958 
 959                 if (aap == &aa_local) {
 960                         rmm_volume_aa_free(aap);
 961                 }
 962         }
 963 
 964 out:

 965         rmm_volumes_free(volumes);
 966         libhal_drive_free(d);
 967 
 968         return (ret);
 969 }
 970 
 971 
 972 /*
 973  * rescan by name
 974  * if name is NULL, rescan all drives
 975  */
 976 boolean_t
 977 rmm_rescan(LibHalContext *hal_ctx, const char *name, boolean_t query)
 978 {
 979         DBusError       error;
 980         GSList          *volumes;
 981         LibHalDrive     *drive = NULL;
 982         const char      *drive_udi;
 983         char            **udis;
 984         int             num_udis;


   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 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  *
  25  * Copyright 2014 Andrew Stormont.
  26  */
  27 
  28 #include <stdio.h>
  29 #include <errno.h>
  30 #include <string.h>
  31 #include <strings.h>
  32 #include <stdarg.h>
  33 #include <fcntl.h>
  34 #include <libintl.h>
  35 #include <stdlib.h>
  36 #include <unistd.h>
  37 #include <ctype.h>
  38 #include <sys/param.h>
  39 #include <sys/types.h>
  40 #include <sys/stat.h>
  41 #include <sys/mnttab.h>
  42 
  43 #include <dbus/dbus.h>
  44 #include <dbus/dbus-glib.h>
  45 #include <dbus/dbus-glib-lowlevel.h>


 792         }
 793 
 794         dprintf("unclaim branch ok %s\n", udi);
 795 
 796         dbus_message_unref(dmesg);
 797         dbus_message_unref(reply);
 798 
 799         return (B_TRUE);
 800 }
 801 
 802 static boolean_t
 803 rmm_action_one(LibHalContext *hal_ctx, const char *name, action_t action,
 804     const char *dev, const char *udi, LibHalVolume *v,
 805     char **opts, int num_opts, char *mountpoint)
 806 {
 807         char            dev_str[MAXPATHLEN];
 808         char            *mountp;
 809         DBusError       error;
 810         boolean_t       ret = B_FALSE;
 811 
 812         dprintf("rmm_action_one %s %s\n", name, action_strings[action]);
 813 
 814         if (strcmp(name, dev) == 0) {
 815                 (void) snprintf(dev_str, sizeof (dev_str), name);
 816         } else {
 817                 (void) snprintf(dev_str, sizeof (dev_str), "%s %s", name, dev);
 818         }
 819 
 820         dbus_error_init(&error);
 821 
 822         switch (action) {
 823         case EJECT:
 824                 ret = rmm_hal_eject(hal_ctx, udi, &error);
 825                 break;
 826         case INSERT:
 827         case REMOUNT:



 828                 ret = rmm_hal_mount(hal_ctx, udi,
 829                     opts, num_opts, mountpoint, &error);
 830                 break;
 831         case UNMOUNT:



 832                 ret = rmm_hal_unmount(hal_ctx, udi, &error);
 833                 break;
 834         case CLOSETRAY:
 835                 ret = rmm_hal_closetray(hal_ctx, udi, &error);
 836                 break;
 837         }
 838 
 839         if (!ret) {
 840                 (void) fprintf(stderr, gettext("%s of %s failed: %s\n"),
 841                     action_strings[action], dev_str, rmm_strerror(&error, -1));
 842                 goto done;
 843         }
 844 
 845         switch (action) {
 846         case EJECT:
 847                 (void) printf(gettext("%s ejected\n"), dev_str);
 848                 break;
 849         case INSERT:
 850         case REMOUNT:
 851                 mountp = rmm_get_mnttab_mount_point(dev);


 900         d = rmm_hal_volume_find(hal_ctx, name, &error, &volumes);
 901         rmm_dbus_error_free(&error);
 902         if (d == NULL) {
 903                 (void) fprintf(stderr, gettext("cannot find '%s'\n"), name);
 904                 return (B_FALSE);
 905         }
 906         d_udi = libhal_drive_get_udi(d);
 907         d_dev = libhal_drive_get_device_file(d);
 908         if ((d_udi == NULL) || (d_dev == NULL)) {
 909                 goto out;
 910         }
 911 
 912         /*
 913          * For those drives that do not require media eject,
 914          * EJECT turns into UNMOUNT.
 915          */
 916         if ((action == EJECT) && !libhal_drive_requires_eject(d)) {
 917                 action = UNMOUNT;
 918         }
 919 
 920         /*
 921          * Assume anything other than EJECT and CLOSETRAY is a
 922          * variant of mount or unmount and requires the device
 923          * to have at least one volume.
 924          */
 925         if (volumes == NULL && (action != EJECT && action != CLOSETRAY)) {
 926                 (void) fprintf(stderr,
 927                     gettext("cannot %s device '%s' with no volumes\n"),
 928                         action_strings[action], name);
 929                 goto out;
 930         }
 931 
 932         /* per drive action */
 933         if ((action == EJECT) || (action == CLOSETRAY)) {
 934                 ret = rmm_action_one(hal_ctx, name, action, d_dev, d_udi, NULL,
 935                     opts, num_opts, NULL);
 936 
 937                 if (!ret || (action == CLOSETRAY)) {
 938                         goto out;
 939                 }
 940         }
 941 
 942         /* per volume action */
 943         for (i = volumes; i != NULL; i = g_slist_next(i)) {
 944                 v = (LibHalVolume *)i->data;
 945                 udi = libhal_volume_get_udi(v);
 946                 dev = libhal_volume_get_device_file(v);
 947 
 948                 if ((udi == NULL) || (dev == NULL)) {
 949                         continue;
 950                 }
 951                 if (aap == &aa_local) {


 955                                 continue;
 956                         }
 957                 }
 958                 aap->aa_action = action;
 959 
 960                 /* ejected above, just need postprocess */
 961                 if (action != EJECT) {
 962                         ret = rmm_action_one(hal_ctx, name, action, dev, udi, v,
 963                             opts, num_opts, mountpoint);
 964                 }
 965                 if (ret) {
 966                         (void) vold_postprocess(hal_ctx, udi, aap);
 967                 }
 968 
 969                 if (aap == &aa_local) {
 970                         rmm_volume_aa_free(aap);
 971                 }
 972         }
 973 
 974 out:
 975         if (volumes != NULL)
 976                 rmm_volumes_free(volumes);
 977         libhal_drive_free(d);
 978 
 979         return (ret);
 980 }
 981 
 982 
 983 /*
 984  * rescan by name
 985  * if name is NULL, rescan all drives
 986  */
 987 boolean_t
 988 rmm_rescan(LibHalContext *hal_ctx, const char *name, boolean_t query)
 989 {
 990         DBusError       error;
 991         GSList          *volumes;
 992         LibHalDrive     *drive = NULL;
 993         const char      *drive_udi;
 994         char            **udis;
 995         int             num_udis;