blob: 1264fd7c1126f689e05da45bba962fca2fcf3ad5 [file] [log] [blame]
Elliott Hughesde727ca2012-08-13 15:45:36 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <ctype.h>
30#include <errno.h>
31#include <grp.h>
32#include <mntent.h>
33#include <netdb.h>
Elliott Hughesde727ca2012-08-13 15:45:36 -070034#include <pthread.h>
35#include <pwd.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39
Elliott Hughes3e898472013-02-12 16:40:24 +000040#include "private/android_filesystem_config.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000041#include "private/ErrnoRestorer.h"
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070042#include "private/libc_logging.h"
Yabin Cuia04c79b2014-11-18 16:14:54 -080043#include "private/ThreadLocalBuffer.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000044
Elliott Hughes7874f1d2014-12-18 13:36:25 -080045// POSIX seems to envisage an implementation where the <pwd.h> functions are
46// implemented by brute-force searching with getpwent(3), and the <grp.h>
47// functions are implemented similarly with getgrent(3). This means that it's
48// okay for all the <grp.h> functions to share state, and all the <passwd.h>
49// functions to share state, but <grp.h> functions can't clobber <passwd.h>
50// functions' state and vice versa.
Yabin Cuia04c79b2014-11-18 16:14:54 -080051
Elliott Hughes7874f1d2014-12-18 13:36:25 -080052GLOBAL_INIT_THREAD_LOCAL_BUFFER(group);
53
54struct group_state_t {
Elliott Hughesde727ca2012-08-13 15:45:36 -070055 group group_;
56 char* group_members_[2];
Elliott Hughesde727ca2012-08-13 15:45:36 -070057 char group_name_buffer_[32];
Elliott Hughes7874f1d2014-12-18 13:36:25 -080058};
59
60static group_state_t* __group_state() {
61 LOCAL_INIT_THREAD_LOCAL_BUFFER(group_state_t*, group, sizeof(group_state_t));
62 if (group_tls_buffer != NULL) {
63 memset(group_tls_buffer, 0, sizeof(group_state_t));
64 group_tls_buffer->group_.gr_mem = group_tls_buffer->group_members_;
65 }
66 return group_tls_buffer;
67}
68
69GLOBAL_INIT_THREAD_LOCAL_BUFFER(passwd);
70
71struct passwd_state_t {
72 passwd passwd_;
Elliott Hughes8b5df392015-01-21 16:19:07 -080073 char name_buffer_[32];
Elliott Hughesde727ca2012-08-13 15:45:36 -070074 char dir_buffer_[32];
75 char sh_buffer_[32];
76};
77
Elliott Hughes7874f1d2014-12-18 13:36:25 -080078static passwd_state_t* __passwd_state() {
79 LOCAL_INIT_THREAD_LOCAL_BUFFER(passwd_state_t*, passwd, sizeof(passwd_state_t));
80 return passwd_tls_buffer;
81}
82
Elliott Hughesde727ca2012-08-13 15:45:36 -070083static int do_getpw_r(int by_name, const char* name, uid_t uid,
84 passwd* dst, char* buf, size_t byte_count,
85 passwd** result) {
86 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
87 // make might.
Elliott Hughes3e898472013-02-12 16:40:24 +000088 ErrnoRestorer errno_restorer;
Elliott Hughesde727ca2012-08-13 15:45:36 -070089 *result = NULL;
90
91 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
92 // storage, so we can call them as long as we copy everything out
93 // before returning.
94 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
95
96 // POSIX allows failure to find a match to be considered a non-error.
97 // Reporting success (0) but with *result NULL is glibc's behavior.
98 if (src == NULL) {
Elliott Hughes3e898472013-02-12 16:40:24 +000099 return (errno == ENOENT) ? 0 : errno;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700100 }
101
102 // Work out where our strings will go in 'buf', and whether we've got
103 // enough space.
104 size_t required_byte_count = 0;
105 dst->pw_name = buf;
106 required_byte_count += strlen(src->pw_name) + 1;
107 dst->pw_dir = buf + required_byte_count;
108 required_byte_count += strlen(src->pw_dir) + 1;
109 dst->pw_shell = buf + required_byte_count;
110 required_byte_count += strlen(src->pw_shell) + 1;
111 if (byte_count < required_byte_count) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700112 return ERANGE;
113 }
114
115 // Copy the strings.
Elliott Hughes3e898472013-02-12 16:40:24 +0000116 snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700117
Calin Juravlec7688742014-05-09 21:50:53 +0100118 // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic.
Elliott Hughesde727ca2012-08-13 15:45:36 -0700119 dst->pw_passwd = NULL;
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800120#if defined(__LP64__)
Calin Juravlec7688742014-05-09 21:50:53 +0100121 dst->pw_gecos = NULL;
122#endif
Elliott Hughesde727ca2012-08-13 15:45:36 -0700123
124 // Copy the integral fields.
125 dst->pw_gid = src->pw_gid;
126 dst->pw_uid = src->pw_uid;
127
128 *result = dst;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700129 return 0;
130}
131
132int getpwnam_r(const char* name, passwd* pwd,
133 char* buf, size_t byte_count, passwd** result) {
134 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
135}
136
137int getpwuid_r(uid_t uid, passwd* pwd,
138 char* buf, size_t byte_count, passwd** result) {
139 return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
140}
141
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800142static passwd* android_iinfo_to_passwd(passwd_state_t* state,
Elliott Hughesde727ca2012-08-13 15:45:36 -0700143 const android_id_info* iinfo) {
Elliott Hughes8b5df392015-01-21 16:19:07 -0800144 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700145 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
146 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
147
148 passwd* pw = &state->passwd_;
Elliott Hughes8b5df392015-01-21 16:19:07 -0800149 pw->pw_name = state->name_buffer_;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700150 pw->pw_uid = iinfo->aid;
151 pw->pw_gid = iinfo->aid;
152 pw->pw_dir = state->dir_buffer_;
153 pw->pw_shell = state->sh_buffer_;
154 return pw;
155}
156
Elliott Hughes8b5df392015-01-21 16:19:07 -0800157static group* android_iinfo_to_group(group_state_t* state,
Elliott Hughesde727ca2012-08-13 15:45:36 -0700158 const android_id_info* iinfo) {
Elliott Hughes8b5df392015-01-21 16:19:07 -0800159 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
160
161 group* gr = &state->group_;
162 gr->gr_name = state->group_name_buffer_;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700163 gr->gr_gid = iinfo->aid;
164 gr->gr_mem[0] = gr->gr_name;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700165 return gr;
166}
167
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800168static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700169 for (size_t n = 0; n < android_id_count; ++n) {
170 if (android_ids[n].aid == id) {
171 return android_iinfo_to_passwd(state, android_ids + n);
172 }
173 }
174 return NULL;
175}
176
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800177static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700178 for (size_t n = 0; n < android_id_count; ++n) {
179 if (!strcmp(android_ids[n].name, name)) {
180 return android_iinfo_to_passwd(state, android_ids + n);
181 }
182 }
183 return NULL;
184}
185
Elliott Hughes8b5df392015-01-21 16:19:07 -0800186static group* android_id_to_group(group_state_t* state, unsigned id) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700187 for (size_t n = 0; n < android_id_count; ++n) {
188 if (android_ids[n].aid == id) {
Elliott Hughes8b5df392015-01-21 16:19:07 -0800189 return android_iinfo_to_group(state, android_ids + n);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700190 }
191 }
192 return NULL;
193}
194
Elliott Hughes8b5df392015-01-21 16:19:07 -0800195static group* android_name_to_group(group_state_t* state, const char* name) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700196 for (size_t n = 0; n < android_id_count; ++n) {
197 if (!strcmp(android_ids[n].name, name)) {
Elliott Hughes8b5df392015-01-21 16:19:07 -0800198 return android_iinfo_to_group(state, android_ids + n);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700199 }
200 }
201 return NULL;
202}
203
204// Translate a user/group name to the corresponding user/group id.
Yabin Cuia04c79b2014-11-18 16:14:54 -0800205// all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only)
Elliott Hughesde727ca2012-08-13 15:45:36 -0700206// u0_a1234 -> 0 * AID_USER + AID_APP + 1234
207// u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
208// u1_system -> 1 * AID_USER + android_ids['system']
209// returns 0 and sets errno to ENOENT in case of error
Elliott Hughesc56af082015-01-22 11:02:59 -0800210static id_t app_id_from_name(const char* name, bool is_group) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800211 char* end;
212 unsigned long userid;
213 bool is_shared_gid = false;
214
215 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
216 end = const_cast<char*>(name+3);
217 userid = 0;
218 is_shared_gid = true;
219 } else if (name[0] == 'u' && isdigit(name[1])) {
220 userid = strtoul(name+1, &end, 10);
221 } else {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700222 errno = ENOENT;
223 return 0;
224 }
225
Elliott Hughesde727ca2012-08-13 15:45:36 -0700226 if (end[0] != '_' || end[1] == 0) {
227 errno = ENOENT;
228 return 0;
229 }
230
231 unsigned long appid = 0;
232 if (end[1] == 'a' && isdigit(end[2])) {
Yabin Cuia04c79b2014-11-18 16:14:54 -0800233 if (is_shared_gid) {
234 // end will point to \0 if the strtoul below succeeds.
235 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
236 if (appid > AID_SHARED_GID_END) {
237 errno = ENOENT;
238 return 0;
239 }
240 } else {
241 // end will point to \0 if the strtoul below succeeds.
242 appid = strtoul(end+2, &end, 10) + AID_APP;
243 }
Elliott Hughesde727ca2012-08-13 15:45:36 -0700244 } else if (end[1] == 'i' && isdigit(end[2])) {
245 // end will point to \0 if the strtoul below succeeds.
246 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
247 } else {
248 for (size_t n = 0; n < android_id_count; n++) {
249 if (!strcmp(android_ids[n].name, end + 1)) {
250 appid = android_ids[n].aid;
251 // Move the end pointer to the null terminator.
252 end += strlen(android_ids[n].name) + 1;
253 }
254 }
255 }
256
257 // Check that the entire string was consumed by one of the 3 cases above.
258 if (end[0] != 0) {
259 errno = ENOENT;
260 return 0;
261 }
262
263 // Check that user id won't overflow.
264 if (userid > 1000) {
265 errno = ENOENT;
266 return 0;
267 }
268
269 // Check that app id is within range.
270 if (appid >= AID_USER) {
271 errno = ENOENT;
272 return 0;
273 }
274
Elliott Hughesc56af082015-01-22 11:02:59 -0800275 return (appid + userid*AID_USER);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700276}
277
Yabin Cuia04c79b2014-11-18 16:14:54 -0800278static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
279 const uid_t appid = uid % AID_USER;
280 const uid_t userid = uid / AID_USER;
Kenny Root8a05a012012-09-13 14:31:50 -0700281 if (appid >= AID_ISOLATED_START) {
282 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Kenny Root8a05a012012-09-13 14:31:50 -0700283 } else if (appid < AID_APP) {
284 for (size_t n = 0; n < android_id_count; n++) {
285 if (android_ids[n].aid == appid) {
286 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
287 return;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700288 }
289 }
Elliott Hughesde727ca2012-08-13 15:45:36 -0700290 } else {
Kenny Root8a05a012012-09-13 14:31:50 -0700291 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700292 }
293}
294
Yabin Cuia04c79b2014-11-18 16:14:54 -0800295static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
296 const uid_t appid = gid % AID_USER;
297 const uid_t userid = gid / AID_USER;
298 if (appid >= AID_ISOLATED_START) {
299 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
300 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
301 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
302 } else if (appid < AID_APP) {
303 for (size_t n = 0; n < android_id_count; n++) {
304 if (android_ids[n].aid == appid) {
305 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
306 return;
307 }
308 }
309 } else {
310 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
311 }
Kenny Root2a54e5e2012-09-13 10:52:52 -0700312}
313
Elliott Hughesde727ca2012-08-13 15:45:36 -0700314// Translate a uid into the corresponding name.
315// 0 to AID_APP-1 -> "system", "radio", etc.
316// AID_APP to AID_ISOLATED_START-1 -> u0_a1234
317// AID_ISOLATED_START to AID_USER-1 -> u0_i1234
318// AID_USER+ -> u1_radio, u1_a1234, u2_i1234, etc.
319// returns a passwd structure (sets errno to ENOENT on failure).
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800320static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700321 if (uid < AID_APP) {
322 errno = ENOENT;
323 return NULL;
324 }
325
Elliott Hughes8b5df392015-01-21 16:19:07 -0800326 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
Yabin Cuia04c79b2014-11-18 16:14:54 -0800327
Kenny Root2a54e5e2012-09-13 10:52:52 -0700328 const uid_t appid = uid % AID_USER;
Kenny Root2a54e5e2012-09-13 10:52:52 -0700329 if (appid < AID_APP) {
330 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
331 } else {
332 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
333 }
334
Elliott Hughesde727ca2012-08-13 15:45:36 -0700335 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
336
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800337 passwd* pw = &state->passwd_;
Elliott Hughes8b5df392015-01-21 16:19:07 -0800338 pw->pw_name = state->name_buffer_;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700339 pw->pw_dir = state->dir_buffer_;
340 pw->pw_shell = state->sh_buffer_;
341 pw->pw_uid = uid;
342 pw->pw_gid = uid;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700343 return pw;
344}
345
346// Translate a gid into the corresponding app_<gid>
347// group structure (sets errno to ENOENT on failure).
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800348static group* app_id_to_group(gid_t gid, group_state_t* state) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700349 if (gid < AID_APP) {
350 errno = ENOENT;
351 return NULL;
352 }
353
Yabin Cuia04c79b2014-11-18 16:14:54 -0800354 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700355
356 group* gr = &state->group_;
357 gr->gr_name = state->group_name_buffer_;
358 gr->gr_gid = gid;
359 gr->gr_mem[0] = gr->gr_name;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700360 return gr;
361}
362
Elliott Hughesde727ca2012-08-13 15:45:36 -0700363passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800364 passwd_state_t* state = __passwd_state();
Elliott Hughesde727ca2012-08-13 15:45:36 -0700365 if (state == NULL) {
366 return NULL;
367 }
368
369 passwd* pw = android_id_to_passwd(state, uid);
370 if (pw != NULL) {
371 return pw;
372 }
373 return app_id_to_passwd(uid, state);
374}
375
376passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800377 passwd_state_t* state = __passwd_state();
Elliott Hughesde727ca2012-08-13 15:45:36 -0700378 if (state == NULL) {
379 return NULL;
380 }
381
382 passwd* pw = android_name_to_passwd(state, login);
383 if (pw != NULL) {
384 return pw;
385 }
Yabin Cuia04c79b2014-11-18 16:14:54 -0800386 return app_id_to_passwd(app_id_from_name(login, false), state);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700387}
388
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700389// All users are in just one group, the one passed in.
390int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800391 if (*ngroups < 1) {
392 *ngroups = 1;
393 return -1;
394 }
395 groups[0] = group;
396 return (*ngroups = 1);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700397}
398
399char* getlogin() { // NOLINT: implementing bad function.
400 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
401 return (pw != NULL) ? pw->pw_name : NULL;
402}
403
404group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800405 group_state_t* state = __group_state();
Elliott Hughesde727ca2012-08-13 15:45:36 -0700406 if (state == NULL) {
407 return NULL;
408 }
409
Elliott Hughes8b5df392015-01-21 16:19:07 -0800410 group* gr = android_id_to_group(state, gid);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700411 if (gr != NULL) {
412 return gr;
413 }
Elliott Hughesde727ca2012-08-13 15:45:36 -0700414 return app_id_to_group(gid, state);
415}
416
417group* getgrnam(const char* name) { // NOLINT: implementing bad function.
Elliott Hughes7874f1d2014-12-18 13:36:25 -0800418 group_state_t* state = __group_state();
Elliott Hughesde727ca2012-08-13 15:45:36 -0700419 if (state == NULL) {
420 return NULL;
421 }
422
Elliott Hughes8b5df392015-01-21 16:19:07 -0800423 if (android_name_to_group(state, name) != 0) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700424 return &state->group_;
425 }
Yabin Cuia04c79b2014-11-18 16:14:54 -0800426 return app_id_to_group(app_id_from_name(name, true), state);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700427}
428
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700429// We don't have an /etc/networks, so all inputs return NULL.
430netent* getnetbyname(const char* /*name*/) {
431 return NULL;
432}
433
434// We don't have an /etc/networks, so all inputs return NULL.
435netent* getnetbyaddr(uint32_t /*net*/, int /*type*/) {
436 return NULL;
437}
438
439// We don't have an /etc/protocols, so all inputs return NULL.
440protoent* getprotobyname(const char* /*name*/) {
441 return NULL;
442}
443
444// We don't have an /etc/protocols, so all inputs return NULL.
445protoent* getprotobynumber(int /*proto*/) {
446 return NULL;
447}
448
Elliott Hughesde727ca2012-08-13 15:45:36 -0700449static void unimplemented_stub(const char* function) {
450 const char* fmt = "%s(3) is not implemented on Android\n";
Elliott Hughes1e980b62013-01-17 18:36:06 -0800451 __libc_format_log(ANDROID_LOG_WARN, "libc", fmt, function);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700452 fprintf(stderr, fmt, function);
453}
454
455#define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
456
Elliott Hughesde727ca2012-08-13 15:45:36 -0700457void endpwent() {
458 UNIMPLEMENTED;
459}
460
Elliott Hughesde727ca2012-08-13 15:45:36 -0700461char* getusershell() {
462 UNIMPLEMENTED;
463 return NULL;
464}
465
466void setusershell() {
467 UNIMPLEMENTED;
468}
469
470void endusershell() {
471 UNIMPLEMENTED;
472}
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200473
Elliott Hughes0e44bc32014-02-24 15:55:31 -0800474// Portable code should use sysconf(_SC_PAGE_SIZE) directly instead.
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200475int getpagesize() {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700476 // We dont use sysconf(3) here because that drags in stdio, which makes static binaries fat.
477 return PAGE_SIZE;
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200478}