| 1 |
/* |
| 2 |
* Copyright (C) 2017 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vratislav Podzimek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <libudev.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <errno.h> |
| 24 |
|
| 25 |
#include "dev_utils.h" |
| 26 |
|
| 27 |
/** |
| 28 |
* bd_utils_dev_utils_error_quark: (skip) |
| 29 |
*/ |
| 30 |
GQuark bd_utils_dev_utils_error_quark (void) |
| 31 |
{ |
| 32 |
return g_quark_from_static_string ("g-bd-utils-dev_utils-error-quark"); |
| 33 |
} |
| 34 |
|
| 35 |
static gboolean _is_block_device (const gchar *path, GError **error) { |
| 36 |
struct stat sb; |
| 37 |
|
| 38 |
if (stat (path, &sb) != 0) { |
| 39 |
g_set_error (error, BD_UTILS_DEV_UTILS_ERROR, BD_UTILS_DEV_UTILS_ERROR_FAILED, |
| 40 |
"Failed to stat '%s': %s", path, strerror_l (errno, _C_LOCALE)); |
| 41 |
return FALSE; |
| 42 |
} |
| 43 |
|
| 44 |
if (!S_ISBLK (sb.st_mode)) { |
| 45 |
g_set_error (error, BD_UTILS_DEV_UTILS_ERROR, BD_UTILS_DEV_UTILS_ERROR_FAILED, |
| 46 |
"'%s' is not a block device", path); |
| 47 |
return FALSE; |
| 48 |
} |
| 49 |
|
| 50 |
return TRUE; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* bd_utils_resolve_device: |
| 55 |
* @dev_spec: specification of the device (e.g. "/dev/sda", any symlink, or the name of a file |
| 56 |
* under "/dev") |
| 57 |
* @error: (out) (optional): place to store error (if any) |
| 58 |
* |
| 59 |
* Returns: (transfer full): the full real path of the device (e.g. "/dev/md126" |
| 60 |
* for "/dev/md/my_raid") or %NULL in case of error |
| 61 |
*/ |
| 62 |
gchar* bd_utils_resolve_device (const gchar *dev_spec, GError **error) { |
| 63 |
g_autofree gchar *path = NULL; |
| 64 |
g_autofree gchar *symlink = NULL; |
| 65 |
GError *l_error = NULL; |
| 66 |
|
| 67 |
if (!g_str_has_prefix (dev_spec, "/dev/")) |
| 68 |
path = g_strdup_printf ("/dev/%s", dev_spec); |
| 69 |
else |
| 70 |
path = g_strdup (dev_spec); |
| 71 |
|
| 72 |
symlink = g_file_read_link (path, &l_error); |
| 73 |
if (!symlink) { |
| 74 |
if (g_error_matches (l_error, G_FILE_ERROR, G_FILE_ERROR_INVAL)) { |
| 75 |
/* invalid argument -> not a symlink -> nothing to resolve */ |
| 76 |
g_clear_error (&l_error); |
| 77 |
if (_is_block_device (path, error)) |
| 78 |
return g_steal_pointer (&path); |
| 79 |
else { |
| 80 |
g_prefix_error (error, "Failed to resolve '%s': ", dev_spec); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
} else { |
| 84 |
/* some other error, just report it */ |
| 85 |
g_propagate_error (error, l_error); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
} |
| 89 |
g_free (path); |
| 90 |
|
| 91 |
if (g_str_has_prefix (symlink, "../")) |
| 92 |
path = g_strdup_printf ("/dev/%s", symlink + 3); |
| 93 |
else |
| 94 |
path = g_strdup_printf ("/dev/%s", symlink); |
| 95 |
|
| 96 |
if (_is_block_device (path, error)) |
| 97 |
return g_steal_pointer (&path); |
| 98 |
else { |
| 99 |
g_prefix_error (error, "Failed to resolve '%s': ", dev_spec); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* bd_utils_get_device_symlinks: |
| 106 |
* @dev_spec: specification of the device (e.g. "/dev/sda", any symlink, or the name of a file |
| 107 |
* under "/dev") |
| 108 |
* @error: (out) (optional): place to store error (if any) |
| 109 |
* |
| 110 |
* Returns: (transfer full) (array zero-terminated=1): a list of all symlinks (known to udev) for the |
| 111 |
* device specified with @dev_spec or %NULL in |
| 112 |
* case of error |
| 113 |
*/ |
| 114 |
gchar** bd_utils_get_device_symlinks (const gchar *dev_spec, GError **error) { |
| 115 |
gchar *dev_path; |
| 116 |
struct udev *context; |
| 117 |
struct udev_device *device; |
| 118 |
struct udev_list_entry *entry = NULL; |
| 119 |
struct udev_list_entry *ent_it = NULL; |
| 120 |
guint64 n_links = 0; |
| 121 |
guint64 i = 0; |
| 122 |
gchar **ret = NULL; |
| 123 |
|
| 124 |
dev_path = bd_utils_resolve_device (dev_spec, error); |
| 125 |
if (!dev_path) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
context = udev_new (); |
| 129 |
/* dev_path is the full path like "/dev/sda", we only need the device name ("sda") */ |
| 130 |
device = udev_device_new_from_subsystem_sysname (context, "block", dev_path + 5); |
| 131 |
|
| 132 |
if (!device) { |
| 133 |
g_set_error (error, BD_UTILS_DEV_UTILS_ERROR, BD_UTILS_DEV_UTILS_ERROR_FAILED, |
| 134 |
"Failed to get information about the device '%s' from udev database", |
| 135 |
dev_path); |
| 136 |
g_free (dev_path); |
| 137 |
udev_unref (context); |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
|
| 141 |
entry = udev_device_get_devlinks_list_entry (device); |
| 142 |
if (!entry) { |
| 143 |
g_set_error (error, BD_UTILS_DEV_UTILS_ERROR, BD_UTILS_DEV_UTILS_ERROR_FAILED, |
| 144 |
"Failed to get symlinks for the device '%s'", dev_path); |
| 145 |
g_free (dev_path); |
| 146 |
udev_device_unref (device); |
| 147 |
udev_unref (context); |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
g_free (dev_path); |
| 151 |
|
| 152 |
ent_it = entry; |
| 153 |
while (ent_it) { |
| 154 |
n_links++; |
| 155 |
ent_it = udev_list_entry_get_next (ent_it); |
| 156 |
} |
| 157 |
|
| 158 |
ret = g_new0 (gchar*, n_links + 1); |
| 159 |
ent_it = entry; |
| 160 |
while (ent_it) { |
| 161 |
ret[i++] = g_strdup (udev_list_entry_get_name (ent_it)); |
| 162 |
ent_it = udev_list_entry_get_next (ent_it); |
| 163 |
} |
| 164 |
ret[i] = NULL; |
| 165 |
|
| 166 |
udev_device_unref (device); |
| 167 |
udev_unref (context); |
| 168 |
|
| 169 |
return ret; |
| 170 |
} |