]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_lib.c
[PATCH] delete extras/multipath-tools as per the author's request
[thirdparty/systemd.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_devname(void)
62 {
63 char *devname;
64
65 devname = getenv("DEVNAME");
66 if (devname != NULL && strlen(devname) > NAME_SIZE)
67 devname[NAME_SIZE-1] = '\0';
68
69 return devname;
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 #define BLOCK_PATH "/block/"
90 #define CLASS_PATH "/class/"
91 #define NET_PATH "/class/net/"
92
93 char get_device_type(const char *path, const char *subsystem)
94 {
95 if (strcmp(subsystem, "block") == 0)
96 return 'b';
97
98 if (strcmp(subsystem, "net") == 0)
99 return 'n';
100
101 if (strncmp(path, BLOCK_PATH, strlen(BLOCK_PATH)) == 0 &&
102 strlen(path) > strlen(BLOCK_PATH))
103 return 'b';
104
105 if (strncmp(path, NET_PATH, strlen(NET_PATH)) == 0 &&
106 strlen(path) > strlen(NET_PATH))
107 return 'n';
108
109 if (strncmp(path, CLASS_PATH, strlen(CLASS_PATH)) == 0 &&
110 strlen(path) > strlen(CLASS_PATH))
111 return 'c';
112
113 return '\0';
114 }
115
116 int file_map(const char *filename, char **buf, size_t *bufsize)
117 {
118 struct stat stats;
119 int fd;
120
121 fd = open(filename, O_RDONLY);
122 if (fd < 0) {
123 return -1;
124 }
125
126 if (fstat(fd, &stats) < 0) {
127 close(fd);
128 return -1;
129 }
130
131 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
132 if (*buf == MAP_FAILED) {
133 close(fd);
134 return -1;
135 }
136 *bufsize = stats.st_size;
137
138 close(fd);
139
140 return 0;
141 }
142
143 void file_unmap(char *buf, size_t bufsize)
144 {
145 munmap(buf, bufsize);
146 }
147
148 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
149 {
150 size_t count = 0;
151
152 for (count = cur; count < buflen && buf[count] != '\n'; count++);
153
154 return count - cur;
155 }
156
157 void leading_slash(char *path)
158 {
159 int len;
160
161 len = strlen(path);
162 if (len > 0 && path[len-1] != '/') {
163 path[len] = '/';
164 path[len+1] = '\0';
165 }
166 }
167
168 void no_leading_slash(char *path)
169 {
170 int len;
171
172 len = strlen(path);
173 if (len > 0 && path[len-1] == '/')
174 path[len-1] = '\0';
175 }
176
177 struct files {
178 struct list_head list;
179 char name[NAME_SIZE];
180 };
181
182 /* sort files in lexical order */
183 static int file_list_insert(char *filename, struct list_head *file_list)
184 {
185 struct files *loop_file;
186 struct files *new_file;
187
188 list_for_each_entry(loop_file, file_list, list) {
189 if (strcmp(loop_file->name, filename) > 0) {
190 break;
191 }
192 }
193
194 new_file = malloc(sizeof(struct files));
195 if (new_file == NULL) {
196 dbg("error malloc");
197 return -ENOMEM;
198 }
199
200 strfieldcpy(new_file->name, filename);
201 list_add_tail(&new_file->list, &loop_file->list);
202 return 0;
203 }
204
205 /* calls function for every file found in specified directory */
206 int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix)
207 {
208 struct dirent *ent;
209 DIR *dir;
210 char *ext;
211 char file[NAME_SIZE];
212 struct files *loop_file;
213 struct files *tmp_file;
214 LIST_HEAD(file_list);
215
216 dbg("open directory '%s'", dirname);
217 dir = opendir(dirname);
218 if (dir == NULL) {
219 dbg("unable to open '%s'", dirname);
220 return -1;
221 }
222
223 while (1) {
224 ent = readdir(dir);
225 if (ent == NULL || ent->d_name[0] == '\0')
226 break;
227
228 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
229 continue;
230
231 /* look for file with specified suffix */
232 ext = strrchr(ent->d_name, '.');
233 if (ext == NULL)
234 continue;
235
236 if (strcmp(ext, suffix) != 0)
237 continue;
238
239 dbg("put file '%s/%s' in list", dirname, ent->d_name);
240 file_list_insert(ent->d_name, &file_list);
241 }
242
243 /* call function for every file in the list */
244 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
245 strfieldcpy(file, dirname);
246 strfieldcat(file, "/");
247 strfieldcat(file, loop_file->name);
248
249 fnct(file);
250
251 list_del(&loop_file->list);
252 free(loop_file);
253 }
254
255 closedir(dir);
256 return 0;
257 }
258
259 /* Set the FD_CLOEXEC flag of desc if value is nonzero,
260 or clear the flag if value is 0.
261 Return 0 on success, or -1 on error with errno set. */
262
263 int set_cloexec_flag (int desc, int value)
264 {
265 int oldflags = fcntl (desc, F_GETFD, 0);
266 /* If reading the flags failed, return error indication now. */
267 if (oldflags < 0)
268 return oldflags;
269 /* Set just the flag we want to set. */
270 if (value != 0)
271 oldflags |= FD_CLOEXEC;
272 else
273 oldflags &= ~FD_CLOEXEC;
274 /* Store modified flag word in the descriptor. */
275 return fcntl (desc, F_SETFD, oldflags);
276 }