]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev_remove.c
[PATCH] 038 release
[thirdparty/systemd.git] / udev_remove.c
CommitLineData
ea733a2f
GKH
1/*
2 * udev-remove.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 *
8 *
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.
12 *
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.
17 *
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.
21 *
22 */
23
24#include <stdlib.h>
25#include <string.h>
26#include <stdio.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <errno.h>
30
31#include "udev.h"
c81b35c0 32#include "udev_lib.h"
ea733a2f 33#include "udev_version.h"
54988802 34#include "logging.h"
ea733a2f 35#include "namedev.h"
8e41d35d 36#include "udevdb.h"
ea733a2f 37
3d150dfb 38static int delete_path(char *path)
ea733a2f 39{
3d150dfb
KS
40 char *pos;
41 int retval;
ea733a2f 42
3d150dfb
KS
43 pos = strrchr(path, '/');
44 while (1) {
45 *pos = '\0';
46 pos = strrchr(path, '/');
47
48 /* don't remove the last one */
49 if ((pos == path) || (pos == NULL))
50 break;
51
52 /* remove if empty */
53 retval = rmdir(path);
92c5ddee
MI
54 if (errno == ENOENT)
55 retval = 0;
3d150dfb
KS
56 if (retval) {
57 if (errno == ENOTEMPTY)
58 return 0;
59 dbg("rmdir(%s) failed with error '%s'",
60 path, strerror(errno));
61 break;
62 }
63 dbg("removed '%s'", path);
5840bc63 64 }
3d150dfb 65 return 0;
ea733a2f
GKH
66}
67
e2eef6d4
MB
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
72 * to do so.
73 */
74static int secure_unlink(const char *filename)
75{
76 int retval;
77
78 retval = chown(filename, 0, 0);
79 if (retval) {
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.
85 */
86 }
87 retval = chmod(filename, 0000);
88 if (retval) {
89 dbg("chmod(%s, 0000) failed with error '%s'",
90 filename, strerror(errno));
91 /* We continue nevertheless. */
92 }
93 retval = unlink(filename);
94 if (errno == ENOENT)
95 retval = 0;
96 if (retval) {
97 dbg("unlink(%s) failed with error '%s'",
98 filename, strerror(errno));
99 }
100 return retval;
101}
102
3d150dfb 103static int delete_node(struct udevice *dev)
ea733a2f 104{
9fe3f9a9
KS
105 char filename[NAME_SIZE];
106 char linkname[NAME_SIZE];
107 char partitionname[NAME_SIZE];
218eae87 108 int retval;
50e5de03 109 int i;
ef672b3d
KS
110 char *pos;
111 int len;
7e89a569 112 int num;
ea733a2f 113
c472e3c8
KS
114 strfieldcpy(filename, udev_root);
115 strfieldcat(filename, dev->name);
ea733a2f 116
54988802 117 info("removing device node '%s'", filename);
e2eef6d4
MB
118 retval = secure_unlink(filename);
119 if (retval)
218eae87 120 return retval;
218eae87 121
7e89a569
KS
122 /* remove all_partitions nodes */
123 num = dev->partitions;
124 if (num > 0) {
125 info("removing all_partitions '%s[1-%i]'", filename, num);
126 if (num > PARTITIONS_COUNT) {
127 info("garbage from udev database, skip all_partitions removal");
128 return -1;
129 }
130 for (i = 1; i <= num; i++) {
c58e36c0
KS
131 strfieldcpy(partitionname, filename);
132 strintcat(partitionname, i);
e2eef6d4 133 secure_unlink(partitionname);
50e5de03
KS
134 }
135 }
136
218eae87 137 /* remove subdirectories */
3d150dfb
KS
138 if (strchr(dev->name, '/'))
139 delete_path(filename);
140
9fe3f9a9 141 foreach_strpart(dev->symlink, " ", pos, len) {
17794d77 142 strfieldcpymax(linkname, pos, len+1);
9fe3f9a9
KS
143 strfieldcpy(filename, udev_root);
144 strfieldcat(filename, linkname);
145
146 dbg("unlinking symlink '%s'", filename);
147 retval = unlink(filename);
148 if (errno == ENOENT)
149 retval = 0;
150 if (retval) {
151 dbg("unlink(%s) failed with error '%s'",
152 filename, strerror(errno));
153 return retval;
154 }
155 if (strchr(dev->symlink, '/')) {
156 delete_path(filename);
218eae87
KS
157 }
158 }
3d150dfb 159
218eae87 160 return retval;
ea733a2f
GKH
161}
162
3d150dfb
KS
163/*
164 * Look up the sysfs path in the database to see if we have named this device
165 * something different from the kernel name. If we have, us it. If not, use
166 * the default kernel name for lack of anything else to know to do.
167 */
eb6c7cd0 168int udev_remove_device(const char *path, const char *subsystem)
ea733a2f 169{
808423c9 170 struct udevice dev;
3d150dfb 171 char *temp;
a56ef382
KS
172 int retval;
173
f61d732a 174 memset(&dev, 0x00, sizeof(dev));
ea733a2f 175
bbbe503e
KS
176 retval = udevdb_get_dev(path, &dev);
177 if (retval != 0) {
178 dbg("'%s' not found in database, falling back on default name", path);
179 temp = strrchr(path, '/');
180 if (temp == NULL)
181 return -ENODEV;
182 strfieldcpy(dev.name, &temp[1]);
183 }
184 dbg("name='%s'", dev.name);
5aebfbcb 185
bbbe503e 186 dev.type = get_device_type(path, subsystem);
9b28a52a 187 dev_d_send(&dev, subsystem, path);
bbbe503e 188 udevdb_delete_dev(path);
f61d732a 189
bbbe503e 190 if (dev.type == 'b' || dev.type == 'c')
f61d732a 191 retval = delete_node(&dev);
bbbe503e 192 else if (dev.type == 'n')
f61d732a 193 retval = 0;
7ac0feeb 194
a56ef382 195 return retval;
ea733a2f 196}