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.
33 #include "udev_version.h"
34 #include "udev_dbus.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))
58 if (errno == ENOTEMPTY)
60 dbg("rmdir(%s) failed with error '%s'",
61 path, strerror(errno));
64 dbg("removed '%s'", path);
69 static int delete_node(struct udevice *dev)
71 char filename[NAME_SIZE];
72 char linkname[NAME_SIZE];
73 char partitionname[NAME_SIZE];
79 strfieldcpy(filename, udev_root);
80 strfieldcat(filename, dev->name);
82 info("removing device node '%s'", filename);
83 retval = unlink(filename);
87 dbg("unlink(%s) failed with error '%s'",
88 filename, strerror(errno));
92 /* remove partition nodes */
93 if (dev->partitions > 0) {
94 info("removing partitions '%s[1-%i]'", filename, dev->partitions);
95 for (i = 1; i <= dev->partitions; i++) {
96 strfieldcpy(partitionname, filename);
97 strintcat(partitionname, i);
98 unlink(partitionname);
102 /* remove subdirectories */
103 if (strchr(dev->name, '/'))
104 delete_path(filename);
106 foreach_strpart(dev->symlink, " ", pos, len) {
107 strfieldcpymax(linkname, pos, len+1);
108 strfieldcpy(filename, udev_root);
109 strfieldcat(filename, linkname);
111 dbg("unlinking symlink '%s'", filename);
112 retval = unlink(filename);
116 dbg("unlink(%s) failed with error '%s'",
117 filename, strerror(errno));
120 if (strchr(dev->symlink, '/')) {
121 delete_path(filename);
129 * Look up the sysfs path in the database to see if we have named this device
130 * something different from the kernel name. If we have, us it. If not, use
131 * the default kernel name for lack of anything else to know to do.
133 int udev_remove_device(char *path, char *subsystem)
139 memset(&dev, 0, sizeof(dev));
141 retval = udevdb_get_dev(path, &dev);
143 dbg("'%s' not found in database, falling back on default name", path);
144 temp = strrchr(path, '/');
147 strfieldcpy(dev.name, &temp[1]);
150 dbg("name is '%s'", dev.name);
151 udevdb_delete_dev(path);
153 dev_d_send(&dev, subsystem);
154 sysbus_send_remove(dev.name, path);
156 retval = delete_node(&dev);