chiark / gitweb /
[PATCH] move all of the DBUS logic into one file and remove all of the #ifdef crud...
[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 "namedev.h"
35 #include "udevdb.h"
36 #include "libsysfs/libsysfs.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 (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);
62         }
63         return 0;
64 }
65
66 static int delete_node(struct udevice *dev)
67 {
68         char filename[255];
69         int retval;
70
71         strncpy(filename, udev_root, sizeof(filename));
72         strncat(filename, dev->name, sizeof(filename));
73
74         dbg("unlinking node '%s'", filename);
75         retval = unlink(filename);
76         if (retval) {
77                 dbg("unlink(%s) failed with error '%s'",
78                         filename, strerror(errno));
79                 return retval;
80         }
81
82         /* remove subdirectories */
83         if (strchr(dev->name, '/'))
84                 delete_path(filename);
85
86         if (*dev->symlink) {
87                 strncpy(filename, udev_root, sizeof(filename));
88                 strncat(filename, dev->symlink, sizeof(filename));
89                 dbg("unlinking symlink '%s'", filename);
90                 retval = unlink(filename);
91                 if (retval) {
92                         dbg("unlink(%s) failed with error '%s'",
93                                 filename, strerror(errno));
94                         return retval;
95                 }
96                 if (strchr(dev->symlink, '/')) {
97                         delete_path(filename);
98                 }
99         }
100
101         return retval;
102 }
103
104 /*
105  * Look up the sysfs path in the database to see if we have named this device
106  * something different from the kernel name.  If we have, us it.  If not, use
107  * the default kernel name for lack of anything else to know to do.
108  */
109 int udev_remove_device(char *path, char *subsystem)
110 {
111         char name[100];
112         struct udevice *dev;
113         char *temp;
114
115         dev = udevdb_get_dev(path);
116         if (dev == NULL) {
117                 dbg("'%s' not found in database, falling back on default name", path);
118                 temp = strrchr(path, '/');
119                 if (temp == NULL)
120                         return -ENODEV;
121                 strncpy(name, &temp[1], sizeof(name));
122         }
123
124         dbg("name is '%s'", dev->name);
125         udevdb_delete_dev(path);
126
127         sysbus_send_remove(name, path);
128
129         return delete_node(dev);
130 }