chiark / gitweb /
volume_id: support for long-filename based labels
[elogind.git] / 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 <fcntl.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <syslog.h>
29 #include <pwd.h>
30 #include <grp.h>
31 #include <sys/types.h>
32 #include <sys/utsname.h>
33
34 #include "udev.h"
35
36
37 int log_priority(const char *priority)
38 {
39         char *endptr;
40         int prio;
41
42         prio = strtol(priority, &endptr, 10);
43         if (endptr[0] == '\0')
44                 return prio;
45         if (strncasecmp(priority, "err", 3) == 0)
46                 return LOG_ERR;
47         if (strcasecmp(priority, "info") == 0)
48                 return LOG_INFO;
49         if (strcasecmp(priority, "debug") == 0)
50                 return LOG_DEBUG;
51         if (string_is_true(priority))
52                 return LOG_ERR;
53
54         return 0;
55 }
56
57 char *name_list_add(struct list_head *name_list, const char *name, int sort)
58 {
59         struct name_entry *loop_name;
60         struct name_entry *new_name;
61
62         list_for_each_entry(loop_name, name_list, node) {
63                 /* avoid doubles */
64                 if (strcmp(loop_name->name, name) == 0) {
65                         dbg("'%s' is already in the list", name);
66                         return loop_name->name;
67                 }
68         }
69
70         if (sort)
71                 list_for_each_entry(loop_name, name_list, node) {
72                         if (sort && strcmp(loop_name->name, name) > 0)
73                                 break;
74                 }
75
76         new_name = malloc(sizeof(struct name_entry));
77         if (new_name == NULL)
78                 return NULL;
79
80         strlcpy(new_name->name, name, sizeof(new_name->name));
81         dbg("adding '%s'", new_name->name);
82         list_add_tail(&new_name->node, &loop_name->node);
83
84         return new_name->name;
85 }
86
87 char *name_list_key_add(struct list_head *name_list, const char *key, const char *value)
88 {
89         struct name_entry *loop_name;
90         struct name_entry *new_name;
91
92         list_for_each_entry(loop_name, name_list, node) {
93                 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
94                         dbg("key already present '%s', replace it", loop_name->name);
95                         snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
96                         loop_name->name[sizeof(loop_name->name)-1] = '\0';
97                         return loop_name->name;
98                 }
99         }
100
101         new_name = malloc(sizeof(struct name_entry));
102         if (new_name == NULL)
103                 return NULL;
104
105         snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
106         new_name->name[sizeof(new_name->name)-1] = '\0';
107         dbg("adding '%s'", new_name->name);
108         list_add_tail(&new_name->node, &loop_name->node);
109
110         return new_name->name;
111 }
112
113 int name_list_key_remove(struct list_head *name_list, const char *key)
114 {
115         struct name_entry *name_loop;
116         struct name_entry *temp_loop;
117         size_t keylen = strlen(key);
118         int retval = 0;
119
120         list_for_each_entry_safe(name_loop, temp_loop, name_list, node) {
121                 if (strncmp(name_loop->name, key, keylen) != 0)
122                         continue;
123                 if (name_loop->name[keylen] != '=')
124                         continue;
125                 list_del(&name_loop->node);
126                 free(name_loop);
127                 retval = 1;
128                 break;
129         }
130         return retval;
131 }
132
133 void name_list_cleanup(struct list_head *name_list)
134 {
135         struct name_entry *name_loop;
136         struct name_entry *temp_loop;
137
138         list_for_each_entry_safe(name_loop, temp_loop, name_list, node) {
139                 list_del(&name_loop->node);
140                 free(name_loop);
141         }
142 }
143
144 /* calls function for every file found in specified directory */
145 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
146 {
147         struct dirent *ent;
148         DIR *dir;
149         char filename[PATH_SIZE];
150
151         dbg("open directory '%s'", dirname);
152         dir = opendir(dirname);
153         if (dir == NULL) {
154                 err("unable to open '%s': %s", dirname, strerror(errno));
155                 return -1;
156         }
157
158         while (1) {
159                 ent = readdir(dir);
160                 if (ent == NULL || ent->d_name[0] == '\0')
161                         break;
162
163                 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
164                         continue;
165
166                 /* look for file matching with specified suffix */
167                 if (suffix != NULL) {
168                         const char *ext;
169
170                         ext = strrchr(ent->d_name, '.');
171                         if (ext == NULL)
172                                 continue;
173                         if (strcmp(ext, suffix) != 0)
174                                 continue;
175                 }
176                 dbg("put file '%s/%s' into list", dirname, ent->d_name);
177
178                 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
179                 filename[sizeof(filename)-1] = '\0';
180                 name_list_add(name_list, filename, 1);
181         }
182
183         closedir(dir);
184         return 0;
185 }
186
187 uid_t lookup_user(const char *user)
188 {
189         struct passwd *pw;
190         uid_t uid = 0;
191
192         errno = 0;
193         pw = getpwnam(user);
194         if (pw == NULL) {
195                 if (errno == 0 || errno == ENOENT || errno == ESRCH)
196                         err("specified user '%s' unknown", user);
197                 else
198                         err("error resolving user '%s': %s", user, strerror(errno));
199         } else
200                 uid = pw->pw_uid;
201
202         return uid;
203 }
204
205 extern gid_t lookup_group(const char *group)
206 {
207         struct group *gr;
208         gid_t gid = 0;
209
210         errno = 0;
211         gr = getgrnam(group);
212         if (gr == NULL) {
213                 if (errno == 0 || errno == ENOENT || errno == ESRCH)
214                         err("specified group '%s' unknown", group);
215                 else
216                         err("error resolving group '%s': %s", group, strerror(errno));
217         } else
218                 gid = gr->gr_gid;
219
220         return gid;
221 }
222