getpwuidandroid.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       getpwuidandroid.cc
00003 ///             Replacements for getpwuid*() calls on Android
00004 ///
00005 
00006 /*
00007     Copyright (C) 2011, RealVNC Ltd.
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "getpwuid.h"
00023 
00024 #include <string.h>
00025 #include <errno.h>
00026 
00027 #define SDCARD_PATH "/mnt/sdcard"
00028 
00029 static void fillInData(uid_t uid, char* emptyString, char* sdcardPath, struct barry_passwd* pwd)
00030 {
00031         pwd->pw_name = emptyString;
00032         pwd->pw_passwd = emptyString;
00033         pwd->pw_uid = uid;
00034         pwd->pw_gid = 0;
00035         pwd->pw_gecos = emptyString;
00036         // This is the important value for Barry, as it's where to store
00037         // config files.
00038         //
00039         // For Android the best place is probably on the sdcard, as then
00040         // it doesn't matter which application is calling it, as long
00041         // as it has android.permission.WRITE_EXTERNAL_STORAGE
00042         pwd->pw_dir = sdcardPath;
00043         pwd->pw_shell = emptyString;
00044 }
00045 
00046 extern "C" struct barry_passwd *barry_getpwuid(uid_t uid)
00047 {
00048         static char emptyString[1] = {0};
00049         static char sdcardString[sizeof(SDCARD_PATH)] = SDCARD_PATH;
00050         static struct barry_passwd gPasswdStruct;
00051         fillInData(uid, emptyString, sdcardString, &gPasswdStruct);
00052         return &gPasswdStruct;
00053 }
00054 
00055 extern "C" int barry_getpwuid_r(uid_t uid, struct barry_passwd *pwd,
00056                    char *buf, size_t buflen, struct barry_passwd **result)
00057 {
00058         if (buflen < sizeof(SDCARD_PATH))
00059                 return ERANGE;
00060         memcpy(buf, SDCARD_PATH, sizeof(SDCARD_PATH));
00061         // Point all the empty entries at the '\0' at the end of the buffer
00062         fillInData(uid, buf + sizeof(SDCARD_PATH) - 1, buf, pwd);
00063         *result = pwd;
00064         return 0;
00065 }