]>
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_version.h"
38 static int delete_path(char *path
)
43 pos
= strrchr(path
, '/');
46 pos
= strrchr(path
, '/');
48 /* don't remove the last one */
49 if ((pos
== path
) || (pos
== NULL
))
57 if (errno
== ENOTEMPTY
)
59 dbg("rmdir(%s) failed with error '%s'",
60 path
, strerror(errno
));
63 dbg("removed '%s'", path
);
68 /** Remove all permissions on the device node, before
69 * unlinking it. This fixes a security issue.
70 * If the user created a hard-link to the device node,
71 * he can't use it any longer, because he lost permission
74 static int secure_unlink(const char *filename
)
78 retval
= chown(filename
, 0, 0);
80 dbg("chown(%s, 0, 0) failed with error '%s'",
81 filename
, strerror(errno
));
82 /* We continue nevertheless.
83 * I think it's very unlikely for chown
84 * to fail here, if the file exists.
87 retval
= chmod(filename
, 0000);
89 dbg("chmod(%s, 0000) failed with error '%s'",
90 filename
, strerror(errno
));
91 /* We continue nevertheless. */
93 retval
= unlink(filename
);
97 dbg("unlink(%s) failed with error '%s'",
98 filename
, strerror(errno
));
103 static int delete_node(struct udevice
*dev
)
105 char filename
[NAME_SIZE
];
106 char linkname
[NAME_SIZE
];
107 char partitionname
[NAME_SIZE
];
113 strfieldcpy(filename
, udev_root
);
114 strfieldcat(filename
, dev
->name
);
116 info("removing device node '%s'", filename
);
117 retval
= secure_unlink(filename
);
121 /* remove partition nodes */
122 if (dev
->partitions
> 0) {
123 info("removing partitions '%s[1-%i]'", filename
, dev
->partitions
);
124 for (i
= 1; i
<= dev
->partitions
; i
++) {
125 strfieldcpy(partitionname
, filename
);
126 strintcat(partitionname
, i
);
127 secure_unlink(partitionname
);
131 /* remove subdirectories */
132 if (strchr(dev
->name
, '/'))
133 delete_path(filename
);
135 foreach_strpart(dev
->symlink
, " ", pos
, len
) {
136 strfieldcpymax(linkname
, pos
, len
+1);
137 strfieldcpy(filename
, udev_root
);
138 strfieldcat(filename
, linkname
);
140 dbg("unlinking symlink '%s'", filename
);
141 retval
= unlink(filename
);
145 dbg("unlink(%s) failed with error '%s'",
146 filename
, strerror(errno
));
149 if (strchr(dev
->symlink
, '/')) {
150 delete_path(filename
);
158 * Look up the sysfs path in the database to see if we have named this device
159 * something different from the kernel name. If we have, us it. If not, use
160 * the default kernel name for lack of anything else to know to do.
162 int udev_remove_device(const char *path
, const char *subsystem
)
168 memset(&dev
, 0x00, sizeof(dev
));
170 retval
= udevdb_get_dev(path
, &dev
);
172 dbg("'%s' not found in database, falling back on default name", path
);
173 temp
= strrchr(path
, '/');
176 strfieldcpy(dev
.name
, &temp
[1]);
178 dbg("name='%s'", dev
.name
);
180 dev
.type
= get_device_type(path
, subsystem
);
181 dev_d_send(&dev
, subsystem
, path
);
182 udevdb_delete_dev(path
);
184 if (dev
.type
== 'b' || dev
.type
== 'c')
185 retval
= delete_node(&dev
);
186 else if (dev
.type
== 'n')