]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev-remove.c
[PATCH] catch replace device by wildcard
[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"
33#include "namedev.h"
8e41d35d 34#include "udevdb.h"
ea733a2f
GKH
35#include "libsysfs/libsysfs.h"
36
37
38/*
a9ce0a41
GKH
39 * Look up the sysfs path in the database to see if we have named this device
40 * something different from the kernel name. If we have, us it. If not, use
41 * the default kernel name for lack of anything else to know to do.
ea733a2f 42 */
5840bc63 43static char *get_name(char *path, int major, int minor)
ea733a2f
GKH
44{
45 static char name[100];
5840bc63 46 struct udevice *dev;
ea733a2f
GKH
47 char *temp;
48
5840bc63
GKH
49 dev = udevdb_get_dev(path);
50 if (dev != NULL) {
51 strcpy(name, dev->name);
a9ce0a41 52 goto exit;
5840bc63 53 }
a9ce0a41 54
f7b4eca4 55 dbg("'%s' not found in database, falling back on default name", path);
5840bc63 56 temp = strrchr(path, '/');
ea733a2f
GKH
57 if (temp == NULL)
58 return NULL;
59 strncpy(name, &temp[1], sizeof(name));
60
a9ce0a41 61exit:
f7b4eca4 62 dbg("name is '%s'", name);
ea733a2f
GKH
63 return &name[0];
64}
65
66/*
67 * We also want to clean up any symlinks that were created in create_node()
68 */
69static int delete_node(char *name)
70{
71 char filename[255];
218eae87 72 int retval;
ea733a2f 73
c056c514 74 strncpy(filename, udev_root, sizeof(filename));
ea733a2f
GKH
75 strncat(filename, name, sizeof(filename));
76
f7b4eca4 77 dbg("unlinking '%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 */
86 if (strchr(name, '/')) {
87 char *pos;
88
89 pos = strrchr(filename, '/');
90 while (1) {
91 *pos = 0x00;
92 pos = strrchr(filename, '/');
93
94 /* don't remove the last one */
95 if ((pos == filename) || (pos == NULL))
96 break;
97
98 /* remove if empty */
99 retval = rmdir(filename);
100 if (retval) {
101 if (errno == ENOTEMPTY)
102 return 0;
103 dbg("rmdir(%s) failed with error '%s'",
104 filename, strerror(errno));
105 break;
106 }
f7b4eca4 107 dbg("removed '%s'", filename);
218eae87
KS
108 }
109 }
110 return retval;
ea733a2f
GKH
111}
112
113int udev_remove_device(char *device, char *subsystem)
114{
115 char *name;
116 int retval = 0;
117
118 name = get_name(device, 0, 0);
119 if (name == NULL) {
120 dbg ("get_name failed");
121 retval = -ENODEV;
122 goto exit;
123 }
124
a9ce0a41 125 udevdb_delete_dev(device);
8e41d35d 126
ea733a2f
GKH
127 return delete_node(name);
128
129exit:
130 return retval;
131}