chiark / gitweb /
[PATCH] hmm, handle net devices with udev?
[elogind.git] / udev_lib.c
1 /*
2  * udev_lib - generic stuff used by udev
3  *
4  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28
29 #include "libsysfs/sysfs/libsysfs.h"
30 #include "udev.h"
31 #include "udev_lib.h"
32
33
34 char *get_action(void)
35 {
36         char *action;
37
38         action = getenv("ACTION");
39         if (action != NULL && strlen(action) > ACTION_SIZE)
40                 action[ACTION_SIZE-1] = '\0';
41
42         return action;
43 }
44
45 char *get_devpath(void)
46 {
47         char *devpath;
48
49         devpath = getenv("DEVPATH");
50         if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
51                 devpath[DEVPATH_SIZE-1] = '\0';
52
53         return devpath;
54 }
55
56 char *get_devnode(void)
57 {
58         char *devnode;
59
60         devnode = getenv("DEVNODE");
61         if (devnode != NULL && strlen(devnode) > NAME_SIZE)
62                 devnode[NAME_SIZE-1] = '\0';
63
64         return devnode;
65 }
66
67 char *get_seqnum(void)
68 {
69         char *seqnum;
70
71         seqnum = getenv("SEQNUM");
72
73         return seqnum;
74 }
75
76 char *get_subsystem(char *subsystem)
77 {
78         if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
79                 subsystem[SUBSYSTEM_SIZE-1] = '\0';
80
81         return subsystem;
82 }
83
84 char get_device_type(const char *path, const char *subsystem)
85 {
86         if (strcmp(subsystem, "block") == 0 ||
87             strstr(path, "/block/") != NULL)
88                 return 'b';
89
90         if (strcmp(subsystem, "net") == 0 ||
91             strstr(path, "/class/net/") != NULL)
92                 return 'n';
93
94         if (strstr(path, "/class/") != NULL)
95                 return 'c';
96
97         return '\0';
98 }
99
100 int file_map(const char *filename, char **buf, size_t *bufsize)
101 {
102         struct stat stats;
103         int fd;
104
105         fd = open(filename, O_RDONLY);
106         if (fd < 0) {
107                 return -1;
108         }
109
110         if (fstat(fd, &stats) < 0) {
111                 return -1;
112         }
113
114         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
115         if (*buf == MAP_FAILED) {
116                 return -1;
117         }
118         *bufsize = stats.st_size;
119
120         close(fd);
121
122         return 0;
123 }
124
125 void file_unmap(char *buf, size_t bufsize)
126 {
127         munmap(buf, bufsize);
128 }
129
130 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
131 {
132         size_t count = 0;
133
134         for (count = cur; count < buflen && buf[count] != '\n'; count++);
135
136         return count - cur;
137 }
138