2 * udev_lib - generic stuff used by udev
4 * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
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.
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.
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.
38 char *get_action(void)
42 action = getenv("ACTION");
43 if (action != NULL && strlen(action) > ACTION_SIZE)
44 action[ACTION_SIZE-1] = '\0';
49 char *get_devpath(void)
53 devpath = getenv("DEVPATH");
54 if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
55 devpath[DEVPATH_SIZE-1] = '\0';
60 char *get_devname(void)
64 devname = getenv("DEVNAME");
65 if (devname != NULL && strlen(devname) > NAME_SIZE)
66 devname[NAME_SIZE-1] = '\0';
71 char *get_seqnum(void)
75 seqnum = getenv("SEQNUM");
80 char *get_subsystem(char *subsystem)
82 if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
83 subsystem[SUBSYSTEM_SIZE-1] = '\0';
88 #define BLOCK_PATH "/block/"
89 #define CLASS_PATH "/class/"
90 #define NET_PATH "/class/net/"
92 char get_device_type(const char *path, const char *subsystem)
94 if (strcmp(subsystem, "block") == 0)
97 if (strcmp(subsystem, "net") == 0)
100 if (strncmp(path, BLOCK_PATH, strlen(BLOCK_PATH)) == 0 &&
101 strlen(path) > strlen(BLOCK_PATH))
104 if (strncmp(path, NET_PATH, strlen(NET_PATH)) == 0 &&
105 strlen(path) > strlen(NET_PATH))
108 if (strncmp(path, CLASS_PATH, strlen(CLASS_PATH)) == 0 &&
109 strlen(path) > strlen(CLASS_PATH))
115 void udev_set_values(struct udevice *udev, const char* devpath, const char *subsystem)
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);
123 int create_path(const char *path)
130 pos = strrchr(p, '/');
131 if (pos == p || pos == NULL)
134 while (pos[-1] == '/')
139 dbg("stat '%s'\n", p);
140 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
143 if (create_path (p) != 0)
146 dbg("mkdir '%s'\n", p);
147 return mkdir(p, 0755);
150 int file_map(const char *filename, char **buf, size_t *bufsize)
155 fd = open(filename, O_RDONLY);
160 if (fstat(fd, &stats) < 0) {
165 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
166 if (*buf == MAP_FAILED) {
170 *bufsize = stats.st_size;
177 void file_unmap(char *buf, size_t bufsize)
179 munmap(buf, bufsize);
182 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
186 for (count = cur; count < buflen && buf[count] != '\n'; count++);
191 void no_trailing_slash(char *path)
196 if (len > 0 && path[len-1] == '/')
201 struct list_head list;
202 char name[NAME_SIZE];
205 /* sort files in lexical order */
206 static int file_list_insert(char *filename, struct list_head *file_list)
208 struct files *loop_file;
209 struct files *new_file;
211 list_for_each_entry(loop_file, file_list, list) {
212 if (strcmp(loop_file->name, filename) > 0) {
217 new_file = malloc(sizeof(struct files));
218 if (new_file == NULL) {
223 strfieldcpy(new_file->name, filename);
224 list_add_tail(&new_file->list, &loop_file->list);
228 /* calls function for every file found in specified directory */
229 int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix)
234 char file[NAME_SIZE];
235 struct files *loop_file;
236 struct files *tmp_file;
237 LIST_HEAD(file_list);
239 dbg("open directory '%s'", dirname);
240 dir = opendir(dirname);
242 dbg("unable to open '%s'", dirname);
248 if (ent == NULL || ent->d_name[0] == '\0')
251 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
254 /* look for file with specified suffix */
255 ext = strrchr(ent->d_name, '.');
259 if (strcmp(ext, suffix) != 0)
262 dbg("put file '%s/%s' in list", dirname, ent->d_name);
263 file_list_insert(ent->d_name, &file_list);
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';
273 list_del(&loop_file->list);
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. */
285 int set_cloexec_flag (int desc, int value)
287 int oldflags = fcntl (desc, F_GETFD, 0);
288 /* If reading the flags failed, return error indication now. */
291 /* Set just the flag we want to set. */
293 oldflags |= FD_CLOEXEC;
295 oldflags &= ~FD_CLOEXEC;
296 /* Store modified flag word in the descriptor. */
297 return fcntl (desc, F_SETFD, oldflags);