chiark / gitweb /
Fix trivial spelling errors in RELEASE-NOTES
[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.h"
32
33 static int delete_node(struct udevice *udev)
34 {
35         char filename[PATH_SIZE];
36         char partitionname[PATH_SIZE];
37         struct name_entry *name_loop;
38         struct stat stats;
39         int retval;
40         int i;
41         int num;
42
43         if (!list_empty(&udev->symlink_list)) {
44                 char symlinks[512] = "";
45
46                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
47                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
48                         filename[sizeof(filename)-1] = '\0';
49
50                         if (stat(filename, &stats) != 0) {
51                                 dbg("symlink '%s' not found", filename);
52                                 continue;
53                         }
54                         if (udev->devt && stats.st_rdev != udev->devt) {
55                                 info("symlink '%s' points to a different device, skip removal", filename);
56                                 continue;
57                         }
58
59                         info("removing symlink '%s'", filename);
60                         unlink(filename);
61
62                         if (strchr(filename, '/'))
63                                 delete_path(filename);
64
65                         strlcat(symlinks, filename, sizeof(symlinks));
66                         strlcat(symlinks, " ", sizeof(symlinks));
67                 }
68
69                 remove_trailing_chars(symlinks, ' ');
70                 if (symlinks[0] != '\0')
71                         setenv("DEVLINKS", symlinks, 1);
72         }
73
74         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
75         filename[sizeof(filename)-1] = '\0';
76
77         if (stat(filename, &stats) != 0) {
78                 dbg("device node '%s' not found", filename);
79                 return -1;
80         }
81         if (udev->devt && stats.st_rdev != udev->devt) {
82                 info("device node '%s' points to a different device, skip removal", filename);
83                 return -1;
84         }
85
86         info("removing device node '%s'", filename);
87         retval = unlink_secure(filename);
88         if (retval)
89                 return retval;
90
91         setenv("DEVNAME", filename, 1);
92
93         num = udev->partitions;
94         if (num > 0) {
95                 info("removing all_partitions '%s[1-%i]'", filename, num);
96                 if (num > 255) {
97                         info("garbage from udev database, skip all_partitions removal");
98                         return -1;
99                 }
100                 for (i = 1; i <= num; i++) {
101                         snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
102                         partitionname[sizeof(partitionname)-1] = '\0';
103                         unlink_secure(partitionname);
104                 }
105         }
106
107         if (strchr(udev->name, '/'))
108                 delete_path(filename);
109
110         return retval;
111 }
112
113 /*
114  * look up the sysfs path in the database to get the node name to remove
115  * If we can't find it, use kernel name for lack of anything else to know to do
116  */
117 int udev_remove_device(struct udevice *udev)
118 {
119         if (major(udev->devt) == 0)
120                 return 0;
121
122         if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
123                 if (udev->ignore_remove) {
124                         dbg("remove event for '%s' requested to be ignored by rule", udev->name);
125                         return 0;
126                 }
127                 dbg("remove name='%s'", udev->name);
128                 udev_db_delete_device(udev);
129         } else {
130                 dbg("'%s' not found in database, using kernel name '%s'", udev->dev->devpath, udev->dev->kernel_name);
131                 strlcpy(udev->name, udev->dev->kernel_name, sizeof(udev->name));
132         }
133
134         return delete_node(udev);
135 }