chiark / gitweb /
[PATCH] v024 release
[elogind.git] / klibc_fixups.c
1 /*
2  * klibc_fixups.c - very simple implementation of stuff missing in klibc
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #ifdef __KLIBC__
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30
31 #include "udev.h"
32 #include "klibc_fixups.h"
33 #include "udev_lib.h"
34 #include "logging.h"
35
36 #define PW_FILE         "/etc/passwd"
37 #define GR_FILE         "/etc/group"
38 #define UTMP_FILE       "/var/run/utmp"
39
40 _syscall1(int, sysinfo, struct sysinfo *, info);
41
42 /* return the id of a passwd style line, selected by the users name */
43 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
44 {
45         unsigned long id = -1;
46         char line[255];
47         char *buf;
48         size_t bufsize;
49         size_t cur;
50         size_t count;
51         char *pos;
52         char *name;
53         char *idstr;
54         char *tail;
55
56         if (file_map(dbfile, &buf, &bufsize) == 0) {
57                 dbg("reading '%s' as db file", dbfile);
58         } else {
59                 dbg("can't open '%s' as db file", dbfile);
60                 return -1;
61         }
62
63         /* loop through the whole file */
64
65         cur = 0;
66         while (1) {
67                 count = buf_get_line(buf, bufsize, cur);
68
69                 strncpy(line, buf + cur, count);
70                 line[count] = '\0';
71                 pos = line;
72
73                 cur += count+1;
74                 if (cur > bufsize)
75                         break;
76
77                 /* get name */
78                 name = strsep(&pos, ":");
79                 if (name == NULL)
80                         continue;
81
82                 /* skip pass */
83                 if (strsep(&pos, ":") == NULL)
84                         continue;
85
86                 /* get id */
87                 idstr = strsep(&pos, ":");
88                 if (idstr == NULL)
89                         continue;
90
91                 if (strcmp(uname, name) == 0) {
92                         id = strtoul(idstr, &tail, 10);
93                         if (tail[0] != '\0')
94                                 id = -1;
95                         else
96                                 dbg("id for '%s' is '%li'", name, id);
97                         break;
98                 }
99         }
100
101         file_unmap(buf, bufsize);
102         return id;
103 }
104
105 struct passwd *getpwnam(const char *name)
106 {
107         static struct passwd pw;
108
109         memset(&pw, 0x00, sizeof(struct passwd));
110         pw.pw_uid = (uid_t) get_id_by_name(name, PW_FILE);
111         if (pw.pw_uid < 0)
112                 return NULL;
113         else
114                 return &pw;
115 }
116
117 struct group *getgrnam(const char *name)
118 {
119         static struct group gr;
120
121         memset(&gr, 0x00, sizeof(struct group));
122         gr.gr_gid = (gid_t) get_id_by_name(name, GR_FILE);
123         if (gr.gr_gid < 0)
124                 return NULL;
125         else
126                 return &gr;
127 }
128
129
130 int ufd = -1;
131
132 void setutent()
133 {
134         if (ufd < 0)
135                 ufd = open(UTMP_FILE, O_RDONLY);
136         fcntl(ufd, F_SETFD, FD_CLOEXEC);
137         lseek(ufd, 0, SEEK_SET);
138 }
139
140 void endutent() {
141         if (ufd < 0)
142                 return;
143         close(ufd);
144         ufd = -1;
145 }
146
147 struct utmp *getutent(void)
148 {
149         static struct utmp utmp;
150         int retval;
151
152         if (ufd < 0) {
153                 setutent();
154                 if (ufd < 0)
155                         return NULL;
156         }
157         retval = read(ufd, &utmp, sizeof(struct utmp));
158         if (retval < 1)
159                 return NULL;
160         return &utmp;
161 }
162
163 #endif