]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev-remove.c
[PATCH] udevd - fix socket path length
[thirdparty/systemd.git] / udev-remove.c
CommitLineData
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"
54988802 34#include "logging.h"
ea733a2f 35#include "namedev.h"
8e41d35d 36#include "udevdb.h"
ea733a2f
GKH
37#include "libsysfs/libsysfs.h"
38
3d150dfb 39static int delete_path(char *path)
ea733a2f 40{
3d150dfb
KS
41 char *pos;
42 int retval;
ea733a2f 43
3d150dfb
KS
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);
5840bc63 63 }
3d150dfb 64 return 0;
ea733a2f
GKH
65}
66
3d150dfb 67static int delete_node(struct udevice *dev)
ea733a2f
GKH
68{
69 char filename[255];
4763256c
KS
70 char *symlinks;
71 char *linkname;
218eae87 72 int retval;
ea733a2f 73
c056c514 74 strncpy(filename, udev_root, sizeof(filename));
3d150dfb 75 strncat(filename, dev->name, sizeof(filename));
ea733a2f 76
54988802 77 info("removing device node '%s'", filename);
218eae87
KS
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 */
3d150dfb
KS
86 if (strchr(dev->name, '/'))
87 delete_path(filename);
88
89 if (*dev->symlink) {
4763256c
KS
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 }
218eae87
KS
109 }
110 }
3d150dfb 111
218eae87 112 return retval;
ea733a2f
GKH
113}
114
3d150dfb
KS
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 */
120int udev_remove_device(char *path, char *subsystem)
ea733a2f 121{
808423c9 122 struct udevice dev;
3d150dfb 123 char *temp;
a56ef382
KS
124 int retval;
125
808423c9 126 memset(&dev, 0, sizeof(dev));
ea733a2f 127
808423c9 128 retval = udevdb_get_dev(path, &dev);
a56ef382 129 if (retval) {
3d150dfb
KS
130 dbg("'%s' not found in database, falling back on default name", path);
131 temp = strrchr(path, '/');
132 if (temp == NULL)
133 return -ENODEV;
808423c9 134 strncpy(dev.name, &temp[1], sizeof(dev.name));
ea733a2f
GKH
135 }
136
808423c9 137 dbg("name is '%s'", dev.name);
3d150dfb 138 udevdb_delete_dev(path);
5aebfbcb 139
808423c9 140 sysbus_send_remove(dev.name, path);
7ac0feeb 141
808423c9 142 retval = delete_node(&dev);
a56ef382 143 return retval;
ea733a2f 144}