chiark / gitweb /
[PATCH] don't sleep if 'dev' file is already present on device add.
[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 "namedev.h"
34 #include "udevdb.h"
35 #include "libsysfs/libsysfs.h"
36
37
38 /*
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.
42  */
43 static char *get_name(char *dev, int major, int minor)
44 {
45         static char name[100];
46         char *temp;
47
48         if (udevdb_get_dev(dev, &name[0], sizeof(name)) == 0)
49                 goto exit;
50
51         temp = strrchr(dev, '/');
52         if (temp == NULL)
53                 return NULL;
54         strncpy(name, &temp[1], sizeof(name));
55
56 exit:
57         dbg("name is %s", name);
58         return &name[0];
59 }
60
61 /*
62  * We also want to clean up any symlinks that were created in create_node()
63  */
64 static int delete_node(char *name)
65 {
66         char filename[255];
67
68         strncpy(filename, UDEV_ROOT, sizeof(filename));
69         strncat(filename, name, sizeof(filename));
70
71         dbg("unlinking %s", filename);
72         return unlink(filename);
73 }
74
75 int udev_remove_device(char *device, char *subsystem)
76 {
77         char *name;
78         int retval = 0;
79
80         name = get_name(device, 0, 0);
81         if (name == NULL) {
82                 dbg ("get_name failed");
83                 retval = -ENODEV;
84                 goto exit;
85         }
86
87         udevdb_delete_udevice(name);
88         udevdb_delete_dev(device);
89
90         return delete_node(name);
91
92 exit:
93         return retval;
94 }