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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
|
#
# Copyright 2026 Zygaena Project Contributors
# Use is subject to license terms.
#
include ../Makefile.lib
HDRS = libutil.h pty.h
HDRDIR = $(SRC)/head
# Hammerhead: amd64-only
SUBDIRS = $(MACH64)
all : TARGET= all
clean : TARGET= clean
clobber : TARGET= clobber
install : TARGET= install
.KEEP_STATE:
all clean clobber install: $(SUBDIRS)
install_h: $(ROOTHDRS)
check: $(CHECKHDRS)
$(SUBDIRS): FRC
@cd $@; pwd; $(MAKE) $(TARGET)
FRC:
include ../Makefile.targ
#
# Copyright 2026 Zygaena Project Contributors
# Use is subject to license terms.
#
LIBRARY = libutil.a
VERS = .1
OBJECTS = pty.o flopen.o pidfile.o humanize_number.o expand_number.o \
fparseln.o
include ../../Makefile.lib
LIBS = $(DYNLIB)
LDLIBS += -lc
SRCDIR = ../common
CPPFLAGS += -I$(SRC)/head
SMATCH = off
.KEEP_STATE:
all: $(LIBS)
include ../../Makefile.targ
#
# Copyright 2026 Zygaena Project Contributors
# Use is subject to license terms.
#
include ../Makefile.com
include ../../Makefile.lib.64
LIBS = $(DYNLIB)
all: $(LIBS)
install: all $(ROOTLIBS64) $(ROOTLINKS64)
/*
* Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
* Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* 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 THE AUTHORS AND CONTRIBUTORS ``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 THE AUTHORS 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.
*
* Adapted for Hammerhead/illumos.
*/
#include <sys/types.h>
#include <errno.h>
#include <inttypes.h>
#include <libutil.h>
#include <limits.h>
#include <stdlib.h>
/*
* Parse a human-readable number string with optional suffix.
* Supported suffixes: k/K (1024), m/M (1024^2), g/G (1024^3),
* t/T (1024^4), p/P (1024^5), e/E (1024^6).
*
* Returns 0 on success, -1 on error (with errno set).
*/
int
expand_number(const char *buf, uint64_t *num)
{
char *endp;
uint64_t result;
unsigned shift;
if (buf == NULL || num == NULL) {
errno = EINVAL;
return (-1);
}
errno = 0;
result = strtoull(buf, &endp, 0);
if (errno != 0)
return (-1);
if (endp == buf) {
/* No valid digits */
errno = EINVAL;
return (-1);
}
switch (*endp) {
case 'e':
case 'E':
shift = 60;
break;
case 'p':
case 'P':
shift = 50;
break;
case 't':
case 'T':
shift = 40;
break;
case 'g':
case 'G':
shift = 30;
break;
case 'm':
case 'M':
shift = 20;
break;
case 'k':
case 'K':
shift = 10;
break;
case '\0':
*num = result;
return (0);
default:
errno = EINVAL;
return (-1);
}
/* Check for overflow */
if (result > (UINT64_MAX >> shift)) {
errno = ERANGE;
return (-1);
}
*num = result << shift;
return (0);
}
/*
* Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
* 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
* in this position and unchanged.
* 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR 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.
*
* Adapted for Hammerhead/illumos.
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
int
flopenat(int dirfd, const char *path, int flags, ...)
{
int fd, operation, serrno, trunc;
struct stat sb, fsb;
mode_t mode;
va_list ap;
mode = 0;
if (flags & O_CREAT) {
va_start(ap, flags);
mode = (mode_t)va_arg(ap, int);
va_end(ap);
}
operation = LOCK_EX;
if (flags & O_NONBLOCK)
operation |= LOCK_NB;
trunc = (flags & O_TRUNC);
flags &= ~O_TRUNC;
for (;;) {
fd = openat(dirfd, path, flags, mode);
if (fd == -1)
return (-1);
if (flock(fd, operation) == -1) {
serrno = errno;
(void) close(fd);
errno = serrno;
return (-1);
}
if (fstat(fd, &fsb) == -1) {
serrno = errno;
(void) close(fd);
errno = serrno;
return (-1);
}
if (fstatat(dirfd, path, &sb, 0) == -1) {
if (errno == ENOENT) {
(void) close(fd);
continue;
}
serrno = errno;
(void) close(fd);
errno = serrno;
return (-1);
}
if (fsb.st_dev == sb.st_dev && fsb.st_ino == sb.st_ino)
break;
(void) close(fd);
}
if (trunc && ftruncate(fd, 0) == -1) {
serrno = errno;
(void) close(fd);
errno = serrno;
return (-1);
}
return (fd);
}
int
flopen(const char *path, int flags, ...)
{
mode_t mode;
va_list ap;
mode = 0;
if (flags & O_CREAT) {
va_start(ap, flags);
mode = (mode_t)va_arg(ap, int);
va_end(ap);
}
return (flopenat(AT_FDCWD, path, flags, mode));
}
/*
* Copyright (c) 1997 Christos Zoulas. 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 THE AUTHOR ``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 THE AUTHOR 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.
*
* Adapted for Hammerhead/illumos.
*/
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libutil.h>
static int
isescaped(const char *sp, const char *p, int esc)
{
const char *cp;
size_t ne = 0;
/* Count preceding escape characters */
for (cp = p; --cp >= sp && *cp == esc; )
ne++;
/* Odd number of escapes means the character is escaped */
return (ne & 1);
}
/*
* Read a line from the given stream, handling:
* - Line continuations (backslash-newline)
* - Comment stripping
* - Escape character processing
*
* delim[0] = comment character (default '#')
* delim[1] = continuation char (default '\\')
* delim[2] = escape character (default '\\')
*
* Returns a malloc'd string (caller must free), or NULL on EOF/error.
*/
char *
fparseln(FILE *fp, size_t *size, size_t *lineno,
const char delim[3], int flags)
{
static const char defs[3] = { '#', '\\', '\\' };
char *buf = NULL, *ptr, *cp;
size_t s, len = 0;
int cnt = 1;
if (delim == NULL)
delim = defs;
while (cnt != 0) {
cnt = 0;
if (feof(fp) != 0) {
if (buf != NULL)
break;
return (NULL);
}
/* Read a line using getline */
ptr = NULL;
s = 0;
if (getline(&ptr, &s, fp) == -1) {
free(ptr);
if (buf != NULL)
break;
return (NULL);
}
if (lineno != NULL)
(*lineno)++;
/* Remove trailing newline */
s = strlen(ptr);
if (s > 0 && ptr[s - 1] == '\n')
ptr[--s] = '\0';
/* Handle continuation */
if (delim[1] && s > 0 && ptr[s - 1] == delim[1] &&
!isescaped(ptr, &ptr[s - 1], delim[2])) {
ptr[--s] = '\0';
cnt = 1;
if (flags & FPARSELN_UNESCCONT) {
/* continuation char already removed */
}
}
/* Handle comments */
if (delim[0]) {
for (cp = ptr; *cp != '\0'; cp++) {
if (*cp == delim[2] && cp[1] != '\0') {
cp++;
continue;
}
if (*cp == delim[0] &&
!isescaped(ptr, cp, delim[2])) {
if (flags & FPARSELN_UNESCCOMM) {
/* Keep unescaped */
}
*cp = '\0';
s = (size_t)(cp - ptr);
break;
}
}
}
/* Handle escape sequences in the result */
if (flags & (FPARSELN_UNESCESC | FPARSELN_UNESCREST)) {
char *dst = ptr;
for (cp = ptr; *cp != '\0'; cp++) {
if (*cp == delim[2]) {
char next = cp[1];
int unescape = 0;
if (next == delim[2] &&
(flags & FPARSELN_UNESCESC))
unescape = 1;
else if (next == delim[0] &&
(flags & FPARSELN_UNESCCOMM))
unescape = 1;
else if (next == delim[1] &&
(flags & FPARSELN_UNESCCONT))
unescape = 1;
else if (flags & FPARSELN_UNESCREST)
unescape = 1;
if (unescape && next != '\0') {
cp++;
*dst++ = *cp;
continue;
}
}
*dst++ = *cp;
}
*dst = '\0';
s = (size_t)(dst - ptr);
}
/* Append to accumulated buffer */
buf = realloc(buf, len + s + 1);
if (buf == NULL) {
free(ptr);
return (NULL);
}
(void) memcpy(buf + len, ptr, s);
len += s;
buf[len] = '\0';
free(ptr);
}
if (size != NULL)
*size = len;
return (buf);
}
/* $NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $ */
/*
* Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
* Copyright 2013 John-Mark Gurney <jmg@FreeBSD.org>
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
*
* 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 THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 THE FOUNDATION 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.
*
* From FreeBSD contrib/bhyve/lib/libutil/humanize_number.c
* Adapted for Hammerhead/illumos.
*/
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <libutil.h>
static const int maxscale = 6;
int
humanize_number(char *buf, size_t len, int64_t quotient,
const char *suffix, int scale, int flags)
{
const char *prefixes, *sep;
int i, r, remainder, s1, s2, sign;
int divisordeccut;
int64_t divisor, max;
size_t baselen;
/* Since so many callers don't check -1, NUL terminate the buffer */
if (len > 0)
buf[0] = '\0';
/* validate args */
if (buf == NULL || suffix == NULL)
return (-1);
if (scale < 0)
return (-1);
else if (scale > maxscale &&
((scale & ~(HN_AUTOSCALE|HN_GETSCALE)) != 0))
return (-1);
if ((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))
return (-1);
/* setup parameters */
remainder = 0;
if (flags & HN_IEC_PREFIXES) {
baselen = 2;
/*
* Use the prefixes for power of two recommended by
* the International Electrotechnical Commission
* (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
*
* HN_IEC_PREFIXES implies a divisor of 1024 here
* (use of HN_DIVISOR_1000 would have triggered
* an assertion earlier).
*/
divisor = 1024;
divisordeccut = 973; /* ceil(.95 * 1024) */
if (flags & HN_B)
prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
else
prefixes = "\0\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
} else {
baselen = 1;
if (flags & HN_DIVISOR_1000) {
divisor = 1000;
divisordeccut = 950;
if (flags & HN_B)
prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
else
prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
} else {
divisor = 1024;
divisordeccut = 973; /* ceil(.95 * 1024) */
if (flags & HN_B)
prefixes = "B\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
else
prefixes = "\0\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
}
}
#define SCALE2PREFIX(scale) (&prefixes[(scale) * 3])
if (quotient < 0) {
sign = -1;
quotient = -quotient;
baselen += 2; /* sign, digit */
} else {
sign = 1;
baselen += 1; /* digit */
}
if (flags & HN_NOSPACE)
sep = "";
else {
sep = " ";
baselen++;
}
baselen += strlen(suffix);
/* Check if enough room for `x y' + suffix + `\0' */
if (len < baselen + 1)
return (-1);
if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
/* See if there is additional columns can be used. */
for (max = 1, i = len - baselen; i-- > 0;)
max *= 10;
/*
* Divide the number until it fits the given column.
* If there will be an overflow by the rounding below,
* divide once more.
*/
for (i = 0;
(quotient >= max || (quotient == max - 1 &&
remainder >= divisordeccut)) && i < maxscale; i++) {
remainder = quotient % divisor;
quotient /= divisor;
}
if (scale & HN_GETSCALE)
return (i);
} else {
for (i = 0; i < scale && i < maxscale; i++) {
remainder = quotient % divisor;
quotient /= divisor;
}
}
/* If a value <= 9.9 after rounding and ... */
/*
* XXX - should we make sure there is enough space for the decimal
* place and if not, don't do HN_DECIMAL?
*/
if (((quotient == 9 && remainder < divisordeccut) || quotient < 9) &&
i > 0 && flags & HN_DECIMAL) {
s1 = (int)quotient + ((remainder * 10 + divisor / 2) /
divisor / 10);
s2 = ((remainder * 10 + divisor / 2) / divisor) % 10;
r = snprintf(buf, len, "%d%s%d%s%s%s",
sign * s1, localeconv()->decimal_point, s2,
sep, SCALE2PREFIX(i), suffix);
} else
r = snprintf(buf, len, "%" PRId64 "%s%s%s",
sign * (quotient + (remainder + divisor / 2) / divisor),
sep, SCALE2PREFIX(i), suffix);
return (r);
}
#
# Copyright 2026 Zygaena Project Contributors
# Use is subject to license terms.
#
$mapfile_version 2
SYMBOL_VERSION HAMMERHEAD_1.0 {
global:
expand_number;
flopen;
flopenat;
forkpty;
fparseln;
humanize_number;
login_tty;
openpty;
pidfile_close;
pidfile_fileno;
pidfile_open;
pidfile_remove;
pidfile_write;
local:
*;
};
/*
* Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* 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 THE AUTHORS AND CONTRIBUTORS ``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 THE AUTHORS 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.
*
* Adapted for Hammerhead/illumos.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libutil.h>
struct pidfh {
int pf_fd;
pid_t pf_pid;
char pf_path[MAXPATHLEN + 1];
dev_t pf_dev;
ino_t pf_ino;
};
static int _pidfile_remove(struct pidfh *pfh, int freeit);
struct pidfh *
pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
{
struct pidfh *pfh;
struct stat sb;
int fd, serrno;
char buf[16];
pfh = malloc(sizeof (*pfh));
if (pfh == NULL)
return (NULL);
if (path == NULL)
(void) snprintf(pfh->pf_path, sizeof (pfh->pf_path),
"/var/run/%s.pid", getprogname());
else
(void) strlcpy(pfh->pf_path, path, sizeof (pfh->pf_path));
fd = flopen(pfh->pf_path,
O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
if (fd == -1) {
if (errno == EWOULDBLOCK) {
/*
* The file is already locked. Read the PID
* of the locking process and return it to
* the caller via pidptr.
*/
if (pidptr != NULL) {
fd = open(pfh->pf_path, O_RDONLY | O_NONBLOCK);
if (fd >= 0) {
ssize_t n = read(fd, buf,
sizeof (buf) - 1);
if (n > 0) {
buf[n] = '\0';
*pidptr = (pid_t)atol(buf);
}
(void) close(fd);
}
}
free(pfh);
errno = EEXIST;
return (NULL);
}
serrno = errno;
free(pfh);
errno = serrno;
return (NULL);
}
/*
* Remember the inode/device so we can verify the file hasn't
* been replaced before we remove it.
*/
if (fstat(fd, &sb) == -1) {
serrno = errno;
(void) unlink(pfh->pf_path);
(void) close(fd);
free(pfh);
errno = serrno;
return (NULL);
}
pfh->pf_fd = fd;
pfh->pf_pid = getpid();
pfh->pf_dev = sb.st_dev;
pfh->pf_ino = sb.st_ino;
return (pfh);
}
int
pidfile_write(struct pidfh *pfh)
{
char buf[16];
int n;
if (pfh == NULL || pfh->pf_fd == -1) {
errno = EINVAL;
return (-1);
}
/*
* Update the PID to the current process — important after fork().
*/
pfh->pf_pid = getpid();
if (ftruncate(pfh->pf_fd, 0) == -1)
return (-1);
n = snprintf(buf, sizeof (buf), "%ld\n", (long)pfh->pf_pid);
if (n < 0 || pwrite(pfh->pf_fd, buf, (size_t)n, 0) != (ssize_t)n) {
errno = (n < 0) ? ENOMEM : EIO;
return (-1);
}
return (0);
}
int
pidfile_close(struct pidfh *pfh)
{
int ret;
if (pfh == NULL || pfh->pf_fd == -1) {
errno = EINVAL;
return (-1);
}
ret = close(pfh->pf_fd);
pfh->pf_fd = -1;
free(pfh);
return (ret);
}
static int
_pidfile_remove(struct pidfh *pfh, int freeit)
{
struct stat sb;
int ret;
if (pfh == NULL || pfh->pf_fd == -1) {
errno = EINVAL;
return (-1);
}
/*
* Only unlink the file if it still belongs to us (same dev/ino).
* This prevents removing a PID file recreated by another process.
*/
if (stat(pfh->pf_path, &sb) == 0) {
if (sb.st_dev == pfh->pf_dev && sb.st_ino == pfh->pf_ino)
(void) unlink(pfh->pf_path);
}
ret = close(pfh->pf_fd);
pfh->pf_fd = -1;
if (freeit)
free(pfh);
return (ret);
}
int
pidfile_remove(struct pidfh *pfh)
{
return (_pidfile_remove(pfh, 1));
}
int
pidfile_fileno(const struct pidfh *pfh)
{
if (pfh == NULL || pfh->pf_fd == -1) {
errno = EINVAL;
return (-1);
}
return (pfh->pf_fd);
}
/*
* Copyright 2026 Zygaena Project Contributors
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* BSD-compatible PTY utility functions for illumos.
*
* openpty() allocates a pseudo-terminal pair using the SysV STREAMS-based
* PTY subsystem (posix_openpt/grantpt/unlockpt/ptsname) and pushes the
* required STREAMS modules (ptem, ldterm) for terminal semantics.
*
* login_tty() prepares the subsidiary (slave) side as the controlling
* terminal for a new session.
*
* forkpty() combines openpty() + fork() + login_tty() for the common
* case of spawning a process with a PTY attached.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stropts.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int
openpty(int *amaster, int *aslave, char *name,
struct termios *termp, struct winsize *winp)
{
int mfd, sfd;
const char *sname;
void (*old_sigchld)(int);
mfd = posix_openpt(O_RDWR | O_NOCTTY);
if (mfd < 0)
return (-1);
/*
* grantpt() on illumos may fork a child process to change ownership
* of the subsidiary device. Temporarily reset SIGCHLD to SIG_DFL
* to prevent interference from the caller's signal handler.
*/
old_sigchld = signal(SIGCHLD, SIG_DFL);
if (grantpt(mfd) < 0) {
(void) signal(SIGCHLD, old_sigchld);
(void) close(mfd);
return (-1);
}
(void) signal(SIGCHLD, old_sigchld);
if (unlockpt(mfd) < 0) {
(void) close(mfd);
return (-1);
}
sname = ptsname(mfd);
if (sname == NULL) {
(void) close(mfd);
return (-1);
}
sfd = open(sname, O_RDWR | O_NOCTTY);
if (sfd < 0) {
(void) close(mfd);
return (-1);
}
/*
* Push STREAMS modules for terminal line discipline.
* Use __I_PUSH_NOCTTY to avoid acquiring the controlling terminal
* during openpty — that's login_tty's job.
* Check with I_FIND first to avoid duplicate pushes.
*/
if (ioctl(sfd, I_FIND, "ptem") == 0) {
if (ioctl(sfd, __I_PUSH_NOCTTY, "ptem") < 0) {
(void) close(mfd);
(void) close(sfd);
return (-1);
}
}
if (ioctl(sfd, I_FIND, "ldterm") == 0) {
if (ioctl(sfd, __I_PUSH_NOCTTY, "ldterm") < 0) {
(void) close(mfd);
(void) close(sfd);
return (-1);
}
}
if (termp != NULL)
(void) tcsetattr(sfd, TCSAFLUSH, termp);
if (winp != NULL)
(void) ioctl(sfd, TIOCSWINSZ, winp);
if (name != NULL)
(void) strcpy(name, sname);
*amaster = mfd;
*aslave = sfd;
return (0);
}
int
login_tty(int fd)
{
char name[256];
/*
* Get the device name while we still have the fd open.
* We need it to reopen without O_NOCTTY below.
*/
if (ttyname_r(fd, name, sizeof (name)) != 0)
return (-1);
/*
* Create a new session. This detaches from any existing
* controlling terminal.
*/
if (setsid() < 0)
return (-1);
/*
* On illumos, a session leader acquires a controlling terminal
* when it opens a terminal device without O_NOCTTY and ptem is
* pushed via I_PUSH (not __I_PUSH_NOCTTY).
*
* Close the fd from openpty (which used O_NOCTTY and
* __I_PUSH_NOCTTY) and reopen the same device. The reopen
* gives us a fresh STREAMS instance; pushing ptem with I_PUSH
* acquires the controlling terminal.
*/
(void) close(fd);
fd = open(name, O_RDWR);
if (fd < 0)
return (-1);
/* Push modules with I_PUSH to acquire controlling terminal */
(void) ioctl(fd, I_PUSH, "ptem");
(void) ioctl(fd, I_PUSH, "ldterm");
(void) dup2(fd, STDIN_FILENO);
(void) dup2(fd, STDOUT_FILENO);
(void) dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
(void) close(fd);
return (0);
}
pid_t
forkpty(int *amaster, char *name,
struct termios *termp, struct winsize *winp)
{
int master, slave;
pid_t pid;
if (openpty(&master, &slave, name, termp, winp) < 0)
return (-1);
pid = fork();
switch (pid) {
case -1:
(void) close(master);
(void) close(slave);
return (-1);
case 0:
/* Child: set up subsidiary as controlling terminal */
(void) close(master);
if (login_tty(slave) < 0)
_exit(1);
return (0);
default:
/* Parent: return master fd */
(void) close(slave);
*amaster = master;
return (pid);
}
}
|