Skip to content

Commit

Permalink
[libcrystax] Add own pwd.h
Browse files Browse the repository at this point in the history
This also fix issue #1003 (missing field pw_gecos in struct passwd)

Signed-off-by: Dmitry Moskalchuk <dm@crystax.net>
  • Loading branch information
dmsck committed Oct 6, 2015
1 parent 6ff73ba commit 84936d0
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 36 deletions.
1 change: 1 addition & 0 deletions sources/crystax/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ include/monetary.h
include/nl_types.h
include/printf.h
include/pthread_workqueue.h
include/pwd.h
include/runetype.h
include/search.h
include/stdio.h
Expand Down
4 changes: 3 additions & 1 deletion sources/crystax/bin/config
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ common_cflags_for_abi()
crystax=$CRYSTAX_SRCDIR
freebsd=$CRYSTAX_SRCDIR/gen/freebsd
bionic=$CRYSTAX_SRCDIR/gen/bionic
systemcore=$CRYSTAX_SRCDIR/gen/systemcore
cxx=$CRYSTAX_SRCDIR/../cxx-stl/system

arch_flags_for_abi $abi $mlib
Expand Down Expand Up @@ -366,9 +367,10 @@ common_cflags_for_abi()
echo "-Wno-unknown-pragmas"
fi

# First, include Bionic headers, which can override some of public headers
# First, include Bionic and system/core headers, which can override some of public headers
echo "-I$bionic/libc/include"
echo "-I$bionic/libc"
echo "-I$systemcore/include"

echo "-I$crystax/include"
echo "-I$crystax/src/include"
Expand Down
8 changes: 8 additions & 0 deletions sources/crystax/bin/gen-bionic-sources
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ g.fixup :redefine_fcntl do |f|
end

# public headers
g.add "libc/include/pwd.h", public: true
g.add "libc/include/sys/auxv.h", public: true
g.add "libc/include/sys/glibc-syscalls.h", public: true
g.add "libc/include/sys/syscall.h", public: true
Expand All @@ -164,9 +165,13 @@ g.add "libc/include/sys/sysinfo.h", public: true

# common headers
g.add "libc/include/machine/posix_limits.h"
g.add "libc/private/ErrnoRestorer.h"
g.add "libc/private/ScopedReaddir.h"
g.add "libc/private/ThreadLocalBuffer.h"
g.add "libc/private/bionic_asm.h"
g.add "libc/private/bionic_macros.h"
g.add "libc/private/libc_events.h"
g.add "libc/private/libc_logging.h"

# include/machine/asm.h
g.add "libc/arch-arm/include/machine/asm.h"
Expand All @@ -176,6 +181,9 @@ g.add "libc/arch-mips64/include/machine/asm.h"
g.add "libc/arch-x86/include/machine/asm.h"
g.add "libc/arch-x86_64/include/machine/asm.h"

# getpwnam/getpwuid
g.add "libc/bionic/stubs.cpp"

# fadvise
g.add "libc/arch-arm/syscalls/__arm_fadvise64_64.S"
g.add "libc/arch-arm64/syscalls/__fadvise64.S"
Expand Down
2 changes: 2 additions & 0 deletions sources/crystax/bin/gen-sources
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ $CWD/gen-bsd-sources || exit 1
$CWD/gen-bionic-symbols || exit 1
$CWD/gen-bionic-sources || exit 1

$CWD/gen-system-core-headers || exit 1

$CWD/gen-compiler-rt-headers || exit 1

$CWD/gen-libpwq-sources || exit 1
Expand Down
156 changes: 156 additions & 0 deletions sources/crystax/bin/gen-system-core-headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env ruby

# Copyright (c) 2011-2015 CrystaX .NET.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY CrystaX .NET ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CrystaX .NET OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of CrystaX .NET.

require 'fileutils'
require 'pathname'
require 'digest'

MYNAME = Pathname.new(File.expand_path(__FILE__)).relative_path_from(Pathname.new(File.expand_path('../..', __FILE__))).to_s

TOPDIR = File.realpath(File.join(File.dirname(__FILE__), '..'))
SRCDIR = File.realpath(File.join(TOPDIR, '..', '..', '..', 'system', 'core'))

PRIVATEDIR = File.join(TOPDIR, 'gen', 'systemcore')
PUBLICDIR = File.join(TOPDIR, 'include')

ARCHS = [
"arm",
"x86",
"mips",
"arm64",
"x86_64",
"mips64",
]

class Generator
def initialize
@files = []
@fixups = {}
end

def license
@license = File.read(File.join(TOPDIR, 'LICENSE')).split("\n").map(&:chomp) if @license.nil?
@license
end
private :license

def relpath(path, from)
Pathname.new(path).relative_path_from(Pathname.new(from)).to_s
end
private :relpath

def fixup(name, &block)
raise "Fixup '#{name}' already added" if @fixups.has_key?(name.to_s)
raise "No code block given" unless block_given?
@fixups[name.to_s] = lambda { |f| block.call(f) }
end

def add(f, options = {})
raise "No such file: #{f}" unless File.exists?(File.join(SRCDIR, f))
raise "File #{f} already added" if @files.include?(f)
@files << {file: f, options: options}
end

def generate
total = @files.size
current = 0
@files.each do |e|
f = e[:file]
options = e[:options]

src = File.join(SRCDIR, f)
mdst = File.join(options[:public] ? File.join(PUBLICDIR, 'crystax', 'systemcore') : PRIVATEDIR, File.dirname(f), "mangled-#{File.basename(f)}")

if options[:public]
f = f.split('/')[2..-1].join('/') if f.split('/')[0..1] == ['libc', 'include']
dst = File.join(PUBLICDIR, f)
else
dst = File.join(PRIVATEDIR, f)
end

next if File.exists?(dst) && File.exists?(mdst) &&
File.mtime(dst) >= File.mtime(mdst) &&
File.mtime(mdst) >= File.mtime(src) &&
File.mtime(dst) >= File.mtime(__FILE__) &&
File.mtime(mdst) >= File.mtime(__FILE__)

current += 1
puts "GEN [#{current}/#{total}] #{f}"

FileUtils.rm_f mdst
FileUtils.mkdir_p File.dirname(mdst)
FileUtils.cp src, mdst

FileUtils.mkdir_p File.dirname(dst)
File.open(dst, "w") do |df|
df.puts "/* WARNING!!! THIS IS AUTO-GENERATED FILE!!! DO NOT MODIFY IT MANUALLY!!! */"
df.puts "/* GENERATED BY: $CRYSTAX/#{MYNAME} */"
df.puts ""

df.puts "/*"
license.each do |line|
df.puts " * #{line}".rstrip
end
df.puts " */"

guard = "__CRYSTAX_INCLUDE_#{Digest::SHA256.new.update(relpath(dst, TOPDIR)).hexdigest}".upcase
df.puts ""
df.puts "#ifndef #{guard}"
df.puts "#define #{guard}"

fixups = options[:fixup]
fixups = [] if fixups.nil?
fixups = [fixups] unless fixups.is_a?(Array)
df.puts "" unless fixups.empty?
fixups.each do |fxname|
fx = @fixups[fxname.to_s]
raise "No such fixup: '#{fxname}'" if fx.nil?
fx.call(df)
end

df.puts ""
if options[:public]
df.puts "#include <crystax/id.h>"
df.puts "#include <#{relpath(mdst, File.join(TOPDIR, 'include'))}>"
else
df.puts "#include \"#{File.basename(mdst)}\""
end

df.puts ""
df.puts "#endif /* #{guard} */"
end
end
end
end

g = Generator.new

g.add "include/private/android_filesystem_config.h"

g.generate
5 changes: 5 additions & 0 deletions sources/crystax/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

#include <crystax/id.h>
#include <sys/limits.h>

#define getpagesize crystax_google_getpagesize
#include <crystax/google/unistd.h>
#undef getpagesize

#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
#define F_ULOCK 0 /* unlock locked section */
Expand All @@ -52,6 +55,8 @@ int getdtablesize();

int pipe2(int *, int);

int getpagesize(void);

__END_DECLS

#endif /* __CRYSTAX_INCLUDE_UNISTD_H_03C293AE50EC4668ABCC15CAF1E1046F */
82 changes: 82 additions & 0 deletions sources/crystax/src/pwent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2011-2014 CrystaX .NET.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY CrystaX .NET ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CrystaX .NET OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of CrystaX .NET.
*/

#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysconf.h>

static struct passwd me;
static struct passwd *entries[] = {NULL, NULL};
static size_t current = 0;

static void init()
{
struct passwd *p;
char *buf;
int buflen;

if (entries[0] != NULL)
return;

buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
if (buflen <= 0) abort();
buf = malloc(buflen);
if (!buf) abort();

getpwuid_r(getuid(), &me, buf, buflen, &p);
if (!p) abort();

entries[0] = &me;
}

struct passwd *getpwent()
{
struct passwd *p;

init();

p = entries[current];
if (current < sizeof(entries)/sizeof(entries[0]) - 1)
++current;

return p;
}

void endpwent()
{
init();
}

void setpwent()
{
init();
current = 0;
}
36 changes: 1 addition & 35 deletions tests/build/crystax-test-posix-interfaces/jni/pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,9 @@ void pwd_check_passwd_fields(struct passwd *s)
s->pw_gid = (gid_t)0;
s->pw_dir = (char*)0;
s->pw_shell = (char*)0;
s->pw_gecos = (char*)0;
}

#if __ANDROID__
/* WARNING!!! These functions not defined in Android, so we define empty stubs here */

int getpwnam_r(const char* s, struct passwd* p, char* n, size_t nl, struct passwd** o)
{
(void)s;
(void)p;
(void)n;
(void)nl;
(void)o;
return -1;
}

int getpwuid_r(uid_t u, struct passwd* p, char* n, size_t nl, struct passwd** o)
{
(void)u;
(void)p;
(void)n;
(void)nl;
(void)o;
return -1;
}

struct passwd* getpwent()
{
return (struct passwd *)0;
}

int setpwent()
{
return -1;
}

#endif /* __ANDROID__ */

void pwd_check_functions()
{
#if __XSI_VISIBLE
Expand Down
3 changes: 3 additions & 0 deletions tests/device/crystax-issue1003-pw_gecos/common.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SRCFILES := main.c
CFLAGS := -Wall -Wextra -Werror
CFLAGS += -UNDEBUG
2 changes: 2 additions & 0 deletions tests/device/crystax-issue1003-pw_gecos/host/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
obj/
2 changes: 2 additions & 0 deletions tests/device/crystax-issue1003-pw_gecos/host/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../common.mk
include $(or $(NDK),../../../..)/tests/onhost.mk
8 changes: 8 additions & 0 deletions tests/device/crystax-issue1003-pw_gecos/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
LOCAL_PATH := $(call my-dir)
include $(LOCAL_PATH)/../common.mk

include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := $(SRCFILES)
LOCAL_CFLAGS := $(CFLAGS)
include $(BUILD_EXECUTABLE)
1 change: 1 addition & 0 deletions tests/device/crystax-issue1003-pw_gecos/jni/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ABI := all
Loading

0 comments on commit 84936d0

Please sign in to comment.