chiark / gitweb /
update README
[elogind.git] / udev_remove.c
1 /*
2  * udev-remove.c
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30
31 #include "udev_libc_wrapper.h"
32 #include "udev.h"
33 #include "udev_utils.h"
34 #include "udev_version.h"
35 #include "logging.h"
36
37 static int delete_path(const char *path)
38 {
39         char *pos;
40         int retval;
41
42         pos = strrchr(path, '/');
43         while (1) {
44                 *pos = '\0';
45                 pos = strrchr(path, '/');
46
47                 /* don't remove the last one */
48                 if ((pos == path) || (pos == NULL))
49                         break;
50
51                 /* remove if empty */
52                 retval = rmdir(path);
53                 if (errno == ENOENT)
54                         retval = 0;
55                 if (retval) {
56                         if (errno == ENOTEMPTY)
57                                 return 0;
58                         err("rmdir(%s) failed: %s", path, strerror(errno));
59                         break;
60                 }
61                 dbg("removed '%s'", path);
62         }
63         return 0;
64 }
65
66 static int delete_node(struct udevice *udev)
67 {
68         char filename[PATH_SIZE];
69         char partitionname[PATH_SIZE];
70         struct name_entry *name_loop;
71         struct stat stats;
72         int retval;
73         int i;
74         int num;
75
76         list_for_each_entry(name_loop, &udev->symlink_list, node) {
77                 snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
78                 filename[sizeof(filename)-1] = '\0';
79
80                 if (stat(filename, &stats) != 0) {
81                         dbg("symlink '%s' not found", filename);
82                         continue;
83                 }
84                 if (udev->devt && stats.st_rdev != udev->devt) {
85                         info("symlink '%s' points to a different device, skip removal", filename);
86                         continue;;
87                 }
88
89                 info("removing symlink '%s'", filename);
90                 unlink(filename);
91
92                 if (strchr(filename, '/'))
93                         delete_path(filename);
94         }
95
96         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
97         filename[sizeof(filename)-1] = '\0';
98
99         if (stat(filename, &stats) != 0) {
100                 dbg("device node '%s' not found", filename);
101                 return -1;
102         }
103         if (udev->devt && stats.st_rdev != udev->devt) {
104                 info("device node '%s' points to a different device, skip removal", filename);
105                 return -1;
106         }
107
108         info("removing device node '%s'", filename);
109         retval = unlink_secure(filename);
110         if (retval)
111                 return retval;
112
113         /* export DEVNAME to the environment */
114         snprintf(udev->devname, sizeof(udev->devname), "%s/%s", udev_root, udev->name);
115         udev->devname[sizeof(udev->devname)-1] = '\0';
116
117         num = udev->partitions;
118         if (num > 0) {
119                 info("removing all_partitions '%s[1-%i]'", filename, num);
120                 if (num > 255) {
121                         info("garbage from udev database, skip all_partitions removal");
122                         return -1;
123                 }
124                 for (i = 1; i <= num; i++) {
125                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
126                         partitionname[sizeof(partitionname)-1] = '\0';
127                         unlink_secure(partitionname);
128                 }
129         }
130
131         if (strchr(udev->name, '/'))
132                 delete_path(filename);
133
134         return retval;
135 }
136
137 /*
138  * look up the sysfs path in the database to get the node name to remove
139  * If we can't find it, use kernel name for lack of anything else to know to do
140  */
141 int udev_remove_device(struct udevice *udev)
142 {
143         if (udev->type != DEV_BLOCK && udev->type != DEV_CLASS)
144                 return 0;
145
146         if (udev_db_get_device(udev, udev->devpath) == 0) {
147                 if (udev->ignore_remove) {
148                         dbg("remove event for '%s' requested to be ignored by rule", udev->name);
149                         return 0;
150                 }
151                 dbg("remove name='%s'", udev->name);
152                 udev_db_delete_device(udev);
153         } else {
154                 dbg("'%s' not found in database, using kernel name '%s'", udev->devpath, udev->kernel_name);
155                 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
156         }
157
158         return delete_node(udev);
159 }