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