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
40 static struct fsid_head fsid_list =
41 TAILQ_HEAD_INITIALIZER(fsid_list);
44 fsid_alias_exists(const char *dev)
46 struct fsid_entry *fsid;
49 if (TAILQ_EMPTY(&fsid_list))
52 TAILQ_FOREACH(fsid, &fsid_list, link) {
53 if (strcmp(fsid->dev_path, dev) == 0) {
63 fsid_check_create_alias(const char *dev)
65 struct fsid_entry *fsid;
66 char full_path[MAXPATHLEN];
67 char link_path[MAXPATHLEN];
70 if (fsid_alias_exists(dev))
73 sprintf(full_path, "/dev/%s", dev);
74 volname = fsid_volname_all(full_path);
78 printf("Volume name for %s is %s\n", dev, volname);
79 fsid = malloc(sizeof(struct fsid_entry));
83 sprintf(link_path, "/dev/vol-by-name/%s", volname);
85 fsid->dev_path = strdup(dev);
86 fsid->link_path = strdup(link_path);
87 if ((fsid->dev_path == NULL) || (fsid->link_path == NULL)) {
92 mkdir("/dev/vol-by-name", 0755);
93 symlink(full_path, link_path);
95 TAILQ_INSERT_TAIL(&fsid_list, fsid, link);
102 fsid_check_remove_alias(const char *dev)
104 struct fsid_entry *fsid, *fsid2;
106 if (!fsid_alias_exists(dev))
109 TAILQ_FOREACH_MUTABLE(fsid, &fsid_list, link, fsid2) {
110 if (strcmp(fsid->dev_path, dev) != 0)
113 TAILQ_REMOVE(&fsid_list, fsid, link);
115 unlink(fsid->link_path);
117 free(fsid->dev_path);
118 free(fsid->link_path);
129 fprintf(stderr, "usage: fsid [-d]\n");
134 main(int argc, char *argv[])
137 struct udev_enumerate *udev_enum;
138 struct udev_list_entry *udev_le, *udev_le_first;
139 struct udev_monitor *udev_monitor;
140 struct udev_device *udev_dev;
143 prop_dictionary_t *dict;
145 int ret, daemon_flag = 0;
146 const char *prop, *dev_path;
148 while ((ch = getopt(argc, argv, "d")) != -1) {
165 udev_enum = udev_enumerate_new(udev);
166 if (udev_enum == NULL)
167 err(1, "udev_enumerate_new");
169 ret = udev_enumerate_add_match_property(udev_enum, "subsystem", "disk");
171 err(1, "udev_enumerate_add_match_property, out, ret=%d\n", ret);
173 ret = udev_enumerate_add_match_property(udev_enum, "alias", "0");
175 err(1, "udev_enumerate_add_match_property, out, ret=%d\n", ret);
177 ret = udev_enumerate_add_nomatch_property(udev_enum, "disk-type", "memory");
179 err(1, "udev_enumerate_add_match_property, out, ret=%d\n", ret);
181 ret = udev_enumerate_add_nomatch_property(udev_enum, "disk-type", "floppy");
183 ret = udev_enumerate_scan_devices(udev_enum);
185 err(1, "udev_enumerate_scan_device ret = %d", ret);
187 udev_le_first = udev_enumerate_get_list_entry(udev_enum);
188 if (udev_le_first == NULL)
189 err(1, "udev_enumerate_get_list_entry error");
191 udev_list_entry_foreach(udev_le, udev_le_first) {
192 udev_dev = udev_list_entry_get_device(udev_le);
193 dev_path = udev_device_get_devnode(udev_dev);
195 dict = udev_device_get_dictionary(udev_dev);
196 printf("xml of new device: %s\n", prop_dictionary_externalize(dict));
198 fsid_check_create_alias(dev_path);
201 udev_enumerate_unref(udev_enum);
205 if (daemon(0, 0) == -1)
209 udev_monitor = udev_monitor_new(udev);
210 ret = udev_monitor_filter_add_match_property(udev_monitor, "subsystem", "disk");
212 err(1, "udev_monitor_filter_add_match_property, out, ret=%d\n", ret);
214 ret = udev_monitor_filter_add_match_property(udev_monitor, "alias", "0");
216 err(1, "udev_monitor_filter_add_match_property, out, ret=%d\n", ret);
218 ret = udev_monitor_filter_add_nomatch_property(udev_monitor, "disk-type", "memory");
220 err(1, "udev_monitor_filter_add_nomatch_property, out, ret=%d\n", ret);
222 ret = udev_monitor_enable_receiving(udev_monitor);
224 err(1, "udev_monitor_enable_receiving ret = %d", ret);
226 while ((udev_dev = udev_monitor_receive_device(udev_monitor))) {
227 if (udev_dev == NULL)
228 err(1, "udev_monitor_receive_device failed");
230 dev_path = udev_device_get_devnode(udev_dev);
231 prop = udev_device_get_action(udev_dev);
233 if (strcmp(prop, "attach") == 0)
234 fsid_check_create_alias(dev_path);
235 else if (strcmp(prop, "detach") == 0)
236 fsid_check_remove_alias(dev_path);
239 udev_monitor_unref(udev_monitor);