chiark / gitweb /
scsi_id, usb_id: request device parent by subsystem
[elogind.git] / udev_libc_wrapper.c
1 /*
2  * udev_libc_wrapper - wrapping of functions missing in a specific libc
3  *                     or not working in a statically compiled binary
4  *
5  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
6  * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  * 
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  * 
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29
30 #include "udev.h"
31
32 #ifdef __KLIBC__
33 #define __OWN_USERDB_PARSER__
34 #endif
35
36 #ifdef __GLIBC__
37 #define __OWN_STRLCPYCAT__
38 #endif
39
40 #ifdef USE_STATIC
41 #define __OWN_USERDB_PARSER__
42 #endif
43
44 #ifdef __OWN_STRLCPYCAT__
45 size_t strlcpy(char *dst, const char *src, size_t size)
46 {
47         size_t bytes = 0;
48         char *q = dst;
49         const char *p = src;
50         char ch;
51
52         while ((ch = *p++)) {
53                 if (bytes+1 < size)
54                         *q++ = ch;
55                 bytes++;
56         }
57
58         /* If size == 0 there is no space for a final null... */
59         if (size)
60                 *q = '\0';
61
62         return bytes;
63 }
64
65 size_t strlcat(char *dst, const char *src, size_t size)
66 {
67         size_t bytes = 0;
68         char *q = dst;
69         const char *p = src;
70         char ch;
71
72         while (bytes < size && *q) {
73                 q++;
74                 bytes++;
75         }
76         if (bytes == size)
77                 return (bytes + strlen(src));
78
79         while ((ch = *p++)) {
80                 if (bytes+1 < size)
81                 *q++ = ch;
82                 bytes++;
83         }
84
85         *q = '\0';
86         return bytes;
87 }
88 #endif /* __OWN_STRLCPYCAT__ */
89
90 #ifndef __OWN_USERDB_PARSER__
91 #include <sys/types.h>
92 #include <pwd.h>
93 #include <grp.h>
94
95 uid_t lookup_user(const char *user)
96 {
97         struct passwd *pw;
98         uid_t uid = 0;
99
100         pw = getpwnam(user);
101         if (pw == NULL)
102                 info("specified user unknown '%s'", user);
103         else
104                 uid = pw->pw_uid;
105
106         return uid;
107 }
108
109 gid_t lookup_group(const char *group)
110 {
111         struct group *gr;
112         gid_t gid = 0;
113
114         gr = getgrnam(group);
115         if (gr == NULL)
116                 info("specified group unknown '%s'", group);
117         else
118                 gid = gr->gr_gid;
119
120         return gid;
121 }
122
123 #else /* __OWN_USERDB_PARSER__ */
124
125 #define PASSWD_FILE             "/etc/passwd"
126 #define GROUP_FILE              "/etc/group"
127
128 /* return the id of a passwd style line, selected by the users name */
129 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
130 {
131         unsigned long id = 0;
132         char line[LINE_SIZE];
133         char *buf;
134         char *bufline;
135         size_t bufsize;
136         size_t cur;
137         size_t count;
138         char *pos;
139         char *name;
140         char *idstr;
141         char *tail;
142
143         if (file_map(dbfile, &buf, &bufsize) != 0) {
144                 err("can't open '%s' as db file: %s", dbfile, strerror(errno));
145                 return 0;
146         }
147         dbg("search '%s' in '%s'", uname, dbfile);
148
149         /* loop through the whole file */
150         cur = 0;
151         while (cur < bufsize) {
152                 count = buf_get_line(buf, bufsize, cur);
153                 bufline = &buf[cur];
154                 cur += count+1;
155
156                 if (count >= sizeof(line))
157                         continue;
158
159                 memcpy(line, bufline, count-1);
160                 line[count-1] = '\0';
161                 pos = line;
162
163                 /* get name */
164                 name = strsep(&pos, ":");
165                 if (name == NULL)
166                         continue;
167
168                 /* skip pass */
169                 if (strsep(&pos, ":") == NULL)
170                         continue;
171
172                 /* get id */
173                 idstr = strsep(&pos, ":");
174                 if (idstr == NULL)
175                         continue;
176
177                 if (strcmp(uname, name) == 0) {
178                         id = strtoul(idstr, &tail, 10);
179                         if (tail[0] != '\0') {
180                                 id = 0;
181                                 dbg("no id found for '%s'",  name);
182                         } else
183                                 dbg("id for '%s' is '%li'", name, id);
184                         break;
185                 }
186         }
187
188         file_unmap(buf, bufsize);
189         return id;
190 }
191
192 uid_t lookup_user(const char *user)
193 {
194         unsigned long id;
195
196         id = get_id_by_name(user, PASSWD_FILE);
197         return (uid_t) id;
198 }
199
200 gid_t lookup_group(const char *group)
201 {
202         unsigned long id;
203
204         id = get_id_by_name(group, GROUP_FILE);
205         return (gid_t) id;
206 }
207 #endif /* __OWN_USERDB_PARSER__ */