chiark / gitweb /
c3a7880b496cb2a9d25554a5e5a59b92daef83c5
[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_libc_wrapper.h"
34 #include "udev.h"
35 #include "udev_utils.h"
36 #include "udev_version.h"
37 #include "namedev.h"
38 #include "udev_db.h"
39 #include "logging.h"
40
41 static int delete_path(const char *path)
42 {
43         char *pos;
44         int retval;
45
46         pos = strrchr(path, '/');
47         while (1) {
48                 *pos = '\0';
49                 pos = strrchr(path, '/');
50
51                 /* don't remove the last one */
52                 if ((pos == path) || (pos == NULL))
53                         break;
54
55                 /* remove if empty */
56                 retval = rmdir(path);
57                 if (errno == ENOENT)
58                         retval = 0;
59                 if (retval) {
60                         if (errno == ENOTEMPTY)
61                                 return 0;
62                         dbg("rmdir(%s) failed with error '%s'",
63                             path, strerror(errno));
64                         break;
65                 }
66                 dbg("removed '%s'", path);
67         }
68         return 0;
69 }
70
71 static int delete_node(struct udevice *udev)
72 {
73         char filename[PATH_SIZE];
74         char partitionname[PATH_SIZE];
75         struct name_entry *name_loop;
76         struct stat stats;
77         int retval;
78         int i;
79         int num;
80
81         list_for_each_entry(name_loop, &udev->symlink_list, node) {
82                 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
83                 filename[sizeof(filename)-1] = '\0';
84
85                 if (stat(filename, &stats) != 0) {
86                         dbg("symlink '%s' not found", filename);
87                         continue;
88                 }
89                 if (udev->devt && stats.st_rdev != udev->devt) {
90                         info("symlink '%s' points to a different device, skip removal", filename);
91                         continue;;
92                 }
93
94                 dbg("removing symlink '%s'", filename);
95                 unlink(filename);
96
97                 if (strchr(filename, '/'))
98                         delete_path(filename);
99         }
100
101         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
102         filename[sizeof(filename)-1] = '\0';
103
104         if (stat(filename, &stats) != 0) {
105                 dbg("device node '%s' not found", filename);
106                 return -1;
107         }
108         if (udev->devt && stats.st_rdev != udev->devt) {
109                 info("device node '%s' points to a different device, skip removal", filename);
110                 return -1;
111         }
112
113         info("removing device node '%s'", filename);
114         retval = unlink_secure(filename);
115         if (retval)
116                 return retval;
117
118         num = udev->partitions;
119         if (num > 0) {
120                 info("removing all_partitions '%s[1-%i]'", filename, num);
121                 if (num > 255) {
122                         info("garbage from udev database, skip all_partitions removal");
123                         return -1;
124                 }
125                 for (i = 1; i <= num; i++) {
126                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
127                         partitionname[sizeof(partitionname)-1] = '\0';
128                         unlink_secure(partitionname);
129                 }
130         }
131
132         if (strchr(udev->name, '/'))
133                 delete_path(filename);
134
135         return retval;
136 }
137
138 /*
139  * look up the sysfs path in the database to get the node name to remove
140  * If we can't find it, use kernel name for lack of anything else to know to do
141  */
142 int udev_remove_device(struct udevice *udev)
143 {
144         const char *temp;
145
146         if (udev->type != BLOCK && udev->type != CLASS)
147                 return 0;
148
149         if (udev_db_get_device(udev, udev->devpath) == 0) {
150                 if (udev->ignore_remove) {
151                         dbg("remove event for '%s' requested to be ignored by rule", udev->name);
152                         return 0;
153                 }
154                 dbg("remove name='%s'", udev->name);
155                 udev_db_delete_device(udev);
156         } else {
157                 /* fall back to kernel name */
158                 temp = strrchr(udev->devpath, '/');
159                 if (temp == NULL)
160                         return -ENODEV;
161                 strlcpy(udev->name, &temp[1], sizeof(udev->name));
162                 dbg("'%s' not found in database, falling back on default name", udev->name);
163         }
164
165         /* use full path to the environment */
166         snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name);
167         udev->devname[sizeof(udev->devname)-1] = '\0';
168
169         return delete_node(udev);
170 }