2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static struct fs_type fs_types[] = {
40 { "HAMMER", hammer_probe, hammer_volname },
41 { "UFS", ufs_probe, ufs_volname },
46 static struct fsid_head fsid_list =
47 TAILQ_HEAD_INITIALIZER(fsid_list);
50 fsid_alias_exists(const char *dev)
52 struct fsid_entry *fsid;
55 if (TAILQ_EMPTY(&fsid_list))
58 TAILQ_FOREACH(fsid, &fsid_list, link) {
59 if (strcmp(fsid->dev_path, dev) == 0) {
69 fsid_check_create_alias(const char *dev)
71 struct fsid_entry *fsid;
72 char full_path[MAXPATHLEN];
73 char link_path[MAXPATHLEN];
77 if (fsid_alias_exists(dev))
80 sprintf(full_path, "/dev/%s", dev);
81 for (i = 0; fs_types[i].fs_name != NULL; i++) {
82 volname = fs_types[i].fs_volname(full_path);
86 printf("Volume name for %s is %s\n", dev, volname);
87 fsid = malloc(sizeof(struct fsid_entry));
91 sprintf(link_path, "/dev/vol-by-name/%s", volname);
93 fsid->dev_path = strdup(dev);
94 fsid->link_path = strdup(link_path);
95 if ((fsid->dev_path == NULL) || (fsid->link_path == NULL)) {
100 mkdir("/dev/vol-by-name", 0755);
101 symlink(full_path, link_path);
103 TAILQ_INSERT_TAIL(&fsid_list, fsid, link);
112 fsid_check_remove_alias(const char *dev)
114 struct fsid_entry *fsid, *fsid2;
116 if (!fsid_alias_exists(dev))
119 TAILQ_FOREACH_MUTABLE(fsid, &fsid_list, link, fsid2) {
120 if (strcmp(fsid->dev_path, dev) != 0)
123 TAILQ_REMOVE(&fsid_list, fsid, link);
125 unlink(fsid->link_path);
127 free(fsid->dev_path);
128 free(fsid->link_path);
139 fprintf(stderr, "usage: fsid [-d]\n");
144 main(int argc, char *argv[])
147 struct udev_enumerate *udev_enum;
148 struct udev_list_entry *udev_le, *udev_le_first;
149 struct udev_monitor *udev_monitor;
150 struct udev_device *udev_dev;
153 prop_dictionary_t *dict;
155 int ret, daemon_flag = 0;
156 const char *prop, *dev_path;
158 while ((ch = getopt(argc, argv, "d")) != -1) {
175 udev_enum = udev_enumerate_new(udev);
176 if (udev_enum == NULL)
177 err(1, "udev_enumerate_new");
179 ret = udev_enumerate_add_match_property(udev_enum, "subsystem", "disk");
181 err(1, "udev_enumerate_add_match_property, out, ret=%d\n", ret);
183 ret = udev_enumerate_add_match_property(udev_enum, "alias", "0");
185 err(1, "udev_enumerate_add_match_property, out, ret=%d\n", ret);
187 ret = udev_enumerate_add_nomatch_property(udev_enum, "disk-type", "memory");
189 err(1, "udev_enumerate_add_match_property, out, ret=%d\n", ret);
192 ret = udev_enumerate_scan_devices(udev_enum);
194 err(1, "udev_enumerate_scan_device ret = %d", ret);
196 udev_le_first = udev_enumerate_get_list_entry(udev_enum);
197 if (udev_le_first == NULL)
198 err(1, "udev_enumerate_get_list_entry error");
200 udev_list_entry_foreach(udev_le, udev_le_first) {
201 udev_dev = udev_list_entry_get_device(udev_le);
202 dev_path = udev_device_get_devnode(udev_dev);
204 dict = udev_device_get_dictionary(udev_dev);
205 printf("xml of new device: %s\n", prop_dictionary_externalize(dict));
207 fsid_check_create_alias(dev_path);
210 udev_enumerate_unref(udev_enum);
214 if (daemon(0, 0) == -1)
218 udev_monitor = udev_monitor_new(udev);
219 ret = udev_monitor_filter_add_match_property(udev_monitor, "subsystem", "disk");
221 err(1, "udev_monitor_filter_add_match_property, out, ret=%d\n", ret);
223 ret = udev_monitor_filter_add_match_property(udev_monitor, "alias", "0");
225 err(1, "udev_monitor_filter_add_match_property, out, ret=%d\n", ret);
227 ret = udev_monitor_filter_add_nomatch_property(udev_monitor, "disk-type", "memory");
229 err(1, "udev_monitor_filter_add_nomatch_property, out, ret=%d\n", ret);
231 ret = udev_monitor_enable_receiving(udev_monitor);
233 err(1, "udev_monitor_enable_receiving ret = %d", ret);
235 while ((udev_dev = udev_monitor_receive_device(udev_monitor))) {
236 if (udev_dev == NULL)
237 err(1, "udev_monitor_receive_device failed");
239 dev_path = udev_device_get_devnode(udev_dev);
240 prop = udev_device_get_action(udev_dev);
242 if (strcmp(prop, "attach") == 0)
243 fsid_check_create_alias(dev_path);
244 else if (strcmp(prop, "detach") == 0)
245 fsid_check_remove_alias(dev_path);
248 udev_monitor_unref(udev_monitor);