]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev_remove.c
e1af3dbef61cf3a0d34a67af13e972eb31dd5c2a
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.
34 #include "udev_utils.h"
35 #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
[NAME_SIZE
];
73 char partitionname
[NAME_SIZE
];
80 snprintf(filename
, NAME_SIZE
, "%s/%s", udev_root
, udev
->name
);
81 filename
[NAME_SIZE
-1] = '\0';
83 info("removing device node '%s'", filename
);
84 retval
= unlink_secure(filename
);
88 /* remove all_partitions nodes */
89 num
= udev
->partitions
;
91 info("removing all_partitions '%s[1-%i]'", filename
, num
);
92 if (num
> PARTITIONS_COUNT
) {
93 info("garbage from udev database, skip all_partitions removal");
96 for (i
= 1; i
<= num
; i
++) {
97 strfieldcpy(partitionname
, filename
);
98 strintcat(partitionname
, i
);
99 unlink_secure(partitionname
);
103 /* remove subdirectories */
104 if (strchr(udev
->name
, '/'))
105 delete_path(filename
);
107 foreach_strpart(udev
->symlink
, " ", pos
, len
) {
108 char linkname
[NAME_SIZE
];
110 strfieldcpymax(linkname
, pos
, len
+1);
111 snprintf(filename
, NAME_SIZE
, "%s/%s", udev_root
, linkname
);
112 filename
[NAME_SIZE
-1] = '\0';
114 dbg("unlinking symlink '%s'", filename
);
115 retval
= unlink(filename
);
119 dbg("unlink(%s) failed with error '%s'",
120 filename
, strerror(errno
));
123 if (strchr(udev
->symlink
, '/')) {
124 delete_path(filename
);
132 * look up the sysfs path in the database to get the node name to remove
133 * If we can't find it, use kernel name for lack of anything else to know to do
135 int udev_remove_device(struct udevice
*udev
)
140 if (udev
->type
!= 'b' && udev
->type
!= 'c')
143 retval
= udev_db_get_device(udev
);
145 /* fall back to kernel name */
146 temp
= strrchr(udev
->devpath
, '/');
149 strfieldcpy(udev
->name
, &temp
[1]);
150 dbg("'%s' not found in database, falling back on default name", udev
->name
);
153 if (udev
->ignore_remove
) {
154 dbg("remove event for '%s' requested to be ignored by rule", udev
->name
);
158 dbg("remove name='%s'", udev
->name
);
159 udev_db_delete_device(udev
);
161 /* use full path to the environment */
162 snprintf(udev
->devname
, NAME_SIZE
, "%s/%s", udev_root
, udev
->name
);
164 return delete_node(udev
);