]>
Commit | Line | Data |
---|---|---|
ea733a2f GKH |
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" | |
c81b35c0 | 32 | #include "udev_lib.h" |
ea733a2f | 33 | #include "udev_version.h" |
54988802 | 34 | #include "logging.h" |
ea733a2f | 35 | #include "namedev.h" |
8e41d35d | 36 | #include "udevdb.h" |
ea733a2f | 37 | |
3d150dfb | 38 | static int delete_path(char *path) |
ea733a2f | 39 | { |
3d150dfb KS |
40 | char *pos; |
41 | int retval; | |
ea733a2f | 42 | |
3d150dfb KS |
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); | |
92c5ddee MI |
54 | if (errno == ENOENT) |
55 | retval = 0; | |
3d150dfb KS |
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); | |
5840bc63 | 64 | } |
3d150dfb | 65 | return 0; |
ea733a2f GKH |
66 | } |
67 | ||
3d150dfb | 68 | static int delete_node(struct udevice *dev) |
ea733a2f | 69 | { |
9fe3f9a9 KS |
70 | char filename[NAME_SIZE]; |
71 | char linkname[NAME_SIZE]; | |
72 | char partitionname[NAME_SIZE]; | |
218eae87 | 73 | int retval; |
50e5de03 | 74 | int i; |
ef672b3d KS |
75 | char *pos; |
76 | int len; | |
ea733a2f | 77 | |
c472e3c8 KS |
78 | strfieldcpy(filename, udev_root); |
79 | strfieldcat(filename, dev->name); | |
ea733a2f | 80 | |
54988802 | 81 | info("removing device node '%s'", filename); |
218eae87 | 82 | retval = unlink(filename); |
92c5ddee MI |
83 | if (errno == ENOENT) |
84 | retval = 0; | |
218eae87 KS |
85 | if (retval) { |
86 | dbg("unlink(%s) failed with error '%s'", | |
87 | filename, strerror(errno)); | |
88 | return retval; | |
89 | } | |
90 | ||
50e5de03 KS |
91 | /* remove partition nodes */ |
92 | if (dev->partitions > 0) { | |
93 | info("removing partitions '%s[1-%i]'", filename, dev->partitions); | |
94 | for (i = 1; i <= dev->partitions; i++) { | |
c58e36c0 KS |
95 | strfieldcpy(partitionname, filename); |
96 | strintcat(partitionname, i); | |
50e5de03 KS |
97 | unlink(partitionname); |
98 | } | |
99 | } | |
100 | ||
218eae87 | 101 | /* remove subdirectories */ |
3d150dfb KS |
102 | if (strchr(dev->name, '/')) |
103 | delete_path(filename); | |
104 | ||
9fe3f9a9 | 105 | foreach_strpart(dev->symlink, " ", pos, len) { |
17794d77 | 106 | strfieldcpymax(linkname, pos, len+1); |
9fe3f9a9 KS |
107 | strfieldcpy(filename, udev_root); |
108 | strfieldcat(filename, linkname); | |
109 | ||
110 | dbg("unlinking symlink '%s'", filename); | |
111 | retval = unlink(filename); | |
112 | if (errno == ENOENT) | |
113 | retval = 0; | |
114 | if (retval) { | |
115 | dbg("unlink(%s) failed with error '%s'", | |
116 | filename, strerror(errno)); | |
117 | return retval; | |
118 | } | |
119 | if (strchr(dev->symlink, '/')) { | |
120 | delete_path(filename); | |
218eae87 KS |
121 | } |
122 | } | |
3d150dfb | 123 | |
218eae87 | 124 | return retval; |
ea733a2f GKH |
125 | } |
126 | ||
3d150dfb KS |
127 | /* |
128 | * Look up the sysfs path in the database to see if we have named this device | |
129 | * something different from the kernel name. If we have, us it. If not, use | |
130 | * the default kernel name for lack of anything else to know to do. | |
131 | */ | |
132 | int udev_remove_device(char *path, char *subsystem) | |
ea733a2f | 133 | { |
808423c9 | 134 | struct udevice dev; |
3d150dfb | 135 | char *temp; |
a56ef382 KS |
136 | int retval; |
137 | ||
f61d732a | 138 | memset(&dev, 0x00, sizeof(dev)); |
ea733a2f | 139 | |
bbbe503e KS |
140 | retval = udevdb_get_dev(path, &dev); |
141 | if (retval != 0) { | |
142 | dbg("'%s' not found in database, falling back on default name", path); | |
143 | temp = strrchr(path, '/'); | |
144 | if (temp == NULL) | |
145 | return -ENODEV; | |
146 | strfieldcpy(dev.name, &temp[1]); | |
147 | } | |
148 | dbg("name='%s'", dev.name); | |
5aebfbcb | 149 | |
bbbe503e | 150 | dev.type = get_device_type(path, subsystem); |
9b28a52a | 151 | dev_d_send(&dev, subsystem, path); |
bbbe503e | 152 | udevdb_delete_dev(path); |
f61d732a | 153 | |
bbbe503e | 154 | if (dev.type == 'b' || dev.type == 'c') |
f61d732a | 155 | retval = delete_node(&dev); |
bbbe503e | 156 | else if (dev.type == 'n') |
f61d732a | 157 | retval = 0; |
7ac0feeb | 158 | |
a56ef382 | 159 | return retval; |
ea733a2f | 160 | } |