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.18 2023/12/12 15:47:41 claudio Exp $ */
337 /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
340 * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
341 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
343 * Permission to use, copy, modify, and distribute this software for any
344 * purpose with or without fee is hereby granted, provided that the above
345 * copyright notice and this permission notice appear in all copies.
347 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
348 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
349 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
350 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
351 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
352 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
353 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
356 #include <sys/types.h>
357 #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)
401 if (max == 0 || max < len) {
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 || buf->max == 0) {
451 if (buf->wpos + len > buf->size)
452 if (ibuf_realloc(buf, len) == -1)
455 b = buf->buf + buf->wpos;
461 ibuf_add(struct ibuf *buf, const void *data, size_t len)
465 if ((b = ibuf_reserve(buf, len)) == NULL)
468 memcpy(b, data, len);
473 ibuf_add_ibuf(struct ibuf *buf, const struct ibuf *from)
475 return ibuf_add(buf, ibuf_data(from), ibuf_size(from));
478 /* remove after tree is converted */
480 ibuf_add_buf(struct ibuf *buf, const struct ibuf *from)
482 return ibuf_add_ibuf(buf, from);
486 ibuf_add_n8(struct ibuf *buf, uint64_t value)
490 if (value > UINT8_MAX) {
495 return ibuf_add(buf, &v, sizeof(v));
499 ibuf_add_n16(struct ibuf *buf, uint64_t value)
503 if (value > UINT16_MAX) {
508 return ibuf_add(buf, &v, sizeof(v));
512 ibuf_add_n32(struct ibuf *buf, uint64_t value)
516 if (value > UINT32_MAX) {
521 return ibuf_add(buf, &v, sizeof(v));
525 ibuf_add_n64(struct ibuf *buf, uint64_t value)
527 value = htobe64(value);
528 return ibuf_add(buf, &value, sizeof(value));
532 ibuf_add_h16(struct ibuf *buf, uint64_t value)
536 if (value > UINT16_MAX) {
541 return ibuf_add(buf, &v, sizeof(v));
545 ibuf_add_h32(struct ibuf *buf, uint64_t value)
549 if (value > UINT32_MAX) {
554 return ibuf_add(buf, &v, sizeof(v));
558 ibuf_add_h64(struct ibuf *buf, uint64_t value)
560 return ibuf_add(buf, &value, sizeof(value));
564 ibuf_add_zero(struct ibuf *buf, size_t len)
568 if ((b = ibuf_reserve(buf, len)) == NULL)
575 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
577 /* only allow seeking between rpos and wpos */
578 if (ibuf_size(buf) < pos || SIZE_MAX - pos < len ||
579 ibuf_size(buf) < pos + len) {
584 return (buf->buf + buf->rpos + pos);
588 ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
592 if ((b = ibuf_seek(buf, pos, len)) == NULL)
595 memcpy(b, data, len);
600 ibuf_set_n8(struct ibuf *buf, size_t pos, uint64_t value)
604 if (value > UINT8_MAX) {
609 return (ibuf_set(buf, pos, &v, sizeof(v)));
613 ibuf_set_n16(struct ibuf *buf, size_t pos, uint64_t value)
617 if (value > UINT16_MAX) {
622 return (ibuf_set(buf, pos, &v, sizeof(v)));
626 ibuf_set_n32(struct ibuf *buf, size_t pos, uint64_t value)
630 if (value > UINT32_MAX) {
635 return (ibuf_set(buf, pos, &v, sizeof(v)));
639 ibuf_set_n64(struct ibuf *buf, size_t pos, uint64_t value)
641 value = htobe64(value);
642 return (ibuf_set(buf, pos, &value, sizeof(value)));
646 ibuf_set_h16(struct ibuf *buf, size_t pos, uint64_t value)
650 if (value > UINT16_MAX) {
655 return (ibuf_set(buf, pos, &v, sizeof(v)));
659 ibuf_set_h32(struct ibuf *buf, size_t pos, uint64_t value)
663 if (value > UINT32_MAX) {
668 return (ibuf_set(buf, pos, &v, sizeof(v)));
672 ibuf_set_h64(struct ibuf *buf, size_t pos, uint64_t value)
674 return (ibuf_set(buf, pos, &value, sizeof(value)));
678 ibuf_data(const struct ibuf *buf)
680 return (buf->buf + buf->rpos);
684 ibuf_size(const struct ibuf *buf)
686 return (buf->wpos - buf->rpos);
690 ibuf_left(const struct ibuf *buf)
694 return (buf->max - buf->wpos);
698 ibuf_truncate(struct ibuf *buf, size_t len)
700 if (ibuf_size(buf) >= len) {
701 buf->wpos = buf->rpos + len;
705 /* only allow to truncate down */
709 return ibuf_add_zero(buf, len - ibuf_size(buf));
713 ibuf_rewind(struct ibuf *buf)
719 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
721 ibuf_enqueue(msgbuf, buf);
725 ibuf_from_buffer(struct ibuf *buf, void *data, size_t len)
727 memset(buf, 0, sizeof(*buf));
729 buf->size = buf->wpos = len;
734 ibuf_from_ibuf(struct ibuf *buf, const struct ibuf *from)
736 ibuf_from_buffer(buf, ibuf_data(from), ibuf_size(from));
740 ibuf_get(struct ibuf *buf, void *data, size_t len)
742 if (ibuf_size(buf) < len) {
747 memcpy(data, ibuf_data(buf), len);
753 ibuf_get_ibuf(struct ibuf *buf, size_t len, struct ibuf *new)
755 if (ibuf_size(buf) < len) {
760 ibuf_from_buffer(new, ibuf_data(buf), len);
766 ibuf_get_n8(struct ibuf *buf, uint8_t *value)
768 return ibuf_get(buf, value, sizeof(*value));
772 ibuf_get_n16(struct ibuf *buf, uint16_t *value)
776 rv = ibuf_get(buf, value, sizeof(*value));
777 *value = be16toh(*value);
782 ibuf_get_n32(struct ibuf *buf, uint32_t *value)
786 rv = ibuf_get(buf, value, sizeof(*value));
787 *value = be32toh(*value);
792 ibuf_get_n64(struct ibuf *buf, uint64_t *value)
796 rv = ibuf_get(buf, value, sizeof(*value));
797 *value = be64toh(*value);
802 ibuf_get_h16(struct ibuf *buf, uint16_t *value)
804 return ibuf_get(buf, value, sizeof(*value));
808 ibuf_get_h32(struct ibuf *buf, uint32_t *value)
810 return ibuf_get(buf, value, sizeof(*value));
814 ibuf_get_h64(struct ibuf *buf, uint64_t *value)
816 return ibuf_get(buf, value, sizeof(*value));
820 ibuf_skip(struct ibuf *buf, size_t len)
822 if (ibuf_size(buf) < len) {
832 ibuf_free(struct ibuf *buf)
836 if (buf->max == 0) /* if buf lives on the stack */
837 abort(); /* abort before causing more harm */
840 freezero(buf->buf, buf->size);
845 ibuf_fd_avail(struct ibuf *buf)
847 return (buf->fd != -1);
851 ibuf_fd_get(struct ibuf *buf)
861 ibuf_fd_set(struct ibuf *buf, int fd)
863 if (buf->max == 0) /* if buf lives on the stack */
864 abort(); /* abort before causing more harm */
871 ibuf_write(struct msgbuf *msgbuf)
873 struct iovec iov[IOV_MAX];
878 memset(&iov, 0, sizeof(iov));
879 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
882 iov[i].iov_base = ibuf_data(buf);
883 iov[i].iov_len = ibuf_size(buf);
888 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
891 if (errno == ENOBUFS)
896 if (n == 0) { /* connection closed */
901 msgbuf_drain(msgbuf, n);
907 msgbuf_init(struct msgbuf *msgbuf)
911 TAILQ_INIT(&msgbuf->bufs);
915 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
917 struct ibuf *buf, *next;
919 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
921 next = TAILQ_NEXT(buf, entry);
922 if (n >= ibuf_size(buf)) {
924 ibuf_dequeue(msgbuf, buf);
933 msgbuf_clear(struct msgbuf *msgbuf)
937 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
938 ibuf_dequeue(msgbuf, buf);
942 msgbuf_write(struct msgbuf *msgbuf)
944 struct iovec iov[IOV_MAX];
945 struct ibuf *buf, *buf0 = NULL;
949 struct cmsghdr *cmsg;
952 char buf[CMSG_SPACE(sizeof(int))];
955 memset(&iov, 0, sizeof(iov));
956 memset(&msg, 0, sizeof(msg));
957 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
958 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
961 if (i > 0 && buf->fd != -1)
963 iov[i].iov_base = ibuf_data(buf);
964 iov[i].iov_len = ibuf_size(buf);
974 msg.msg_control = (caddr_t)&cmsgbuf.buf;
975 msg.msg_controllen = sizeof(cmsgbuf.buf);
976 cmsg = CMSG_FIRSTHDR(&msg);
977 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
978 cmsg->cmsg_level = SOL_SOCKET;
979 cmsg->cmsg_type = SCM_RIGHTS;
980 *(int *)CMSG_DATA(cmsg) = buf0->fd;
984 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
987 if (errno == ENOBUFS)
992 if (n == 0) { /* connection closed */
998 * assumption: fd got sent if sendmsg sent anything
999 * this works because fds are passed one at a time
1006 msgbuf_drain(msgbuf, n);
1012 msgbuf_queuelen(struct msgbuf *msgbuf)
1014 return (msgbuf->queued);
1018 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
1020 if (buf->max == 0) /* if buf lives on the stack */
1021 abort(); /* abort before causing more harm */
1022 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
1027 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
1029 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
1037 TAILQ_ENTRY(imsg_fd) entry;
1041 int imsg_fd_overhead = 0;
1043 static int imsg_dequeue_fd(struct imsgbuf *);
1046 imsg_init(struct imsgbuf *imsgbuf, int fd)
1048 msgbuf_init(&imsgbuf->w);
1049 memset(&imsgbuf->r, 0, sizeof(imsgbuf->r));
1052 imsgbuf->pid = getpid();
1053 TAILQ_INIT(&imsgbuf->fds);
1057 imsg_read(struct imsgbuf *imsgbuf)
1060 struct cmsghdr *cmsg;
1063 char buf[CMSG_SPACE(sizeof(int) * 1)];
1068 struct imsg_fd *ifd;
1070 memset(&msg, 0, sizeof(msg));
1071 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
1073 iov.iov_base = imsgbuf->r.buf + imsgbuf->r.wpos;
1074 iov.iov_len = sizeof(imsgbuf->r.buf) - imsgbuf->r.wpos;
1077 msg.msg_control = &cmsgbuf.buf;
1078 msg.msg_controllen = sizeof(cmsgbuf.buf);
1080 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
1084 if (getdtablecount() + imsg_fd_overhead +
1085 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
1086 >= getdtablesize()) {
1092 if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) {
1098 imsgbuf->r.wpos += n;
1100 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
1101 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1102 if (cmsg->cmsg_level == SOL_SOCKET &&
1103 cmsg->cmsg_type == SCM_RIGHTS) {
1108 * We only accept one file descriptor. Due to C
1109 * padding rules, our control buffer might contain
1110 * more than one fd, and we must close them.
1112 j = ((char *)cmsg + cmsg->cmsg_len -
1113 (char *)CMSG_DATA(cmsg)) / sizeof(int);
1114 for (i = 0; i < j; i++) {
1115 fd = ((int *)CMSG_DATA(cmsg))[i];
1118 TAILQ_INSERT_TAIL(&imsgbuf->fds, ifd,
1125 /* we do not handle other ctl data level */
1134 imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
1137 size_t av, left, datalen;
1139 av = imsgbuf->r.wpos;
1141 if (IMSG_HEADER_SIZE > av)
1144 memcpy(&m.hdr, imsgbuf->r.buf, sizeof(m.hdr));
1145 if (m.hdr.len < IMSG_HEADER_SIZE ||
1146 m.hdr.len > MAX_IMSGSIZE) {
1157 datalen = m.hdr.len - IMSG_HEADER_SIZE;
1158 imsgbuf->r.rptr = imsgbuf->r.buf + IMSG_HEADER_SIZE;
1160 if ((m.buf = ibuf_open(datalen)) == NULL)
1162 if (ibuf_add(m.buf, imsgbuf->r.rptr, datalen) == -1) {
1163 /* this should never fail */
1167 m.data = ibuf_data(m.buf);
1170 if (m.hdr.flags & IMSGF_HASFD)
1171 m.fd = imsg_dequeue_fd(imsgbuf);
1173 if (m.hdr.len < av) {
1174 left = av - m.hdr.len;
1175 memmove(&imsgbuf->r.buf, imsgbuf->r.buf + m.hdr.len, left);
1176 imsgbuf->r.wpos = left;
1178 imsgbuf->r.wpos = 0;
1181 return (datalen + IMSG_HEADER_SIZE);
1185 imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
1187 if (imsg->buf == NULL) {
1191 return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
1195 imsg_get_data(struct imsg *imsg, void *data, size_t len)
1201 if (imsg->buf == NULL || ibuf_size(imsg->buf) != len) {
1205 return ibuf_get(imsg->buf, data, len);
1209 imsg_get_fd(struct imsg *imsg)
1218 imsg_get_id(struct imsg *imsg)
1220 return (imsg->hdr.peerid);
1224 imsg_get_len(struct imsg *imsg)
1226 if (imsg->buf == NULL)
1228 return ibuf_size(imsg->buf);
1232 imsg_get_pid(struct imsg *imsg)
1234 return (imsg->hdr.pid);
1238 imsg_get_type(struct imsg *imsg)
1240 return (imsg->hdr.type);
1244 imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
1245 int fd, const void *data, size_t datalen)
1249 if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
1252 if (imsg_add(wbuf, data, datalen) == -1)
1255 ibuf_fd_set(wbuf, fd);
1256 imsg_close(imsgbuf, wbuf);
1262 imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
1263 int fd, const struct iovec *iov, int iovcnt)
1269 for (i = 0; i < iovcnt; i++)
1270 datalen += iov[i].iov_len;
1272 if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
1275 for (i = 0; i < iovcnt; i++)
1276 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
1279 ibuf_fd_set(wbuf, fd);
1280 imsg_close(imsgbuf, wbuf);
1286 * Enqueue imsg with payload from ibuf buf. fd passing is not possible
1287 * with this function.
1290 imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
1291 pid_t pid, struct ibuf *buf)
1293 struct ibuf *hdrbuf = NULL;
1294 struct imsg_hdr hdr;
1297 if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
1303 hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
1306 if ((hdr.pid = pid) == 0)
1307 hdr.pid = imsgbuf->pid;
1309 if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
1311 if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
1314 ibuf_close(&imsgbuf->w, hdrbuf);
1315 ibuf_close(&imsgbuf->w, buf);
1327 * Forward imsg to another channel. Any attached fd is closed.
1330 imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
1335 if (msg->fd != -1) {
1340 if (msg->buf != NULL) {
1341 ibuf_rewind(msg->buf);
1342 len = ibuf_size(msg->buf);
1345 if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
1346 msg->hdr.pid, len)) == NULL)
1349 if (msg->buf != NULL) {
1350 if (ibuf_add_buf(wbuf, msg->buf) == -1) {
1356 imsg_close(imsgbuf, wbuf);
1361 imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
1365 struct imsg_hdr hdr;
1367 datalen += IMSG_HEADER_SIZE;
1368 if (datalen > MAX_IMSGSIZE) {
1376 if ((hdr.pid = pid) == 0)
1377 hdr.pid = imsgbuf->pid;
1378 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
1381 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
1388 imsg_add(struct ibuf *msg, const void *data, size_t datalen)
1391 if (ibuf_add(msg, data, datalen) == -1) {
1399 imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
1401 struct imsg_hdr *hdr;
1403 hdr = (struct imsg_hdr *)msg->buf;
1405 hdr->flags &= ~IMSGF_HASFD;
1406 if (ibuf_fd_avail(msg))
1407 hdr->flags |= IMSGF_HASFD;
1408 hdr->len = ibuf_size(msg);
1410 ibuf_close(&imsgbuf->w, msg);
1414 imsg_free(struct imsg *imsg)
1416 ibuf_free(imsg->buf);
1420 imsg_dequeue_fd(struct imsgbuf *imsgbuf)
1423 struct imsg_fd *ifd;
1425 if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
1429 TAILQ_REMOVE(&imsgbuf->fds, ifd, entry);
1436 imsg_flush(struct imsgbuf *imsgbuf)
1438 while (imsgbuf->w.queued)
1439 if (msgbuf_write(&imsgbuf->w) <= 0)
1445 imsg_clear(struct imsgbuf *imsgbuf)
1449 msgbuf_clear(&imsgbuf->w);
1450 while ((fd = imsg_dequeue_fd(imsgbuf)) != -1)
1453 #endif /* HAVE_LIB_IMSG */
1456 * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
1458 * Redistribution and use in source and binary forms, with or without
1459 * modification, are permitted provided that the following conditions
1461 * 1. Redistributions of source code must retain the above copyright
1462 * notice, this list of conditions and the following disclaimer.
1463 * 2. Redistributions in binary form must reproduce the above copyright
1464 * notice, this list of conditions and the following disclaimer in
1465 * the documentation and/or other materials provided with the
1467 * 3. The name of the author may not be used to endorse or promote
1468 * products derived from this software without specific prior written
1471 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
1472 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1473 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1474 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
1475 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1476 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1477 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1478 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
1479 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1480 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1481 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1484 * Find the first occurrence of the byte string s in byte string l.
1487 memmem(const void *l, size_t l_len, const void *s, size_t s_len)
1489 const char *cur, *last;
1493 /* a zero length needle should just return the haystack */
1497 /* "s" must be smaller or equal to "l" */
1501 /* special case where s_len == 1 */
1503 return memchr(l, *cs, l_len);
1505 /* the last position where its possible to find "s" in "l" */
1506 last = cl + l_len - s_len;
1508 for (cur = cl; cur <= last; cur++)
1509 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
1514 #endif /* !HAVE_MEMMEM */
1517 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
1519 * Permission to use, copy, modify, and distribute this software for any
1520 * purpose with or without fee is hereby granted, provided that the above
1521 * copyright notice and this permission notice appear in all copies.
1523 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1524 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1525 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1526 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1527 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1528 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1529 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1537 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
1540 memrchr(const void *s, int c, size_t n)
1542 const unsigned char *cp;
1545 cp = (unsigned char *)s + n;
1547 if (*(--cp) == (unsigned char)c)
1553 #endif /* !HAVE_MEMRCHR */
1556 * Copyright (c) 1987, 1993, 1994
1557 * The Regents of the University of California. All rights reserved.
1559 * Redistribution and use in source and binary forms, with or without
1560 * modification, are permitted provided that the following conditions
1562 * 1. Redistributions of source code must retain the above copyright
1563 * notice, this list of conditions and the following disclaimer.
1564 * 2. Redistributions in binary form must reproduce the above copyright
1565 * notice, this list of conditions and the following disclaimer in the
1566 * documentation and/or other materials provided with the distribution.
1567 * 3. Neither the name of the University nor the names of its contributors
1568 * may be used to endorse or promote products derived from this software
1569 * without specific prior written permission.
1571 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1572 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1573 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1574 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1575 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1576 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1577 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1578 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1579 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1580 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1584 /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
1590 int BSDopterr = 1, /* if error message should be printed */
1591 BSDoptind = 1, /* index into parent argv vector */
1592 BSDoptopt, /* character checked for validity */
1593 BSDoptreset; /* reset getopt */
1594 char *BSDoptarg; /* argument associated with option */
1596 #define BADCH (int)'?'
1597 #define BADARG (int)':'
1602 * Parse argc/argv argument vector.
1605 BSDgetopt(int nargc, char *const *nargv, const char *ostr)
1607 static const char *place = EMSG; /* option letter processing */
1608 char *oli; /* option letter list index */
1613 if (BSDoptreset || !*place) { /* update scanning pointer */
1615 if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
1619 if (place[1] && *++place == '-') { /* found "--" */
1626 } /* option letter okay? */
1627 if ((BSDoptopt = (int)*place++) == (int)':' ||
1628 !(oli = strchr(ostr, BSDoptopt))) {
1630 * if the user didn't specify '-' as an option,
1631 * assume it means -1.
1633 if (BSDoptopt == (int)'-')
1637 if (BSDopterr && *ostr != ':')
1638 (void)fprintf(stderr,
1639 "%s: unknown option -- %c\n", getprogname(),
1643 if (*++oli != ':') { /* don't need argument */
1648 else { /* need an argument */
1649 if (*place) /* no white space */
1650 BSDoptarg = (char *)place;
1651 else if (nargc <= ++BSDoptind) { /* no arg */
1656 (void)fprintf(stderr,
1657 "%s: option requires an argument -- %c\n",
1658 getprogname(), BSDoptopt);
1661 else /* white space */
1662 BSDoptarg = nargv[BSDoptind];
1666 return (BSDoptopt); /* dump back option letter */
1669 #if !HAVE_REALLOCARRAY
1671 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
1673 * Permission to use, copy, modify, and distribute this software for any
1674 * purpose with or without fee is hereby granted, provided that the above
1675 * copyright notice and this permission notice appear in all copies.
1677 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1678 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1679 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1680 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1681 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1682 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1683 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1686 #include <sys/types.h>
1692 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1693 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1695 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1698 reallocarray(void *optr, size_t nmemb, size_t size)
1700 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1701 nmemb > 0 && SIZE_MAX / nmemb < size) {
1705 return realloc(optr, size * nmemb);
1707 #endif /* !HAVE_REALLOCARRAY */
1708 #if !HAVE_RECALLOCARRAY
1710 * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
1712 * Permission to use, copy, modify, and distribute this software for any
1713 * purpose with or without fee is hereby granted, provided that the above
1714 * copyright notice and this permission notice appear in all copies.
1716 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1717 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1718 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1719 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1720 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1721 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1722 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1725 /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
1734 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1735 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1737 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1740 recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
1742 size_t oldsize, newsize;
1746 return calloc(newnmemb, size);
1748 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1749 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
1753 newsize = newnmemb * size;
1755 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1756 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
1760 oldsize = oldnmemb * size;
1763 * Don't bother too much if we're shrinking just a bit,
1764 * we do not shrink for series of small steps, oh well.
1766 if (newsize <= oldsize) {
1767 size_t d = oldsize - newsize;
1769 if (d < oldsize / 2 && d < (size_t)getpagesize()) {
1770 memset((char *)ptr + newsize, 0, d);
1775 newptr = malloc(newsize);
1779 if (newsize > oldsize) {
1780 memcpy(newptr, ptr, oldsize);
1781 memset((char *)newptr + oldsize, 0, newsize - oldsize);
1783 memcpy(newptr, ptr, newsize);
1785 explicit_bzero(ptr, oldsize);
1790 #endif /* !HAVE_RECALLOCARRAY */
1791 #if !HAVE_SETPROCTITLE
1793 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
1795 * Permission to use, copy, modify, and distribute this software for any
1796 * purpose with or without fee is hereby granted, provided that the above
1797 * copyright notice and this permission notice appear in all copies.
1799 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1800 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1801 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1802 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1803 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1804 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1805 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1808 # if HAVE_PR_SET_NAME
1809 #include <sys/types.h>
1813 #include <sys/prctl.h>
1815 setproctitle(const char *fmt, ...)
1817 char title[16], name[16], *cp;
1822 vsnprintf(title, sizeof(title), fmt, ap);
1825 used = snprintf(name, sizeof(name), "%s: %s", getprogname(), title);
1826 if (used >= (int)sizeof(name)) {
1827 cp = strrchr(name, ' ');
1831 prctl(PR_SET_NAME, name);
1835 setproctitle(const char *fmt, ...)
1840 #endif /* !HAVE_SETPROCTITLE */
1843 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1845 * Permission to use, copy, modify, and distribute this software for any
1846 * purpose with or without fee is hereby granted, provided that the above
1847 * copyright notice and this permission notice appear in all copies.
1849 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1850 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1851 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1852 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1853 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1854 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1855 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1858 #include <sys/types.h>
1862 * Appends src to string dst of size siz (unlike strncat, siz is the
1863 * full size of dst, not space left). At most siz-1 characters
1864 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
1865 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
1866 * If retval >= siz, truncation occurred.
1869 strlcat(char *dst, const char *src, size_t siz)
1872 const char *s = src;
1876 /* Find the end of dst and adjust bytes left but don't go past end */
1877 while (n-- != 0 && *d != '\0')
1883 return(dlen + strlen(s));
1884 while (*s != '\0') {
1893 return(dlen + (s - src)); /* count does not include NUL */
1895 #endif /* !HAVE_STRLCAT */
1898 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1900 * Permission to use, copy, modify, and distribute this software for any
1901 * purpose with or without fee is hereby granted, provided that the above
1902 * copyright notice and this permission notice appear in all copies.
1904 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1905 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1906 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1907 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1908 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1909 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1910 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1913 #include <sys/types.h>
1917 * Copy src to string dst of size siz. At most siz-1 characters
1918 * will be copied. Always NUL terminates (unless siz == 0).
1919 * Returns strlen(src); if retval >= siz, truncation occurred.
1922 strlcpy(char *dst, const char *src, size_t siz)
1925 const char *s = src;
1928 /* Copy as many bytes as will fit */
1931 if ((*d++ = *s++) == '\0')
1936 /* Not enough room in dst, add NUL and traverse rest of src */
1939 *d = '\0'; /* NUL-terminate dst */
1944 return(s - src - 1); /* count does not include NUL */
1946 #endif /* !HAVE_STRLCPY */
1950 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1952 * Permission to use, copy, modify, and distribute this software for any
1953 * purpose with or without fee is hereby granted, provided that the above
1954 * copyright notice and this permission notice appear in all copies.
1956 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1957 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1958 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1959 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1960 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1961 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1962 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1965 #include <sys/types.h>
1972 strndup(const char *str, size_t maxlen)
1977 len = strnlen(str, maxlen);
1978 copy = malloc(len + 1);
1980 (void)memcpy(copy, str, len);
1986 #endif /* !HAVE_STRNDUP */
1991 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1993 * Permission to use, copy, modify, and distribute this software for any
1994 * purpose with or without fee is hereby granted, provided that the above
1995 * copyright notice and this permission notice appear in all copies.
1997 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1998 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1999 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2000 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2001 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2002 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2003 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2006 #include <sys/types.h>
2010 strnlen(const char *str, size_t maxlen)
2014 for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
2017 return (size_t)(cp - str);
2019 #endif /* !HAVE_STRNLEN */
2022 * Copyright (c) 2004 Ted Unangst and Todd Miller
2023 * All rights reserved.
2025 * Permission to use, copy, modify, and distribute this software for any
2026 * purpose with or without fee is hereby granted, provided that the above
2027 * copyright notice and this permission notice appear in all copies.
2029 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2030 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2031 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2032 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2033 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2034 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2035 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2047 strtonum(const char *numstr, long long minval, long long maxval,
2048 const char **errstrp)
2058 { "invalid", EINVAL },
2059 { "too small", ERANGE },
2060 { "too large", ERANGE },
2065 if (minval > maxval) {
2068 ll = strtoll(numstr, &ep, 10);
2069 if (numstr == ep || *ep != '\0')
2071 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
2073 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
2076 if (errstrp != NULL)
2077 *errstrp = ev[error].errstr;
2078 errno = ev[error].err;
2084 #endif /* !HAVE_STRTONUM */
2085 #if !HAVE_TIMESPECSUB
2086 #include <sys/time.h>
2088 timespecsub(struct timespec *a, struct timespec *b, struct timespec *ret)
2090 ret->tv_sec = a->tv_sec - b->tv_sec;
2091 ret->tv_nsec = a->tv_nsec - b->tv_nsec;
2092 if (ret->tv_nsec < 0) {
2094 ret->tv_nsec += 1000000000L;
2097 #endif /* !HAVE_TIMESPECSUB */