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