1 f36fd90a 2022-07-09 op #include "config.h"
4 f36fd90a 2022-07-09 op * Copyright (c) 1993
5 f36fd90a 2022-07-09 op * The Regents of the University of California. All rights reserved.
7 f36fd90a 2022-07-09 op * Redistribution and use in source and binary forms, with or without
8 f36fd90a 2022-07-09 op * modification, are permitted provided that the following conditions
10 f36fd90a 2022-07-09 op * 1. Redistributions of source code must retain the above copyright
11 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer.
12 f36fd90a 2022-07-09 op * 2. Redistributions in binary form must reproduce the above copyright
13 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer in the
14 f36fd90a 2022-07-09 op * documentation and/or other materials provided with the distribution.
15 f36fd90a 2022-07-09 op * 3. Neither the name of the University nor the names of its contributors
16 f36fd90a 2022-07-09 op * may be used to endorse or promote products derived from this software
17 f36fd90a 2022-07-09 op * without specific prior written permission.
19 f36fd90a 2022-07-09 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 f36fd90a 2022-07-09 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 f36fd90a 2022-07-09 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 f36fd90a 2022-07-09 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 f36fd90a 2022-07-09 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 f36fd90a 2022-07-09 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 f36fd90a 2022-07-09 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 f36fd90a 2022-07-09 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 f36fd90a 2022-07-09 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 f36fd90a 2022-07-09 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 f36fd90a 2022-07-09 op * SUCH DAMAGE.
32 f36fd90a 2022-07-09 op #include <errno.h>
33 f36fd90a 2022-07-09 op #include <stdarg.h>
34 f36fd90a 2022-07-09 op #include <stdio.h>
35 f36fd90a 2022-07-09 op #include <stdlib.h>
36 f36fd90a 2022-07-09 op #include <string.h>
39 f36fd90a 2022-07-09 op vwarnx(const char *fmt, va_list ap)
41 f36fd90a 2022-07-09 op fprintf(stderr, "%s: ", getprogname());
42 f36fd90a 2022-07-09 op if (fmt != NULL)
43 f36fd90a 2022-07-09 op vfprintf(stderr, fmt, ap);
44 f36fd90a 2022-07-09 op fprintf(stderr, "\n");
48 f36fd90a 2022-07-09 op vwarnc(int code, const char *fmt, va_list ap)
50 f36fd90a 2022-07-09 op fprintf(stderr, "%s: ", getprogname());
51 f36fd90a 2022-07-09 op if (fmt != NULL) {
52 f36fd90a 2022-07-09 op vfprintf(stderr, fmt, ap);
53 f36fd90a 2022-07-09 op fprintf(stderr, ": ");
55 f36fd90a 2022-07-09 op fprintf(stderr, "%s\n", strerror(code));
59 f36fd90a 2022-07-09 op vwarn(const char *fmt, va_list ap)
63 f36fd90a 2022-07-09 op sverrno = errno;
64 f36fd90a 2022-07-09 op fprintf(stderr, "%s: ", getprogname());
65 f36fd90a 2022-07-09 op if (fmt != NULL) {
66 f36fd90a 2022-07-09 op vfprintf(stderr, fmt, ap);
67 f36fd90a 2022-07-09 op fprintf(stderr, ": ");
69 f36fd90a 2022-07-09 op fprintf(stderr, "%s\n", strerror(sverrno));
73 f36fd90a 2022-07-09 op verrc(int eval, int code, const char *fmt, va_list ap)
75 f36fd90a 2022-07-09 op fprintf(stderr, "%s: ", getprogname());
76 f36fd90a 2022-07-09 op if (fmt != NULL) {
77 f36fd90a 2022-07-09 op vfprintf(stderr, fmt, ap);
78 f36fd90a 2022-07-09 op fprintf(stderr, ": ");
80 f36fd90a 2022-07-09 op fprintf(stderr, "%s\n", strerror(code));
85 f36fd90a 2022-07-09 op verrx(int eval, const char *fmt, va_list ap)
87 f36fd90a 2022-07-09 op fprintf(stderr, "%s: ", getprogname());
88 f36fd90a 2022-07-09 op if (fmt != NULL)
89 f36fd90a 2022-07-09 op vfprintf(stderr, fmt, ap);
90 f36fd90a 2022-07-09 op fprintf(stderr, "\n");
95 f36fd90a 2022-07-09 op verr(int eval, const char *fmt, va_list ap)
99 f36fd90a 2022-07-09 op sverrno = errno;
100 f36fd90a 2022-07-09 op fprintf(stderr, "%s: ", getprogname());
101 f36fd90a 2022-07-09 op if (fmt != NULL) {
102 f36fd90a 2022-07-09 op vfprintf(stderr, fmt, ap);
103 f36fd90a 2022-07-09 op fprintf(stderr, ": ");
105 f36fd90a 2022-07-09 op fprintf(stderr, "%s\n", strerror(sverrno));
110 f36fd90a 2022-07-09 op err(int eval, const char *fmt, ...)
114 f36fd90a 2022-07-09 op va_start(ap, fmt);
115 f36fd90a 2022-07-09 op verr(eval, fmt, ap);
120 f36fd90a 2022-07-09 op errc(int eval, int code, const char *fmt, ...)
124 f36fd90a 2022-07-09 op va_start(ap, fmt);
125 f36fd90a 2022-07-09 op verrc(eval, code, fmt, ap);
130 f36fd90a 2022-07-09 op errx(int eval, const char *fmt, ...)
134 f36fd90a 2022-07-09 op va_start(ap, fmt);
135 f36fd90a 2022-07-09 op verrx(eval, fmt, ap);
140 f36fd90a 2022-07-09 op warn(const char *fmt, ...)
144 f36fd90a 2022-07-09 op va_start(ap, fmt);
145 f36fd90a 2022-07-09 op vwarn(fmt, ap);
150 f36fd90a 2022-07-09 op warnc(int code, const char *fmt, ...)
154 f36fd90a 2022-07-09 op va_start(ap, fmt);
155 f36fd90a 2022-07-09 op vwarnc(code, fmt, ap);
160 f36fd90a 2022-07-09 op warnx(const char *fmt, ...)
164 f36fd90a 2022-07-09 op va_start(ap, fmt);
165 f36fd90a 2022-07-09 op vwarnx(fmt, ap);
168 f36fd90a 2022-07-09 op #endif /* !HAVE_ERR */
169 f36fd90a 2022-07-09 op #if !HAVE_FLOCK
170 f36fd90a 2022-07-09 op #include <errno.h>
171 f36fd90a 2022-07-09 op #include <fcntl.h>
172 f36fd90a 2022-07-09 op #include <string.h>
175 f36fd90a 2022-07-09 op * flock(2) emulation on top of fcntl advisory locks. This is "good
176 f36fd90a 2022-07-09 op * enough" for amused, not a _real_ emulation. flock and fcntl locks
177 f36fd90a 2022-07-09 op * have subtly different behaviours!
180 f36fd90a 2022-07-09 op flock(int fd, int op)
182 f36fd90a 2022-07-09 op struct flock l;
185 f36fd90a 2022-07-09 op memset(&l, 0, sizeof(l));
186 f36fd90a 2022-07-09 op l.l_whence = SEEK_SET;
188 f36fd90a 2022-07-09 op if (op & LOCK_SH)
189 f36fd90a 2022-07-09 op l.l_type = F_RDLCK;
190 f36fd90a 2022-07-09 op else if (op & LOCK_EX)
191 f36fd90a 2022-07-09 op l.l_type = F_WRLCK;
193 f36fd90a 2022-07-09 op errno = EINVAL;
197 f36fd90a 2022-07-09 op cmd = F_SETLKW;
198 f36fd90a 2022-07-09 op if (op & LOCK_NB)
199 f36fd90a 2022-07-09 op cmd = F_SETLK;
201 f36fd90a 2022-07-09 op return fcntl(fd, cmd, &l);
203 f36fd90a 2022-07-09 op #endif /* HAVE_FLOCK */
204 f36fd90a 2022-07-09 op #if !HAVE_FREEZERO
205 f36fd90a 2022-07-09 op #include <stdlib.h>
206 f36fd90a 2022-07-09 op #include <string.h>
209 f36fd90a 2022-07-09 op freezero(void *ptr, size_t len)
211 f36fd90a 2022-07-09 op if (ptr == NULL)
213 f36fd90a 2022-07-09 op memset(ptr, 0, len);
216 f36fd90a 2022-07-09 op #endif /* HAVE_FREEZERO */
217 f36fd90a 2022-07-09 op #if !HAVE_EXPLICIT_BZERO
218 f36fd90a 2022-07-09 op /* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
220 f36fd90a 2022-07-09 op * Public domain.
221 f36fd90a 2022-07-09 op * Written by Ted Unangst
224 f36fd90a 2022-07-09 op #include <string.h>
227 f36fd90a 2022-07-09 op * explicit_bzero - don't let the compiler optimize away bzero
230 f36fd90a 2022-07-09 op #if HAVE_MEMSET_S
233 f36fd90a 2022-07-09 op explicit_bzero(void *p, size_t n)
237 f36fd90a 2022-07-09 op (void)memset_s(p, n, 0, n);
240 f36fd90a 2022-07-09 op #else /* HAVE_MEMSET_S */
242 f36fd90a 2022-07-09 op #include <strings.h>
245 f36fd90a 2022-07-09 op * Indirect memset through a volatile pointer to hopefully avoid
246 f36fd90a 2022-07-09 op * dead-store optimisation eliminating the call.
248 c2297fa3 2023-12-10 op static void (* volatile ssh_memset)(void *, int, size_t) =
249 c2297fa3 2023-12-10 op (void (*volatile)(void *, int, size_t))memset;
252 f36fd90a 2022-07-09 op explicit_bzero(void *p, size_t n)
257 f36fd90a 2022-07-09 op * clang -fsanitize=memory needs to intercept memset-like functions
258 f36fd90a 2022-07-09 op * to correctly detect memory initialisation. Make sure one is called
259 f36fd90a 2022-07-09 op * directly since our indirection trick above sucessfully confuses it.
261 f36fd90a 2022-07-09 op #if defined(__has_feature)
262 f36fd90a 2022-07-09 op # if __has_feature(memory_sanitizer)
263 f36fd90a 2022-07-09 op memset(p, 0, n);
267 f36fd90a 2022-07-09 op ssh_memset(p, 0, n);
270 f36fd90a 2022-07-09 op #endif /* HAVE_MEMSET_S */
271 f36fd90a 2022-07-09 op #endif /* !HAVE_EXPLICIT_BZERO */
272 4e457e62 2023-12-10 op #if !HAVE_GETDTABLESIZE
273 4e457e62 2023-12-10 op /* public domain */
274 4e457e62 2023-12-10 op #include <unistd.h>
277 4e457e62 2023-12-10 op getdtablesize(void)
279 4e457e62 2023-12-10 op return sysconf(_SC_OPEN_MAX);
281 4e457e62 2023-12-10 op #endif /* !HAVE_GETDTABLESIZE */
282 f36fd90a 2022-07-09 op #if !HAVE_GETPROGNAME
284 f36fd90a 2022-07-09 op * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
285 f36fd90a 2022-07-09 op * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
286 f36fd90a 2022-07-09 op * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
288 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
289 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
290 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
292 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
293 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
294 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
295 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
296 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
297 f36fd90a 2022-07-09 op * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
298 f36fd90a 2022-07-09 op * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
301 f36fd90a 2022-07-09 op #include <sys/types.h>
303 f36fd90a 2022-07-09 op #include <errno.h>
305 f36fd90a 2022-07-09 op #if HAVE_GETEXECNAME
306 f36fd90a 2022-07-09 op #include <stdlib.h>
308 f36fd90a 2022-07-09 op getprogname(void)
310 f36fd90a 2022-07-09 op return getexecname();
312 f36fd90a 2022-07-09 op #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
314 f36fd90a 2022-07-09 op getprogname(void)
316 f36fd90a 2022-07-09 op return (program_invocation_short_name);
318 f36fd90a 2022-07-09 op #elif HAVE___PROGNAME
320 f36fd90a 2022-07-09 op getprogname(void)
322 f36fd90a 2022-07-09 op extern char *__progname;
324 f36fd90a 2022-07-09 op return (__progname);
327 f36fd90a 2022-07-09 op #warning No getprogname available.
329 f36fd90a 2022-07-09 op getprogname(void)
331 f36fd90a 2022-07-09 op return ("amused");
334 f36fd90a 2022-07-09 op #endif /* !HAVE_GETPROGNAME */
335 82556d5b 2023-09-07 op #if !HAVE_LIB_IMSG
336 fb5efbfa 2024-01-21 op /* $OpenBSD: imsg-buffer.c,v 1.18 2023/12/12 15:47:41 claudio Exp $ */
337 fb5efbfa 2024-01-21 op /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
340 fb5efbfa 2024-01-21 op * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
341 f36fd90a 2022-07-09 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
343 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
344 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
345 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
347 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
348 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
349 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
350 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
351 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
352 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
353 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
356 f36fd90a 2022-07-09 op #include <sys/types.h>
357 f36fd90a 2022-07-09 op #include <sys/socket.h>
358 f36fd90a 2022-07-09 op #include <sys/uio.h>
360 f36fd90a 2022-07-09 op #include <limits.h>
361 f36fd90a 2022-07-09 op #include <errno.h>
362 263426d8 2023-10-09 op #include <endian.h>
363 fb5efbfa 2024-01-21 op #include <stdint.h>
364 f36fd90a 2022-07-09 op #include <stdlib.h>
365 f36fd90a 2022-07-09 op #include <string.h>
366 f36fd90a 2022-07-09 op #include <unistd.h>
368 f36fd90a 2022-07-09 op #include "imsg.h"
370 f36fd90a 2022-07-09 op static int ibuf_realloc(struct ibuf *, size_t);
371 f36fd90a 2022-07-09 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
372 f36fd90a 2022-07-09 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
373 3ac93df9 2023-07-02 op static void msgbuf_drain(struct msgbuf *, size_t);
375 f36fd90a 2022-07-09 op struct ibuf *
376 f36fd90a 2022-07-09 op ibuf_open(size_t len)
378 f36fd90a 2022-07-09 op struct ibuf *buf;
380 3ac93df9 2023-07-02 op if (len == 0) {
381 3ac93df9 2023-07-02 op errno = EINVAL;
382 3ac93df9 2023-07-02 op return (NULL);
384 f36fd90a 2022-07-09 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
385 f36fd90a 2022-07-09 op return (NULL);
386 3ac93df9 2023-07-02 op if ((buf->buf = calloc(len, 1)) == NULL) {
388 f36fd90a 2022-07-09 op return (NULL);
390 f36fd90a 2022-07-09 op buf->size = buf->max = len;
391 f36fd90a 2022-07-09 op buf->fd = -1;
393 f36fd90a 2022-07-09 op return (buf);
396 f36fd90a 2022-07-09 op struct ibuf *
397 f36fd90a 2022-07-09 op ibuf_dynamic(size_t len, size_t max)
399 f36fd90a 2022-07-09 op struct ibuf *buf;
401 fb5efbfa 2024-01-21 op if (max == 0 || max < len) {
402 3ac93df9 2023-07-02 op errno = EINVAL;
403 f36fd90a 2022-07-09 op return (NULL);
406 3ac93df9 2023-07-02 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
407 f36fd90a 2022-07-09 op return (NULL);
408 3ac93df9 2023-07-02 op if (len > 0) {
409 3ac93df9 2023-07-02 op if ((buf->buf = calloc(len, 1)) == NULL) {
411 3ac93df9 2023-07-02 op return (NULL);
414 3ac93df9 2023-07-02 op buf->size = len;
415 3ac93df9 2023-07-02 op buf->max = max;
416 3ac93df9 2023-07-02 op buf->fd = -1;
418 f36fd90a 2022-07-09 op return (buf);
422 f36fd90a 2022-07-09 op ibuf_realloc(struct ibuf *buf, size_t len)
424 f36fd90a 2022-07-09 op unsigned char *b;
426 f36fd90a 2022-07-09 op /* on static buffers max is eq size and so the following fails */
427 3ac93df9 2023-07-02 op if (len > SIZE_MAX - buf->wpos || buf->wpos + len > buf->max) {
428 f36fd90a 2022-07-09 op errno = ERANGE;
432 f36fd90a 2022-07-09 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
433 f36fd90a 2022-07-09 op if (b == NULL)
435 f36fd90a 2022-07-09 op buf->buf = b;
436 f36fd90a 2022-07-09 op buf->size = buf->wpos + len;
442 f36fd90a 2022-07-09 op ibuf_reserve(struct ibuf *buf, size_t len)
446 fb5efbfa 2024-01-21 op if (len > SIZE_MAX - buf->wpos || buf->max == 0) {
447 3ac93df9 2023-07-02 op errno = ERANGE;
448 3ac93df9 2023-07-02 op return (NULL);
451 f36fd90a 2022-07-09 op if (buf->wpos + len > buf->size)
452 f36fd90a 2022-07-09 op if (ibuf_realloc(buf, len) == -1)
453 f36fd90a 2022-07-09 op return (NULL);
455 f36fd90a 2022-07-09 op b = buf->buf + buf->wpos;
456 f36fd90a 2022-07-09 op buf->wpos += len;
461 3ac93df9 2023-07-02 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
465 3ac93df9 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
468 3ac93df9 2023-07-02 op memcpy(b, data, len);
473 fb5efbfa 2024-01-21 op ibuf_add_ibuf(struct ibuf *buf, const struct ibuf *from)
475 fb5efbfa 2024-01-21 op return ibuf_add(buf, ibuf_data(from), ibuf_size(from));
478 fb5efbfa 2024-01-21 op /* remove after tree is converted */
480 3ac93df9 2023-07-02 op ibuf_add_buf(struct ibuf *buf, const struct ibuf *from)
482 fb5efbfa 2024-01-21 op return ibuf_add_ibuf(buf, from);
486 3ac93df9 2023-07-02 op ibuf_add_n8(struct ibuf *buf, uint64_t value)
490 3ac93df9 2023-07-02 op if (value > UINT8_MAX) {
491 3ac93df9 2023-07-02 op errno = EINVAL;
495 3ac93df9 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
499 3ac93df9 2023-07-02 op ibuf_add_n16(struct ibuf *buf, uint64_t value)
503 3ac93df9 2023-07-02 op if (value > UINT16_MAX) {
504 3ac93df9 2023-07-02 op errno = EINVAL;
507 3ac93df9 2023-07-02 op v = htobe16(value);
508 3ac93df9 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
512 3ac93df9 2023-07-02 op ibuf_add_n32(struct ibuf *buf, uint64_t value)
516 3ac93df9 2023-07-02 op if (value > UINT32_MAX) {
517 3ac93df9 2023-07-02 op errno = EINVAL;
520 3ac93df9 2023-07-02 op v = htobe32(value);
521 3ac93df9 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
525 3ac93df9 2023-07-02 op ibuf_add_n64(struct ibuf *buf, uint64_t value)
527 3ac93df9 2023-07-02 op value = htobe64(value);
528 3ac93df9 2023-07-02 op return ibuf_add(buf, &value, sizeof(value));
532 fb5efbfa 2024-01-21 op ibuf_add_h16(struct ibuf *buf, uint64_t value)
536 fb5efbfa 2024-01-21 op if (value > UINT16_MAX) {
537 fb5efbfa 2024-01-21 op errno = EINVAL;
541 fb5efbfa 2024-01-21 op return ibuf_add(buf, &v, sizeof(v));
545 fb5efbfa 2024-01-21 op ibuf_add_h32(struct ibuf *buf, uint64_t value)
549 fb5efbfa 2024-01-21 op if (value > UINT32_MAX) {
550 fb5efbfa 2024-01-21 op errno = EINVAL;
554 fb5efbfa 2024-01-21 op return ibuf_add(buf, &v, sizeof(v));
558 fb5efbfa 2024-01-21 op ibuf_add_h64(struct ibuf *buf, uint64_t value)
560 fb5efbfa 2024-01-21 op return ibuf_add(buf, &value, sizeof(value));
564 3ac93df9 2023-07-02 op ibuf_add_zero(struct ibuf *buf, size_t len)
568 3ac93df9 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
570 fb5efbfa 2024-01-21 op memset(b, 0, len);
575 f36fd90a 2022-07-09 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
577 fb5efbfa 2024-01-21 op /* only allow seeking between rpos and wpos */
578 fb5efbfa 2024-01-21 op if (ibuf_size(buf) < pos || SIZE_MAX - pos < len ||
579 fb5efbfa 2024-01-21 op ibuf_size(buf) < pos + len) {
580 3ac93df9 2023-07-02 op errno = ERANGE;
581 f36fd90a 2022-07-09 op return (NULL);
584 fb5efbfa 2024-01-21 op return (buf->buf + buf->rpos + pos);
588 3ac93df9 2023-07-02 op ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
592 3ac93df9 2023-07-02 op if ((b = ibuf_seek(buf, pos, len)) == NULL)
595 3ac93df9 2023-07-02 op memcpy(b, data, len);
600 3ac93df9 2023-07-02 op ibuf_set_n8(struct ibuf *buf, size_t pos, uint64_t value)
604 3ac93df9 2023-07-02 op if (value > UINT8_MAX) {
605 3ac93df9 2023-07-02 op errno = EINVAL;
609 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
613 3ac93df9 2023-07-02 op ibuf_set_n16(struct ibuf *buf, size_t pos, uint64_t value)
617 3ac93df9 2023-07-02 op if (value > UINT16_MAX) {
618 3ac93df9 2023-07-02 op errno = EINVAL;
621 3ac93df9 2023-07-02 op v = htobe16(value);
622 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
626 3ac93df9 2023-07-02 op ibuf_set_n32(struct ibuf *buf, size_t pos, uint64_t value)
630 3ac93df9 2023-07-02 op if (value > UINT32_MAX) {
631 3ac93df9 2023-07-02 op errno = EINVAL;
634 3ac93df9 2023-07-02 op v = htobe32(value);
635 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
639 3ac93df9 2023-07-02 op ibuf_set_n64(struct ibuf *buf, size_t pos, uint64_t value)
641 3ac93df9 2023-07-02 op value = htobe64(value);
642 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &value, sizeof(value)));
646 fb5efbfa 2024-01-21 op ibuf_set_h16(struct ibuf *buf, size_t pos, uint64_t value)
650 fb5efbfa 2024-01-21 op if (value > UINT16_MAX) {
651 fb5efbfa 2024-01-21 op errno = EINVAL;
655 fb5efbfa 2024-01-21 op return (ibuf_set(buf, pos, &v, sizeof(v)));
659 fb5efbfa 2024-01-21 op ibuf_set_h32(struct ibuf *buf, size_t pos, uint64_t value)
663 fb5efbfa 2024-01-21 op if (value > UINT32_MAX) {
664 fb5efbfa 2024-01-21 op errno = EINVAL;
668 fb5efbfa 2024-01-21 op return (ibuf_set(buf, pos, &v, sizeof(v)));
672 fb5efbfa 2024-01-21 op ibuf_set_h64(struct ibuf *buf, size_t pos, uint64_t value)
674 fb5efbfa 2024-01-21 op return (ibuf_set(buf, pos, &value, sizeof(value)));
678 fb5efbfa 2024-01-21 op ibuf_data(const struct ibuf *buf)
680 fb5efbfa 2024-01-21 op return (buf->buf + buf->rpos);
684 fb5efbfa 2024-01-21 op ibuf_size(const struct ibuf *buf)
686 fb5efbfa 2024-01-21 op return (buf->wpos - buf->rpos);
690 fb5efbfa 2024-01-21 op ibuf_left(const struct ibuf *buf)
692 fb5efbfa 2024-01-21 op if (buf->max == 0)
694 f36fd90a 2022-07-09 op return (buf->max - buf->wpos);
698 fb5efbfa 2024-01-21 op ibuf_truncate(struct ibuf *buf, size_t len)
700 fb5efbfa 2024-01-21 op if (ibuf_size(buf) >= len) {
701 fb5efbfa 2024-01-21 op buf->wpos = buf->rpos + len;
704 fb5efbfa 2024-01-21 op if (buf->max == 0) {
705 fb5efbfa 2024-01-21 op /* only allow to truncate down */
706 fb5efbfa 2024-01-21 op errno = ERANGE;
709 fb5efbfa 2024-01-21 op return ibuf_add_zero(buf, len - ibuf_size(buf));
713 fb5efbfa 2024-01-21 op ibuf_rewind(struct ibuf *buf)
715 fb5efbfa 2024-01-21 op buf->rpos = 0;
719 f36fd90a 2022-07-09 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
721 f36fd90a 2022-07-09 op ibuf_enqueue(msgbuf, buf);
725 fb5efbfa 2024-01-21 op ibuf_from_buffer(struct ibuf *buf, void *data, size_t len)
727 fb5efbfa 2024-01-21 op memset(buf, 0, sizeof(*buf));
728 fb5efbfa 2024-01-21 op buf->buf = data;
729 fb5efbfa 2024-01-21 op buf->size = buf->wpos = len;
730 fb5efbfa 2024-01-21 op buf->fd = -1;
734 fb5efbfa 2024-01-21 op ibuf_from_ibuf(struct ibuf *buf, const struct ibuf *from)
736 fb5efbfa 2024-01-21 op ibuf_from_buffer(buf, ibuf_data(from), ibuf_size(from));
740 fb5efbfa 2024-01-21 op ibuf_get(struct ibuf *buf, void *data, size_t len)
742 fb5efbfa 2024-01-21 op if (ibuf_size(buf) < len) {
743 fb5efbfa 2024-01-21 op errno = EBADMSG;
747 fb5efbfa 2024-01-21 op memcpy(data, ibuf_data(buf), len);
748 fb5efbfa 2024-01-21 op buf->rpos += len;
753 fb5efbfa 2024-01-21 op ibuf_get_ibuf(struct ibuf *buf, size_t len, struct ibuf *new)
755 fb5efbfa 2024-01-21 op if (ibuf_size(buf) < len) {
756 fb5efbfa 2024-01-21 op errno = EBADMSG;
760 fb5efbfa 2024-01-21 op ibuf_from_buffer(new, ibuf_data(buf), len);
761 fb5efbfa 2024-01-21 op buf->rpos += len;
766 fb5efbfa 2024-01-21 op ibuf_get_n8(struct ibuf *buf, uint8_t *value)
768 fb5efbfa 2024-01-21 op return ibuf_get(buf, value, sizeof(*value));
772 fb5efbfa 2024-01-21 op ibuf_get_n16(struct ibuf *buf, uint16_t *value)
776 fb5efbfa 2024-01-21 op rv = ibuf_get(buf, value, sizeof(*value));
777 fb5efbfa 2024-01-21 op *value = be16toh(*value);
782 fb5efbfa 2024-01-21 op ibuf_get_n32(struct ibuf *buf, uint32_t *value)
786 fb5efbfa 2024-01-21 op rv = ibuf_get(buf, value, sizeof(*value));
787 fb5efbfa 2024-01-21 op *value = be32toh(*value);
792 fb5efbfa 2024-01-21 op ibuf_get_n64(struct ibuf *buf, uint64_t *value)
796 fb5efbfa 2024-01-21 op rv = ibuf_get(buf, value, sizeof(*value));
797 fb5efbfa 2024-01-21 op *value = be64toh(*value);
802 fb5efbfa 2024-01-21 op ibuf_get_h16(struct ibuf *buf, uint16_t *value)
804 fb5efbfa 2024-01-21 op return ibuf_get(buf, value, sizeof(*value));
808 fb5efbfa 2024-01-21 op ibuf_get_h32(struct ibuf *buf, uint32_t *value)
810 fb5efbfa 2024-01-21 op return ibuf_get(buf, value, sizeof(*value));
814 fb5efbfa 2024-01-21 op ibuf_get_h64(struct ibuf *buf, uint64_t *value)
816 fb5efbfa 2024-01-21 op return ibuf_get(buf, value, sizeof(*value));
820 fb5efbfa 2024-01-21 op ibuf_skip(struct ibuf *buf, size_t len)
822 fb5efbfa 2024-01-21 op if (ibuf_size(buf) < len) {
823 fb5efbfa 2024-01-21 op errno = EBADMSG;
827 fb5efbfa 2024-01-21 op buf->rpos += len;
832 3ac93df9 2023-07-02 op ibuf_free(struct ibuf *buf)
834 3ac93df9 2023-07-02 op if (buf == NULL)
836 fb5efbfa 2024-01-21 op if (buf->max == 0) /* if buf lives on the stack */
837 fb5efbfa 2024-01-21 op abort(); /* abort before causing more harm */
838 3ac93df9 2023-07-02 op if (buf->fd != -1)
839 3ac93df9 2023-07-02 op close(buf->fd);
840 3ac93df9 2023-07-02 op freezero(buf->buf, buf->size);
845 3ac93df9 2023-07-02 op ibuf_fd_avail(struct ibuf *buf)
847 3ac93df9 2023-07-02 op return (buf->fd != -1);
851 3ac93df9 2023-07-02 op ibuf_fd_get(struct ibuf *buf)
855 3ac93df9 2023-07-02 op fd = buf->fd;
856 3ac93df9 2023-07-02 op buf->fd = -1;
861 3ac93df9 2023-07-02 op ibuf_fd_set(struct ibuf *buf, int fd)
863 fb5efbfa 2024-01-21 op if (buf->max == 0) /* if buf lives on the stack */
864 fb5efbfa 2024-01-21 op abort(); /* abort before causing more harm */
865 3ac93df9 2023-07-02 op if (buf->fd != -1)
866 3ac93df9 2023-07-02 op close(buf->fd);
867 3ac93df9 2023-07-02 op buf->fd = fd;
871 f36fd90a 2022-07-09 op ibuf_write(struct msgbuf *msgbuf)
873 f36fd90a 2022-07-09 op struct iovec iov[IOV_MAX];
874 f36fd90a 2022-07-09 op struct ibuf *buf;
875 f36fd90a 2022-07-09 op unsigned int i = 0;
878 f36fd90a 2022-07-09 op memset(&iov, 0, sizeof(iov));
879 f36fd90a 2022-07-09 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
880 f36fd90a 2022-07-09 op if (i >= IOV_MAX)
882 fb5efbfa 2024-01-21 op iov[i].iov_base = ibuf_data(buf);
883 fb5efbfa 2024-01-21 op iov[i].iov_len = ibuf_size(buf);
888 f36fd90a 2022-07-09 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
889 f36fd90a 2022-07-09 op if (errno == EINTR)
891 f36fd90a 2022-07-09 op if (errno == ENOBUFS)
892 f36fd90a 2022-07-09 op errno = EAGAIN;
896 f36fd90a 2022-07-09 op if (n == 0) { /* connection closed */
901 f36fd90a 2022-07-09 op msgbuf_drain(msgbuf, n);
907 f36fd90a 2022-07-09 op msgbuf_init(struct msgbuf *msgbuf)
909 f36fd90a 2022-07-09 op msgbuf->queued = 0;
910 f36fd90a 2022-07-09 op msgbuf->fd = -1;
911 f36fd90a 2022-07-09 op TAILQ_INIT(&msgbuf->bufs);
915 f36fd90a 2022-07-09 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
917 f36fd90a 2022-07-09 op struct ibuf *buf, *next;
919 f36fd90a 2022-07-09 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
920 f36fd90a 2022-07-09 op buf = next) {
921 f36fd90a 2022-07-09 op next = TAILQ_NEXT(buf, entry);
922 fb5efbfa 2024-01-21 op if (n >= ibuf_size(buf)) {
923 fb5efbfa 2024-01-21 op n -= ibuf_size(buf);
924 f36fd90a 2022-07-09 op ibuf_dequeue(msgbuf, buf);
926 f36fd90a 2022-07-09 op buf->rpos += n;
933 f36fd90a 2022-07-09 op msgbuf_clear(struct msgbuf *msgbuf)
935 f36fd90a 2022-07-09 op struct ibuf *buf;
937 f36fd90a 2022-07-09 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
938 f36fd90a 2022-07-09 op ibuf_dequeue(msgbuf, buf);
942 f36fd90a 2022-07-09 op msgbuf_write(struct msgbuf *msgbuf)
944 f36fd90a 2022-07-09 op struct iovec iov[IOV_MAX];
945 f36fd90a 2022-07-09 op struct ibuf *buf, *buf0 = NULL;
946 f36fd90a 2022-07-09 op unsigned int i = 0;
948 f36fd90a 2022-07-09 op struct msghdr msg;
949 f36fd90a 2022-07-09 op struct cmsghdr *cmsg;
951 f36fd90a 2022-07-09 op struct cmsghdr hdr;
952 f36fd90a 2022-07-09 op char buf[CMSG_SPACE(sizeof(int))];
955 f36fd90a 2022-07-09 op memset(&iov, 0, sizeof(iov));
956 f36fd90a 2022-07-09 op memset(&msg, 0, sizeof(msg));
957 f36fd90a 2022-07-09 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
958 f36fd90a 2022-07-09 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
959 f36fd90a 2022-07-09 op if (i >= IOV_MAX)
961 f36fd90a 2022-07-09 op if (i > 0 && buf->fd != -1)
963 fb5efbfa 2024-01-21 op iov[i].iov_base = ibuf_data(buf);
964 fb5efbfa 2024-01-21 op iov[i].iov_len = ibuf_size(buf);
966 f36fd90a 2022-07-09 op if (buf->fd != -1)
970 f36fd90a 2022-07-09 op msg.msg_iov = iov;
971 f36fd90a 2022-07-09 op msg.msg_iovlen = i;
973 f36fd90a 2022-07-09 op if (buf0 != NULL) {
974 f36fd90a 2022-07-09 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
975 f36fd90a 2022-07-09 op msg.msg_controllen = sizeof(cmsgbuf.buf);
976 f36fd90a 2022-07-09 op cmsg = CMSG_FIRSTHDR(&msg);
977 f36fd90a 2022-07-09 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
978 f36fd90a 2022-07-09 op cmsg->cmsg_level = SOL_SOCKET;
979 f36fd90a 2022-07-09 op cmsg->cmsg_type = SCM_RIGHTS;
980 f36fd90a 2022-07-09 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
984 f36fd90a 2022-07-09 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
985 f36fd90a 2022-07-09 op if (errno == EINTR)
987 f36fd90a 2022-07-09 op if (errno == ENOBUFS)
988 f36fd90a 2022-07-09 op errno = EAGAIN;
992 f36fd90a 2022-07-09 op if (n == 0) { /* connection closed */
998 f36fd90a 2022-07-09 op * assumption: fd got sent if sendmsg sent anything
999 f36fd90a 2022-07-09 op * this works because fds are passed one at a time
1001 f36fd90a 2022-07-09 op if (buf0 != NULL) {
1002 f36fd90a 2022-07-09 op close(buf0->fd);
1003 f36fd90a 2022-07-09 op buf0->fd = -1;
1006 f36fd90a 2022-07-09 op msgbuf_drain(msgbuf, n);
1012 fb5efbfa 2024-01-21 op msgbuf_queuelen(struct msgbuf *msgbuf)
1014 fb5efbfa 2024-01-21 op return (msgbuf->queued);
1018 f36fd90a 2022-07-09 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
1020 fb5efbfa 2024-01-21 op if (buf->max == 0) /* if buf lives on the stack */
1021 fb5efbfa 2024-01-21 op abort(); /* abort before causing more harm */
1022 f36fd90a 2022-07-09 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
1023 f36fd90a 2022-07-09 op msgbuf->queued++;
1027 f36fd90a 2022-07-09 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
1029 f36fd90a 2022-07-09 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
1030 f36fd90a 2022-07-09 op msgbuf->queued--;
1031 f36fd90a 2022-07-09 op ibuf_free(buf);
1034 f36fd90a 2022-07-09 op /* imsg.c */
1036 fb5efbfa 2024-01-21 op struct imsg_fd {
1037 fb5efbfa 2024-01-21 op TAILQ_ENTRY(imsg_fd) entry;
1041 f36fd90a 2022-07-09 op int imsg_fd_overhead = 0;
1043 fb5efbfa 2024-01-21 op static int imsg_dequeue_fd(struct imsgbuf *);
1046 fb5efbfa 2024-01-21 op imsg_init(struct imsgbuf *imsgbuf, int fd)
1048 fb5efbfa 2024-01-21 op msgbuf_init(&imsgbuf->w);
1049 fb5efbfa 2024-01-21 op memset(&imsgbuf->r, 0, sizeof(imsgbuf->r));
1050 fb5efbfa 2024-01-21 op imsgbuf->fd = fd;
1051 fb5efbfa 2024-01-21 op imsgbuf->w.fd = fd;
1052 fb5efbfa 2024-01-21 op imsgbuf->pid = getpid();
1053 fb5efbfa 2024-01-21 op TAILQ_INIT(&imsgbuf->fds);
1057 fb5efbfa 2024-01-21 op imsg_read(struct imsgbuf *imsgbuf)
1059 f36fd90a 2022-07-09 op struct msghdr msg;
1060 f36fd90a 2022-07-09 op struct cmsghdr *cmsg;
1062 f36fd90a 2022-07-09 op struct cmsghdr hdr;
1063 f36fd90a 2022-07-09 op char buf[CMSG_SPACE(sizeof(int) * 1)];
1065 f36fd90a 2022-07-09 op struct iovec iov;
1066 f36fd90a 2022-07-09 op ssize_t n = -1;
1068 f36fd90a 2022-07-09 op struct imsg_fd *ifd;
1070 f36fd90a 2022-07-09 op memset(&msg, 0, sizeof(msg));
1071 f36fd90a 2022-07-09 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
1073 fb5efbfa 2024-01-21 op iov.iov_base = imsgbuf->r.buf + imsgbuf->r.wpos;
1074 fb5efbfa 2024-01-21 op iov.iov_len = sizeof(imsgbuf->r.buf) - imsgbuf->r.wpos;
1075 f36fd90a 2022-07-09 op msg.msg_iov = &iov;
1076 f36fd90a 2022-07-09 op msg.msg_iovlen = 1;
1077 f36fd90a 2022-07-09 op msg.msg_control = &cmsgbuf.buf;
1078 f36fd90a 2022-07-09 op msg.msg_controllen = sizeof(cmsgbuf.buf);
1080 f36fd90a 2022-07-09 op if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
1081 f36fd90a 2022-07-09 op return (-1);
1084 f36fd90a 2022-07-09 op if (getdtablecount() + imsg_fd_overhead +
1085 f36fd90a 2022-07-09 op (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
1086 f36fd90a 2022-07-09 op >= getdtablesize()) {
1087 f36fd90a 2022-07-09 op errno = EAGAIN;
1089 f36fd90a 2022-07-09 op return (-1);
1092 fb5efbfa 2024-01-21 op if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) {
1093 f36fd90a 2022-07-09 op if (errno == EINTR)
1098 fb5efbfa 2024-01-21 op imsgbuf->r.wpos += n;
1100 f36fd90a 2022-07-09 op for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
1101 f36fd90a 2022-07-09 op cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1102 f36fd90a 2022-07-09 op if (cmsg->cmsg_level == SOL_SOCKET &&
1103 f36fd90a 2022-07-09 op cmsg->cmsg_type == SCM_RIGHTS) {
1108 f36fd90a 2022-07-09 op * We only accept one file descriptor. Due to C
1109 f36fd90a 2022-07-09 op * padding rules, our control buffer might contain
1110 f36fd90a 2022-07-09 op * more than one fd, and we must close them.
1112 f36fd90a 2022-07-09 op j = ((char *)cmsg + cmsg->cmsg_len -
1113 f36fd90a 2022-07-09 op (char *)CMSG_DATA(cmsg)) / sizeof(int);
1114 f36fd90a 2022-07-09 op for (i = 0; i < j; i++) {
1115 f36fd90a 2022-07-09 op fd = ((int *)CMSG_DATA(cmsg))[i];
1116 f36fd90a 2022-07-09 op if (ifd != NULL) {
1117 f36fd90a 2022-07-09 op ifd->fd = fd;
1118 fb5efbfa 2024-01-21 op TAILQ_INSERT_TAIL(&imsgbuf->fds, ifd,
1125 f36fd90a 2022-07-09 op /* we do not handle other ctl data level */
1134 fb5efbfa 2024-01-21 op imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
1136 fb5efbfa 2024-01-21 op struct imsg m;
1137 f36fd90a 2022-07-09 op size_t av, left, datalen;
1139 fb5efbfa 2024-01-21 op av = imsgbuf->r.wpos;
1141 f36fd90a 2022-07-09 op if (IMSG_HEADER_SIZE > av)
1144 fb5efbfa 2024-01-21 op memcpy(&m.hdr, imsgbuf->r.buf, sizeof(m.hdr));
1145 fb5efbfa 2024-01-21 op if (m.hdr.len < IMSG_HEADER_SIZE ||
1146 fb5efbfa 2024-01-21 op m.hdr.len > MAX_IMSGSIZE) {
1147 f36fd90a 2022-07-09 op errno = ERANGE;
1148 f36fd90a 2022-07-09 op return (-1);
1150 fb5efbfa 2024-01-21 op if (m.hdr.len > av)
1154 fb5efbfa 2024-01-21 op m.buf = NULL;
1155 fb5efbfa 2024-01-21 op m.data = NULL;
1157 fb5efbfa 2024-01-21 op datalen = m.hdr.len - IMSG_HEADER_SIZE;
1158 fb5efbfa 2024-01-21 op imsgbuf->r.rptr = imsgbuf->r.buf + IMSG_HEADER_SIZE;
1159 fb5efbfa 2024-01-21 op if (datalen != 0) {
1160 fb5efbfa 2024-01-21 op if ((m.buf = ibuf_open(datalen)) == NULL)
1161 fb5efbfa 2024-01-21 op return (-1);
1162 fb5efbfa 2024-01-21 op if (ibuf_add(m.buf, imsgbuf->r.rptr, datalen) == -1) {
1163 fb5efbfa 2024-01-21 op /* this should never fail */
1164 fb5efbfa 2024-01-21 op ibuf_free(m.buf);
1165 fb5efbfa 2024-01-21 op return (-1);
1167 fb5efbfa 2024-01-21 op m.data = ibuf_data(m.buf);
1170 fb5efbfa 2024-01-21 op if (m.hdr.flags & IMSGF_HASFD)
1171 fb5efbfa 2024-01-21 op m.fd = imsg_dequeue_fd(imsgbuf);
1173 fb5efbfa 2024-01-21 op if (m.hdr.len < av) {
1174 fb5efbfa 2024-01-21 op left = av - m.hdr.len;
1175 fb5efbfa 2024-01-21 op memmove(&imsgbuf->r.buf, imsgbuf->r.buf + m.hdr.len, left);
1176 fb5efbfa 2024-01-21 op imsgbuf->r.wpos = left;
1178 fb5efbfa 2024-01-21 op imsgbuf->r.wpos = 0;
1181 f36fd90a 2022-07-09 op return (datalen + IMSG_HEADER_SIZE);
1185 fb5efbfa 2024-01-21 op imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
1187 fb5efbfa 2024-01-21 op if (imsg->buf == NULL) {
1188 fb5efbfa 2024-01-21 op errno = EBADMSG;
1189 fb5efbfa 2024-01-21 op return (-1);
1191 fb5efbfa 2024-01-21 op return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
1195 fb5efbfa 2024-01-21 op imsg_get_data(struct imsg *imsg, void *data, size_t len)
1197 fb5efbfa 2024-01-21 op if (len == 0) {
1198 fb5efbfa 2024-01-21 op errno = EINVAL;
1199 fb5efbfa 2024-01-21 op return (-1);
1201 fb5efbfa 2024-01-21 op if (imsg->buf == NULL || ibuf_size(imsg->buf) != len) {
1202 fb5efbfa 2024-01-21 op errno = EBADMSG;
1203 fb5efbfa 2024-01-21 op return (-1);
1205 fb5efbfa 2024-01-21 op return ibuf_get(imsg->buf, data, len);
1209 fb5efbfa 2024-01-21 op imsg_get_fd(struct imsg *imsg)
1211 fb5efbfa 2024-01-21 op int fd = imsg->fd;
1213 fb5efbfa 2024-01-21 op imsg->fd = -1;
1218 fb5efbfa 2024-01-21 op imsg_get_id(struct imsg *imsg)
1220 fb5efbfa 2024-01-21 op return (imsg->hdr.peerid);
1224 fb5efbfa 2024-01-21 op imsg_get_len(struct imsg *imsg)
1226 fb5efbfa 2024-01-21 op if (imsg->buf == NULL)
1228 fb5efbfa 2024-01-21 op return ibuf_size(imsg->buf);
1232 fb5efbfa 2024-01-21 op imsg_get_pid(struct imsg *imsg)
1234 fb5efbfa 2024-01-21 op return (imsg->hdr.pid);
1238 fb5efbfa 2024-01-21 op imsg_get_type(struct imsg *imsg)
1240 fb5efbfa 2024-01-21 op return (imsg->hdr.type);
1244 fb5efbfa 2024-01-21 op imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
1245 fb5efbfa 2024-01-21 op int fd, const void *data, size_t datalen)
1247 f36fd90a 2022-07-09 op struct ibuf *wbuf;
1249 fb5efbfa 2024-01-21 op if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
1250 f36fd90a 2022-07-09 op return (-1);
1252 f36fd90a 2022-07-09 op if (imsg_add(wbuf, data, datalen) == -1)
1253 f36fd90a 2022-07-09 op return (-1);
1255 3ac93df9 2023-07-02 op ibuf_fd_set(wbuf, fd);
1256 fb5efbfa 2024-01-21 op imsg_close(imsgbuf, wbuf);
1262 fb5efbfa 2024-01-21 op imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
1263 f36fd90a 2022-07-09 op int fd, const struct iovec *iov, int iovcnt)
1265 f36fd90a 2022-07-09 op struct ibuf *wbuf;
1267 fb5efbfa 2024-01-21 op size_t datalen = 0;
1269 f36fd90a 2022-07-09 op for (i = 0; i < iovcnt; i++)
1270 f36fd90a 2022-07-09 op datalen += iov[i].iov_len;
1272 fb5efbfa 2024-01-21 op if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
1273 f36fd90a 2022-07-09 op return (-1);
1275 f36fd90a 2022-07-09 op for (i = 0; i < iovcnt; i++)
1276 f36fd90a 2022-07-09 op if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
1277 f36fd90a 2022-07-09 op return (-1);
1279 3ac93df9 2023-07-02 op ibuf_fd_set(wbuf, fd);
1280 fb5efbfa 2024-01-21 op imsg_close(imsgbuf, wbuf);
1286 fb5efbfa 2024-01-21 op * Enqueue imsg with payload from ibuf buf. fd passing is not possible
1287 fb5efbfa 2024-01-21 op * with this function.
1290 fb5efbfa 2024-01-21 op imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
1291 3ac93df9 2023-07-02 op pid_t pid, struct ibuf *buf)
1293 fb5efbfa 2024-01-21 op struct ibuf *hdrbuf = NULL;
1294 3ac93df9 2023-07-02 op struct imsg_hdr hdr;
1295 3ac93df9 2023-07-02 op int save_errno;
1297 3ac93df9 2023-07-02 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
1298 3ac93df9 2023-07-02 op errno = ERANGE;
1302 3ac93df9 2023-07-02 op hdr.type = type;
1303 3ac93df9 2023-07-02 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
1304 3ac93df9 2023-07-02 op hdr.flags = 0;
1305 fb5efbfa 2024-01-21 op hdr.peerid = id;
1306 3ac93df9 2023-07-02 op if ((hdr.pid = pid) == 0)
1307 fb5efbfa 2024-01-21 op hdr.pid = imsgbuf->pid;
1309 fb5efbfa 2024-01-21 op if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
1311 fb5efbfa 2024-01-21 op if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
1314 fb5efbfa 2024-01-21 op ibuf_close(&imsgbuf->w, hdrbuf);
1315 fb5efbfa 2024-01-21 op ibuf_close(&imsgbuf->w, buf);
1319 3ac93df9 2023-07-02 op save_errno = errno;
1320 3ac93df9 2023-07-02 op ibuf_free(buf);
1321 fb5efbfa 2024-01-21 op ibuf_free(hdrbuf);
1322 3ac93df9 2023-07-02 op errno = save_errno;
1323 3ac93df9 2023-07-02 op return (-1);
1327 fb5efbfa 2024-01-21 op * Forward imsg to another channel. Any attached fd is closed.
1330 fb5efbfa 2024-01-21 op imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
1332 fb5efbfa 2024-01-21 op struct ibuf *wbuf;
1333 fb5efbfa 2024-01-21 op size_t len = 0;
1335 fb5efbfa 2024-01-21 op if (msg->fd != -1) {
1336 fb5efbfa 2024-01-21 op close(msg->fd);
1337 fb5efbfa 2024-01-21 op msg->fd = -1;
1340 fb5efbfa 2024-01-21 op if (msg->buf != NULL) {
1341 fb5efbfa 2024-01-21 op ibuf_rewind(msg->buf);
1342 fb5efbfa 2024-01-21 op len = ibuf_size(msg->buf);
1345 fb5efbfa 2024-01-21 op if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
1346 fb5efbfa 2024-01-21 op msg->hdr.pid, len)) == NULL)
1347 fb5efbfa 2024-01-21 op return (-1);
1349 fb5efbfa 2024-01-21 op if (msg->buf != NULL) {
1350 fb5efbfa 2024-01-21 op if (ibuf_add_buf(wbuf, msg->buf) == -1) {
1351 fb5efbfa 2024-01-21 op ibuf_free(wbuf);
1352 fb5efbfa 2024-01-21 op return (-1);
1356 fb5efbfa 2024-01-21 op imsg_close(imsgbuf, wbuf);
1360 f36fd90a 2022-07-09 op struct ibuf *
1361 fb5efbfa 2024-01-21 op imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
1362 fb5efbfa 2024-01-21 op size_t datalen)
1364 f36fd90a 2022-07-09 op struct ibuf *wbuf;
1365 f36fd90a 2022-07-09 op struct imsg_hdr hdr;
1367 f36fd90a 2022-07-09 op datalen += IMSG_HEADER_SIZE;
1368 f36fd90a 2022-07-09 op if (datalen > MAX_IMSGSIZE) {
1369 f36fd90a 2022-07-09 op errno = ERANGE;
1370 f36fd90a 2022-07-09 op return (NULL);
1373 f36fd90a 2022-07-09 op hdr.type = type;
1374 f36fd90a 2022-07-09 op hdr.flags = 0;
1375 fb5efbfa 2024-01-21 op hdr.peerid = id;
1376 f36fd90a 2022-07-09 op if ((hdr.pid = pid) == 0)
1377 fb5efbfa 2024-01-21 op hdr.pid = imsgbuf->pid;
1378 f36fd90a 2022-07-09 op if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
1379 f36fd90a 2022-07-09 op return (NULL);
1381 f36fd90a 2022-07-09 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
1382 f36fd90a 2022-07-09 op return (NULL);
1384 f36fd90a 2022-07-09 op return (wbuf);
1388 fb5efbfa 2024-01-21 op imsg_add(struct ibuf *msg, const void *data, size_t datalen)
1390 f36fd90a 2022-07-09 op if (datalen)
1391 f36fd90a 2022-07-09 op if (ibuf_add(msg, data, datalen) == -1) {
1392 f36fd90a 2022-07-09 op ibuf_free(msg);
1393 f36fd90a 2022-07-09 op return (-1);
1395 f36fd90a 2022-07-09 op return (datalen);
1399 fb5efbfa 2024-01-21 op imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
1401 f36fd90a 2022-07-09 op struct imsg_hdr *hdr;
1403 f36fd90a 2022-07-09 op hdr = (struct imsg_hdr *)msg->buf;
1405 f36fd90a 2022-07-09 op hdr->flags &= ~IMSGF_HASFD;
1406 3ac93df9 2023-07-02 op if (ibuf_fd_avail(msg))
1407 f36fd90a 2022-07-09 op hdr->flags |= IMSGF_HASFD;
1408 3ac93df9 2023-07-02 op hdr->len = ibuf_size(msg);
1410 fb5efbfa 2024-01-21 op ibuf_close(&imsgbuf->w, msg);
1414 f36fd90a 2022-07-09 op imsg_free(struct imsg *imsg)
1416 fb5efbfa 2024-01-21 op ibuf_free(imsg->buf);
1420 fb5efbfa 2024-01-21 op imsg_dequeue_fd(struct imsgbuf *imsgbuf)
1423 f36fd90a 2022-07-09 op struct imsg_fd *ifd;
1425 fb5efbfa 2024-01-21 op if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
1426 f36fd90a 2022-07-09 op return (-1);
1428 f36fd90a 2022-07-09 op fd = ifd->fd;
1429 fb5efbfa 2024-01-21 op TAILQ_REMOVE(&imsgbuf->fds, ifd, entry);
1432 f36fd90a 2022-07-09 op return (fd);
1436 fb5efbfa 2024-01-21 op imsg_flush(struct imsgbuf *imsgbuf)
1438 fb5efbfa 2024-01-21 op while (imsgbuf->w.queued)
1439 fb5efbfa 2024-01-21 op if (msgbuf_write(&imsgbuf->w) <= 0)
1440 f36fd90a 2022-07-09 op return (-1);
1445 fb5efbfa 2024-01-21 op imsg_clear(struct imsgbuf *imsgbuf)
1449 fb5efbfa 2024-01-21 op msgbuf_clear(&imsgbuf->w);
1450 fb5efbfa 2024-01-21 op while ((fd = imsg_dequeue_fd(imsgbuf)) != -1)
1453 82556d5b 2023-09-07 op #endif /* HAVE_LIB_IMSG */
1454 f36fd90a 2022-07-09 op #if !HAVE_MEMMEM
1456 f36fd90a 2022-07-09 op * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
1458 f36fd90a 2022-07-09 op * Redistribution and use in source and binary forms, with or without
1459 f36fd90a 2022-07-09 op * modification, are permitted provided that the following conditions
1461 f36fd90a 2022-07-09 op * 1. Redistributions of source code must retain the above copyright
1462 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer.
1463 f36fd90a 2022-07-09 op * 2. Redistributions in binary form must reproduce the above copyright
1464 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer in
1465 f36fd90a 2022-07-09 op * the documentation and/or other materials provided with the
1466 f36fd90a 2022-07-09 op * distribution.
1467 f36fd90a 2022-07-09 op * 3. The name of the author may not be used to endorse or promote
1468 f36fd90a 2022-07-09 op * products derived from this software without specific prior written
1469 f36fd90a 2022-07-09 op * permission.
1471 f36fd90a 2022-07-09 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
1472 f36fd90a 2022-07-09 op * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1473 f36fd90a 2022-07-09 op * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1474 f36fd90a 2022-07-09 op * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
1475 f36fd90a 2022-07-09 op * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1476 f36fd90a 2022-07-09 op * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1477 f36fd90a 2022-07-09 op * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1478 f36fd90a 2022-07-09 op * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
1479 f36fd90a 2022-07-09 op * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1480 f36fd90a 2022-07-09 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1481 f36fd90a 2022-07-09 op * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1484 f36fd90a 2022-07-09 op * Find the first occurrence of the byte string s in byte string l.
1487 f36fd90a 2022-07-09 op memmem(const void *l, size_t l_len, const void *s, size_t s_len)
1489 f36fd90a 2022-07-09 op const char *cur, *last;
1490 f36fd90a 2022-07-09 op const char *cl = l;
1491 f36fd90a 2022-07-09 op const char *cs = s;
1493 f36fd90a 2022-07-09 op /* a zero length needle should just return the haystack */
1494 f36fd90a 2022-07-09 op if (l_len == 0)
1495 f36fd90a 2022-07-09 op return (void *)cl;
1497 f36fd90a 2022-07-09 op /* "s" must be smaller or equal to "l" */
1498 f36fd90a 2022-07-09 op if (l_len < s_len)
1499 f36fd90a 2022-07-09 op return NULL;
1501 f36fd90a 2022-07-09 op /* special case where s_len == 1 */
1502 f36fd90a 2022-07-09 op if (s_len == 1)
1503 f36fd90a 2022-07-09 op return memchr(l, *cs, l_len);
1505 f36fd90a 2022-07-09 op /* the last position where its possible to find "s" in "l" */
1506 f36fd90a 2022-07-09 op last = cl + l_len - s_len;
1508 f36fd90a 2022-07-09 op for (cur = cl; cur <= last; cur++)
1509 f36fd90a 2022-07-09 op if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
1510 f36fd90a 2022-07-09 op return (void *)cur;
1512 f36fd90a 2022-07-09 op return NULL;
1514 f36fd90a 2022-07-09 op #endif /* !HAVE_MEMMEM */
1515 f36fd90a 2022-07-09 op #if !HAVE_MEMRCHR
1517 f36fd90a 2022-07-09 op * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
1519 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1520 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1521 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1523 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1524 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1525 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1526 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1527 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1528 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1529 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1533 f36fd90a 2022-07-09 op #include <string.h>
1536 f36fd90a 2022-07-09 op * Reverse memchr()
1537 f36fd90a 2022-07-09 op * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
1540 f36fd90a 2022-07-09 op memrchr(const void *s, int c, size_t n)
1542 f36fd90a 2022-07-09 op const unsigned char *cp;
1544 f36fd90a 2022-07-09 op if (n != 0) {
1545 f36fd90a 2022-07-09 op cp = (unsigned char *)s + n;
1547 f36fd90a 2022-07-09 op if (*(--cp) == (unsigned char)c)
1548 f36fd90a 2022-07-09 op return((void *)cp);
1549 f36fd90a 2022-07-09 op } while (--n != 0);
1551 f36fd90a 2022-07-09 op return(NULL);
1553 f36fd90a 2022-07-09 op #endif /* !HAVE_MEMRCHR */
1554 f36fd90a 2022-07-09 op #if !HAVE_OPTRESET
1556 f36fd90a 2022-07-09 op * Copyright (c) 1987, 1993, 1994
1557 f36fd90a 2022-07-09 op * The Regents of the University of California. All rights reserved.
1559 f36fd90a 2022-07-09 op * Redistribution and use in source and binary forms, with or without
1560 f36fd90a 2022-07-09 op * modification, are permitted provided that the following conditions
1562 f36fd90a 2022-07-09 op * 1. Redistributions of source code must retain the above copyright
1563 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer.
1564 f36fd90a 2022-07-09 op * 2. Redistributions in binary form must reproduce the above copyright
1565 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer in the
1566 f36fd90a 2022-07-09 op * documentation and/or other materials provided with the distribution.
1567 f36fd90a 2022-07-09 op * 3. Neither the name of the University nor the names of its contributors
1568 f36fd90a 2022-07-09 op * may be used to endorse or promote products derived from this software
1569 f36fd90a 2022-07-09 op * without specific prior written permission.
1571 f36fd90a 2022-07-09 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1572 f36fd90a 2022-07-09 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1573 f36fd90a 2022-07-09 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1574 f36fd90a 2022-07-09 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1575 f36fd90a 2022-07-09 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1576 f36fd90a 2022-07-09 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1577 f36fd90a 2022-07-09 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1578 f36fd90a 2022-07-09 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1579 f36fd90a 2022-07-09 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1580 f36fd90a 2022-07-09 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1581 f36fd90a 2022-07-09 op * SUCH DAMAGE.
1584 f36fd90a 2022-07-09 op /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
1586 f36fd90a 2022-07-09 op #include <stdio.h>
1587 f36fd90a 2022-07-09 op #include <stdlib.h>
1588 f36fd90a 2022-07-09 op #include <string.h>
1590 f36fd90a 2022-07-09 op int BSDopterr = 1, /* if error message should be printed */
1591 f36fd90a 2022-07-09 op BSDoptind = 1, /* index into parent argv vector */
1592 f36fd90a 2022-07-09 op BSDoptopt, /* character checked for validity */
1593 f36fd90a 2022-07-09 op BSDoptreset; /* reset getopt */
1594 f36fd90a 2022-07-09 op char *BSDoptarg; /* argument associated with option */
1596 f36fd90a 2022-07-09 op #define BADCH (int)'?'
1597 f36fd90a 2022-07-09 op #define BADARG (int)':'
1598 f36fd90a 2022-07-09 op #define EMSG ""
1602 f36fd90a 2022-07-09 op * Parse argc/argv argument vector.
1605 f36fd90a 2022-07-09 op BSDgetopt(int nargc, char *const *nargv, const char *ostr)
1607 f36fd90a 2022-07-09 op static const char *place = EMSG; /* option letter processing */
1608 f36fd90a 2022-07-09 op char *oli; /* option letter list index */
1610 f36fd90a 2022-07-09 op if (ostr == NULL)
1611 f36fd90a 2022-07-09 op return (-1);
1613 f36fd90a 2022-07-09 op if (BSDoptreset || !*place) { /* update scanning pointer */
1614 f36fd90a 2022-07-09 op BSDoptreset = 0;
1615 f36fd90a 2022-07-09 op if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
1616 f36fd90a 2022-07-09 op place = EMSG;
1617 f36fd90a 2022-07-09 op return (-1);
1619 f36fd90a 2022-07-09 op if (place[1] && *++place == '-') { /* found "--" */
1620 f36fd90a 2022-07-09 op if (place[1])
1621 f36fd90a 2022-07-09 op return (BADCH);
1622 f36fd90a 2022-07-09 op ++BSDoptind;
1623 f36fd90a 2022-07-09 op place = EMSG;
1624 f36fd90a 2022-07-09 op return (-1);
1626 f36fd90a 2022-07-09 op } /* option letter okay? */
1627 f36fd90a 2022-07-09 op if ((BSDoptopt = (int)*place++) == (int)':' ||
1628 f36fd90a 2022-07-09 op !(oli = strchr(ostr, BSDoptopt))) {
1630 f36fd90a 2022-07-09 op * if the user didn't specify '-' as an option,
1631 f36fd90a 2022-07-09 op * assume it means -1.
1633 f36fd90a 2022-07-09 op if (BSDoptopt == (int)'-')
1634 f36fd90a 2022-07-09 op return (-1);
1635 f36fd90a 2022-07-09 op if (!*place)
1636 f36fd90a 2022-07-09 op ++BSDoptind;
1637 f36fd90a 2022-07-09 op if (BSDopterr && *ostr != ':')
1638 f36fd90a 2022-07-09 op (void)fprintf(stderr,
1639 f36fd90a 2022-07-09 op "%s: unknown option -- %c\n", getprogname(),
1641 f36fd90a 2022-07-09 op return (BADCH);
1643 f36fd90a 2022-07-09 op if (*++oli != ':') { /* don't need argument */
1644 f36fd90a 2022-07-09 op BSDoptarg = NULL;
1645 f36fd90a 2022-07-09 op if (!*place)
1646 f36fd90a 2022-07-09 op ++BSDoptind;
1648 f36fd90a 2022-07-09 op else { /* need an argument */
1649 f36fd90a 2022-07-09 op if (*place) /* no white space */
1650 f36fd90a 2022-07-09 op BSDoptarg = (char *)place;
1651 f36fd90a 2022-07-09 op else if (nargc <= ++BSDoptind) { /* no arg */
1652 f36fd90a 2022-07-09 op place = EMSG;
1653 f36fd90a 2022-07-09 op if (*ostr == ':')
1654 f36fd90a 2022-07-09 op return (BADARG);
1655 f36fd90a 2022-07-09 op if (BSDopterr)
1656 f36fd90a 2022-07-09 op (void)fprintf(stderr,
1657 f36fd90a 2022-07-09 op "%s: option requires an argument -- %c\n",
1658 f36fd90a 2022-07-09 op getprogname(), BSDoptopt);
1659 f36fd90a 2022-07-09 op return (BADCH);
1661 f36fd90a 2022-07-09 op else /* white space */
1662 f36fd90a 2022-07-09 op BSDoptarg = nargv[BSDoptind];
1663 f36fd90a 2022-07-09 op place = EMSG;
1664 f36fd90a 2022-07-09 op ++BSDoptind;
1666 f36fd90a 2022-07-09 op return (BSDoptopt); /* dump back option letter */
1669 f36fd90a 2022-07-09 op #if !HAVE_REALLOCARRAY
1671 f36fd90a 2022-07-09 op * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
1673 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1674 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1675 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1677 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1678 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1679 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1680 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1681 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1682 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1683 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1686 f36fd90a 2022-07-09 op #include <sys/types.h>
1687 f36fd90a 2022-07-09 op #include <errno.h>
1688 f36fd90a 2022-07-09 op #include <stdint.h>
1689 f36fd90a 2022-07-09 op #include <stdlib.h>
1692 f36fd90a 2022-07-09 op * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1693 f36fd90a 2022-07-09 op * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1695 f36fd90a 2022-07-09 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1698 f36fd90a 2022-07-09 op reallocarray(void *optr, size_t nmemb, size_t size)
1700 f36fd90a 2022-07-09 op if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1701 f36fd90a 2022-07-09 op nmemb > 0 && SIZE_MAX / nmemb < size) {
1702 f36fd90a 2022-07-09 op errno = ENOMEM;
1703 f36fd90a 2022-07-09 op return NULL;
1705 f36fd90a 2022-07-09 op return realloc(optr, size * nmemb);
1707 f36fd90a 2022-07-09 op #endif /* !HAVE_REALLOCARRAY */
1708 f36fd90a 2022-07-09 op #if !HAVE_RECALLOCARRAY
1710 f36fd90a 2022-07-09 op * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
1712 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1713 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1714 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1716 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1717 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1718 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1719 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1720 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1721 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1722 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1725 f36fd90a 2022-07-09 op /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
1727 f36fd90a 2022-07-09 op #include <errno.h>
1728 f36fd90a 2022-07-09 op #include <stdlib.h>
1729 f36fd90a 2022-07-09 op #include <stdint.h>
1730 f36fd90a 2022-07-09 op #include <string.h>
1731 f36fd90a 2022-07-09 op #include <unistd.h>
1734 f36fd90a 2022-07-09 op * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1735 f36fd90a 2022-07-09 op * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1737 f36fd90a 2022-07-09 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1740 f36fd90a 2022-07-09 op recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
1742 f36fd90a 2022-07-09 op size_t oldsize, newsize;
1743 f36fd90a 2022-07-09 op void *newptr;
1745 f36fd90a 2022-07-09 op if (ptr == NULL)
1746 f36fd90a 2022-07-09 op return calloc(newnmemb, size);
1748 f36fd90a 2022-07-09 op if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1749 f36fd90a 2022-07-09 op newnmemb > 0 && SIZE_MAX / newnmemb < size) {
1750 f36fd90a 2022-07-09 op errno = ENOMEM;
1751 f36fd90a 2022-07-09 op return NULL;
1753 f36fd90a 2022-07-09 op newsize = newnmemb * size;
1755 f36fd90a 2022-07-09 op if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1756 f36fd90a 2022-07-09 op oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
1757 f36fd90a 2022-07-09 op errno = EINVAL;
1758 f36fd90a 2022-07-09 op return NULL;
1760 f36fd90a 2022-07-09 op oldsize = oldnmemb * size;
1763 f36fd90a 2022-07-09 op * Don't bother too much if we're shrinking just a bit,
1764 f36fd90a 2022-07-09 op * we do not shrink for series of small steps, oh well.
1766 f36fd90a 2022-07-09 op if (newsize <= oldsize) {
1767 f36fd90a 2022-07-09 op size_t d = oldsize - newsize;
1769 f36fd90a 2022-07-09 op if (d < oldsize / 2 && d < (size_t)getpagesize()) {
1770 f36fd90a 2022-07-09 op memset((char *)ptr + newsize, 0, d);
1775 f36fd90a 2022-07-09 op newptr = malloc(newsize);
1776 f36fd90a 2022-07-09 op if (newptr == NULL)
1777 f36fd90a 2022-07-09 op return NULL;
1779 f36fd90a 2022-07-09 op if (newsize > oldsize) {
1780 f36fd90a 2022-07-09 op memcpy(newptr, ptr, oldsize);
1781 f36fd90a 2022-07-09 op memset((char *)newptr + oldsize, 0, newsize - oldsize);
1783 f36fd90a 2022-07-09 op memcpy(newptr, ptr, newsize);
1785 f36fd90a 2022-07-09 op explicit_bzero(ptr, oldsize);
1788 f36fd90a 2022-07-09 op return newptr;
1790 f36fd90a 2022-07-09 op #endif /* !HAVE_RECALLOCARRAY */
1791 f36fd90a 2022-07-09 op #if !HAVE_SETPROCTITLE
1793 f36fd90a 2022-07-09 op * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
1795 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1796 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1797 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1799 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1800 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1801 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1802 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1803 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1804 f36fd90a 2022-07-09 op * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1805 f36fd90a 2022-07-09 op * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1808 f36fd90a 2022-07-09 op # if HAVE_PR_SET_NAME
1809 f36fd90a 2022-07-09 op #include <sys/types.h>
1810 f36fd90a 2022-07-09 op #include <stdarg.h>
1811 f36fd90a 2022-07-09 op #include <stdio.h>
1812 f36fd90a 2022-07-09 op #include <string.h>
1813 f36fd90a 2022-07-09 op #include <sys/prctl.h>
1815 f36fd90a 2022-07-09 op setproctitle(const char *fmt, ...)
1817 f36fd90a 2022-07-09 op char title[16], name[16], *cp;
1821 f36fd90a 2022-07-09 op va_start(ap, fmt);
1822 f36fd90a 2022-07-09 op vsnprintf(title, sizeof(title), fmt, ap);
1825 f36fd90a 2022-07-09 op used = snprintf(name, sizeof(name), "%s: %s", getprogname(), title);
1826 f36fd90a 2022-07-09 op if (used >= (int)sizeof(name)) {
1827 f36fd90a 2022-07-09 op cp = strrchr(name, ' ');
1828 f36fd90a 2022-07-09 op if (cp != NULL)
1831 f36fd90a 2022-07-09 op prctl(PR_SET_NAME, name);
1835 f36fd90a 2022-07-09 op setproctitle(const char *fmt, ...)
1840 f36fd90a 2022-07-09 op #endif /* !HAVE_SETPROCTITLE */
1841 f36fd90a 2022-07-09 op #if !HAVE_STRLCAT
1843 f36fd90a 2022-07-09 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1845 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1846 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1847 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1849 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1850 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1851 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1852 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1853 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1854 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1855 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1858 f36fd90a 2022-07-09 op #include <sys/types.h>
1859 f36fd90a 2022-07-09 op #include <string.h>
1862 f36fd90a 2022-07-09 op * Appends src to string dst of size siz (unlike strncat, siz is the
1863 f36fd90a 2022-07-09 op * full size of dst, not space left). At most siz-1 characters
1864 f36fd90a 2022-07-09 op * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
1865 f36fd90a 2022-07-09 op * Returns strlen(src) + MIN(siz, strlen(initial dst)).
1866 f36fd90a 2022-07-09 op * If retval >= siz, truncation occurred.
1869 f36fd90a 2022-07-09 op strlcat(char *dst, const char *src, size_t siz)
1871 f36fd90a 2022-07-09 op char *d = dst;
1872 f36fd90a 2022-07-09 op const char *s = src;
1873 f36fd90a 2022-07-09 op size_t n = siz;
1874 f36fd90a 2022-07-09 op size_t dlen;
1876 f36fd90a 2022-07-09 op /* Find the end of dst and adjust bytes left but don't go past end */
1877 f36fd90a 2022-07-09 op while (n-- != 0 && *d != '\0')
1879 f36fd90a 2022-07-09 op dlen = d - dst;
1880 f36fd90a 2022-07-09 op n = siz - dlen;
1883 f36fd90a 2022-07-09 op return(dlen + strlen(s));
1884 f36fd90a 2022-07-09 op while (*s != '\0') {
1885 f36fd90a 2022-07-09 op if (n != 1) {
1893 f36fd90a 2022-07-09 op return(dlen + (s - src)); /* count does not include NUL */
1895 f36fd90a 2022-07-09 op #endif /* !HAVE_STRLCAT */
1896 f36fd90a 2022-07-09 op #if !HAVE_STRLCPY
1898 f36fd90a 2022-07-09 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1900 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1901 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1902 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1904 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1905 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1906 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1907 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1908 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1909 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1910 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1913 f36fd90a 2022-07-09 op #include <sys/types.h>
1914 f36fd90a 2022-07-09 op #include <string.h>
1917 f36fd90a 2022-07-09 op * Copy src to string dst of size siz. At most siz-1 characters
1918 f36fd90a 2022-07-09 op * will be copied. Always NUL terminates (unless siz == 0).
1919 f36fd90a 2022-07-09 op * Returns strlen(src); if retval >= siz, truncation occurred.
1922 f36fd90a 2022-07-09 op strlcpy(char *dst, const char *src, size_t siz)
1924 f36fd90a 2022-07-09 op char *d = dst;
1925 f36fd90a 2022-07-09 op const char *s = src;
1926 f36fd90a 2022-07-09 op size_t n = siz;
1928 f36fd90a 2022-07-09 op /* Copy as many bytes as will fit */
1929 f36fd90a 2022-07-09 op if (n != 0) {
1930 f36fd90a 2022-07-09 op while (--n != 0) {
1931 f36fd90a 2022-07-09 op if ((*d++ = *s++) == '\0')
1936 f36fd90a 2022-07-09 op /* Not enough room in dst, add NUL and traverse rest of src */
1937 f36fd90a 2022-07-09 op if (n == 0) {
1938 f36fd90a 2022-07-09 op if (siz != 0)
1939 f36fd90a 2022-07-09 op *d = '\0'; /* NUL-terminate dst */
1940 f36fd90a 2022-07-09 op while (*s++)
1944 f36fd90a 2022-07-09 op return(s - src - 1); /* count does not include NUL */
1946 f36fd90a 2022-07-09 op #endif /* !HAVE_STRLCPY */
1947 f36fd90a 2022-07-09 op #if !HAVE_STRNDUP
1948 f36fd90a 2022-07-09 op /* $OpenBSD$ */
1950 f36fd90a 2022-07-09 op * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1952 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1953 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1954 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1956 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1957 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1958 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1959 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1960 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1961 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1962 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1965 f36fd90a 2022-07-09 op #include <sys/types.h>
1967 f36fd90a 2022-07-09 op #include <stddef.h>
1968 f36fd90a 2022-07-09 op #include <stdlib.h>
1969 f36fd90a 2022-07-09 op #include <string.h>
1972 f36fd90a 2022-07-09 op strndup(const char *str, size_t maxlen)
1977 f36fd90a 2022-07-09 op len = strnlen(str, maxlen);
1978 f36fd90a 2022-07-09 op copy = malloc(len + 1);
1979 f36fd90a 2022-07-09 op if (copy != NULL) {
1980 f36fd90a 2022-07-09 op (void)memcpy(copy, str, len);
1981 f36fd90a 2022-07-09 op copy[len] = '\0';
1984 f36fd90a 2022-07-09 op return copy;
1986 f36fd90a 2022-07-09 op #endif /* !HAVE_STRNDUP */
1987 f36fd90a 2022-07-09 op #if !HAVE_STRNLEN
1988 f36fd90a 2022-07-09 op /* $OpenBSD$ */
1991 f36fd90a 2022-07-09 op * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1993 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1994 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1995 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1997 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1998 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1999 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2000 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2001 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2002 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2003 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2006 f36fd90a 2022-07-09 op #include <sys/types.h>
2007 f36fd90a 2022-07-09 op #include <string.h>
2010 f36fd90a 2022-07-09 op strnlen(const char *str, size_t maxlen)
2012 f36fd90a 2022-07-09 op const char *cp;
2014 f36fd90a 2022-07-09 op for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
2017 f36fd90a 2022-07-09 op return (size_t)(cp - str);
2019 f36fd90a 2022-07-09 op #endif /* !HAVE_STRNLEN */
2020 f36fd90a 2022-07-09 op #if !HAVE_STRTONUM
2022 f36fd90a 2022-07-09 op * Copyright (c) 2004 Ted Unangst and Todd Miller
2023 f36fd90a 2022-07-09 op * All rights reserved.
2025 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
2026 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
2027 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
2029 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2030 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2031 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2032 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2033 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2034 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2035 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2038 f36fd90a 2022-07-09 op #include <errno.h>
2039 f36fd90a 2022-07-09 op #include <limits.h>
2040 f36fd90a 2022-07-09 op #include <stdlib.h>
2042 f36fd90a 2022-07-09 op #define INVALID 1
2043 f36fd90a 2022-07-09 op #define TOOSMALL 2
2044 f36fd90a 2022-07-09 op #define TOOLARGE 3
2047 f36fd90a 2022-07-09 op strtonum(const char *numstr, long long minval, long long maxval,
2048 f36fd90a 2022-07-09 op const char **errstrp)
2050 f36fd90a 2022-07-09 op long long ll = 0;
2051 f36fd90a 2022-07-09 op int error = 0;
2053 f36fd90a 2022-07-09 op struct errval {
2054 f36fd90a 2022-07-09 op const char *errstr;
2057 f36fd90a 2022-07-09 op { NULL, 0 },
2058 f36fd90a 2022-07-09 op { "invalid", EINVAL },
2059 f36fd90a 2022-07-09 op { "too small", ERANGE },
2060 f36fd90a 2022-07-09 op { "too large", ERANGE },
2063 f36fd90a 2022-07-09 op ev[0].err = errno;
2065 f36fd90a 2022-07-09 op if (minval > maxval) {
2066 f36fd90a 2022-07-09 op error = INVALID;
2068 f36fd90a 2022-07-09 op ll = strtoll(numstr, &ep, 10);
2069 f36fd90a 2022-07-09 op if (numstr == ep || *ep != '\0')
2070 f36fd90a 2022-07-09 op error = INVALID;
2071 f36fd90a 2022-07-09 op else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
2072 f36fd90a 2022-07-09 op error = TOOSMALL;
2073 f36fd90a 2022-07-09 op else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
2074 f36fd90a 2022-07-09 op error = TOOLARGE;
2076 f36fd90a 2022-07-09 op if (errstrp != NULL)
2077 f36fd90a 2022-07-09 op *errstrp = ev[error].errstr;
2078 f36fd90a 2022-07-09 op errno = ev[error].err;
2082 f36fd90a 2022-07-09 op return (ll);
2084 f36fd90a 2022-07-09 op #endif /* !HAVE_STRTONUM */
2085 31e12e67 2023-09-07 op #if !HAVE_TIMESPECSUB
2086 31e12e67 2023-09-07 op #include <sys/time.h>
2088 31e12e67 2023-09-07 op timespecsub(struct timespec *a, struct timespec *b, struct timespec *ret)
2090 31e12e67 2023-09-07 op ret->tv_sec = a->tv_sec - b->tv_sec;
2091 31e12e67 2023-09-07 op ret->tv_nsec = a->tv_nsec - b->tv_nsec;
2092 31e12e67 2023-09-07 op if (ret->tv_nsec < 0) {
2093 31e12e67 2023-09-07 op ret->tv_sec--;
2094 31e12e67 2023-09-07 op ret->tv_nsec += 1000000000L;
2097 31e12e67 2023-09-07 op #endif /* !HAVE_TIMESPECSUB */