chiark / gitweb /
[PATCH] simplify permission application
[elogind.git] / udev_utils.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 #include <sys/utsname.h>
32
33 #include "udev.h"
34 #include "logging.h"
35 #include "udev_utils.h"
36 #include "list.h"
37
38
39 void udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
40 {
41         memset(udev, 0x00, sizeof(struct udevice));
42
43         if (devpath)
44                 strfieldcpy(udev->devpath, devpath);
45         if (subsystem)
46                 strfieldcpy(udev->subsystem, subsystem);
47
48         if (strcmp(udev->subsystem, "block") == 0)
49                 udev->type = 'b';
50         else if (strcmp(udev->subsystem, "net") == 0)
51                 udev->type = 'n';
52         else if (strncmp(udev->devpath, "/block/", 7) == 0)
53                 udev->type = 'b';
54         else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
55                 udev->type = 'n';
56         else if (strncmp(udev->devpath, "/class/", 7) == 0)
57                 udev->type = 'c';
58
59         udev->mode = default_mode;
60         strfieldcpy(udev->owner, default_owner);
61         strfieldcpy(udev->group, default_group);
62 }
63
64 int kernel_release_satisfactory(int version, int patchlevel, int sublevel)
65 {
66         static int kversion = 0;
67         static int kpatchlevel;
68         static int ksublevel;
69
70         if (kversion == 0) {
71                 struct utsname uts;
72                 if (uname(&uts) != 0)
73                         return -1;
74
75                 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
76                         kversion = 0;
77                         return -1;
78                 }
79         }
80
81         if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
82                 return 1;
83         else
84                 return 0;
85 }
86
87 int create_path(const char *path)
88 {
89         char p[NAME_SIZE];
90         char *pos;
91         struct stat stats;
92
93         strcpy (p, path);
94         pos = strrchr(p, '/');
95         if (pos == p || pos == NULL)
96                 return 0;
97
98         while (pos[-1] == '/')
99                 pos--;
100
101         pos[0] = '\0';
102
103         dbg("stat '%s'\n", p);
104         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
105                 return 0;
106
107         if (create_path (p) != 0)
108                 return -1;
109
110         dbg("mkdir '%s'\n", p);
111         return mkdir(p, 0755);
112 }
113
114 int file_map(const char *filename, char **buf, size_t *bufsize)
115 {
116         struct stat stats;
117         int fd;
118
119         fd = open(filename, O_RDONLY);
120         if (fd < 0) {
121                 return -1;
122         }
123
124         if (fstat(fd, &stats) < 0) {
125                 close(fd);
126                 return -1;
127         }
128
129         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
130         if (*buf == MAP_FAILED) {
131                 close(fd);
132                 return -1;
133         }
134         *bufsize = stats.st_size;
135
136         close(fd);
137
138         return 0;
139 }
140
141 void file_unmap(char *buf, size_t bufsize)
142 {
143         munmap(buf, bufsize);
144 }
145
146 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
147 {
148         size_t count = 0;
149
150         for (count = cur; count < buflen && buf[count] != '\n'; count++);
151
152         return count - cur;
153 }
154
155 void no_trailing_slash(char *path)
156 {
157         int len;
158
159         len = strlen(path);
160         if (len > 0 && path[len-1] == '/')
161                 path[len-1] = '\0';
162 }
163
164 struct files {
165         struct list_head list;
166         char name[NAME_SIZE];
167 };
168
169 /* sort files in lexical order */
170 static int file_list_insert(char *filename, struct list_head *file_list)
171 {
172         struct files *loop_file;
173         struct files *new_file;
174
175         list_for_each_entry(loop_file, file_list, list) {
176                 if (strcmp(loop_file->name, filename) > 0) {
177                         break;
178                 }
179         }
180
181         new_file = malloc(sizeof(struct files));
182         if (new_file == NULL) {
183                 dbg("error malloc");
184                 return -ENOMEM;
185         }
186
187         strfieldcpy(new_file->name, filename);
188         list_add_tail(&new_file->list, &loop_file->list);
189         return 0;
190 }
191
192 /* calls function for every file found in specified directory */
193 int call_foreach_file(file_fnct_t fnct, const char *dirname,
194                       const char *suffix, void *data)
195 {
196         struct dirent *ent;
197         DIR *dir;
198         char *ext;
199         struct files *loop_file;
200         struct files *tmp_file;
201         LIST_HEAD(file_list);
202
203         dbg("open directory '%s'", dirname);
204         dir = opendir(dirname);
205         if (dir == NULL) {
206                 dbg("unable to open '%s'", dirname);
207                 return -1;
208         }
209
210         while (1) {
211                 ent = readdir(dir);
212                 if (ent == NULL || ent->d_name[0] == '\0')
213                         break;
214
215                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
216                         continue;
217
218                 /* look for file with specified suffix */
219                 ext = strrchr(ent->d_name, '.');
220                 if (ext == NULL)
221                         continue;
222
223                 if (strcmp(ext, suffix) != 0)
224                         continue;
225
226                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
227                 file_list_insert(ent->d_name, &file_list);
228         }
229
230         /* call function for every file in the list */
231         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
232                 char filename[NAME_SIZE];
233
234                 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
235                 filename[NAME_SIZE-1] = '\0';
236
237                 fnct(filename, data);
238
239                 list_del(&loop_file->list);
240                 free(loop_file);
241         }
242
243         closedir(dir);
244         return 0;
245 }