chiark / gitweb /
[PATCH] big cleanup of internal udev api
[elogind.git] / udev_remove.c
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 <stddef.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <errno.h>
31
32 #include "udev.h"
33 #include "udev_lib.h"
34 #include "udev_version.h"
35 #include "logging.h"
36 #include "namedev.h"
37 #include "udevdb.h"
38
39 static int delete_path(char *path)
40 {
41         char *pos;
42         int retval;
43
44         pos = strrchr(path, '/');
45         while (1) {
46                 *pos = '\0';
47                 pos = strrchr(path, '/');
48
49                 /* don't remove the last one */
50                 if ((pos == path) || (pos == NULL))
51                         break;
52
53                 /* remove if empty */
54                 retval = rmdir(path);
55                 if (errno == ENOENT)
56                         retval = 0;
57                 if (retval) {
58                         if (errno == ENOTEMPTY)
59                                 return 0;
60                         dbg("rmdir(%s) failed with error '%s'",
61                             path, strerror(errno));
62                         break;
63                 }
64                 dbg("removed '%s'", path);
65         }
66         return 0;
67 }
68
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
73   * to do so.
74   */
75 static int secure_unlink(const char *filename)
76 {
77         int retval;
78
79         retval = chown(filename, 0, 0);
80         if (retval) {
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.
86                  */
87         }
88         retval = chmod(filename, 0000);
89         if (retval) {
90                 dbg("chmod(%s, 0000) failed with error '%s'",
91                     filename, strerror(errno));
92                 /* We continue nevertheless. */
93         }
94         retval = unlink(filename);
95         if (errno == ENOENT)
96                 retval = 0;
97         if (retval) {
98                 dbg("unlink(%s) failed with error '%s'",
99                         filename, strerror(errno));
100         }
101         return retval;
102 }
103
104 static int delete_node(struct udevice *dev)
105 {
106         char filename[NAME_SIZE];
107         char linkname[NAME_SIZE];
108         char partitionname[NAME_SIZE];
109         int retval;
110         int i;
111         char *pos;
112         int len;
113         int num;
114
115         strfieldcpy(filename, udev_root);
116         strfieldcat(filename, dev->name);
117
118         info("removing device node '%s'", filename);
119         retval = secure_unlink(filename);
120         if (retval)
121                 return retval;
122
123         /* remove all_partitions nodes */
124         num = dev->partitions;
125         if (num > 0) {
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");
129                         return -1;
130                 }
131                 for (i = 1; i <= num; i++) {
132                         strfieldcpy(partitionname, filename);
133                         strintcat(partitionname, i);
134                         secure_unlink(partitionname);
135                 }
136         }
137
138         /* remove subdirectories */
139         if (strchr(dev->name, '/'))
140                 delete_path(filename);
141
142         foreach_strpart(dev->symlink, " ", pos, len) {
143                 strfieldcpymax(linkname, pos, len+1);
144                 strfieldcpy(filename, udev_root);
145                 strfieldcat(filename, linkname);
146
147                 dbg("unlinking symlink '%s'", filename);
148                 retval = unlink(filename);
149                 if (errno == ENOENT)
150                         retval = 0;
151                 if (retval) {
152                         dbg("unlink(%s) failed with error '%s'",
153                                 filename, strerror(errno));
154                         return retval;
155                 }
156                 if (strchr(dev->symlink, '/')) {
157                         delete_path(filename);
158                 }
159         }
160
161         return retval;
162 }
163
164 /*
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
167  */
168 int udev_remove_device(struct udevice *udev)
169 {
170         struct udevice db_dev;
171         char *temp;
172         int retval;
173
174         memset(&db_dev, 0x00, sizeof(struct udevice));
175
176         retval = udevdb_get_dev(udev->devpath, &db_dev);
177         if (retval == 0) {
178                 /* get stored values in our device */
179                 memcpy(udev, &db_dev, UDEVICE_DB_LEN);
180         } else {
181                 /* fall back to kernel name */
182                 temp = strrchr(udev->devpath, '/');
183                 if (temp == NULL)
184                         return -ENODEV;
185                 strfieldcpy(udev->name, &temp[1]);
186                 dbg("'%s' not found in database, falling back on default name", udev->name);
187         }
188         dbg("remove name='%s'", udev->name);
189
190         dev_d_send(udev);
191         udevdb_delete_dev(udev->devpath);
192
193         if (udev->type == 'b' || udev->type == 'c')
194                 retval = delete_node(udev);
195         else
196                 retval = 0;
197
198         return retval;
199 }