]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev-remove.c
[PATCH] udev - trivial style cleanup
[thirdparty/systemd.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 #include "libsysfs/libsysfs.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 (retval) {
56 if (errno == ENOTEMPTY)
57 return 0;
58 dbg("rmdir(%s) failed with error '%s'",
59 path, strerror(errno));
60 break;
61 }
62 dbg("removed '%s'", path);
63 }
64 return 0;
65 }
66
67 static int delete_node(struct udevice *dev)
68 {
69 char filename[255];
70 char *symlinks;
71 char *linkname;
72 int retval;
73
74 strncpy(filename, udev_root, sizeof(filename));
75 strncat(filename, dev->name, sizeof(filename));
76
77 info("removing device node '%s'", filename);
78 retval = unlink(filename);
79 if (retval) {
80 dbg("unlink(%s) failed with error '%s'",
81 filename, strerror(errno));
82 return retval;
83 }
84
85 /* remove subdirectories */
86 if (strchr(dev->name, '/'))
87 delete_path(filename);
88
89 if (*dev->symlink) {
90 symlinks = dev->symlink;
91 while (1) {
92 linkname = strsep(&symlinks, " ");
93 if (linkname == NULL)
94 break;
95
96 strncpy(filename, udev_root, sizeof(filename));
97 strncat(filename, linkname, sizeof(filename));
98
99 dbg("unlinking symlink '%s'", filename);
100 retval = unlink(filename);
101 if (retval) {
102 dbg("unlink(%s) failed with error '%s'",
103 filename, strerror(errno));
104 return retval;
105 }
106 if (strchr(dev->symlink, '/')) {
107 delete_path(filename);
108 }
109 }
110 }
111
112 return retval;
113 }
114
115 /*
116 * Look up the sysfs path in the database to see if we have named this device
117 * something different from the kernel name. If we have, us it. If not, use
118 * the default kernel name for lack of anything else to know to do.
119 */
120 int udev_remove_device(char *path, char *subsystem)
121 {
122 struct udevice dev;
123 char *temp;
124 int retval;
125
126 memset(&dev, 0, sizeof(dev));
127
128 retval = udevdb_get_dev(path, &dev);
129 if (retval) {
130 dbg("'%s' not found in database, falling back on default name", path);
131 temp = strrchr(path, '/');
132 if (temp == NULL)
133 return -ENODEV;
134 strncpy(dev.name, &temp[1], sizeof(dev.name));
135 }
136
137 dbg("name is '%s'", dev.name);
138 udevdb_delete_dev(path);
139
140 sysbus_send_remove(dev.name, path);
141
142 retval = delete_node(&dev);
143 return retval;
144 }