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) =
249 (void (*volatile)(void *, int, size_t))memset;
252 explicit_bzero(void *p, size_t n)
257 * clang -fsanitize=memory needs to intercept memset-like functions
258 * to correctly detect memory initialisation. Make sure one is called
259 * directly since our indirection trick above sucessfully confuses it.
261 #if defined(__has_feature)
262 # if __has_feature(memory_sanitizer)
270 #endif /* HAVE_MEMSET_S */
271 #endif /* !HAVE_EXPLICIT_BZERO */
272 #if !HAVE_GETDTABLESIZE
279 return sysconf(_SC_OPEN_MAX);
281 #endif /* !HAVE_GETDTABLESIZE */
282 #if !HAVE_GETPROGNAME
284 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
285 * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
286 * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
288 * Permission to use, copy, modify, and distribute this software for any
289 * purpose with or without fee is hereby granted, provided that the above
290 * copyright notice and this permission notice appear in all copies.
292 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
293 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
294 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
295 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
296 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
297 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
298 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
301 #include <sys/types.h>
310 return getexecname();
312 #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
316 return (program_invocation_short_name);
318 #elif HAVE___PROGNAME
322 extern char *__progname;
327 #warning No getprogname available.
334 #endif /* !HAVE_GETPROGNAME */
336 /* $OpenBSD: imsg-buffer.c,v 1.16 2023/06/19 17:19:50 claudio Exp $ */
337 /* $OpenBSD: imsg.c,v 1.19 2023/06/19 17:19:50 claudio Exp $ */
340 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
342 * Permission to use, copy, modify, and distribute this software for any
343 * purpose with or without fee is hereby granted, provided that the above
344 * copyright notice and this permission notice appear in all copies.
346 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
347 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
348 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
349 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
350 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
351 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
352 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
357 #include <sys/types.h>
358 #include <sys/socket.h>
370 static int ibuf_realloc(struct ibuf *, size_t);
371 static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
372 static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
373 static void msgbuf_drain(struct msgbuf *, size_t);
376 ibuf_open(size_t len)
384 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
386 if ((buf->buf = calloc(len, 1)) == NULL) {
390 buf->size = buf->max = len;
397 ibuf_dynamic(size_t len, size_t max)
406 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
409 if ((buf->buf = calloc(len, 1)) == NULL) {
422 ibuf_realloc(struct ibuf *buf, size_t len)
426 /* on static buffers max is eq size and so the following fails */
427 if (len > SIZE_MAX - buf->wpos || buf->wpos + len > buf->max) {
432 b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
436 buf->size = buf->wpos + len;
442 ibuf_reserve(struct ibuf *buf, size_t len)
446 if (len > SIZE_MAX - buf->wpos) {
451 if (buf->wpos + len > buf->size)
452 if (ibuf_realloc(buf, len) == -1)
455 b = buf->buf + buf->wpos;
462 ibuf_add(struct ibuf *buf, const void *data, size_t len)
466 if ((b = ibuf_reserve(buf, len)) == NULL)
469 memcpy(b, data, len);
474 ibuf_add_buf(struct ibuf *buf, const struct ibuf *from)
476 return ibuf_add(buf, from->buf, from->wpos);
480 ibuf_add_n8(struct ibuf *buf, uint64_t value)
484 if (value > UINT8_MAX) {
489 return ibuf_add(buf, &v, sizeof(v));
493 ibuf_add_n16(struct ibuf *buf, uint64_t value)
497 if (value > UINT16_MAX) {
502 return ibuf_add(buf, &v, sizeof(v));
506 ibuf_add_n32(struct ibuf *buf, uint64_t value)
510 if (value > UINT32_MAX) {
515 return ibuf_add(buf, &v, sizeof(v));
519 ibuf_add_n64(struct ibuf *buf, uint64_t value)
521 value = htobe64(value);
522 return ibuf_add(buf, &value, sizeof(value));
526 ibuf_add_zero(struct ibuf *buf, size_t len)
530 if ((b = ibuf_reserve(buf, len)) == NULL)
536 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
538 /* only allowed to seek in already written parts */
539 if (len > SIZE_MAX - pos || pos + len > buf->wpos) {
544 return (buf->buf + pos);
548 ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
552 if ((b = ibuf_seek(buf, pos, len)) == NULL)
555 memcpy(b, data, len);
560 ibuf_set_n8(struct ibuf *buf, size_t pos, uint64_t value)
564 if (value > UINT8_MAX) {
569 return (ibuf_set(buf, pos, &v, sizeof(v)));
573 ibuf_set_n16(struct ibuf *buf, size_t pos, uint64_t value)
577 if (value > UINT16_MAX) {
582 return (ibuf_set(buf, pos, &v, sizeof(v)));
586 ibuf_set_n32(struct ibuf *buf, size_t pos, uint64_t value)
590 if (value > UINT32_MAX) {
595 return (ibuf_set(buf, pos, &v, sizeof(v)));
599 ibuf_set_n64(struct ibuf *buf, size_t pos, uint64_t value)
601 value = htobe64(value);
602 return (ibuf_set(buf, pos, &value, sizeof(value)));
606 ibuf_data(struct ibuf *buf)
612 ibuf_size(struct ibuf *buf)
618 ibuf_left(struct ibuf *buf)
620 return (buf->max - buf->wpos);
624 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
626 ibuf_enqueue(msgbuf, buf);
630 ibuf_free(struct ibuf *buf)
638 freezero(buf->buf, buf->size);
643 ibuf_fd_avail(struct ibuf *buf)
645 return (buf->fd != -1);
649 ibuf_fd_get(struct ibuf *buf)
661 ibuf_fd_set(struct ibuf *buf, int fd)
669 ibuf_write(struct msgbuf *msgbuf)
671 struct iovec iov[IOV_MAX];
676 memset(&iov, 0, sizeof(iov));
677 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
680 iov[i].iov_base = buf->buf + buf->rpos;
681 iov[i].iov_len = buf->wpos - buf->rpos;
686 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
689 if (errno == ENOBUFS)
694 if (n == 0) { /* connection closed */
699 msgbuf_drain(msgbuf, n);
705 msgbuf_init(struct msgbuf *msgbuf)
709 TAILQ_INIT(&msgbuf->bufs);
713 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
715 struct ibuf *buf, *next;
717 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
719 next = TAILQ_NEXT(buf, entry);
720 if (n >= buf->wpos - buf->rpos) {
721 n -= buf->wpos - buf->rpos;
722 ibuf_dequeue(msgbuf, buf);
731 msgbuf_clear(struct msgbuf *msgbuf)
735 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
736 ibuf_dequeue(msgbuf, buf);
740 msgbuf_write(struct msgbuf *msgbuf)
742 struct iovec iov[IOV_MAX];
743 struct ibuf *buf, *buf0 = NULL;
747 struct cmsghdr *cmsg;
750 char buf[CMSG_SPACE(sizeof(int))];
753 memset(&iov, 0, sizeof(iov));
754 memset(&msg, 0, sizeof(msg));
755 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
756 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
759 if (i > 0 && buf->fd != -1)
761 iov[i].iov_base = buf->buf + buf->rpos;
762 iov[i].iov_len = buf->wpos - buf->rpos;
772 msg.msg_control = (caddr_t)&cmsgbuf.buf;
773 msg.msg_controllen = sizeof(cmsgbuf.buf);
774 cmsg = CMSG_FIRSTHDR(&msg);
775 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
776 cmsg->cmsg_level = SOL_SOCKET;
777 cmsg->cmsg_type = SCM_RIGHTS;
778 *(int *)CMSG_DATA(cmsg) = buf0->fd;
782 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
785 if (errno == ENOBUFS)
790 if (n == 0) { /* connection closed */
796 * assumption: fd got sent if sendmsg sent anything
797 * this works because fds are passed one at a time
804 msgbuf_drain(msgbuf, n);
810 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
812 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
817 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
819 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
832 int imsg_fd_overhead = 0;
834 static int imsg_get_fd(struct imsgbuf *);
837 imsg_init(struct imsgbuf *ibuf, int fd)
839 msgbuf_init(&ibuf->w);
840 memset(&ibuf->r, 0, sizeof(ibuf->r));
843 ibuf->pid = getpid();
844 TAILQ_INIT(&ibuf->fds);
848 imsg_read(struct imsgbuf *ibuf)
851 struct cmsghdr *cmsg;
854 char buf[CMSG_SPACE(sizeof(int) * 1)];
861 memset(&msg, 0, sizeof(msg));
862 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
864 iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
865 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
868 msg.msg_control = &cmsgbuf.buf;
869 msg.msg_controllen = sizeof(cmsgbuf.buf);
871 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
875 if (getdtablecount() + imsg_fd_overhead +
876 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
877 >= getdtablesize()) {
883 if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
891 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
892 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
893 if (cmsg->cmsg_level == SOL_SOCKET &&
894 cmsg->cmsg_type == SCM_RIGHTS) {
899 * We only accept one file descriptor. Due to C
900 * padding rules, our control buffer might contain
901 * more than one fd, and we must close them.
903 j = ((char *)cmsg + cmsg->cmsg_len -
904 (char *)CMSG_DATA(cmsg)) / sizeof(int);
905 for (i = 0; i < j; i++) {
906 fd = ((int *)CMSG_DATA(cmsg))[i];
909 TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
916 /* we do not handle other ctl data level */
925 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
927 size_t av, left, datalen;
931 if (IMSG_HEADER_SIZE > av)
934 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
935 if (imsg->hdr.len < IMSG_HEADER_SIZE ||
936 imsg->hdr.len > MAX_IMSGSIZE) {
940 if (imsg->hdr.len > av)
942 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
943 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
946 else if ((imsg->data = malloc(datalen)) == NULL)
949 if (imsg->hdr.flags & IMSGF_HASFD)
950 imsg->fd = imsg_get_fd(ibuf);
955 memcpy(imsg->data, ibuf->r.rptr, datalen);
957 if (imsg->hdr.len < av) {
958 left = av - imsg->hdr.len;
959 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
964 return (datalen + IMSG_HEADER_SIZE);
968 imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
969 int fd, const void *data, uint16_t datalen)
973 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
976 if (imsg_add(wbuf, data, datalen) == -1)
979 ibuf_fd_set(wbuf, fd);
980 imsg_close(ibuf, wbuf);
986 imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
987 int fd, const struct iovec *iov, int iovcnt)
992 for (i = 0; i < iovcnt; i++)
993 datalen += iov[i].iov_len;
995 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
998 for (i = 0; i < iovcnt; i++)
999 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
1002 ibuf_fd_set(wbuf, fd);
1003 imsg_close(ibuf, wbuf);
1009 imsg_compose_ibuf(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
1010 pid_t pid, struct ibuf *buf)
1012 struct ibuf *wbuf = NULL;
1013 struct imsg_hdr hdr;
1016 if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
1022 hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
1024 hdr.peerid = peerid;
1025 if ((hdr.pid = pid) == 0)
1026 hdr.pid = ibuf->pid;
1028 if ((wbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
1030 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
1033 ibuf_close(&ibuf->w, wbuf);
1034 ibuf_close(&ibuf->w, buf);
1046 imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
1050 struct imsg_hdr hdr;
1052 datalen += IMSG_HEADER_SIZE;
1053 if (datalen > MAX_IMSGSIZE) {
1060 hdr.peerid = peerid;
1061 if ((hdr.pid = pid) == 0)
1062 hdr.pid = ibuf->pid;
1063 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
1066 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
1073 imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
1076 if (ibuf_add(msg, data, datalen) == -1) {
1084 imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
1086 struct imsg_hdr *hdr;
1088 hdr = (struct imsg_hdr *)msg->buf;
1090 hdr->flags &= ~IMSGF_HASFD;
1091 if (ibuf_fd_avail(msg))
1092 hdr->flags |= IMSGF_HASFD;
1093 hdr->len = ibuf_size(msg);
1095 ibuf_close(&ibuf->w, msg);
1099 imsg_free(struct imsg *imsg)
1101 freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
1105 imsg_get_fd(struct imsgbuf *ibuf)
1108 struct imsg_fd *ifd;
1110 if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
1114 TAILQ_REMOVE(&ibuf->fds, ifd, entry);
1121 imsg_flush(struct imsgbuf *ibuf)
1123 while (ibuf->w.queued)
1124 if (msgbuf_write(&ibuf->w) <= 0)
1130 imsg_clear(struct imsgbuf *ibuf)
1134 msgbuf_clear(&ibuf->w);
1135 while ((fd = imsg_get_fd(ibuf)) != -1)
1138 #endif /* HAVE_LIB_IMSG */
1141 * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
1143 * Redistribution and use in source and binary forms, with or without
1144 * modification, are permitted provided that the following conditions
1146 * 1. Redistributions of source code must retain the above copyright
1147 * notice, this list of conditions and the following disclaimer.
1148 * 2. Redistributions in binary form must reproduce the above copyright
1149 * notice, this list of conditions and the following disclaimer in
1150 * the documentation and/or other materials provided with the
1152 * 3. The name of the author may not be used to endorse or promote
1153 * products derived from this software without specific prior written
1156 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
1157 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1158 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1159 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
1160 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1161 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1162 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1163 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
1164 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1165 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1166 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1169 * Find the first occurrence of the byte string s in byte string l.
1172 memmem(const void *l, size_t l_len, const void *s, size_t s_len)
1174 const char *cur, *last;
1178 /* a zero length needle should just return the haystack */
1182 /* "s" must be smaller or equal to "l" */
1186 /* special case where s_len == 1 */
1188 return memchr(l, *cs, l_len);
1190 /* the last position where its possible to find "s" in "l" */
1191 last = cl + l_len - s_len;
1193 for (cur = cl; cur <= last; cur++)
1194 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
1199 #endif /* !HAVE_MEMMEM */
1202 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
1204 * Permission to use, copy, modify, and distribute this software for any
1205 * purpose with or without fee is hereby granted, provided that the above
1206 * copyright notice and this permission notice appear in all copies.
1208 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1209 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1210 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1211 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1212 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1213 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1214 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1222 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
1225 memrchr(const void *s, int c, size_t n)
1227 const unsigned char *cp;
1230 cp = (unsigned char *)s + n;
1232 if (*(--cp) == (unsigned char)c)
1238 #endif /* !HAVE_MEMRCHR */
1241 * Copyright (c) 1987, 1993, 1994
1242 * The Regents of the University of California. All rights reserved.
1244 * Redistribution and use in source and binary forms, with or without
1245 * modification, are permitted provided that the following conditions
1247 * 1. Redistributions of source code must retain the above copyright
1248 * notice, this list of conditions and the following disclaimer.
1249 * 2. Redistributions in binary form must reproduce the above copyright
1250 * notice, this list of conditions and the following disclaimer in the
1251 * documentation and/or other materials provided with the distribution.
1252 * 3. Neither the name of the University nor the names of its contributors
1253 * may be used to endorse or promote products derived from this software
1254 * without specific prior written permission.
1256 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1257 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1258 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1259 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1260 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1261 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1262 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1263 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1264 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1265 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1269 /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
1275 int BSDopterr = 1, /* if error message should be printed */
1276 BSDoptind = 1, /* index into parent argv vector */
1277 BSDoptopt, /* character checked for validity */
1278 BSDoptreset; /* reset getopt */
1279 char *BSDoptarg; /* argument associated with option */
1281 #define BADCH (int)'?'
1282 #define BADARG (int)':'
1287 * Parse argc/argv argument vector.
1290 BSDgetopt(int nargc, char *const *nargv, const char *ostr)
1292 static const char *place = EMSG; /* option letter processing */
1293 char *oli; /* option letter list index */
1298 if (BSDoptreset || !*place) { /* update scanning pointer */
1300 if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
1304 if (place[1] && *++place == '-') { /* found "--" */
1311 } /* option letter okay? */
1312 if ((BSDoptopt = (int)*place++) == (int)':' ||
1313 !(oli = strchr(ostr, BSDoptopt))) {
1315 * if the user didn't specify '-' as an option,
1316 * assume it means -1.
1318 if (BSDoptopt == (int)'-')
1322 if (BSDopterr && *ostr != ':')
1323 (void)fprintf(stderr,
1324 "%s: unknown option -- %c\n", getprogname(),
1328 if (*++oli != ':') { /* don't need argument */
1333 else { /* need an argument */
1334 if (*place) /* no white space */
1335 BSDoptarg = (char *)place;
1336 else if (nargc <= ++BSDoptind) { /* no arg */
1341 (void)fprintf(stderr,
1342 "%s: option requires an argument -- %c\n",
1343 getprogname(), BSDoptopt);
1346 else /* white space */
1347 BSDoptarg = nargv[BSDoptind];
1351 return (BSDoptopt); /* dump back option letter */
1354 #if !HAVE_REALLOCARRAY
1356 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
1358 * Permission to use, copy, modify, and distribute this software for any
1359 * purpose with or without fee is hereby granted, provided that the above
1360 * copyright notice and this permission notice appear in all copies.
1362 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1363 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1364 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1365 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1366 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1367 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1368 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1371 #include <sys/types.h>
1377 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1378 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1380 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1383 reallocarray(void *optr, size_t nmemb, size_t size)
1385 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1386 nmemb > 0 && SIZE_MAX / nmemb < size) {
1390 return realloc(optr, size * nmemb);
1392 #endif /* !HAVE_REALLOCARRAY */
1393 #if !HAVE_RECALLOCARRAY
1395 * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
1397 * Permission to use, copy, modify, and distribute this software for any
1398 * purpose with or without fee is hereby granted, provided that the above
1399 * copyright notice and this permission notice appear in all copies.
1401 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1402 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1403 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1404 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1405 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1406 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1407 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1410 /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
1419 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1420 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1422 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1425 recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
1427 size_t oldsize, newsize;
1431 return calloc(newnmemb, size);
1433 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1434 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
1438 newsize = newnmemb * size;
1440 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1441 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
1445 oldsize = oldnmemb * size;
1448 * Don't bother too much if we're shrinking just a bit,
1449 * we do not shrink for series of small steps, oh well.
1451 if (newsize <= oldsize) {
1452 size_t d = oldsize - newsize;
1454 if (d < oldsize / 2 && d < (size_t)getpagesize()) {
1455 memset((char *)ptr + newsize, 0, d);
1460 newptr = malloc(newsize);
1464 if (newsize > oldsize) {
1465 memcpy(newptr, ptr, oldsize);
1466 memset((char *)newptr + oldsize, 0, newsize - oldsize);
1468 memcpy(newptr, ptr, newsize);
1470 explicit_bzero(ptr, oldsize);
1475 #endif /* !HAVE_RECALLOCARRAY */
1476 #if !HAVE_SETPROCTITLE
1478 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
1480 * Permission to use, copy, modify, and distribute this software for any
1481 * purpose with or without fee is hereby granted, provided that the above
1482 * copyright notice and this permission notice appear in all copies.
1484 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1485 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1486 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1487 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1488 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1489 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1490 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1493 # if HAVE_PR_SET_NAME
1494 #include <sys/types.h>
1498 #include <sys/prctl.h>
1500 setproctitle(const char *fmt, ...)
1502 char title[16], name[16], *cp;
1507 vsnprintf(title, sizeof(title), fmt, ap);
1510 used = snprintf(name, sizeof(name), "%s: %s", getprogname(), title);
1511 if (used >= (int)sizeof(name)) {
1512 cp = strrchr(name, ' ');
1516 prctl(PR_SET_NAME, name);
1520 setproctitle(const char *fmt, ...)
1525 #endif /* !HAVE_SETPROCTITLE */
1528 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1530 * Permission to use, copy, modify, and distribute this software for any
1531 * purpose with or without fee is hereby granted, provided that the above
1532 * copyright notice and this permission notice appear in all copies.
1534 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1535 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1536 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1537 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1538 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1539 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1540 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1543 #include <sys/types.h>
1547 * Appends src to string dst of size siz (unlike strncat, siz is the
1548 * full size of dst, not space left). At most siz-1 characters
1549 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
1550 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
1551 * If retval >= siz, truncation occurred.
1554 strlcat(char *dst, const char *src, size_t siz)
1557 const char *s = src;
1561 /* Find the end of dst and adjust bytes left but don't go past end */
1562 while (n-- != 0 && *d != '\0')
1568 return(dlen + strlen(s));
1569 while (*s != '\0') {
1578 return(dlen + (s - src)); /* count does not include NUL */
1580 #endif /* !HAVE_STRLCAT */
1583 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1585 * Permission to use, copy, modify, and distribute this software for any
1586 * purpose with or without fee is hereby granted, provided that the above
1587 * copyright notice and this permission notice appear in all copies.
1589 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1590 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1591 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1592 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1593 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1594 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1595 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1598 #include <sys/types.h>
1602 * Copy src to string dst of size siz. At most siz-1 characters
1603 * will be copied. Always NUL terminates (unless siz == 0).
1604 * Returns strlen(src); if retval >= siz, truncation occurred.
1607 strlcpy(char *dst, const char *src, size_t siz)
1610 const char *s = src;
1613 /* Copy as many bytes as will fit */
1616 if ((*d++ = *s++) == '\0')
1621 /* Not enough room in dst, add NUL and traverse rest of src */
1624 *d = '\0'; /* NUL-terminate dst */
1629 return(s - src - 1); /* count does not include NUL */
1631 #endif /* !HAVE_STRLCPY */
1635 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1637 * Permission to use, copy, modify, and distribute this software for any
1638 * purpose with or without fee is hereby granted, provided that the above
1639 * copyright notice and this permission notice appear in all copies.
1641 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1642 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1643 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1644 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1645 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1646 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1647 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1650 #include <sys/types.h>
1657 strndup(const char *str, size_t maxlen)
1662 len = strnlen(str, maxlen);
1663 copy = malloc(len + 1);
1665 (void)memcpy(copy, str, len);
1671 #endif /* !HAVE_STRNDUP */
1676 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1678 * Permission to use, copy, modify, and distribute this software for any
1679 * purpose with or without fee is hereby granted, provided that the above
1680 * copyright notice and this permission notice appear in all copies.
1682 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1683 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1684 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1685 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1686 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1687 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1688 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1691 #include <sys/types.h>
1695 strnlen(const char *str, size_t maxlen)
1699 for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
1702 return (size_t)(cp - str);
1704 #endif /* !HAVE_STRNLEN */
1707 * Copyright (c) 2004 Ted Unangst and Todd Miller
1708 * All rights reserved.
1710 * Permission to use, copy, modify, and distribute this software for any
1711 * purpose with or without fee is hereby granted, provided that the above
1712 * copyright notice and this permission notice appear in all copies.
1714 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1715 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1716 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1717 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1718 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1719 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1720 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1732 strtonum(const char *numstr, long long minval, long long maxval,
1733 const char **errstrp)
1743 { "invalid", EINVAL },
1744 { "too small", ERANGE },
1745 { "too large", ERANGE },
1750 if (minval > maxval) {
1753 ll = strtoll(numstr, &ep, 10);
1754 if (numstr == ep || *ep != '\0')
1756 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
1758 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
1761 if (errstrp != NULL)
1762 *errstrp = ev[error].errstr;
1763 errno = ev[error].err;
1769 #endif /* !HAVE_STRTONUM */
1770 #if !HAVE_TIMESPECSUB
1771 #include <sys/time.h>
1773 timespecsub(struct timespec *a, struct timespec *b, struct timespec *ret)
1775 ret->tv_sec = a->tv_sec - b->tv_sec;
1776 ret->tv_nsec = a->tv_nsec - b->tv_nsec;
1777 if (ret->tv_nsec < 0) {
1779 ret->tv_nsec += 1000000000L;
1782 #endif /* !HAVE_TIMESPECSUB */