chiark / gitweb /
fix name compare bug name_list_key_add()
[elogind.git] / udev / udev_utils.c
1 /*
2  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  *      This program is free software; you can redistribute it and/or modify it
5  *      under the terms of the GNU General Public License as published by the
6  *      Free Software Foundation version 2 of the License.
7  * 
8  *      This program is distributed in the hope that it will be useful, but
9  *      WITHOUT ANY WARRANTY; without even the implied warranty of
10  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *      General Public License for more details.
12  * 
13  *      You should have received a copy of the GNU General Public License along
14  *      with this program; if not, write to the Free Software Foundation, Inc.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <syslog.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <sys/types.h>
33 #include <sys/utsname.h>
34
35 #include "udev.h"
36
37
38 int log_priority(const char *priority)
39 {
40         char *endptr;
41         int prio;
42
43         prio = strtol(priority, &endptr, 10);
44         if (endptr[0] == '\0')
45                 return prio;
46         if (strncasecmp(priority, "err", 3) == 0)
47                 return LOG_ERR;
48         if (strcasecmp(priority, "info") == 0)
49                 return LOG_INFO;
50         if (strcasecmp(priority, "debug") == 0)
51                 return LOG_DEBUG;
52         if (string_is_true(priority))
53                 return LOG_ERR;
54
55         return 0;
56 }
57
58 struct name_entry *name_list_add(struct udev *udev, struct list_head *name_list, const char *name, int sort)
59 {
60         struct name_entry *name_loop;
61         struct name_entry *name_new;
62
63         /* avoid duplicate entries */
64         list_for_each_entry(name_loop, name_list, node) {
65                 if (strcmp(name_loop->name, name) == 0) {
66                         dbg(udev, "'%s' is already in the list\n", name);
67                         return name_loop;
68                 }
69         }
70
71         if (sort)
72                 list_for_each_entry(name_loop, name_list, node) {
73                         if (strcmp(name_loop->name, name) > 0)
74                                 break;
75                 }
76
77         name_new = malloc(sizeof(struct name_entry));
78         if (name_new == NULL)
79                 return NULL;
80         memset(name_new, 0x00, sizeof(struct name_entry));
81         strlcpy(name_new->name, name, sizeof(name_new->name));
82         dbg(udev, "adding '%s'\n", name_new->name);
83         list_add_tail(&name_new->node, &name_loop->node);
84
85         return name_new;
86 }
87
88 struct name_entry *name_list_key_add(struct udev *udev, struct list_head *name_list, const char *key, const char *value)
89 {
90         struct name_entry *name_loop;
91         struct name_entry *name_new;
92         size_t keylen = strlen(key);
93
94         list_for_each_entry(name_loop, name_list, node) {
95                 if (strncmp(name_loop->name, key, keylen) != 0)
96                         continue;
97                 if (name_loop->name[keylen] != '=')
98                         continue;
99                 dbg(udev, "key already present '%s', replace it\n", name_loop->name);
100                 snprintf(name_loop->name, sizeof(name_loop->name), "%s=%s", key, value);
101                 name_loop->name[sizeof(name_loop->name)-1] = '\0';
102                 return name_loop;
103         }
104
105         name_new = malloc(sizeof(struct name_entry));
106         if (name_new == NULL)
107                 return NULL;
108         memset(name_new, 0x00, sizeof(struct name_entry));
109         snprintf(name_new->name, sizeof(name_new->name), "%s=%s", key, value);
110         name_new->name[sizeof(name_new->name)-1] = '\0';
111         dbg(udev, "adding '%s'\n", name_new->name);
112         list_add_tail(&name_new->node, &name_loop->node);
113
114         return name_new;
115 }
116
117 int name_list_key_remove(struct udev *udev, struct list_head *name_list, const char *key)
118 {
119         struct name_entry *name_loop;
120         struct name_entry *name_tmp;
121         size_t keylen = strlen(key);
122         int retval = 0;
123
124         list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
125                 if (strncmp(name_loop->name, key, keylen) != 0)
126                         continue;
127                 if (name_loop->name[keylen] != '=')
128                         continue;
129                 list_del(&name_loop->node);
130                 free(name_loop);
131                 retval = 1;
132                 break;
133         }
134         return retval;
135 }
136
137 void name_list_cleanup(struct udev *udev, struct list_head *name_list)
138 {
139         struct name_entry *name_loop;
140         struct name_entry *name_tmp;
141
142         list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
143                 list_del(&name_loop->node);
144                 free(name_loop);
145         }
146 }
147
148 /* calls function for every file found in specified directory */
149 int add_matching_files(struct udev *udev, struct list_head *name_list, const char *dirname, const char *suffix)
150 {
151         struct dirent *ent;
152         DIR *dir;
153         char filename[PATH_SIZE];
154
155         dbg(udev, "open directory '%s'\n", dirname);
156         dir = opendir(dirname);
157         if (dir == NULL) {
158                 err(udev, "unable to open '%s': %s\n", dirname, strerror(errno));
159                 return -1;
160         }
161
162         while (1) {
163                 ent = readdir(dir);
164                 if (ent == NULL || ent->d_name[0] == '\0')
165                         break;
166
167                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
168                         continue;
169
170                 /* look for file matching with specified suffix */
171                 if (suffix != NULL) {
172                         const char *ext;
173
174                         ext = strrchr(ent->d_name, '.');
175                         if (ext == NULL)
176                                 continue;
177                         if (strcmp(ext, suffix) != 0)
178                                 continue;
179                 }
180                 dbg(udev, "put file '%s/%s' into list\n", dirname, ent->d_name);
181
182                 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
183                 filename[sizeof(filename)-1] = '\0';
184                 name_list_add(udev, name_list, filename, 1);
185         }
186
187         closedir(dir);
188         return 0;
189 }
190
191 uid_t lookup_user(struct udev *udev, const char *user)
192 {
193         struct passwd *pw;
194         uid_t uid = 0;
195
196         errno = 0;
197         pw = getpwnam(user);
198         if (pw == NULL) {
199                 if (errno == 0 || errno == ENOENT || errno == ESRCH)
200                         err(udev, "specified user '%s' unknown\n", user);
201                 else
202                         err(udev, "error resolving user '%s': %s\n", user, strerror(errno));
203         } else
204                 uid = pw->pw_uid;
205
206         return uid;
207 }
208
209 extern gid_t lookup_group(struct udev *udev, const char *group)
210 {
211         struct group *gr;
212         gid_t gid = 0;
213
214         errno = 0;
215         gr = getgrnam(group);
216         if (gr == NULL) {
217                 if (errno == 0 || errno == ENOENT || errno == ESRCH)
218                         err(udev, "specified group '%s' unknown\n", group);
219                 else
220                         err(udev, "error resolving group '%s': %s\n", group, strerror(errno));
221         } else
222                 gid = gr->gr_gid;
223
224         return gid;
225 }
226