Skip to content

Commit 74d5670

Browse files
committed
porting from GNU Linux/glibc to other platforms
porting from GNU Linux/glibc to other platforms - mcdb_make.c - mcdb_fallocate() emulates posix_fallocate() - code_attributes.h - protect C++ from C99 restrict - mcdb.spec - use RPM_OPT_FLAGS and RPM_ARCH - Makefile - isolate linker flags specific to GNU ld or platform ld (todo: still should set soname and linker map on other platforms, but that can happen when interfacing nss_mcdb with vendor-specific APIs) - comment out nss_mcdb_misc*.h from other files to fully disable (depending on aliases.h and netinet/ether.h is not portable) (some platforms do not have struct aliasent and/or struct ether_addr) - __hpux: strict conformance with C99 and _XOPEN_SOURCE 600 - __sun: requires _XOPEN_SOURCE 600 with C99 - __sun: define shell functions 'foo()' without 'function' for /bin/sh - _AIX does not have /etc/shadow split struct spwd code to separate files nss_mcdb_authn*.[ch] - _AIX makes it exceedingly difficult to avoid defining _ALL_SOURCE and still get basic mmap constants and networking definitions
1 parent 503c34d commit 74d5670

35 files changed

+836
-392
lines changed

Diff for: Makefile

+122-60
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,89 @@ else
1111
PREFIX_USR?=/usr
1212
endif
1313

14-
ifeq (i686,$(TARGET_CPU))
15-
CFLAGS+=-m32
16-
LDFLAGS+=-m32
17-
endif
18-
ifeq (x86_64,$(TARGET_CPU))
14+
ifeq (x86_64,$(RPM_ARCH))
15+
ABI_BITS=64
16+
LIB_BITS=64
17+
else
18+
ifneq (,$(wildcard /lib64))
1919
ABI_BITS=64
20-
CFLAGS+=-m64
21-
LDFLAGS+=-m64
20+
LIB_BITS=64
21+
endif
2222
endif
2323

24-
ifeq (,$(TARGET_CPU))
25-
_HAS_LIB64:=$(wildcard /lib64)
26-
ifneq (,$(_HAS_LIB64))
27-
ABI_BITS=64
28-
ABI_FLAGS=-m64
24+
# 'gmake ABI_BITS=64' for 64-bit build (recommended on all 64-bit platforms)
25+
ifeq (64,$(ABI_BITS))
26+
ifeq ($(OSNAME),Linux)
27+
ABI_FLAGS=-m64
28+
endif
29+
ifeq ($(OSNAME),AIX)
30+
AR+=-X64
31+
ABI_FLAGS=-maix64
2932
endif
33+
ifeq ($(OSNAME),HP-UX)
34+
ABI_FLAGS=-mlp64
3035
endif
36+
ifeq ($(OSNAME),SunOS)
37+
ABI_FLAGS=-m64
38+
endif
39+
endif
40+
41+
ifneq (,$(RPM_OPT_FLAGS))
42+
CFLAGS+=$(RPM_OPT_FLAGS) -std=c99
43+
LDFLAGS+=$(RPM_OPT_FLAGS)
44+
else
45+
CC=gcc -pipe
46+
CFLAGS+=-Wall -Winline -pedantic -ansi -std=c99 -O3 -g $(ABI_FLAGS)
47+
LDFLAGS+=$(ABI_FLAGS)
48+
endif
49+
50+
# Thread-safety (e.g. for thread-specific errno)
51+
# (vendor compilers might need additional compiler flags, e.g. Sun Studio -mt)
52+
CFLAGS+=-D_THREAD_SAFE
3153

32-
CC=gcc -pipe
33-
CFLAGS+=$(ABI_FLAGS) -Wall -Winline -pedantic -ansi -std=c99 -D_THREAD_SAFE -O3
34-
CFLAGS+=-g -D_FORTIFY_SOURCE=2 -fstack-protector
35-
LDFLAGS+=-Wl,-O,1 -Wl,--hash-style,gnu -Wl,-z,relro,-z,now $(ABI_FLAGS)
3654
# To disable uint32 and nointr C99 inline functions:
3755
# -DNO_C99INLINE
3856
# Another option to smaller binary is -Os instead of -O3, and remove -Winline
3957

58+
OSNAME:=$(shell /bin/uname -s)
59+
ifeq ($(OSNAME),Linux)
60+
ifneq (,$(strip $(filter-out /usr,$(PREFIX))))
61+
RPATH= -Wl,-rpath,$(PREFIX)/lib$(LIB_BITS)
62+
endif
63+
ifeq (,$(RPM_OPT_FLAGS))
64+
CFLAGS+=-D_FORTIFY_SOURCE=2 -fstack-protector
65+
endif
66+
LDFLAGS+=-Wl,-O,1 -Wl,--hash-style,gnu -Wl,-z,relro,-z,now
67+
mcdbctl nss_mcdbctl t/testmcdbmake t/testmcdbrand t/testzero: \
68+
LDFLAGS+=-Wl,-z,noexecstack
69+
endif
70+
ifeq ($(OSNAME),AIX)
71+
ifneq (,$(strip $(filter-out /usr,$(PREFIX))))
72+
RPATH= -Wl,-b,libpath:$(PREFIX)/lib$(LIB_BITS)
73+
endif
74+
# -lpthreads (AIX) for pthread_mutex_{lock,unlock}() in mcdb.o and nss_mcdb.o
75+
libmcdb.so lib32/libmcdb.so libnss_mcdb.so.2 lib32/libnss_mcdb.so.2 \
76+
mcdbctl nss_mcdbctl t/testmcdbrand: \
77+
LDFLAGS+=-lpthreads
78+
endif
79+
ifeq ($(OSNAME),HP-UX)
80+
ifneq (,$(strip $(filter-out /usr,$(PREFIX))))
81+
RPATH= -Wl,+b,$(PREFIX)/lib$(LIB_BITS)
82+
endif
83+
endif
84+
ifeq ($(OSNAME),SunOS)
85+
ifneq (,$(strip $(filter-out /usr,$(PREFIX))))
86+
RPATH= -Wl,-R,$(PREFIX)/lib$(LIB_BITS)
87+
endif
88+
CFLAGS+=-D_POSIX_PTHREAD_SEMANTICS
89+
# -lsocket -lnsl for inet_pton() in nss_mcdb_netdb.o and nss_mcdb_netdb_make.o
90+
libnss_mcdb.so.2 lib32/libnss_mcdb.so.2 nss_mcdbctl: \
91+
LDFLAGS+=-lsocket -lnsl
92+
# -lrt for fdatasync() in mcdb_make.o
93+
libmcdb.so lib32/libmcdb.so mcdbctl nss_mcdbctl: \
94+
LDFLAGS+=-lrt
95+
endif
96+
4097
# heavy handed dependencies
4198
_DEPENDENCIES_ON_ALL_HEADERS_Makefile:= $(wildcard *.h) Makefile
4299

@@ -46,72 +103,75 @@ _DEPENDENCIES_ON_ALL_HEADERS_Makefile:= $(wildcard *.h) Makefile
46103
nss_mcdb.o: CFLAGS+=-DNSS_MCDB_PATH='"$(PREFIX)/etc/mcdb/"'
47104
lib32/nss_mcdb.o: CFLAGS+=-DNSS_MCDB_PATH='"$(PREFIX)/etc/mcdb/"'
48105

49-
PIC_OBJS:= mcdb.o mcdb_make.o mcdb_makefmt.o mcdb_makefn.o \
50-
nointr.o uint32.o nss_mcdb.o nss_mcdb_acct.o nss_mcdb_netdb.o
106+
PIC_OBJS:= mcdb.o mcdb_make.o mcdb_makefmt.o mcdb_makefn.o nointr.o uint32.o \
107+
nss_mcdb.o nss_mcdb_acct.o nss_mcdb_authn.o nss_mcdb_netdb.o
51108
$(PIC_OBJS): CFLAGS+= -fpic
52109

53110
# (nointr.o, uint32.o need not be included when fully inlined; adds 10K to .so)
54-
libnss_mcdb.so.2: mcdb.o nss_mcdb.o nss_mcdb_acct.o nss_mcdb_netdb.o
55-
$(CC) -o $@ -shared -fpic -Wl,-soname,$(@F) \
56-
-Wl,--version-script,nss_mcdb.map \
57-
$(LDFLAGS) \
58-
$^
111+
ifeq ($(OSNAME),Linux)
112+
libnss_mcdb.so.2: LDFLAGS+=-Wl,-soname,$(@F) -Wl,--version-script,nss_mcdb.map
113+
endif
114+
libnss_mcdb.so.2: mcdb.o \
115+
nss_mcdb.o nss_mcdb_acct.o nss_mcdb_authn.o nss_mcdb_netdb.o
116+
$(CC) -o $@ -shared -fpic $(LDFLAGS) $^
59117

118+
ifeq ($(OSNAME),Linux)
119+
libmcdb.so: LDFLAGS+=-Wl,-soname,mcdb
120+
endif
60121
libmcdb.so: mcdb.o mcdb_make.o mcdb_makefmt.o mcdb_makefn.o nointr.o uint32.o
61-
$(CC) -o $@ -shared -fpic -Wl,-soname,mcdb \
62-
$(LDFLAGS) \
63-
$^
122+
$(CC) -o $@ -shared -fpic $(LDFLAGS) $^
64123

65124
libmcdb.a: mcdb.o mcdb_error.o mcdb_make.o mcdb_makefmt.o mcdb_makefn.o \
66125
nointr.o uint32.o
67126
$(AR) -r $@ $^
68127

69-
libnss_mcdb.a: nss_mcdb.o nss_mcdb_acct.o nss_mcdb_netdb.o
128+
libnss_mcdb.a: nss_mcdb.o nss_mcdb_acct.o nss_mcdb_authn.o nss_mcdb_netdb.o
70129
$(AR) -r $@ $^
71130

72-
libnss_mcdb_make.a: nss_mcdb_make.o nss_mcdb_acct_make.o nss_mcdb_netdb_make.o
131+
libnss_mcdb_make.a: nss_mcdb_make.o nss_mcdb_acct_make.o nss_mcdb_authn_make.o \
132+
nss_mcdb_netdb_make.o
73133
$(AR) -r $@ $^
74134

75135
mcdbctl: mcdbctl.o libmcdb.a
76-
$(CC) -o $@ $(LDFLAGS) -Wl,-z,noexecstack $^
136+
$(CC) -o $@ $(LDFLAGS) $^
77137

78138
t/%.o: CFLAGS+=-I$(CURDIR)
79139

80140
t/testmcdbmake: t/testmcdbmake.o libmcdb.a
81-
$(CC) -o $@ $(LDFLAGS) -Wl,-z,noexecstack $^
141+
$(CC) -o $@ $(LDFLAGS) $^
82142

83143
t/testmcdbrand: t/testmcdbrand.o libmcdb.a
84-
$(CC) -o $@ $(LDFLAGS) -Wl,-z,noexecstack $^
144+
$(CC) -o $@ $(LDFLAGS) $^
85145

86146
t/testzero: t/testzero.o libmcdb.a
87-
$(CC) -o $@ $(LDFLAGS) -Wl,-z,noexecstack $^
147+
$(CC) -o $@ $(LDFLAGS) $^
88148

89149
nss_mcdbctl: nss_mcdbctl.o libnss_mcdb_make.a libmcdb.a
90-
$(CC) -o $@ $(LDFLAGS) -Wl,-z,noexecstack $^
150+
$(CC) -o $@ $(LDFLAGS) $^
91151

92-
$(PREFIX)/lib$(ABI_BITS) $(PREFIX)/sbin:
152+
$(PREFIX)/lib$(LIB_BITS) $(PREFIX)/sbin:
93153
/bin/mkdir -p -m 0755 $@
94-
ifneq (,$(ABI_BITS))
154+
ifneq (,$(LIB_BITS))
95155
$(PREFIX)/lib $(PREFIX_USR)/lib:
96156
/bin/mkdir -p -m 0755 $@
97157
endif
98158
ifneq ($(PREFIX_USR),$(PREFIX))
99-
$(PREFIX_USR)/lib$(ABI_BITS) $(PREFIX_USR)/bin:
159+
$(PREFIX_USR)/lib$(LIB_BITS) $(PREFIX_USR)/bin:
100160
/bin/mkdir -p -m 0755 $@
101-
$(PREFIX_USR)/lib$(ABI_BITS)/libnss_mcdb.so.2: \
102-
$(PREFIX)/lib$(ABI_BITS)/libnss_mcdb.so.2 $(PREFIX_USR)/lib$(ABI_BITS)
161+
$(PREFIX_USR)/lib$(LIB_BITS)/libnss_mcdb.so.2: \
162+
$(PREFIX)/lib$(LIB_BITS)/libnss_mcdb.so.2 $(PREFIX_USR)/lib$(LIB_BITS)
103163
[ -L $@ ] || /bin/ln -s ../../lib/$(<F) $@
104164
endif
105165

106166
# (update library atomically (important to avoid crashing running programs))
107167
# (could use /usr/bin/install if available)
108-
$(PREFIX)/lib$(ABI_BITS)/libnss_mcdb.so.2: libnss_mcdb.so.2 \
109-
$(PREFIX)/lib$(ABI_BITS)
168+
$(PREFIX)/lib$(LIB_BITS)/libnss_mcdb.so.2: libnss_mcdb.so.2 \
169+
$(PREFIX)/lib$(LIB_BITS)
110170
/bin/cp -f $< $@.$$$$ \
111171
&& /bin/mv -f $@.$$$$ $@
112172

113-
$(PREFIX_USR)/lib$(ABI_BITS)/libmcdb.so: libmcdb.so \
114-
$(PREFIX_USR)/lib$(ABI_BITS)
173+
$(PREFIX_USR)/lib$(LIB_BITS)/libmcdb.so: libmcdb.so \
174+
$(PREFIX_USR)/lib$(LIB_BITS)
115175
/bin/cp -f $< $@.$$$$ \
116176
&& /bin/mv -f $@.$$$$ $@
117177

@@ -133,41 +193,44 @@ install-doc: CHANGELOG COPYING FAQ INSTALL NOTES README
133193
/bin/mkdir -p -m 0755 $(PREFIX_USR)/share/doc/mcdb
134194
umask 333; \
135195
/bin/cp -f --preserve=timestamps $^ $(PREFIX_USR)/share/doc/mcdb
136-
install: $(PREFIX)/lib$(ABI_BITS)/libnss_mcdb.so.2 \
137-
$(PREFIX_USR)/lib$(ABI_BITS)/libnss_mcdb.so.2 \
138-
$(PREFIX_USR)/lib$(ABI_BITS)/libmcdb.so \
196+
install: $(PREFIX)/lib$(LIB_BITS)/libnss_mcdb.so.2 \
197+
$(PREFIX_USR)/lib$(LIB_BITS)/libnss_mcdb.so.2 \
198+
$(PREFIX_USR)/lib$(LIB_BITS)/libmcdb.so \
139199
$(PREFIX_USR)/bin/mcdbctl $(PREFIX)/sbin/nss_mcdbctl \
140200
install-headers
141201
/bin/mkdir -p -m 0755 $(PREFIX)/etc/mcdb
142202

143203

144204
# also create 32-bit libraries for /lib on systems with /lib and /lib64
145-
ifeq (,$(TARGET_CPU))
146-
ifneq (,$(_HAS_LIB64))
147-
ifneq (,$(ABI_BITS))
205+
ifeq (,$(RPM_ARCH))
206+
ifeq (64,$(LIB_BITS))
148207
ifeq (,$(wildcard lib32))
149208
$(shell mkdir lib32)
150209
endif
151210
lib32/%.o: %.c $(_DEPENDENCIES_ON_ALL_HEADERS_Makefile)
152211
$(CC) -o $@ $(CFLAGS) -c $<
153212

154213
LIB32_PIC_OBJS:= $(addprefix lib32/,$(PIC_OBJS))
155-
$(LIB32_PIC_OBJS): ABI_BITS=32
214+
$(LIB32_PIC_OBJS): LIB_BITS=32
156215
$(LIB32_PIC_OBJS): ABI_FLAGS=-m32
157216
$(LIB32_PIC_OBJS): CFLAGS+= -fpic
217+
218+
ifeq ($(OSNAME),Linux)
219+
lib32/libnss_mcdb.so.2: \
220+
LDFLAGS+=-Wl,-soname,$(@F) -Wl,--version-script,nss_mcdb.map
221+
endif
158222
lib32/libnss_mcdb.so.2: ABI_FLAGS=-m32
159223
lib32/libnss_mcdb.so.2: $(addprefix lib32/, \
160-
mcdb.o nss_mcdb.o nss_mcdb_acct.o nss_mcdb_netdb.o)
161-
$(CC) -o $@ -shared -fpic -Wl,-soname,$(@F) \
162-
-Wl,--version-script,nss_mcdb.map \
163-
$(LDFLAGS) \
164-
$^
224+
mcdb.o nss_mcdb.o nss_mcdb_acct.o nss_mcdb_authn.o nss_mcdb_netdb.o)
225+
$(CC) -o $@ -shared -fpic $(LDFLAGS) $^
226+
227+
ifeq ($(OSNAME),Linux)
228+
lib32/libmcdb.so: LDFLAGS+=-Wl,-soname,mcdb
229+
endif
165230
lib32/libmcdb.so: ABI_FLAGS=-m32
166231
lib32/libmcdb.so: $(addprefix lib32/, \
167232
mcdb.o mcdb_make.o mcdb_makefmt.o mcdb_makefn.o nointr.o uint32.o)
168-
$(CC) -o $@ -shared -fpic -Wl,-soname,mcdb \
169-
$(LDFLAGS) \
170-
$^
233+
$(CC) -o $@ -shared -fpic $(LDFLAGS) $^
171234

172235
ifneq ($(PREFIX_USR),$(PREFIX))
173236
$(PREFIX_USR)/lib/libnss_mcdb.so.2: $(PREFIX)/lib/libnss_mcdb.so.2 \
@@ -183,13 +246,12 @@ $(PREFIX_USR)/lib/libmcdb.so: lib32/libmcdb.so $(PREFIX_USR)/lib
183246
/bin/cp -f $< $@.$$$$ \
184247
&& /bin/mv -f $@.$$$$ $@
185248

186-
all: lib32/libnss_mcdb.so.2
249+
all: lib32/libnss_mcdb.so.2 lib32/libmcdb.so
187250

188251
install: $(PREFIX)/lib/libnss_mcdb.so.2 $(PREFIX_USR)/lib/libnss_mcdb.so.2 \
189252
$(PREFIX_USR)/lib/libmcdb.so
190253
endif
191254
endif
192-
endif
193255

194256

195257
.PHONY: test test64
@@ -211,7 +273,7 @@ endif
211273

212274
.PHONY: clean
213275
clean:
214-
! [ "$$($(usr_bin_id) -u)" = "0" ]
276+
[ "$$($(usr_bin_id) -u)" != "0" ]
215277
$(RM) *.o t/*.o
216278
$(RM) -r lib32
217279
$(RM) libmcdb.a libnss_mcdb.a libnss_mcdb_make.a

Diff for: NOTES

+17
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,20 @@ code to handle the specific data set, including a replacement for the djb hash
220220
function. (mcdb code could be easily tweaked to support 64-bit hslots and
221221
64-bit hash values, but the on-disk format would be incompatible with mcdb.)
222222

223+
224+
225+
Portability Notes
226+
-----------------
227+
228+
229+
warning: visibility attribute not supported in this configuration; ignored
230+
--------------------------------------------------------------------------
231+
This is a harmless warning and occurs on some platforms where the gcc extension
232+
__attribute__((visibility("..."))) is not supported. On Solaris on SPARC,
233+
newer versions of gcc (e.g. 4.6.1) support the visibility attribute, while
234+
earlier versions of gcc might not.
235+
236+
HP-UX
237+
-----
238+
Compilation requires newer version of gcc (e.g. 4.6.1)
239+

Diff for: code_attributes.h

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
extern "C" {
2727
#endif
2828

29+
#ifndef __STDC_C99
30+
#ifndef restrict
31+
#define restrict
32+
#endif
33+
#endif
34+
2935
/*
3036
* C inline functions defined in header
3137
* Discussion of nuances of "extern inline" and inline functions in C headers:

Diff for: mcdb.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ static pthread_mutex_t mcdb_global_mutex = PTHREAD_MUTEX_INITIALIZER;
8282
#define O_CLOEXEC 0
8383
#endif
8484

85+
/*(posix_madvise, defines not provided in Solaris 10, even w/ __EXTENSIONS__)*/
86+
#if (defined(__sun) || defined(__hpux)) && !defined(POSIX_MADV_NORMAL)
87+
extern int madvise(caddr_t, size_t, int);
88+
#define posix_madvise(addr,len,advice) madvise((caddr_t)(addr),(len),(advice))
89+
#define POSIX_MADV_NORMAL 0
90+
#define POSIX_MADV_RANDOM 1
91+
#define POSIX_MADV_SEQUENTIAL 2
92+
#define POSIX_MADV_WILLNEED 3
93+
#define POSIX_MADV_DONTNEED 4
94+
#endif
8595

8696
/* Note: tagc of 0 ('\0') is reserved to indicate no tag */
8797

@@ -467,7 +477,7 @@ mcdb_mmap_create(struct mcdb_mmap * restrict map,
467477
const size_t dlen = strlen(dname);
468478
if (sizeof(map->fnamebuf) >= dlen+flen+2)
469479
fbuf = map->fnamebuf;
470-
else if ((fbuf = fn_malloc(dlen+flen+2) == NULL)) {
480+
else if ((fbuf = fn_malloc(dlen+flen+2)) == NULL) {
471481
mcdb_mmap_destroy_h(map);
472482
return NULL;
473483
}

Diff for: mcdb.h

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
#ifndef INCLUDED_MCDB_H
2626
#define INCLUDED_MCDB_H
2727

28+
#ifdef _AIX /*mmap constants and basic networking on AIX require non-standard*/
29+
#define _ALL_SOURCE
30+
#endif
31+
#ifdef __hpux /*HP-UX strict conformance with C99 and _XOPEN_SOURCE 600*/
32+
#ifndef _XOPEN_SOURCE
33+
#define _XOPEN_SOURCE 600
34+
#endif
35+
#endif
36+
2837
#include <stdbool.h> /* bool */
2938
#include <stdint.h> /* uint32_t, uintptr_t */
3039
#include <unistd.h> /* size_t */

Diff for: mcdb.spec

-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ This package contains mcdb shared libraries.
4545

4646

4747
%build
48-
export TARGET_CPU=%{_target_cpu}
4948
make %{?_smp_mflags} PREFIX=
5049

5150

5251
%install
5352
rm -rf $RPM_BUILD_ROOT
54-
export TARGET_CPU=%{_target_cpu}
5553
make install PREFIX=$RPM_BUILD_ROOT PREFIX_USR=$RPM_BUILD_ROOT/usr
5654
make install-doc PREFIX=$RPM_BUILD_ROOT PREFIX_USR=$RPM_BUILD_ROOT/usr
5755
make install-headers PREFIX=$RPM_BUILD_ROOT PREFIX_USR=$RPM_BUILD_ROOT/usr

Diff for: mcdb_error.c

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#ifndef _POSIX_C_SOURCE /* 200112L for XSI-compliant sterror_r() */
2626
#define _POSIX_C_SOURCE 200112L
2727
#endif
28+
#ifdef __hpux
29+
#ifndef _XOPEN_SOURCE /* strerror_r() */
30+
#define _XOPEN_SOURCE 600
31+
#endif
32+
#endif
2833

2934
#include "mcdb_error.h"
3035

0 commit comments

Comments
 (0)