chiark / gitweb /
[PATCH] make start_udev use udevstart binary
[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 "klibc_fixups.h"
32 #include "logging.h"
33
34 #define PW_FILE         "/etc/passwd"
35 #define GR_FILE         "/etc/group"
36 #define UTMP_FILE       "/var/run/utmp"
37
38
39 /* return the id of a passwd style line, selected by the users name */
40 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
41 {
42         unsigned long id = -1;
43         FILE *file;
44         char buf[255];
45         char *pos;
46         char *name;
47         char *idstr;
48         char *tail;
49
50         file = fopen(dbfile, "r");
51         if (file == NULL) {
52                 dbg("unable to open file '%s'", dbfile);
53                 return -1;
54         }
55
56         while (1) {
57                 pos = fgets(buf, sizeof(buf), file);
58                 if (pos == NULL)
59                         break;
60
61                 /* get name */
62                 name = strsep(&pos, ":");
63                 if (name == NULL)
64                         continue;
65
66                 /* skip pass */
67                 if (strsep(&pos, ":") == NULL)
68                         continue;
69
70                 /* get id */
71                 idstr = strsep(&pos, ":");
72                 if (idstr == NULL)
73                         continue;
74
75                 if (strcmp(uname, name) == 0) {
76                         id = strtoul(idstr, &tail, 10);
77                         if (tail[0] != '\0')
78                                 id = -1;
79                         else
80                                 dbg("id for '%s' is '%li'", name, id);
81                         break;
82                 }
83         }
84
85         fclose(file);
86         return id;
87 }
88
89 struct passwd *getpwnam(const char *name)
90 {
91         static struct passwd pw;
92
93         memset(&pw, 0x00, sizeof(struct passwd));
94         pw.pw_uid = (uid_t) get_id_by_name(name, PW_FILE);
95         if (pw.pw_uid < 0)
96                 return NULL;
97         else
98                 return &pw;
99 }
100
101 struct group *getgrnam(const char *name)
102 {
103         static struct group gr;
104
105         memset(&gr, 0x00, sizeof(struct group));
106         gr.gr_gid = (gid_t) get_id_by_name(name, GR_FILE);
107         if (gr.gr_gid < 0)
108                 return NULL;
109         else
110                 return &gr;
111 }
112
113
114 int ufd = -1;
115
116 void setutent()
117 {
118         if (ufd < 0)
119                 ufd = open(UTMP_FILE, O_RDONLY);
120         fcntl(ufd, F_SETFD, FD_CLOEXEC);
121         lseek(ufd, 0, SEEK_SET);
122 }
123
124 void endutent() {
125         if (ufd < 0)
126                 return;
127         close(ufd);
128         ufd = -1;
129 }
130
131 struct utmp *getutent(void)
132 {
133         static struct utmp utmp;
134         int retval;
135
136         if (ufd < 0) {
137                 setutent();
138                 if (ufd < 0)
139                         return NULL;
140         }
141         retval = read(ufd, &utmp, sizeof(struct utmp));
142         if (retval < 1)
143                 return NULL;
144         return &utmp;
145 }
146
147 #endif