]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev_remove.c
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_libc_wrapper.h"
35 #include "udev_utils.h"
36 #include "udev_version.h"
40 static int delete_path(const char *path
)
45 pos
= strrchr(path
, '/');
48 pos
= strrchr(path
, '/');
50 /* don't remove the last one */
51 if ((pos
== path
) || (pos
== NULL
))
59 if (errno
== ENOTEMPTY
)
61 dbg("rmdir(%s) failed with error '%s'",
62 path
, strerror(errno
));
65 dbg("removed '%s'", path
);
70 static int delete_node(struct udevice
*udev
)
72 char filename
[PATH_SIZE
];
73 char partitionname
[PATH_SIZE
];
74 struct name_entry
*name_loop
;
80 list_for_each_entry(name_loop
, &udev
->symlink_list
, node
) {
81 snprintf(filename
, sizeof(filename
), "%s/%s", udev_root
, name_loop
->name
);
82 filename
[sizeof(filename
)-1] = '\0';
84 if (stat(filename
, &stats
) != 0) {
85 dbg("symlink '%s' not found", filename
);
88 if (udev
->devt
&& stats
.st_rdev
!= udev
->devt
) {
89 info("symlink '%s' points to a different device, skip removal", filename
);
93 dbg("removing symlink '%s'", filename
);
96 if (strchr(filename
, '/'))
97 delete_path(filename
);
100 snprintf(filename
, sizeof(filename
), "%s/%s", udev_root
, udev
->name
);
101 filename
[sizeof(filename
)-1] = '\0';
103 if (stat(filename
, &stats
) != 0) {
104 dbg("device node '%s' not found", filename
);
107 if (udev
->devt
&& stats
.st_rdev
!= udev
->devt
) {
108 info("device node '%s' points to a different device, skip removal", filename
);
112 info("removing device node '%s'", filename
);
113 retval
= unlink_secure(filename
);
117 num
= udev
->partitions
;
119 info("removing all_partitions '%s[1-%i]'", filename
, num
);
121 info("garbage from udev database, skip all_partitions removal");
124 for (i
= 1; i
<= num
; i
++) {
125 snprintf(partitionname
, sizeof(partitionname
), "%s%d", filename
, i
);
126 partitionname
[sizeof(partitionname
)-1] = '\0';
127 unlink_secure(partitionname
);
131 if (strchr(udev
->name
, '/'))
132 delete_path(filename
);
138 * look up the sysfs path in the database to get the node name to remove
139 * If we can't find it, use kernel name for lack of anything else to know to do
141 int udev_remove_device(struct udevice
*udev
)
143 if (udev
->type
!= DEV_BLOCK
&& udev
->type
!= DEV_CLASS
)
146 if (udev_db_get_device(udev
, udev
->devpath
) == 0) {
147 if (udev
->ignore_remove
) {
148 dbg("remove event for '%s' requested to be ignored by rule", udev
->name
);
151 dbg("remove name='%s'", udev
->name
);
152 udev_db_delete_device(udev
);
154 dbg("'%s' not found in database, using kernel name '%s'", udev
->devpath
, udev
->kernel_name
);
155 strlcpy(udev
->name
, udev
->kernel_name
, sizeof(udev
->name
));
157 /* use full path to the environment */
158 snprintf(udev
->devname
, sizeof(udev
->devname
), "%s/%s", udev_root
, udev
->name
);
159 udev
->devname
[sizeof(udev
->devname
)-1] = '\0';
161 return delete_node(udev
);