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"
33 #include "udev_dbus.h"
37 #include "libsysfs/libsysfs.h"
39 static int delete_path(char *path)
44 pos = strrchr(path, '/');
47 pos = strrchr(path, '/');
49 /* don't remove the last one */
50 if ((pos == path) || (pos == NULL))
56 if (errno == ENOTEMPTY)
58 dbg("rmdir(%s) failed with error '%s'",
59 path, strerror(errno));
62 dbg("removed '%s'", path);
67 static int delete_node(struct udevice *dev)
74 strncpy(filename, udev_root, sizeof(filename));
75 strncat(filename, dev->name, sizeof(filename));
77 info("removing device node '%s'", filename);
78 retval = unlink(filename);
80 dbg("unlink(%s) failed with error '%s'",
81 filename, strerror(errno));
85 /* remove subdirectories */
86 if (strchr(dev->name, '/'))
87 delete_path(filename);
90 symlinks = dev->symlink;
92 linkname = strsep(&symlinks, " ");
96 strncpy(filename, udev_root, sizeof(filename));
97 strncat(filename, linkname, sizeof(filename));
99 dbg("unlinking symlink '%s'", filename);
100 retval = unlink(filename);
102 dbg("unlink(%s) failed with error '%s'",
103 filename, strerror(errno));
106 if (strchr(dev->symlink, '/')) {
107 delete_path(filename);
116 * Look up the sysfs path in the database to see if we have named this device
117 * something different from the kernel name. If we have, us it. If not, use
118 * the default kernel name for lack of anything else to know to do.
120 int udev_remove_device(char *path, char *subsystem)
126 memset(&dev, 0, sizeof(dev));
128 retval = udevdb_get_dev(path, &dev);
130 dbg("'%s' not found in database, falling back on default name", path);
131 temp = strrchr(path, '/');
134 strncpy(dev.name, &temp[1], sizeof(dev.name));
137 dbg("name is '%s'", dev.name);
138 udevdb_delete_dev(path);
140 sysbus_send_remove(dev.name, path);
142 retval = delete_node(&dev);