chiark / gitweb /
[PATCH] clean up chassis_id coding style.
[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 "libsysfs/sysfs/libsysfs.h"
33 #include "udev.h"
34 #include "logging.h"
35 #include "udev_lib.h"
36 #include "list.h"
37
38
39 char *get_action(void)
40 {
41         char *action;
42
43         action = getenv("ACTION");
44         if (action != NULL && strlen(action) > ACTION_SIZE)
45                 action[ACTION_SIZE-1] = '\0';
46
47         return action;
48 }
49
50 char *get_devpath(void)
51 {
52         char *devpath;
53
54         devpath = getenv("DEVPATH");
55         if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
56                 devpath[DEVPATH_SIZE-1] = '\0';
57
58         return devpath;
59 }
60
61 char *get_devnode(void)
62 {
63         char *devnode;
64
65         devnode = getenv("DEVNODE");
66         if (devnode != NULL && strlen(devnode) > NAME_SIZE)
67                 devnode[NAME_SIZE-1] = '\0';
68
69         return devnode;
70 }
71
72 char *get_seqnum(void)
73 {
74         char *seqnum;
75
76         seqnum = getenv("SEQNUM");
77
78         return seqnum;
79 }
80
81 char *get_subsystem(char *subsystem)
82 {
83         if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
84                 subsystem[SUBSYSTEM_SIZE-1] = '\0';
85
86         return subsystem;
87 }
88
89 char get_device_type(const char *path, const char *subsystem)
90 {
91         if (strcmp(subsystem, "block") == 0 ||
92             strstr(path, "/block/") != NULL)
93                 return 'b';
94
95         if (strcmp(subsystem, "net") == 0 ||
96             strstr(path, "/class/net/") != NULL)
97                 return 'n';
98
99         if (strstr(path, "/class/") != NULL)
100                 return 'c';
101
102         return '\0';
103 }
104
105 int file_map(const char *filename, char **buf, size_t *bufsize)
106 {
107         struct stat stats;
108         int fd;
109
110         fd = open(filename, O_RDONLY);
111         if (fd < 0) {
112                 return -1;
113         }
114
115         if (fstat(fd, &stats) < 0) {
116                 return -1;
117         }
118
119         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
120         if (*buf == MAP_FAILED) {
121                 return -1;
122         }
123         *bufsize = stats.st_size;
124
125         close(fd);
126
127         return 0;
128 }
129
130 void file_unmap(char *buf, size_t bufsize)
131 {
132         munmap(buf, bufsize);
133 }
134
135 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
136 {
137         size_t count = 0;
138
139         for (count = cur; count < buflen && buf[count] != '\n'; count++);
140
141         return count - cur;
142 }
143
144 struct files {
145         struct list_head list;
146         char name[NAME_SIZE];
147 };
148
149 /* sort files in lexical order */
150 static int file_list_insert(char *filename, struct list_head *file_list)
151 {
152         struct files *loop_file;
153         struct files *new_file;
154
155         list_for_each_entry(loop_file, file_list, list) {
156                 if (strcmp(loop_file->name, filename) > 0) {
157                         break;
158                 }
159         }
160
161         new_file = malloc(sizeof(struct files));
162         if (new_file == NULL) {
163                 dbg("error malloc");
164                 return -ENOMEM;
165         }
166
167         strfieldcpy(new_file->name, filename);
168         list_add_tail(&new_file->list, &loop_file->list);
169         return 0;
170 }
171
172 /* calls function for file or every file found in directory */
173 int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix)
174 {
175         struct dirent *ent;
176         DIR *dir;
177         char *ext;
178         char file[NAME_SIZE];
179         struct files *loop_file;
180         struct files *tmp_file;
181         LIST_HEAD(file_list);
182
183         dbg("open directory '%s'", dirname);
184         dir = opendir(dirname);
185         if (dir == NULL) {
186                 dbg("unable to open '%s'", dirname);
187                 return -1;
188         }
189
190         while (1) {
191                 ent = readdir(dir);
192                 if (ent == NULL || ent->d_name[0] == '\0')
193                         break;
194
195                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
196                         continue;
197
198                 /* look for file with specified suffix */
199                 ext = strrchr(ent->d_name, '.');
200                 if (ext == NULL)
201                         continue;
202
203                 if (strcmp(ext, suffix) != 0)
204                         continue;
205
206                 dbg("put file '%s/%s' in list", dirname, ent->d_name);
207                 file_list_insert(ent->d_name, &file_list);
208         }
209
210         /* call function for every file in the list */
211         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
212                 strfieldcpy(file, dirname);
213                 strfieldcat(file, "/");
214                 strfieldcat(file, loop_file->name);
215
216                 fnct(file);
217
218                 list_del(&loop_file->list);
219                 free(loop_file);
220         }
221
222         closedir(dir);
223         return 0;
224 }