Commit 21d12b02112097f0c195dceb1892c95b7b957b36

Guillem Jover 2023-04-04T23:59:05

build: On macOS do not build functions provided by the system We have never built before on macOS, so we can exclude all the functions that are currently provided in the system. Closes: #1 Closes: !3

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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
diff --git a/configure.ac b/configure.ac
index c2bd521..d6446a0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -148,9 +148,6 @@ AC_SEARCH_LIBS([SHA512Update], [md], [
 ])
 LIBS="$saved_LIBS"
 
-AM_CONDITIONAL([NEED_TRANSPARENT_LIBMD],
-  [test "x$need_transparent_libmd" = "xyes"])
-
 AS_CASE([$host_os],
   [*-gnu*], [
     # In old glibc versions (< 2.17) clock_gettime() is in librt.
@@ -305,8 +302,19 @@ AC_CHECK_FUNCS([\
   open_memstream \
   pstat_getproc \
   sysconf \
+  uid_from_user \
+  gid_from_group \
+  user_from_uid \
+  group_from_gid \
 ])
 
+need_arc4random=yes
+need_md5=yes
+need_nlist=yes
+need_strl=yes
+need_strmode=yes
+need_pwcache=yes
+need_fpurge=yes
 need_funopen=yes
 AS_CASE([$host_os],
   [*-musl*], [
@@ -319,12 +327,31 @@ AS_CASE([$host_os],
     need_funopen=yes
   ],
   [darwin*], [
+    # On macOS these are provided by the system, and libbsd has never built
+    # there, so we can avoid providing these with no ABI breakage.
+    need_arc4random=no
+    need_transparent_libmd=no
+    need_md5=no
+    need_nlist=no
+    need_strl=no
+    need_strmode=no
+    need_pwcache=no
+    need_fpurge=no
     # On macOS we do not have fopencookie(), and cannot implement it.
     need_funopen=no
   ],
 )
 
 AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xyes"])
+
+AM_CONDITIONAL([NEED_ARC4RANDOM], [test "x$need_arc4random" = "xyes"])
+AM_CONDITIONAL([NEED_TRANSPARENT_LIBMD], [test "x$need_transparent_libmd" = "xyes"])
+AM_CONDITIONAL([NEED_MD5], [test "x$need_md5" = "xyes"])
+AM_CONDITIONAL([NEED_NLIST], [test "x$need_nlist" = "xyes"])
+AM_CONDITIONAL([NEED_STRL], [test "x$need_strl" = "xyes"])
+AM_CONDITIONAL([NEED_STRMODE], [test "x$need_strmode" = "xyes"])
+AM_CONDITIONAL([NEED_PWCACHE], [test "x$need_pwcache" = "xyes"])
+AM_CONDITIONAL([NEED_FPURGE], [test "x$need_fpurge" = "xyes"])
 AM_CONDITIONAL([NEED_FUNOPEN], [test "x$need_funopen" = "xyes"])
 AS_IF([test "x$need_funopen" = "xno" && \
        test "x$ac_cv_func_funopen" != "xyes" && \
diff --git a/include/Makefile.am b/include/Makefile.am
index ca0d06f..31d86b4 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -17,8 +17,6 @@ nobase_include_HEADERS = \
 	bsd/grp.h \
 	bsd/inttypes.h \
 	bsd/libutil.h \
-	bsd/md5.h \
-	bsd/nlist.h \
 	bsd/pwd.h \
 	bsd/readpassphrase.h \
 	bsd/stdio.h \
@@ -30,3 +28,15 @@ nobase_include_HEADERS = \
 	bsd/vis.h \
 	bsd/wchar.h \
 	# EOL
+
+if NEED_MD5
+nobase_include_HEADERS += \
+	bsd/md5.h \
+	# EOL
+endif
+
+if NEED_NLIST
+nobase_include_HEADERS += \
+	bsd/nlist.h \
+	# EOL
+endif
diff --git a/include/bsd/grp.h b/include/bsd/grp.h
index b2705e5..acf3d25 100644
--- a/include/bsd/grp.h
+++ b/include/bsd/grp.h
@@ -44,8 +44,10 @@
 __BEGIN_DECLS
 int
 gid_from_group(const char *, gid_t *);
+#ifndef __APPLE__
 const char *
 group_from_gid(gid_t, int);
+#endif
 __END_DECLS
 
 #endif
diff --git a/include/bsd/pwd.h b/include/bsd/pwd.h
index 798af4b..38214ae 100644
--- a/include/bsd/pwd.h
+++ b/include/bsd/pwd.h
@@ -44,8 +44,10 @@
 __BEGIN_DECLS
 int
 uid_from_user(const char *, uid_t *);
+#ifndef __APPLE__
 const char *
 user_from_uid(uid_t, int);
+#endif
 __END_DECLS
 
 #endif
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index 5732fd1..bba13af 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -51,6 +51,7 @@
 #include <stdint.h>
 
 __BEGIN_DECLS
+#if !defined(__APPLE__)
 #if !defined(__GLIBC__) || \
     !__GLIBC_PREREQ(2, 36) || \
     !defined(_DEFAULT_SOURCE)
@@ -60,6 +61,7 @@ uint32_t arc4random_uniform(uint32_t upper_bound);
 #endif
 void arc4random_stir(void);
 void arc4random_addrandom(unsigned char *dat, int datlen);
+#endif
 
 int dehumanize_number(const char *str, int64_t *size);
 
diff --git a/include/bsd/string.h b/include/bsd/string.h
index 4f2b71c..1996697 100644
--- a/include/bsd/string.h
+++ b/include/bsd/string.h
@@ -41,10 +41,14 @@
 #include <sys/types.h>
 
 __BEGIN_DECLS
+#ifndef __APPLE__
 size_t strlcpy(char *dst, const char *src, size_t siz);
 size_t strlcat(char *dst, const char *src, size_t siz);
+#endif
 char *strnstr(const char *str, const char *find, size_t str_len);
+#ifndef __APPLE__
 void strmode(mode_t mode, char *str);
+#endif
 
 #if !defined(__GLIBC__) || \
     !__GLIBC_PREREQ(2, 25) || \
diff --git a/man/Makefile.am b/man/Makefile.am
index 6d1c86a..bc30c87 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -177,13 +177,10 @@ dist_man_MANS = \
 	freezero.3bsd \
 	fmtcheck.3bsd \
 	fparseln.3bsd \
-	fpurge.3bsd \
 	getbsize.3bsd \
 	getmode.3bsd \
 	getpeereid.3bsd \
 	getprogname.3bsd \
-	gid_from_group.3bsd \
-	group_from_gid.3bsd \
 	heapsort.3bsd \
 	humanize_number.3bsd \
 	le16dec.3bsd \
@@ -193,15 +190,12 @@ dist_man_MANS = \
 	le64dec.3bsd \
 	le64enc.3bsd \
 	libbsd.7 \
-	md5.3bsd \
 	mergesort.3bsd \
-	nlist.3bsd \
 	pidfile.3bsd \
 	pidfile_close.3bsd \
 	pidfile_open.3bsd \
 	pidfile_remove.3bsd \
 	pidfile_write.3bsd \
-	pwcache.3bsd \
 	queue.3bsd \
 	radixsort.3bsd \
 	readpassphrase.3bsd \
@@ -219,9 +213,6 @@ dist_man_MANS = \
 	sl_init.3bsd \
 	sradixsort.3bsd \
 	stringlist.3bsd \
-	strlcat.3bsd \
-	strlcpy.3bsd \
-	strmode.3bsd \
 	strnstr.3bsd \
 	strnunvis.3bsd \
 	strnvis.3bsd \
@@ -244,14 +235,53 @@ dist_man_MANS = \
 	timespecsub.3bsd \
 	timeval.3bsd \
 	tree.3bsd \
-	uid_from_user.3bsd \
 	unvis.3bsd \
-	user_from_uid.3bsd \
 	vis.3bsd \
 	wcslcat.3bsd \
 	wcslcpy.3bsd \
 	# EOL
 
+if NEED_MD5
+dist_man_MANS += \
+	md5.3bsd \
+	# EOL
+endif
+
+if NEED_NLIST
+dist_man_MANS += \
+	nlist.3bsd \
+	# EOL
+endif
+
+if NEED_STRL
+dist_man_MANS += \
+	strlcat.3bsd \
+	strlcpy.3bsd \
+	# EOL
+endif
+
+if NEED_STRMODE
+dist_man_MANS += \
+	strmode.3bsd \
+	# EOL
+endif
+
+if NEED_PWCACHE
+dist_man_MANS += \
+	pwcache.3bsd \
+	uid_from_user.3bsd \
+	user_from_uid.3bsd \
+	gid_from_group.3bsd \
+	group_from_gid.3bsd \
+	# EOL
+endif
+
+if NEED_FPURGE
+dist_man_MANS += \
+	fpurge.3bsd \
+	# EOL
+endif
+
 if NEED_FUNOPEN
 dist_man_MANS += \
 	funopen.3bsd \
diff --git a/src/Makefile.am b/src/Makefile.am
index 4781bdb..c02561e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -75,14 +75,7 @@ EXTRA_libbsd_la_DEPENDENCIES += \
 	# EOL
 endif
 libbsd_la_SOURCES = \
-	arc4random.c \
-	arc4random.h \
-	arc4random_linux.h \
-	arc4random_uniform.c \
-	arc4random_unix.h \
-	arc4random_win.h \
 	bsd_getopt.c \
-	chacha_private.h \
 	closefrom.c \
 	dehumanize_number.c \
 	err.c \
@@ -94,7 +87,6 @@ libbsd_la_SOURCES = \
 	flopen.c \
 	fmtcheck.c \
 	fparseln.c \
-	fpurge.c \
 	getbsize.c \
 	getpeereid.c \
 	heapsort.c \
@@ -102,12 +94,9 @@ libbsd_la_SOURCES = \
 	inet_net_pton.c \
 	local-elf.h \
 	local-link.h \
-	md5.c \
 	merge.c \
-	nlist.c \
 	pidfile.c \
 	progname.c \
-	pwcache.c \
 	radixsort.c \
 	readpassphrase.c \
 	reallocarray.c \
@@ -115,10 +104,7 @@ libbsd_la_SOURCES = \
 	recallocarray.c \
 	setmode.c \
 	setproctitle.c \
-	strlcat.c \
-	strlcpy.c \
 	stringlist.c \
-	strmode.c \
 	strnstr.c \
 	strtoi.c \
 	strtonum.c \
@@ -130,11 +116,59 @@ libbsd_la_SOURCES = \
 	wcslcpy.c \
 	# EOL
 
+if NEED_ARC4RANDOM
 if !HAVE_GETENTROPY
 libbsd_la_SOURCES += \
 	getentropy.c \
 	# EOL
 endif
+libbsd_la_SOURCES += \
+	arc4random.c \
+	arc4random.h \
+	arc4random_linux.h \
+	arc4random_uniform.c \
+	arc4random_unix.h \
+	arc4random_win.h \
+	chacha_private.h \
+	# EOL
+endif
+
+if NEED_MD5
+libbsd_la_SOURCES += \
+	md5.c \
+	# EOL
+endif
+
+if NEED_NLIST
+libbsd_la_SOURCES += \
+	nlist.c \
+	# EOL
+endif
+
+if NEED_STRL
+libbsd_la_SOURCES += \
+	strlcat.c \
+	strlcpy.c \
+	# EOL
+endif
+
+if NEED_STRMODE
+libbsd_la_SOURCES += \
+	strmode.c \
+	# EOL
+endif
+
+if NEED_PWCACHE
+libbsd_la_SOURCES += \
+	pwcache.c \
+	# EOL
+endif
+
+if NEED_FPURGE
+libbsd_la_SOURCES += \
+	fpurge.c \
+	# EOL
+endif
 
 if NEED_FUNOPEN
 libbsd_la_SOURCES += \
diff --git a/src/pwcache.c b/src/pwcache.c
index d54daa0..99609d9 100644
--- a/src/pwcache.c
+++ b/src/pwcache.c
@@ -103,6 +103,7 @@ st_hash(const char *name, size_t len, int tabsz)
 	return key % tabsz;
 }
 
+#ifndef HAVE_USER_FROM_UID
 /*
  * uidtb_start
  *	creates an an empty uidtb
@@ -124,7 +125,9 @@ uidtb_start(void)
 	}
 	return 0;
 }
+#endif
 
+#ifndef HAVE_GROUP_FROM_GID
 /*
  * gidtb_start
  *	creates an an empty gidtb
@@ -146,6 +149,7 @@ gidtb_start(void)
 	}
 	return 0;
 }
+#endif
 
 /*
  * usrtb_start
@@ -191,6 +195,7 @@ grptb_start(void)
 	return 0;
 }
 
+#ifndef HAVE_USER_FROM_UID
 /*
  * user_from_uid()
  *	caches the name (if any) for the uid. If noname clear, we always
@@ -251,7 +256,9 @@ user_from_uid(uid_t uid, int noname)
 	}
 	return ptr->name;
 }
+#endif
 
+#ifndef HAVE_GROUP_FROM_GID
 /*
  * group_from_gid()
  *	caches the name (if any) for the gid. If noname clear, we always
@@ -312,6 +319,7 @@ group_from_gid(gid_t gid, int noname)
 	}
 	return ptr->name;
 }
+#endif
 
 /*
  * uid_from_user()
diff --git a/test/Makefile.am b/test/Makefile.am
index 985e3e1..7d220f7 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -40,30 +40,46 @@ check_PROGRAMS = \
 	humanize \
 	fgetln \
 	fparseln \
-	fpurge \
-	md5 \
-	nlist \
 	proctitle-init \
 	progname \
-	pwcache \
 	setmode \
-	strl \
-	strmode \
 	strnstr \
 	strtonum \
 	vis \
 	vis-openbsd \
 	# EOL
 
+if NEED_NLIST
+check_PROGRAMS += nlist
+endif
+
+if NEED_STRL
+check_PROGRAMS += strl
+endif
+
+if NEED_STRMODE
+check_PROGRAMS += strmode
+endif
+
+if NEED_PWCACHE
+check_PROGRAMS += pwcache
+endif
+
+if NEED_FPURGE
+check_PROGRAMS += fpurge
+endif
+
 if NEED_FUNOPEN
 check_PROGRAMS += funopen
 endif
 
+if NEED_ARC4RANDOM
 if HAVE_LIBTESTU01
 arc4random_LDADD = $(LDADD) $(TESTU01_LIBS)
 
 check_PROGRAMS += arc4random
 endif
+endif
 
 if BUILD_LIBBSD_CTOR
 proctitle_LDFLAGS = \
@@ -75,10 +91,14 @@ proctitle_LDFLAGS = \
 check_PROGRAMS += proctitle
 endif
 
+if NEED_MD5
+check_PROGRAMS += md5
+
 if NEED_TRANSPARENT_LIBMD
 # On the installed system this is handled via the ld script.
 md5_LDADD = $(LDADD) $(MD5_LIBS)
 endif
+endif
 
 fgetln_SOURCES = test-stream.c test-stream.h fgetln.c
 fgetln_CFLAGS = -Wno-deprecated-declarations
diff --git a/test/overlay.c b/test/overlay.c
index d82f14e..af83232 100644
--- a/test/overlay.c
+++ b/test/overlay.c
@@ -28,12 +28,14 @@
  * other headers through magic macros, to check that the overlay is working
  * properly. */
 #include <errno.h>
+#ifndef __APPLE__
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
 #ifdef HAVE_GRP_H
 #include <grp.h>
 #endif
+#endif
 #include <stdint.h>
 
 /* Include libbsd overlayed headers that might get partially included. */