6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "udev_version.h"
35 #include "libsysfs/libsysfs.h"
37 static int delete_path(char *path)
42 pos = strrchr(path, '/');
45 pos = strrchr(path, '/');
47 /* don't remove the last one */
48 if ((pos == path) || (pos == NULL))
54 if (errno == ENOTEMPTY)
56 dbg("rmdir(%s) failed with error '%s'",
57 path, strerror(errno));
60 dbg("removed '%s'", path);
65 static int delete_node(struct udevice *dev)
70 strncpy(filename, udev_root, sizeof(filename));
71 strncat(filename, dev->name, sizeof(filename));
73 dbg("unlinking node '%s'", filename);
74 retval = unlink(filename);
76 dbg("unlink(%s) failed with error '%s'",
77 filename, strerror(errno));
81 /* remove subdirectories */
82 if (strchr(dev->name, '/'))
83 delete_path(filename);
86 strncpy(filename, udev_root, sizeof(filename));
87 strncat(filename, dev->symlink, sizeof(filename));
88 dbg("unlinking symlink '%s'", filename);
89 retval = unlink(filename);
91 dbg("unlink(%s) failed with error '%s'",
92 filename, strerror(errno));
95 if (strchr(dev->symlink, '/')) {
96 delete_path(filename);
104 /** Send out a signal that a device node is deleted
106 * @param name Name of the device node, e.g. /udev/sda1
107 * @param path Sysfs path of device
109 static void sysbus_send_remove(const char* name, const char *path)
112 DBusMessage* message;
113 DBusMessageIter iter;
115 if (sysbus_connection == NULL)
118 strncpy(filename, udev_root, sizeof(filename));
119 strncat(filename, name, sizeof(filename));
121 /* object, interface, member */
122 message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor",
123 "org.kernel.udev.NodeMonitor",
126 dbus_message_iter_init(message, &iter);
127 dbus_message_iter_append_string(&iter, filename);
128 dbus_message_iter_append_string(&iter, path);
130 if ( !dbus_connection_send(sysbus_connection, message, NULL) )
131 dbg("error sending d-bus signal");
133 dbus_message_unref(message);
135 dbus_connection_flush(sysbus_connection);
137 #endif /* USE_DBUS */
140 * Look up the sysfs path in the database to see if we have named this device
141 * something different from the kernel name. If we have, us it. If not, use
142 * the default kernel name for lack of anything else to know to do.
144 int udev_remove_device(char *path, char *subsystem)
150 dev = udevdb_get_dev(path);
152 dbg("'%s' not found in database, falling back on default name", path);
153 temp = strrchr(path, '/');
156 strncpy(name, &temp[1], sizeof(name));
159 dbg("name is '%s'", dev->name);
160 udevdb_delete_dev(path);
163 sysbus_send_remove(name, device);
164 #endif /* USE_DBUS */
166 return delete_node(dev);