]>
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.
34 #include "udev_version.h"
39 static int delete_path(const 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 /** Remove all permissions on the device node, before
70 * unlinking it. This fixes a security issue.
71 * If the user created a hard-link to the device node,
72 * he can't use it any longer, because he lost permission
75 static int secure_unlink(const char *filename
)
79 retval
= chown(filename
, 0, 0);
81 dbg("chown(%s, 0, 0) failed with error '%s'",
82 filename
, strerror(errno
));
83 /* We continue nevertheless.
84 * I think it's very unlikely for chown
85 * to fail here, if the file exists.
88 retval
= chmod(filename
, 0000);
90 dbg("chmod(%s, 0000) failed with error '%s'",
91 filename
, strerror(errno
));
92 /* We continue nevertheless. */
94 retval
= unlink(filename
);
98 dbg("unlink(%s) failed with error '%s'",
99 filename
, strerror(errno
));
104 static int delete_node(struct udevice
*dev
)
106 char filename
[NAME_SIZE
];
107 char linkname
[NAME_SIZE
];
108 char partitionname
[NAME_SIZE
];
115 strfieldcpy(filename
, udev_root
);
116 strfieldcat(filename
, dev
->name
);
118 info("removing device node '%s'", filename
);
119 retval
= secure_unlink(filename
);
123 /* remove all_partitions nodes */
124 num
= dev
->partitions
;
126 info("removing all_partitions '%s[1-%i]'", filename
, num
);
127 if (num
> PARTITIONS_COUNT
) {
128 info("garbage from udev database, skip all_partitions removal");
131 for (i
= 1; i
<= num
; i
++) {
132 strfieldcpy(partitionname
, filename
);
133 strintcat(partitionname
, i
);
134 secure_unlink(partitionname
);
138 /* remove subdirectories */
139 if (strchr(dev
->name
, '/'))
140 delete_path(filename
);
142 foreach_strpart(dev
->symlink
, " ", pos
, len
) {
143 strfieldcpymax(linkname
, pos
, len
+1);
144 strfieldcpy(filename
, udev_root
);
145 strfieldcat(filename
, linkname
);
147 dbg("unlinking symlink '%s'", filename
);
148 retval
= unlink(filename
);
152 dbg("unlink(%s) failed with error '%s'",
153 filename
, strerror(errno
));
156 if (strchr(dev
->symlink
, '/')) {
157 delete_path(filename
);
165 * look up the sysfs path in the database to get the node name to remove
166 * If we can't find it, use kernel name for lack of anything else to know to do
168 int udev_remove_device(struct udevice
*udev
)
170 struct udevice db_dev
;
174 if (udev
->type
!= 'b' && udev
->type
!= 'c')
177 retval
= udevdb_get_dev(udev
->devpath
, &db_dev
);
179 /* copy over the stored values to our device */
180 memcpy(udev
, &db_dev
, UDEVICE_DB_LEN
);
182 /* fall back to kernel name */
183 temp
= strrchr(udev
->devpath
, '/');
186 strfieldcpy(udev
->name
, &temp
[1]);
187 dbg("'%s' not found in database, falling back on default name", udev
->name
);
190 dbg("remove name='%s'", udev
->name
);
191 udevdb_delete_dev(udev
->devpath
);
193 /* use full path to the environment */
194 snprintf(udev
->devname
, NAME_SIZE
-1, "%s%s", udev_root
, udev
->name
);
196 return delete_node(udev
);