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