chiark / gitweb /
[PATCH] I broke the extras/ again. Add simple build test script now.
[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 #include <sys/stat.h>
32
33 #include "udev.h"
34 #include "udev_utils.h"
35 #include "udev_version.h"
36 #include "namedev.h"
37 #include "udev_db.h"
38 #include "logging.h"
39
40 static int delete_path(const char *path)
41 {
42         char *pos;
43         int retval;
44
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);
56                 if (errno == ENOENT)
57                         retval = 0;
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);
66         }
67         return 0;
68 }
69
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
105 static int delete_node(struct udevice *udev)
106 {
107         char filename[NAME_SIZE];
108         char partitionname[NAME_SIZE];
109         int retval;
110         int i;
111         char *pos;
112         int len;
113         int num;
114
115         snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
116         filename[NAME_SIZE-1] = '\0';
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 = udev->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(udev->name, '/'))
140                 delete_path(filename);
141
142         foreach_strpart(udev->symlink, " ", pos, len) {
143                 char linkname[NAME_SIZE];
144
145                 strfieldcpymax(linkname, pos, len+1);
146                 snprintf(filename, NAME_SIZE, "%s/%s", udev_root, linkname);
147                 filename[NAME_SIZE-1] = '\0';
148
149                 dbg("unlinking symlink '%s'", filename);
150                 retval = unlink(filename);
151                 if (errno == ENOENT)
152                         retval = 0;
153                 if (retval) {
154                         dbg("unlink(%s) failed with error '%s'",
155                                 filename, strerror(errno));
156                         return retval;
157                 }
158                 if (strchr(udev->symlink, '/')) {
159                         delete_path(filename);
160                 }
161         }
162
163         return retval;
164 }
165
166 /*
167  * look up the sysfs path in the database to get the node name to remove
168  * If we can't find it, use kernel name for lack of anything else to know to do
169  */
170 int udev_remove_device(struct udevice *udev)
171 {
172         const char *temp;
173         int retval;
174
175         if (udev->type != 'b' && udev->type != 'c')
176                 return 0;
177
178         retval = udev_db_get_device(udev);
179         if (retval) {
180                 /* fall back to kernel name */
181                 temp = strrchr(udev->devpath, '/');
182                 if (temp == NULL)
183                         return -ENODEV;
184                 strfieldcpy(udev->name, &temp[1]);
185                 dbg("'%s' not found in database, falling back on default name", udev->name);
186         }
187
188         if (udev->ignore_remove) {
189                 dbg("remove event for '%s' requested to be ignored by rule", udev->name);
190                 return 0;
191         }
192
193         dbg("remove name='%s'", udev->name);
194         udev_db_delete_device(udev);
195
196         /* use full path to the environment */
197         snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name);
198
199         return delete_node(udev);
200 }