chiark / gitweb /
[PATCH] SCSI logical and physical names for udev
[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 <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "udev.h"
32 #include "udev_lib.h"
33 #include "udev_version.h"
34 #include "udev_dbus.h"
35 #include "logging.h"
36 #include "namedev.h"
37 #include "udevdb.h"
38
39 static int delete_path(char *path)
40 {
41         char *pos;
42         int retval;
43
44         pos = strrchr(path, '/');
45         while (1) {
46                 *pos = '\0';
47                 pos = strrchr(path, '/');
48
49                 /* don't remove the last one */
50                 if ((pos == path) || (pos == NULL))
51                         break;
52
53                 /* remove if empty */
54                 retval = rmdir(path);
55                 if (errno == ENOENT)
56                         retval = 0;
57                 if (retval) {
58                         if (errno == ENOTEMPTY)
59                                 return 0;
60                         dbg("rmdir(%s) failed with error '%s'",
61                             path, strerror(errno));
62                         break;
63                 }
64                 dbg("removed '%s'", path);
65         }
66         return 0;
67 }
68
69 static int delete_node(struct udevice *dev)
70 {
71         char filename[NAME_SIZE];
72         char linkname[NAME_SIZE];
73         char partitionname[NAME_SIZE];
74         int retval;
75         int i;
76         char *pos;
77         int len;
78
79         strfieldcpy(filename, udev_root);
80         strfieldcat(filename, dev->name);
81
82         info("removing device node '%s'", filename);
83         retval = unlink(filename);
84         if (errno == ENOENT)
85                 retval = 0;
86         if (retval) {
87                 dbg("unlink(%s) failed with error '%s'",
88                         filename, strerror(errno));
89                 return retval;
90         }
91
92         /* remove partition nodes */
93         if (dev->partitions > 0) {
94                 info("removing partitions '%s[1-%i]'", filename, dev->partitions);
95                 for (i = 1; i <= dev->partitions; i++) {
96                         strfieldcpy(partitionname, filename);
97                         strintcat(partitionname, i);
98                         unlink(partitionname);
99                 }
100         }
101
102         /* remove subdirectories */
103         if (strchr(dev->name, '/'))
104                 delete_path(filename);
105
106         foreach_strpart(dev->symlink, " ", pos, len) {
107                 strfieldcpymax(linkname, pos, len+1);
108                 strfieldcpy(filename, udev_root);
109                 strfieldcat(filename, linkname);
110
111                 dbg("unlinking symlink '%s'", filename);
112                 retval = unlink(filename);
113                 if (errno == ENOENT)
114                         retval = 0;
115                 if (retval) {
116                         dbg("unlink(%s) failed with error '%s'",
117                                 filename, strerror(errno));
118                         return retval;
119                 }
120                 if (strchr(dev->symlink, '/')) {
121                         delete_path(filename);
122                 }
123         }
124
125         return retval;
126 }
127
128 /*
129  * Look up the sysfs path in the database to see if we have named this device
130  * something different from the kernel name.  If we have, us it.  If not, use
131  * the default kernel name for lack of anything else to know to do.
132  */
133 int udev_remove_device(char *path, char *subsystem)
134 {
135         struct udevice dev;
136         char *temp;
137         int retval;
138
139         memset(&dev, 0, sizeof(dev));
140
141         retval = udevdb_get_dev(path, &dev);
142         if (retval) {
143                 dbg("'%s' not found in database, falling back on default name", path);
144                 temp = strrchr(path, '/');
145                 if (temp == NULL)
146                         return -ENODEV;
147                 strfieldcpy(dev.name, &temp[1]);
148         }
149
150         dbg("name is '%s'", dev.name);
151         udevdb_delete_dev(path);
152
153         sysbus_send_remove(dev.name, path);
154
155         retval = delete_node(&dev);
156         return retval;
157 }