~behda/+junk/udisks2.original

« back to all changes in this revision

Viewing changes to tools/umount-udisks.c

  • Committer: behda
  • Date: 2014-05-24 15:15:11 UTC
  • Revision ID: pauvitk@gmail.com-20140524151511-3vtr0uubjewx3z2j
Initial commit of source code and Debian packaging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 
2
 *
 
3
 * Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include <glib/gi18n.h>
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <unistd.h>
 
29
#include <string.h>
 
30
 
 
31
#include <udisks/udisks.h>
 
32
 
 
33
static UDisksObject *
 
34
lookup_object_for_block (UDisksClient  *client,
 
35
                         dev_t          block_device)
 
36
{
 
37
  UDisksObject *ret;
 
38
  GList *objects;
 
39
  GList *l;
 
40
 
 
41
  ret = NULL;
 
42
 
 
43
  objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client));
 
44
  for (l = objects; l != NULL; l = l->next)
 
45
    {
 
46
      UDisksObject *object = UDISKS_OBJECT (l->data);
 
47
      UDisksBlock *block;
 
48
 
 
49
      block = udisks_object_peek_block (object);
 
50
      if (block != NULL)
 
51
        {
 
52
          if (block_device == udisks_block_get_device_number (block))
 
53
            {
 
54
              ret = g_object_ref (object);
 
55
              goto out;
 
56
            }
 
57
        }
 
58
    }
 
59
 
 
60
 out:
 
61
  g_list_foreach (objects, (GFunc) g_object_unref, NULL);
 
62
  g_list_free (objects);
 
63
 
 
64
  return ret;
 
65
}
 
66
 
 
67
int
 
68
main (int argc, char *argv[])
 
69
{
 
70
  gint ret;
 
71
  dev_t block_device;
 
72
  UDisksClient *client;
 
73
  GError *error;
 
74
  struct stat statbuf;
 
75
  UDisksObject *object;
 
76
  UDisksFilesystem *filesystem;
 
77
  GVariantBuilder builder;
 
78
 
 
79
  ret = 1;
 
80
  client = NULL;
 
81
  object = NULL;
 
82
 
 
83
  g_type_init ();
 
84
 
 
85
  if (argc < 2 || strlen (argv[1]) == 0)
 
86
    {
 
87
      g_printerr ("%s: this program is only supposed to be invoked by umount(8).\n", argv[0]);
 
88
      goto out;
 
89
    }
 
90
 
 
91
  if (stat (argv[1], &statbuf) < 0)
 
92
    {
 
93
      g_printerr ("%s: error calling stat on %s: %m\n", argv[0], argv[1]);
 
94
      goto out;
 
95
    }
 
96
 
 
97
  if (S_ISBLK (statbuf.st_mode))
 
98
    block_device = statbuf.st_rdev;
 
99
  else
 
100
    block_device = statbuf.st_dev;
 
101
 
 
102
  error = NULL;
 
103
  client = udisks_client_new_sync (NULL, /* GCancellable */
 
104
                                   &error);
 
105
  if (client == NULL)
 
106
    {
 
107
      g_printerr ("Error connecting to the udisks daemon: %s\n", error->message);
 
108
      g_error_free (error);
 
109
      goto out;
 
110
    }
 
111
 
 
112
  object = lookup_object_for_block (client, block_device);
 
113
  if (object == NULL)
 
114
    {
 
115
      g_printerr ("Error finding object for block device %d:%d\n", major (block_device), minor (block_device));
 
116
      goto out;
 
117
    }
 
118
 
 
119
  filesystem = udisks_object_peek_filesystem (object);
 
120
  if (filesystem == NULL)
 
121
    {
 
122
      g_printerr ("Block device %d:%d is not a mountable filesystem.\n", major (block_device), minor (block_device));
 
123
      goto out;
 
124
    }
 
125
 
 
126
  error = NULL;
 
127
  g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
 
128
  if (!udisks_filesystem_call_unmount_sync (filesystem,
 
129
                                            g_variant_builder_end (&builder), /* options */
 
130
                                            NULL, /* GCancellable */
 
131
                                            &error))
 
132
    {
 
133
      g_printerr ("Error unmounting block device %d:%d: %s\n", major (block_device), minor (block_device), error->message);
 
134
      g_error_free (error);
 
135
      goto out;
 
136
    }
 
137
 
 
138
  ret = 0;
 
139
 
 
140
 out:
 
141
  if (object != NULL)
 
142
    g_object_unref (object);
 
143
  if (client != NULL)
 
144
    g_object_unref (client);
 
145
  return ret;
 
146
}