chiark / gitweb /
[PATCH] got "remove of named devices" working.
[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  * Here would go a call to the naming deamon, to get the name we want to have
40  * for this device.  But for now, let's just default to whatever the kernel is
41  * calling the device as that will keep the "old-style" naming policy
42  */
43 static char *get_name(char *dev, int major, int minor)
44 {
45         static char name[100];
46         char *temp;
47
48         temp = udevdb_get_udevice_by_sysfs(dev);
49         dbg("udevdb_get_udevice_by_sysfs returned %s", temp);
50         if (temp != NULL)
51                 return temp;
52         
53         temp = strrchr(dev, '/');
54         if (temp == NULL)
55                 return NULL;
56         strncpy(name, &temp[1], sizeof(name));
57
58         dbg("name is %s", name);
59
60         return &name[0];
61 }
62
63 /*
64  * We also want to clean up any symlinks that were created in create_node()
65  */
66 static int delete_node(char *name)
67 {
68         char filename[255];
69
70         strncpy(filename, UDEV_ROOT, sizeof(filename));
71         strncat(filename, name, sizeof(filename));
72
73         dbg("unlinking %s", filename);
74         return unlink(filename);
75 }
76
77 int udev_remove_device(char *device, char *subsystem)
78 {
79         char *name;
80         int retval = 0;
81
82         name = get_name(device, 0, 0);
83         if (name == NULL) {
84                 dbg ("get_name failed");
85                 retval = -ENODEV;
86                 goto exit;
87         }
88
89         udevdb_delete_udevice(name);
90
91         return delete_node(name);
92
93 exit:
94         return retval;
95 }