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 <stdlib.h>
  28 #include <unistd.h>
  29 #include <fcntl.h>
  30 #include <string.h>
  31 #include <strings.h>
  32 #include <dirent.h>
  33 #include <signal.h>
  34 #include <errno.h>
  35 #include <libintl.h>
  36 #include <sys/types.h>
  37 
  38 #include "vold.h"
  39 #include "rmm_common.h"
  40 
  41 char *progname = "volrmmount";
  42 
  43 static void
  44 usage()
  45 {
  46         (void) fprintf(stderr,
  47             gettext(
  48             "\nusage: %s [-i | -e] [DEVICE | NICKNAME]\n"),
  49             progname);
  50         (void) fprintf(stderr,
  51             gettext("or:    %s -d\n"), progname);
  52         (void) fprintf(stderr,
  53             gettext(
  54             "options:\t-i        simulate volume being put in/inserted\n"));
  55         (void) fprintf(stderr,
  56             gettext(
  57             "options:\t-e        simulate volume being taken out/ejected\n"));
  58         (void) fprintf(stderr,
  59             gettext("options:\t-d        show default device\n"));
  60         (void) fprintf(stderr,
  61             gettext(
  62             "\nThis command is deprecated. Use rmmount(1) instead.\n"));
  63 }
  64 
  65 int
  66 main(int argc, char **argv)
  67 {
  68         const char      *opts = "ied";
  69         int             c;
  70         boolean_t       opt_insert = B_FALSE;
  71         boolean_t       opt_eject = B_FALSE;
  72         boolean_t       opt_default = B_FALSE;
  73         action_t        action;
  74         LibHalContext   *hal_ctx;
  75         DBusError       error;
  76         rmm_error_t     rmm_error;
  77         LibHalDrive     *d;
  78         GSList          *volumes;
  79         const char      *default_name;
  80         int             ret = 0;
  81 
  82         while ((c = getopt(argc, argv, opts)) != EOF) {
  83                 switch (c) {
  84                 case 'i':
  85                         opt_insert = B_TRUE;
  86                         action = REMOUNT;
  87                         break;
  88                 case 'e':
  89                         opt_eject = B_TRUE;
  90                         action = UNMOUNT;
  91                         break;
  92                 case 'd':
  93                         opt_default = B_TRUE;
  94                         break;
  95                 default:
  96                         usage();
  97                         return (1);
  98                 }
  99         }
 100         if ((opt_insert && opt_eject) ||
 101             (!opt_insert && !opt_eject && !opt_default)) {
 102                 usage();
 103                 return (1);
 104         }
 105 
 106         if ((hal_ctx = rmm_hal_init(0, 0, 0, 0, &error, &rmm_error)) == NULL) {
 107                 (void) fprintf(stderr,
 108                     gettext("HAL initialization failed: %s\n"),
 109                     rmm_strerror(&error, rmm_error));
 110                 rmm_dbus_error_free(&error);
 111                 return (1);
 112         }
 113 
 114         if (opt_default) {
 115                 /* -d: print default name and exit */
 116                 if ((d = rmm_hal_volume_find_default(hal_ctx, &error,
 117                     &default_name, &volumes)) == NULL) {
 118                         default_name = "nothing inserted";
 119                 } else {
 120                         rmm_volumes_free(volumes);
 121                         libhal_drive_free(d);
 122                 }
 123                 (void) printf(gettext("Default device is: %s\n"), default_name);
 124         } else if (optind == argc) {
 125                 /* no name provided, use default */
 126                 if ((d = rmm_hal_volume_find_default(hal_ctx, &error,
 127                     &default_name, &volumes)) == NULL) {
 128                         (void) fprintf(stderr,
 129                             gettext("No default media available\n"));
 130                         ret = 1;
 131                 } else {
 132                         rmm_volumes_free(volumes);
 133                         libhal_drive_free(d);
 134                         if (!rmm_action(hal_ctx, default_name, action,
 135                             0, 0, 0, 0)) {
 136                                 ret = 1;
 137                         }
 138                 }
 139         } else {
 140                 for (; optind < argc; optind++) {
 141                         if (rmm_action(hal_ctx, argv[optind], action,
 142                             0, 0, 0, 0) != 0) {
 143                                 ret = 1;
 144                         }
 145                 }
 146         }
 147 
 148         rmm_hal_fini(hal_ctx);
 149 
 150         return (ret);
 151 }