chiark / gitweb /
[PATCH] cleanup db functions
[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 static int delete_node(struct udevice *udev)
71 {
72         char filename[NAME_SIZE];
73         char partitionname[NAME_SIZE];
74         struct stat stats;
75         int retval;
76         int i;
77         char *pos;
78         int len;
79         int num;
80
81         snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
82         filename[NAME_SIZE-1] = '\0';
83
84         dbg("checking major/minor of device node '%s'", filename);
85         if (stat(filename, &stats) != 0)
86                 return -1;
87
88         if (udev->devt && stats.st_rdev != udev->devt) {
89                 info("device node '%s' points to a different device, skip removal", filename);
90                 return -1;
91         }
92
93         info("removing device node '%s'", filename);
94         retval = unlink_secure(filename);
95         if (retval)
96                 return retval;
97
98         /* remove all_partitions nodes */
99         num = udev->partitions;
100         if (num > 0) {
101                 info("removing all_partitions '%s[1-%i]'", filename, num);
102                 if (num > 255) {
103                         info("garbage from udev database, skip all_partitions removal");
104                         return -1;
105                 }
106                 for (i = 1; i <= num; i++) {
107                         strfieldcpy(partitionname, filename);
108                         strintcat(partitionname, i);
109                         unlink_secure(partitionname);
110                 }
111         }
112
113         /* remove subdirectories */
114         if (strchr(udev->name, '/'))
115                 delete_path(filename);
116
117         foreach_strpart(udev->symlink, " ", pos, len) {
118                 char linkname[NAME_SIZE];
119
120                 strfieldcpymax(linkname, pos, len+1);
121                 snprintf(filename, NAME_SIZE, "%s/%s", udev_root, linkname);
122                 filename[NAME_SIZE-1] = '\0';
123
124                 dbg("unlinking symlink '%s'", filename);
125                 retval = unlink(filename);
126                 if (errno == ENOENT)
127                         retval = 0;
128                 if (retval) {
129                         dbg("unlink(%s) failed with error '%s'",
130                                 filename, strerror(errno));
131                         return retval;
132                 }
133                 if (strchr(udev->symlink, '/')) {
134                         delete_path(filename);
135                 }
136         }
137
138         return retval;
139 }
140
141 /*
142  * look up the sysfs path in the database to get the node name to remove
143  * If we can't find it, use kernel name for lack of anything else to know to do
144  */
145 int udev_remove_device(struct udevice *udev)
146 {
147         const char *temp;
148         int retval;
149
150         if (udev->type != BLOCK && udev->type != CLASS)
151                 return 0;
152
153         retval = udev_db_get_device_by_devpath(udev, udev->devpath);
154         if (retval) {
155                 /* fall back to kernel name */
156                 temp = strrchr(udev->devpath, '/');
157                 if (temp == NULL)
158                         return -ENODEV;
159                 strfieldcpy(udev->name, &temp[1]);
160                 dbg("'%s' not found in database, falling back on default name", udev->name);
161         }
162
163         if (udev->ignore_remove) {
164                 dbg("remove event for '%s' requested to be ignored by rule", udev->name);
165                 return 0;
166         }
167
168         dbg("remove name='%s'", udev->name);
169         udev_db_delete_device(udev);
170
171         /* use full path to the environment */
172         snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name);
173
174         return delete_node(udev);
175 }