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