5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 vwarnx(const char *fmt, va_list ap)
41 fprintf(stderr, "%s: ", getprogname());
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
48 vwarnc(int code, const char *fmt, va_list ap)
50 fprintf(stderr, "%s: ", getprogname());
52 vfprintf(stderr, fmt, ap);
53 fprintf(stderr, ": ");
55 fprintf(stderr, "%s\n", strerror(code));
59 vwarn(const char *fmt, va_list ap)
64 fprintf(stderr, "%s: ", getprogname());
66 vfprintf(stderr, fmt, ap);
67 fprintf(stderr, ": ");
69 fprintf(stderr, "%s\n", strerror(sverrno));
73 verrc(int eval, int code, const char *fmt, va_list ap)
75 fprintf(stderr, "%s: ", getprogname());
77 vfprintf(stderr, fmt, ap);
78 fprintf(stderr, ": ");
80 fprintf(stderr, "%s\n", strerror(code));
85 verrx(int eval, const char *fmt, va_list ap)
87 fprintf(stderr, "%s: ", getprogname());
89 vfprintf(stderr, fmt, ap);
90 fprintf(stderr, "\n");
95 verr(int eval, const char *fmt, va_list ap)
100 fprintf(stderr, "%s: ", getprogname());
102 vfprintf(stderr, fmt, ap);
103 fprintf(stderr, ": ");
105 fprintf(stderr, "%s\n", strerror(sverrno));
110 err(int eval, const char *fmt, ...)
120 errc(int eval, int code, const char *fmt, ...)
125 verrc(eval, code, fmt, ap);
130 errx(int eval, const char *fmt, ...)
135 verrx(eval, fmt, ap);
140 warn(const char *fmt, ...)
150 warnc(int code, const char *fmt, ...)
155 vwarnc(code, fmt, ap);
160 warnx(const char *fmt, ...)
168 #endif /* !HAVE_ERR */
175 * flock(2) emulation on top of fcntl advisory locks. This is "good
176 * enough" for amused, not a _real_ emulation. flock and fcntl locks
177 * have subtly different behaviours!
180 flock(int fd, int op)
185 memset(&l, 0, sizeof(l));
186 l.l_whence = SEEK_SET;
190 else if (op & LOCK_EX)
201 return fcntl(fd, cmd, &l);
203 #endif /* HAVE_FLOCK */
209 freezero(void *ptr, size_t len)
216 #endif /* HAVE_FREEZERO */
217 #if !HAVE_EXPLICIT_BZERO
218 /* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
221 * Written by Ted Unangst
227 * explicit_bzero - don't let the compiler optimize away bzero
233 explicit_bzero(void *p, size_t n)
237 (void)memset_s(p, n, 0, n);
240 #else /* HAVE_MEMSET_S */
245 * Indirect memset through a volatile pointer to hopefully avoid
246 * dead-store optimisation eliminating the call.
248 static void (* volatile ssh_memset)(void *, int, size_t) = memset;
251 explicit_bzero(void *p, size_t n)
256 * clang -fsanitize=memory needs to intercept memset-like functions
257 * to correctly detect memory initialisation. Make sure one is called
258 * directly since our indirection trick above sucessfully confuses it.
260 #if defined(__has_feature)
261 # if __has_feature(memory_sanitizer)
269 #endif /* HAVE_MEMSET_S */
270 #endif /* !HAVE_EXPLICIT_BZERO */
271 #if !HAVE_GETPROGNAME
273 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
274 * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
275 * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
277 * Permission to use, copy, modify, and distribute this software for any
278 * purpose with or without fee is hereby granted, provided that the above
279 * copyright notice and this permission notice appear in all copies.
281 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
282 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
283 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
284 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
285 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
286 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
287 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
290 #include <sys/types.h>
299 return getexecname();
301 #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
305 return (program_invocation_short_name);
307 #elif HAVE___PROGNAME
311 extern char *__progname;
316 #warning No getprogname available.
323 #endif /* !HAVE_GETPROGNAME */
325 /* $OpenBSD: imsg-buffer.c,v 1.13 2021/03/31 17:42:24 eric Exp $ */
326 /* $OpenBSD: imsg.c,v 1.17 2022/01/28 10:41:44 claudio Exp $ */
329 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
331 * Permission to use, copy, modify, and distribute this software for any
332 * purpose with or without fee is hereby granted, provided that the above
333 * copyright notice and this permission notice appear in all copies.
335 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
336 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
337 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
338 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
339 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
340 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
341 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
346 #include <sys/types.h>
347 #include <sys/socket.h>
358 static int ibuf_realloc(struct ibuf *, size_t);
359 static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
360 static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
363 ibuf_open(size_t len)
367 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
369 if ((buf->buf = malloc(len)) == NULL) {
373 buf->size = buf->max = len;
380 ibuf_dynamic(size_t len, size_t max)
387 if ((buf = ibuf_open(len)) == NULL)
397 ibuf_realloc(struct ibuf *buf, size_t len)
401 /* on static buffers max is eq size and so the following fails */
402 if (buf->wpos + len > buf->max) {
407 b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
411 buf->size = buf->wpos + len;
417 ibuf_add(struct ibuf *buf, const void *data, size_t len)
419 if (buf->wpos + len > buf->size)
420 if (ibuf_realloc(buf, len) == -1)
423 memcpy(buf->buf + buf->wpos, data, len);
429 ibuf_reserve(struct ibuf *buf, size_t len)
433 if (buf->wpos + len > buf->size)
434 if (ibuf_realloc(buf, len) == -1)
437 b = buf->buf + buf->wpos;
443 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
445 /* only allowed to seek in already written parts */
446 if (pos + len > buf->wpos)
449 return (buf->buf + pos);
453 ibuf_size(struct ibuf *buf)
459 ibuf_left(struct ibuf *buf)
461 return (buf->max - buf->wpos);
465 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
467 ibuf_enqueue(msgbuf, buf);
471 ibuf_write(struct msgbuf *msgbuf)
473 struct iovec iov[IOV_MAX];
478 memset(&iov, 0, sizeof(iov));
479 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
482 iov[i].iov_base = buf->buf + buf->rpos;
483 iov[i].iov_len = buf->wpos - buf->rpos;
488 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
491 if (errno == ENOBUFS)
496 if (n == 0) { /* connection closed */
501 msgbuf_drain(msgbuf, n);
507 ibuf_free(struct ibuf *buf)
511 freezero(buf->buf, buf->size);
516 msgbuf_init(struct msgbuf *msgbuf)
520 TAILQ_INIT(&msgbuf->bufs);
524 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
526 struct ibuf *buf, *next;
528 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
530 next = TAILQ_NEXT(buf, entry);
531 if (buf->rpos + n >= buf->wpos) {
532 n -= buf->wpos - buf->rpos;
533 ibuf_dequeue(msgbuf, buf);
542 msgbuf_clear(struct msgbuf *msgbuf)
546 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
547 ibuf_dequeue(msgbuf, buf);
551 msgbuf_write(struct msgbuf *msgbuf)
553 struct iovec iov[IOV_MAX];
554 struct ibuf *buf, *buf0 = NULL;
558 struct cmsghdr *cmsg;
561 char buf[CMSG_SPACE(sizeof(int))];
564 memset(&iov, 0, sizeof(iov));
565 memset(&msg, 0, sizeof(msg));
566 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
567 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
570 if (i > 0 && buf->fd != -1)
572 iov[i].iov_base = buf->buf + buf->rpos;
573 iov[i].iov_len = buf->wpos - buf->rpos;
583 msg.msg_control = (caddr_t)&cmsgbuf.buf;
584 msg.msg_controllen = sizeof(cmsgbuf.buf);
585 cmsg = CMSG_FIRSTHDR(&msg);
586 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
587 cmsg->cmsg_level = SOL_SOCKET;
588 cmsg->cmsg_type = SCM_RIGHTS;
589 *(int *)CMSG_DATA(cmsg) = buf0->fd;
593 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
596 if (errno == ENOBUFS)
601 if (n == 0) { /* connection closed */
607 * assumption: fd got sent if sendmsg sent anything
608 * this works because fds are passed one at a time
615 msgbuf_drain(msgbuf, n);
621 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
623 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
628 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
630 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
641 int imsg_fd_overhead = 0;
643 static int imsg_get_fd(struct imsgbuf *);
646 imsg_init(struct imsgbuf *ibuf, int fd)
648 msgbuf_init(&ibuf->w);
649 memset(&ibuf->r, 0, sizeof(ibuf->r));
652 ibuf->pid = getpid();
653 TAILQ_INIT(&ibuf->fds);
657 imsg_read(struct imsgbuf *ibuf)
660 struct cmsghdr *cmsg;
663 char buf[CMSG_SPACE(sizeof(int) * 1)];
670 memset(&msg, 0, sizeof(msg));
671 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
673 iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
674 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
677 msg.msg_control = &cmsgbuf.buf;
678 msg.msg_controllen = sizeof(cmsgbuf.buf);
680 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
684 if (getdtablecount() + imsg_fd_overhead +
685 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
686 >= getdtablesize()) {
692 if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
700 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
701 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
702 if (cmsg->cmsg_level == SOL_SOCKET &&
703 cmsg->cmsg_type == SCM_RIGHTS) {
708 * We only accept one file descriptor. Due to C
709 * padding rules, our control buffer might contain
710 * more than one fd, and we must close them.
712 j = ((char *)cmsg + cmsg->cmsg_len -
713 (char *)CMSG_DATA(cmsg)) / sizeof(int);
714 for (i = 0; i < j; i++) {
715 fd = ((int *)CMSG_DATA(cmsg))[i];
718 TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
725 /* we do not handle other ctl data level */
734 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
736 size_t av, left, datalen;
740 if (IMSG_HEADER_SIZE > av)
743 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
744 if (imsg->hdr.len < IMSG_HEADER_SIZE ||
745 imsg->hdr.len > MAX_IMSGSIZE) {
749 if (imsg->hdr.len > av)
751 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
752 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
755 else if ((imsg->data = malloc(datalen)) == NULL)
758 if (imsg->hdr.flags & IMSGF_HASFD)
759 imsg->fd = imsg_get_fd(ibuf);
764 memcpy(imsg->data, ibuf->r.rptr, datalen);
766 if (imsg->hdr.len < av) {
767 left = av - imsg->hdr.len;
768 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
773 return (datalen + IMSG_HEADER_SIZE);
777 imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
778 int fd, const void *data, uint16_t datalen)
782 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
785 if (imsg_add(wbuf, data, datalen) == -1)
790 imsg_close(ibuf, wbuf);
796 imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
797 int fd, const struct iovec *iov, int iovcnt)
802 for (i = 0; i < iovcnt; i++)
803 datalen += iov[i].iov_len;
805 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
808 for (i = 0; i < iovcnt; i++)
809 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
814 imsg_close(ibuf, wbuf);
821 imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
827 datalen += IMSG_HEADER_SIZE;
828 if (datalen > MAX_IMSGSIZE) {
836 if ((hdr.pid = pid) == 0)
838 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
841 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
848 imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
851 if (ibuf_add(msg, data, datalen) == -1) {
859 imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
861 struct imsg_hdr *hdr;
863 hdr = (struct imsg_hdr *)msg->buf;
865 hdr->flags &= ~IMSGF_HASFD;
867 hdr->flags |= IMSGF_HASFD;
869 hdr->len = (uint16_t)msg->wpos;
871 ibuf_close(&ibuf->w, msg);
875 imsg_free(struct imsg *imsg)
877 freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
881 imsg_get_fd(struct imsgbuf *ibuf)
886 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
890 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
897 imsg_flush(struct imsgbuf *ibuf)
899 while (ibuf->w.queued)
900 if (msgbuf_write(&ibuf->w) <= 0)
906 imsg_clear(struct imsgbuf *ibuf)
910 msgbuf_clear(&ibuf->w);
911 while ((fd = imsg_get_fd(ibuf)) != -1)
914 #endif /* HAVE_IMSG */
917 * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
919 * Redistribution and use in source and binary forms, with or without
920 * modification, are permitted provided that the following conditions
922 * 1. Redistributions of source code must retain the above copyright
923 * notice, this list of conditions and the following disclaimer.
924 * 2. Redistributions in binary form must reproduce the above copyright
925 * notice, this list of conditions and the following disclaimer in
926 * the documentation and/or other materials provided with the
928 * 3. The name of the author may not be used to endorse or promote
929 * products derived from this software without specific prior written
932 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
933 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
934 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
935 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
936 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
937 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
938 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
939 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
940 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
941 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
942 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
945 * Find the first occurrence of the byte string s in byte string l.
948 memmem(const void *l, size_t l_len, const void *s, size_t s_len)
950 const char *cur, *last;
954 /* a zero length needle should just return the haystack */
958 /* "s" must be smaller or equal to "l" */
962 /* special case where s_len == 1 */
964 return memchr(l, *cs, l_len);
966 /* the last position where its possible to find "s" in "l" */
967 last = cl + l_len - s_len;
969 for (cur = cl; cur <= last; cur++)
970 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
975 #endif /* !HAVE_MEMMEM */
978 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
980 * Permission to use, copy, modify, and distribute this software for any
981 * purpose with or without fee is hereby granted, provided that the above
982 * copyright notice and this permission notice appear in all copies.
984 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
985 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
986 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
987 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
988 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
989 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
990 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
998 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
1001 memrchr(const void *s, int c, size_t n)
1003 const unsigned char *cp;
1006 cp = (unsigned char *)s + n;
1008 if (*(--cp) == (unsigned char)c)
1014 #endif /* !HAVE_MEMRCHR */
1017 * Copyright (c) 1987, 1993, 1994
1018 * The Regents of the University of California. All rights reserved.
1020 * Redistribution and use in source and binary forms, with or without
1021 * modification, are permitted provided that the following conditions
1023 * 1. Redistributions of source code must retain the above copyright
1024 * notice, this list of conditions and the following disclaimer.
1025 * 2. Redistributions in binary form must reproduce the above copyright
1026 * notice, this list of conditions and the following disclaimer in the
1027 * documentation and/or other materials provided with the distribution.
1028 * 3. Neither the name of the University nor the names of its contributors
1029 * may be used to endorse or promote products derived from this software
1030 * without specific prior written permission.
1032 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1033 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1034 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1035 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1036 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1037 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1038 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1039 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1040 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1041 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1045 /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
1051 int BSDopterr = 1, /* if error message should be printed */
1052 BSDoptind = 1, /* index into parent argv vector */
1053 BSDoptopt, /* character checked for validity */
1054 BSDoptreset; /* reset getopt */
1055 char *BSDoptarg; /* argument associated with option */
1057 #define BADCH (int)'?'
1058 #define BADARG (int)':'
1063 * Parse argc/argv argument vector.
1066 BSDgetopt(int nargc, char *const *nargv, const char *ostr)
1068 static const char *place = EMSG; /* option letter processing */
1069 char *oli; /* option letter list index */
1074 if (BSDoptreset || !*place) { /* update scanning pointer */
1076 if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
1080 if (place[1] && *++place == '-') { /* found "--" */
1087 } /* option letter okay? */
1088 if ((BSDoptopt = (int)*place++) == (int)':' ||
1089 !(oli = strchr(ostr, BSDoptopt))) {
1091 * if the user didn't specify '-' as an option,
1092 * assume it means -1.
1094 if (BSDoptopt == (int)'-')
1098 if (BSDopterr && *ostr != ':')
1099 (void)fprintf(stderr,
1100 "%s: unknown option -- %c\n", getprogname(),
1104 if (*++oli != ':') { /* don't need argument */
1109 else { /* need an argument */
1110 if (*place) /* no white space */
1111 BSDoptarg = (char *)place;
1112 else if (nargc <= ++BSDoptind) { /* no arg */
1117 (void)fprintf(stderr,
1118 "%s: option requires an argument -- %c\n",
1119 getprogname(), BSDoptopt);
1122 else /* white space */
1123 BSDoptarg = nargv[BSDoptind];
1127 return (BSDoptopt); /* dump back option letter */
1130 #if !HAVE_REALLOCARRAY
1132 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
1134 * Permission to use, copy, modify, and distribute this software for any
1135 * purpose with or without fee is hereby granted, provided that the above
1136 * copyright notice and this permission notice appear in all copies.
1138 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1139 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1140 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1141 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1142 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1143 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1144 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1147 #include <sys/types.h>
1153 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1154 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1156 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1159 reallocarray(void *optr, size_t nmemb, size_t size)
1161 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1162 nmemb > 0 && SIZE_MAX / nmemb < size) {
1166 return realloc(optr, size * nmemb);
1168 #endif /* !HAVE_REALLOCARRAY */
1169 #if !HAVE_RECALLOCARRAY
1171 * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
1173 * Permission to use, copy, modify, and distribute this software for any
1174 * purpose with or without fee is hereby granted, provided that the above
1175 * copyright notice and this permission notice appear in all copies.
1177 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1178 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1179 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1180 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1181 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1182 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1183 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1186 /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
1195 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1196 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1198 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1201 recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
1203 size_t oldsize, newsize;
1207 return calloc(newnmemb, size);
1209 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1210 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
1214 newsize = newnmemb * size;
1216 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1217 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
1221 oldsize = oldnmemb * size;
1224 * Don't bother too much if we're shrinking just a bit,
1225 * we do not shrink for series of small steps, oh well.
1227 if (newsize <= oldsize) {
1228 size_t d = oldsize - newsize;
1230 if (d < oldsize / 2 && d < (size_t)getpagesize()) {
1231 memset((char *)ptr + newsize, 0, d);
1236 newptr = malloc(newsize);
1240 if (newsize > oldsize) {
1241 memcpy(newptr, ptr, oldsize);
1242 memset((char *)newptr + oldsize, 0, newsize - oldsize);
1244 memcpy(newptr, ptr, newsize);
1246 explicit_bzero(ptr, oldsize);
1251 #endif /* !HAVE_RECALLOCARRAY */
1252 #if !HAVE_SETPROCTITLE
1254 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
1256 * Permission to use, copy, modify, and distribute this software for any
1257 * purpose with or without fee is hereby granted, provided that the above
1258 * copyright notice and this permission notice appear in all copies.
1260 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1261 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1262 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1263 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1264 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1265 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1266 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1269 # if HAVE_PR_SET_NAME
1270 #include <sys/types.h>
1274 #include <sys/prctl.h>
1276 setproctitle(const char *fmt, ...)
1278 char title[16], name[16], *cp;
1283 vsnprintf(title, sizeof(title), fmt, ap);
1286 used = snprintf(name, sizeof(name), "%s: %s", getprogname(), title);
1287 if (used >= (int)sizeof(name)) {
1288 cp = strrchr(name, ' ');
1292 prctl(PR_SET_NAME, name);
1296 setproctitle(const char *fmt, ...)
1301 #endif /* !HAVE_SETPROCTITLE */
1304 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1306 * Permission to use, copy, modify, and distribute this software for any
1307 * purpose with or without fee is hereby granted, provided that the above
1308 * copyright notice and this permission notice appear in all copies.
1310 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1311 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1312 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1313 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1314 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1315 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1316 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1319 #include <sys/types.h>
1323 * Appends src to string dst of size siz (unlike strncat, siz is the
1324 * full size of dst, not space left). At most siz-1 characters
1325 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
1326 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
1327 * If retval >= siz, truncation occurred.
1330 strlcat(char *dst, const char *src, size_t siz)
1333 const char *s = src;
1337 /* Find the end of dst and adjust bytes left but don't go past end */
1338 while (n-- != 0 && *d != '\0')
1344 return(dlen + strlen(s));
1345 while (*s != '\0') {
1354 return(dlen + (s - src)); /* count does not include NUL */
1356 #endif /* !HAVE_STRLCAT */
1359 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1361 * Permission to use, copy, modify, and distribute this software for any
1362 * purpose with or without fee is hereby granted, provided that the above
1363 * copyright notice and this permission notice appear in all copies.
1365 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1366 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1367 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1368 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1369 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1370 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1371 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1374 #include <sys/types.h>
1378 * Copy src to string dst of size siz. At most siz-1 characters
1379 * will be copied. Always NUL terminates (unless siz == 0).
1380 * Returns strlen(src); if retval >= siz, truncation occurred.
1383 strlcpy(char *dst, const char *src, size_t siz)
1386 const char *s = src;
1389 /* Copy as many bytes as will fit */
1392 if ((*d++ = *s++) == '\0')
1397 /* Not enough room in dst, add NUL and traverse rest of src */
1400 *d = '\0'; /* NUL-terminate dst */
1405 return(s - src - 1); /* count does not include NUL */
1407 #endif /* !HAVE_STRLCPY */
1411 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1413 * Permission to use, copy, modify, and distribute this software for any
1414 * purpose with or without fee is hereby granted, provided that the above
1415 * copyright notice and this permission notice appear in all copies.
1417 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1418 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1419 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1420 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1421 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1422 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1423 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1426 #include <sys/types.h>
1433 strndup(const char *str, size_t maxlen)
1438 len = strnlen(str, maxlen);
1439 copy = malloc(len + 1);
1441 (void)memcpy(copy, str, len);
1447 #endif /* !HAVE_STRNDUP */
1452 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1454 * Permission to use, copy, modify, and distribute this software for any
1455 * purpose with or without fee is hereby granted, provided that the above
1456 * copyright notice and this permission notice appear in all copies.
1458 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1459 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1460 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1461 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1462 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1463 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1464 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1467 #include <sys/types.h>
1471 strnlen(const char *str, size_t maxlen)
1475 for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
1478 return (size_t)(cp - str);
1480 #endif /* !HAVE_STRNLEN */
1483 * Copyright (c) 2004 Ted Unangst and Todd Miller
1484 * All rights reserved.
1486 * Permission to use, copy, modify, and distribute this software for any
1487 * purpose with or without fee is hereby granted, provided that the above
1488 * copyright notice and this permission notice appear in all copies.
1490 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1491 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1492 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1493 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1494 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1495 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1496 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1508 strtonum(const char *numstr, long long minval, long long maxval,
1509 const char **errstrp)
1519 { "invalid", EINVAL },
1520 { "too small", ERANGE },
1521 { "too large", ERANGE },
1526 if (minval > maxval) {
1529 ll = strtoll(numstr, &ep, 10);
1530 if (numstr == ep || *ep != '\0')
1532 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
1534 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
1537 if (errstrp != NULL)
1538 *errstrp = ev[error].errstr;
1539 errno = ev[error].err;
1545 #endif /* !HAVE_STRTONUM */