Debian Codegraph @

tools/umount-udisks.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2007-2010 David Zeuthen <[email protected]>
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 <sys/sysmacros.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include <udisks/udisks.h>
33
34 static UDisksObject *
35 lookup_object_for_block (UDisksClient *client,
36 dev_t block_device)
37 {
38 UDisksObject *ret;
39 GList *objects;
40 GList *l;
41
42 ret = NULL;
43
44 objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client));
45 for (l = objects; l != NULL; l = l->next)
46 {
47 UDisksObject *object = UDISKS_OBJECT (l->data);
48 UDisksBlock *block;
49
50 block = udisks_object_peek_block (object);
51 if (block != NULL)
52 {
53 if (block_device == udisks_block_get_device_number (block))
54 {
55 ret = g_object_ref (object);
56 goto out;
57 }
58 }
59 }
60
61 out:
62 g_list_free_full (objects, g_object_unref);
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 if (argc < 2 || strlen (argv[1]) == 0)
84 {
85 g_printerr ("%s: this program is only supposed to be invoked by umount(8).\n", argv[0]);
86 goto out;
87 }
88
89 if (stat (argv[1], &statbuf) < 0)
90 {
91 g_printerr ("%s: error calling stat on %s: %m\n", argv[0], argv[1]);
92 goto out;
93 }
94
95 if (S_ISBLK (statbuf.st_mode))
96 block_device = statbuf.st_rdev;
97 else
98 block_device = statbuf.st_dev;
99
100 error = NULL;
101 client = udisks_client_new_sync (NULL, /* GCancellable */
102 &error);
103 if (client == NULL)
104 {
105 g_printerr ("Error connecting to the udisks daemon: %s\n", error->message);
106 g_clear_error (&error);
107 goto out;
108 }
109
110 object = lookup_object_for_block (client, block_device);
111 if (object == NULL)
112 {
113 g_printerr ("Error finding object for block device %u:%u\n", major (block_device), minor (block_device));
114 goto out;
115 }
116
117 filesystem = udisks_object_peek_filesystem (object);
118 if (filesystem == NULL)
119 {
120 g_printerr ("Block device %u:%u is not a mountable filesystem.\n", major (block_device), minor (block_device));
121 goto out;
122 }
123
124 error = NULL;
125 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
126 if (!udisks_filesystem_call_unmount_sync (filesystem,
127 g_variant_builder_end (&builder), /* options */
128 NULL, /* GCancellable */
129 &error))
130 {
131 g_printerr ("Error unmounting block device %u:%u: %s\n", major (block_device), minor (block_device), error->message);
132 g_clear_error (&error);
133 goto out;
134 }
135
136 ret = 0;
137
138 out:
139 if (object != NULL)
140 g_object_unref (object);
141 if (client != NULL)
142 g_object_unref (client);
143 return ret;
144 }