chiark / gitweb /
[PATCH] replace tdb database by simple lockless file database
[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 <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31
32 #include "udev.h"
33 #include "logging.h"
34 #include "udev_lib.h"
35 #include "list.h"
36
37
38 char *get_action(void)
39 {
40         char *action;
41
42         action = getenv("ACTION");
43         if (action != NULL && strlen(action) > ACTION_SIZE)
44                 action[ACTION_SIZE-1] = '\0';
45
46         return action;
47 }
48
49 char *get_devpath(void)
50 {
51         char *devpath;
52
53         devpath = getenv("DEVPATH");
54         if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
55                 devpath[DEVPATH_SIZE-1] = '\0';
56
57         return devpath;
58 }
59
60 char *get_devname(void)
61 {
62         char *devname;
63
64         devname = getenv("DEVNAME");
65         if (devname != NULL && strlen(devname) > NAME_SIZE)
66                 devname[NAME_SIZE-1] = '\0';
67
68         return devname;
69 }
70
71 char *get_seqnum(void)
72 {
73         char *seqnum;
74
75         seqnum = getenv("SEQNUM");
76
77         return seqnum;
78 }
79
80 char *get_subsystem(char *subsystem)
81 {
82         if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
83                 subsystem[SUBSYSTEM_SIZE-1] = '\0';
84
85         return subsystem;
86 }
87
88 #define BLOCK_PATH              "/block/"
89 #define CLASS_PATH              "/class/"
90 #define NET_PATH                "/class/net/"
91
92 char get_device_type(const char *path, const char *subsystem)
93 {
94         if (strcmp(subsystem, "block") == 0)
95                 return 'b';
96
97         if (strcmp(subsystem, "net") == 0)
98                 return 'n';
99
100         if (strncmp(path, BLOCK_PATH, strlen(BLOCK_PATH)) == 0 &&
101             strlen(path) > strlen(BLOCK_PATH))
102                 return 'b';
103
104         if (strncmp(path, NET_PATH, strlen(NET_PATH)) == 0 &&
105             strlen(path) > strlen(NET_PATH))
106                 return 'n';
107
108         if (strncmp(path, CLASS_PATH, strlen(CLASS_PATH)) == 0 &&
109             strlen(path) > strlen(CLASS_PATH))
110                 return 'c';
111
112         return '\0';
113 }
114
115 void udev_set_values(struct udevice *udev, const char* devpath, const char *subsystem)
116 {
117         memset(udev, 0x00, sizeof(struct udevice));
118         strfieldcpy(udev->devpath, devpath);
119         strfieldcpy(udev->subsystem, subsystem);
120         udev->type = get_device_type(devpath, subsystem);
121 }
122
123 int create_path(const char *path)
124 {
125         char p[NAME_SIZE];
126         char *pos;
127         struct stat stats;
128
129         strcpy (p, path);
130         pos = strrchr(p, '/');
131         if (pos == p || pos == NULL)
132                 return 0;
133
134         while (pos[-1] == '/')
135                 pos--;
136
137         pos[0] = '\0';
138
139         dbg("stat '%s'\n", p);
140         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
141                 return 0;
142
143         if (create_path (p) != 0)
144                 return -1;
145
146         dbg("mkdir '%s'\n", p);
147         return mkdir(p, 0755);
148 }
149
150 int file_map(const char *filename, char **buf, size_t *bufsize)
151 {
152         struct stat stats;
153         int fd;
154
155         fd = open(filename, O_RDONLY);
156         if (fd < 0) {
157                 return -1;
158         }
159
160         if (fstat(fd, &stats) < 0) {
161                 close(fd);
162                 return -1;
163         }
164
165         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
166         if (*buf == MAP_FAILED) {
167                 close(fd);
168                 return -1;
169         }
170         *bufsize = stats.st_size;
171
172         close(fd);
173
174         return 0;
175 }
176
177 void file_unmap(char *buf, size_t bufsize)
178 {
179         munmap(buf, bufsize);
180 }
181
182 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
183 {
184         size_t count = 0;
185
186         for (count = cur; count < buflen && buf[count] != '\n'; count++);
187
188         return count - cur;
189 }
190
191 void no_trailing_slash(char *path)
192 {
193         int len;
194
195         len = strlen(path);
196         if (len > 0 && path[len-1] == '/')
197                 path[len-1] = '\0';
198 }
199
200 struct files {
201         struct list_head list;
202         char name[NAME_SIZE];
203 };
204
205 /* sort files in lexical order */
206 static int file_list_insert(char *filename, struct list_head *file_list)
207 {
208         struct files *loop_file;
209         struct files *new_file;
210
211         list_for_each_entry(loop_file, file_list, list) {
212                 if (strcmp(loop_file->name, filename) > 0) {
213                         break;
214                 }
215         }
216
217         new_file = malloc(sizeof(struct files));
218         if (new_file == NULL) {
219                 dbg("error malloc");
220                 return -ENOMEM;
221         }
222
223         strfieldcpy(new_file->name, filename);
224         list_add_tail(&new_file->list, &loop_file->list);
225         return 0;
226 }
227
228 /* calls function for every file found in specified directory */
229 int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix)
230 {
231         struct dirent *ent;
232         DIR *dir;
233         char *ext;
234         char file[NAME_SIZE];
235         struct files *loop_file;
236         struct files *tmp_file;
237         LIST_HEAD(file_list);
238
239         dbg("open directory '%s'", dirname);
240         dir = opendir(dirname);
241         if (dir == NULL) {
242                 dbg("unable to open '%s'", dirname);
243                 return -1;
244         }
245
246         while (1) {
247                 ent = readdir(dir);
248                 if (ent == NULL || ent->d_name[0] == '\0')
249                         break;
250
251                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
252                         continue;
253
254                 /* look for file with specified suffix */
255                 ext = strrchr(ent->d_name, '.');
256                 if (ext == NULL)
257                         continue;
258
259                 if (strcmp(ext, suffix) != 0)
260                         continue;
261
262                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
263                 file_list_insert(ent->d_name, &file_list);
264         }
265
266         /* call function for every file in the list */
267         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
268                 snprintf(file, NAME_SIZE-1, "%s/%s", dirname, loop_file->name);
269                 file[NAME_SIZE-1] = '\0';
270
271                 fnct(file);
272
273                 list_del(&loop_file->list);
274                 free(loop_file);
275         }
276
277         closedir(dir);
278         return 0;
279 }
280
281 /* Set the FD_CLOEXEC  flag of desc if value is nonzero,
282    or clear the flag if value is 0.
283    Return 0 on success, or -1 on error with errno  set. */ 
284         
285 int set_cloexec_flag (int desc, int value)
286 {
287         int oldflags = fcntl (desc, F_GETFD, 0);
288         /* If reading the flags failed, return error indication now. */
289         if (oldflags < 0)
290                 return oldflags;
291         /* Set just the flag we want to set. */
292         if (value != 0)
293                 oldflags |= FD_CLOEXEC;
294         else
295                 oldflags &= ~FD_CLOEXEC;
296         /* Store modified flag word in the descriptor. */
297         return fcntl (desc, F_SETFD, oldflags);
298 }