]>
Commit | Line | Data |
---|---|---|
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> | |
7a947ce5 | 26 | #include <stddef.h> |
ea733a2f GKH |
27 | #include <stdio.h> |
28 | #include <fcntl.h> | |
29 | #include <unistd.h> | |
30 | #include <errno.h> | |
1ceba936 | 31 | #include <sys/stat.h> |
ea733a2f GKH |
32 | |
33 | #include "udev.h" | |
c81b35c0 | 34 | #include "udev_lib.h" |
ea733a2f | 35 | #include "udev_version.h" |
54988802 | 36 | #include "logging.h" |
ea733a2f | 37 | #include "namedev.h" |
8e41d35d | 38 | #include "udevdb.h" |
ea733a2f | 39 | |
5d24c6ca | 40 | static int delete_path(const char *path) |
ea733a2f | 41 | { |
3d150dfb KS |
42 | char *pos; |
43 | int retval; | |
ea733a2f | 44 | |
3d150dfb KS |
45 | pos = strrchr(path, '/'); |
46 | while (1) { | |
47 | *pos = '\0'; | |
48 | pos = strrchr(path, '/'); | |
49 | ||
50 | /* don't remove the last one */ | |
51 | if ((pos == path) || (pos == NULL)) | |
52 | break; | |
53 | ||
54 | /* remove if empty */ | |
55 | retval = rmdir(path); | |
92c5ddee MI |
56 | if (errno == ENOENT) |
57 | retval = 0; | |
3d150dfb KS |
58 | if (retval) { |
59 | if (errno == ENOTEMPTY) | |
60 | return 0; | |
61 | dbg("rmdir(%s) failed with error '%s'", | |
62 | path, strerror(errno)); | |
63 | break; | |
64 | } | |
65 | dbg("removed '%s'", path); | |
5840bc63 | 66 | } |
3d150dfb | 67 | return 0; |
ea733a2f GKH |
68 | } |
69 | ||
e2eef6d4 MB |
70 | /** Remove all permissions on the device node, before |
71 | * unlinking it. This fixes a security issue. | |
72 | * If the user created a hard-link to the device node, | |
73 | * he can't use it any longer, because he lost permission | |
74 | * to do so. | |
75 | */ | |
76 | static int secure_unlink(const char *filename) | |
77 | { | |
78 | int retval; | |
79 | ||
80 | retval = chown(filename, 0, 0); | |
81 | if (retval) { | |
82 | dbg("chown(%s, 0, 0) failed with error '%s'", | |
83 | filename, strerror(errno)); | |
84 | /* We continue nevertheless. | |
85 | * I think it's very unlikely for chown | |
86 | * to fail here, if the file exists. | |
87 | */ | |
88 | } | |
89 | retval = chmod(filename, 0000); | |
90 | if (retval) { | |
91 | dbg("chmod(%s, 0000) failed with error '%s'", | |
92 | filename, strerror(errno)); | |
93 | /* We continue nevertheless. */ | |
94 | } | |
95 | retval = unlink(filename); | |
96 | if (errno == ENOENT) | |
97 | retval = 0; | |
98 | if (retval) { | |
99 | dbg("unlink(%s) failed with error '%s'", | |
100 | filename, strerror(errno)); | |
101 | } | |
102 | return retval; | |
103 | } | |
104 | ||
3d150dfb | 105 | static int delete_node(struct udevice *dev) |
ea733a2f | 106 | { |
9fe3f9a9 KS |
107 | char filename[NAME_SIZE]; |
108 | char linkname[NAME_SIZE]; | |
109 | char partitionname[NAME_SIZE]; | |
218eae87 | 110 | int retval; |
50e5de03 | 111 | int i; |
ef672b3d KS |
112 | char *pos; |
113 | int len; | |
7e89a569 | 114 | int num; |
ea733a2f | 115 | |
c472e3c8 KS |
116 | strfieldcpy(filename, udev_root); |
117 | strfieldcat(filename, dev->name); | |
ea733a2f | 118 | |
54988802 | 119 | info("removing device node '%s'", filename); |
e2eef6d4 MB |
120 | retval = secure_unlink(filename); |
121 | if (retval) | |
218eae87 | 122 | return retval; |
218eae87 | 123 | |
7e89a569 KS |
124 | /* remove all_partitions nodes */ |
125 | num = dev->partitions; | |
126 | if (num > 0) { | |
127 | info("removing all_partitions '%s[1-%i]'", filename, num); | |
128 | if (num > PARTITIONS_COUNT) { | |
129 | info("garbage from udev database, skip all_partitions removal"); | |
130 | return -1; | |
131 | } | |
132 | for (i = 1; i <= num; i++) { | |
c58e36c0 KS |
133 | strfieldcpy(partitionname, filename); |
134 | strintcat(partitionname, i); | |
e2eef6d4 | 135 | secure_unlink(partitionname); |
50e5de03 KS |
136 | } |
137 | } | |
138 | ||
218eae87 | 139 | /* remove subdirectories */ |
3d150dfb KS |
140 | if (strchr(dev->name, '/')) |
141 | delete_path(filename); | |
142 | ||
9fe3f9a9 | 143 | foreach_strpart(dev->symlink, " ", pos, len) { |
17794d77 | 144 | strfieldcpymax(linkname, pos, len+1); |
9fe3f9a9 KS |
145 | strfieldcpy(filename, udev_root); |
146 | strfieldcat(filename, linkname); | |
147 | ||
148 | dbg("unlinking symlink '%s'", filename); | |
149 | retval = unlink(filename); | |
150 | if (errno == ENOENT) | |
151 | retval = 0; | |
152 | if (retval) { | |
153 | dbg("unlink(%s) failed with error '%s'", | |
154 | filename, strerror(errno)); | |
155 | return retval; | |
156 | } | |
157 | if (strchr(dev->symlink, '/')) { | |
158 | delete_path(filename); | |
218eae87 KS |
159 | } |
160 | } | |
3d150dfb | 161 | |
218eae87 | 162 | return retval; |
ea733a2f GKH |
163 | } |
164 | ||
3d150dfb | 165 | /* |
7a947ce5 KS |
166 | * look up the sysfs path in the database to get the node name to remove |
167 | * If we can't find it, use kernel name for lack of anything else to know to do | |
3d150dfb | 168 | */ |
7a947ce5 | 169 | int udev_remove_device(struct udevice *udev) |
ea733a2f | 170 | { |
7a947ce5 | 171 | struct udevice db_dev; |
5d24c6ca | 172 | const char *temp; |
a56ef382 KS |
173 | int retval; |
174 | ||
5d24c6ca KS |
175 | if (udev->type != 'b' && udev->type != 'c') |
176 | return 0; | |
ea733a2f | 177 | |
7a947ce5 KS |
178 | retval = udevdb_get_dev(udev->devpath, &db_dev); |
179 | if (retval == 0) { | |
5d24c6ca | 180 | /* copy over the stored values to our device */ |
7a947ce5 KS |
181 | memcpy(udev, &db_dev, UDEVICE_DB_LEN); |
182 | } else { | |
183 | /* fall back to kernel name */ | |
184 | temp = strrchr(udev->devpath, '/'); | |
bbbe503e KS |
185 | if (temp == NULL) |
186 | return -ENODEV; | |
7a947ce5 KS |
187 | strfieldcpy(udev->name, &temp[1]); |
188 | dbg("'%s' not found in database, falling back on default name", udev->name); | |
bbbe503e | 189 | } |
5aebfbcb | 190 | |
5d24c6ca | 191 | dbg("remove name='%s'", udev->name); |
7a947ce5 | 192 | udevdb_delete_dev(udev->devpath); |
f61d732a | 193 | |
5d24c6ca KS |
194 | /* use full path to the environment */ |
195 | snprintf(udev->devname, NAME_SIZE-1, "%s%s", udev_root, udev->name); | |
7ac0feeb | 196 | |
5d24c6ca | 197 | return delete_node(udev); |
ea733a2f | 198 | } |