1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/*
* <pwd.h> wrapper functions.
*
* Authors:
* Jonathan Pryor (jonpryor@vt.edu)
*
* Copyright (C) 2004 Jonathan Pryor
*/
#include <pwd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "mph.h"
G_BEGIN_DECLS
struct Mono_Posix_Syscall__Passwd {
/* string */ char *pw_name;
/* string */ char *pw_passwd;
/* uid_t */ mph_uid_t pw_uid;
/* gid_t */ mph_gid_t pw_gid;
/* string */ char *pw_gecos;
/* string */ char *pw_dir;
/* string */ char *pw_shell;
/* string */ char *_pw_buf_;
};
static const size_t
passwd_offsets[] = {
offsetof (struct passwd, pw_name),
offsetof (struct passwd, pw_passwd),
offsetof (struct passwd, pw_gecos),
offsetof (struct passwd, pw_dir),
offsetof (struct passwd, pw_shell)
};
static const size_t
mph_passwd_offsets[] = {
offsetof (struct Mono_Posix_Syscall__Passwd, pw_name),
offsetof (struct Mono_Posix_Syscall__Passwd, pw_passwd),
offsetof (struct Mono_Posix_Syscall__Passwd, pw_gecos),
offsetof (struct Mono_Posix_Syscall__Passwd, pw_dir),
offsetof (struct Mono_Posix_Syscall__Passwd, pw_shell)
};
/*
* Copy the native `passwd' structure to it's managed representation.
*
* To minimize separate mallocs, all the strings are allocated within the same
* memory block (stored in _pw_buf_).
*/
static int
copy_passwd (struct Mono_Posix_Syscall__Passwd *to, struct passwd *from)
{
char *buf;
buf = _mph_copy_structure_strings (to, mph_passwd_offsets,
from, passwd_offsets, sizeof(passwd_offsets)/sizeof(passwd_offsets[0]));
to->pw_uid = from->pw_uid;
to->pw_gid = from->pw_gid;
to->_pw_buf_ = buf;
if (buf == NULL) {
return -1;
}
return 0;
}
gint32
Mono_Posix_Syscall_getpwnam (const char *name, struct Mono_Posix_Syscall__Passwd *pwbuf)
{
struct passwd *pw;
if (pwbuf == NULL) {
errno = EFAULT;
return -1;
}
errno = 0;
pw = getpwnam (name);
if (pw == NULL)
return -1;
if (copy_passwd (pwbuf, pw) == -1) {
errno = ENOMEM;
return -1;
}
return 0;
}
gint32
Mono_Posix_Syscall_getpwuid (mph_uid_t uid, struct Mono_Posix_Syscall__Passwd *pwbuf)
{
struct passwd *pw;
if (pwbuf == NULL) {
errno = EFAULT;
return -1;
}
errno = 0;
pw = getpwuid (uid);
if (pw == NULL) {
return -1;
}
if (copy_passwd (pwbuf, pw) == -1) {
errno = ENOMEM;
return -1;
}
return 0;
}
#ifdef HAVE_GETPWNAM_R
gint32
Mono_Posix_Syscall_getpwnam_r (const char *name,
struct Mono_Posix_Syscall__Passwd *pwbuf,
void **pwbufp)
{
char *buf, *buf2;
size_t buflen;
int r;
struct passwd _pwbuf;
if (pwbuf == NULL) {
errno = EFAULT;
return -1;
}
buf = buf2 = NULL;
buflen = 2;
do {
buf2 = realloc (buf, buflen *= 2);
if (buf2 == NULL) {
free (buf);
errno = ENOMEM;
return -1;
}
buf = buf2;
errno = 0;
} while ((r = getpwnam_r (name, &_pwbuf, buf, buflen, (struct passwd**) pwbufp)) &&
recheck_range (r));
if (r == 0 && !(*pwbufp))
/* On solaris, this function returns 0 even if the entry was not found */
r = errno = ENOENT;
if (r == 0 && copy_passwd (pwbuf, &_pwbuf) == -1)
r = errno = ENOMEM;
free (buf);
return r;
}
#endif /* ndef HAVE_GETPWNAM_R */
#ifdef HAVE_GETPWUID_R
gint32
Mono_Posix_Syscall_getpwuid_r (mph_uid_t uid,
struct Mono_Posix_Syscall__Passwd *pwbuf,
void **pwbufp)
{
char *buf, *buf2;
size_t buflen;
int r;
struct passwd _pwbuf;
if (pwbuf == NULL) {
errno = EFAULT;
return -1;
}
buf = buf2 = NULL;
buflen = 2;
do {
buf2 = realloc (buf, buflen *= 2);
if (buf2 == NULL) {
free (buf);
errno = ENOMEM;
return -1;
}
buf = buf2;
errno = 0;
} while ((r = getpwuid_r (uid, &_pwbuf, buf, buflen, (struct passwd**) pwbufp)) &&
recheck_range (r));
if (r == 0 && copy_passwd (pwbuf, &_pwbuf) == -1)
r = errno = ENOMEM;
free (buf);
return r;
}
#endif /* ndef HAVE_GETPWUID_R */
gint32
Mono_Posix_Syscall_getpwent (struct Mono_Posix_Syscall__Passwd *pwbuf)
{
struct passwd *pw;
if (pwbuf == NULL) {
errno = EFAULT;
return -1;
}
errno = 0;
pw = getpwent ();
if (pw == NULL)
return -1;
if (copy_passwd (pwbuf, pw) == -1) {
errno = ENOMEM;
return -1;
}
return 0;
}
#ifdef HAVE_FGETPWENT
gint32
Mono_Posix_Syscall_fgetpwent (void *stream, struct Mono_Posix_Syscall__Passwd *pwbuf)
{
struct passwd *pw;
if (pwbuf == NULL) {
errno = EFAULT;
return -1;
}
errno = 0;
pw = fgetpwent ((FILE*) stream);
if (pw == NULL)
return -1;
if (copy_passwd (pwbuf, pw) == -1) {
errno = ENOMEM;
return -1;
}
return 0;
}
#endif /* ndef HAVE_FGETPWENT */
G_END_DECLS
/*
* vim: noexpandtab
*/
|