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 3ac93df9 2023-07-02 op /* $OpenBSD: imsg-buffer.c,v 1.16 2023/06/19 17:19:50 claudio Exp $ */
337 3ac93df9 2023-07-02 op /* $OpenBSD: imsg.c,v 1.19 2023/06/19 17:19:50 claudio Exp $ */
340 f36fd90a 2022-07-09 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
342 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
343 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
344 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
346 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
347 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
348 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
349 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
350 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
351 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
352 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
355 f36fd90a 2022-07-09 op #include "config.h"
357 f36fd90a 2022-07-09 op #include <sys/types.h>
358 f36fd90a 2022-07-09 op #include <sys/socket.h>
359 f36fd90a 2022-07-09 op #include <sys/uio.h>
361 f36fd90a 2022-07-09 op #include <limits.h>
362 f36fd90a 2022-07-09 op #include <errno.h>
363 263426d8 2023-10-09 op #include <endian.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 3ac93df9 2023-07-02 op if (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 3ac93df9 2023-07-02 op if (len > SIZE_MAX - buf->wpos) {
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;
457 3ac93df9 2023-07-02 op memset(b, 0, len);
462 3ac93df9 2023-07-02 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
466 3ac93df9 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
469 3ac93df9 2023-07-02 op memcpy(b, data, len);
474 3ac93df9 2023-07-02 op ibuf_add_buf(struct ibuf *buf, const struct ibuf *from)
476 3ac93df9 2023-07-02 op return ibuf_add(buf, from->buf, from->wpos);
480 3ac93df9 2023-07-02 op ibuf_add_n8(struct ibuf *buf, uint64_t value)
484 3ac93df9 2023-07-02 op if (value > UINT8_MAX) {
485 3ac93df9 2023-07-02 op errno = EINVAL;
489 3ac93df9 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
493 3ac93df9 2023-07-02 op ibuf_add_n16(struct ibuf *buf, uint64_t value)
497 3ac93df9 2023-07-02 op if (value > UINT16_MAX) {
498 3ac93df9 2023-07-02 op errno = EINVAL;
501 3ac93df9 2023-07-02 op v = htobe16(value);
502 3ac93df9 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
506 3ac93df9 2023-07-02 op ibuf_add_n32(struct ibuf *buf, uint64_t value)
510 3ac93df9 2023-07-02 op if (value > UINT32_MAX) {
511 3ac93df9 2023-07-02 op errno = EINVAL;
514 3ac93df9 2023-07-02 op v = htobe32(value);
515 3ac93df9 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
519 3ac93df9 2023-07-02 op ibuf_add_n64(struct ibuf *buf, uint64_t value)
521 3ac93df9 2023-07-02 op value = htobe64(value);
522 3ac93df9 2023-07-02 op return ibuf_add(buf, &value, sizeof(value));
526 3ac93df9 2023-07-02 op ibuf_add_zero(struct ibuf *buf, size_t len)
530 3ac93df9 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
536 f36fd90a 2022-07-09 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
538 f36fd90a 2022-07-09 op /* only allowed to seek in already written parts */
539 3ac93df9 2023-07-02 op if (len > SIZE_MAX - pos || pos + len > buf->wpos) {
540 3ac93df9 2023-07-02 op errno = ERANGE;
541 f36fd90a 2022-07-09 op return (NULL);
544 f36fd90a 2022-07-09 op return (buf->buf + pos);
548 3ac93df9 2023-07-02 op ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
552 3ac93df9 2023-07-02 op if ((b = ibuf_seek(buf, pos, len)) == NULL)
555 3ac93df9 2023-07-02 op memcpy(b, data, len);
560 3ac93df9 2023-07-02 op ibuf_set_n8(struct ibuf *buf, size_t pos, uint64_t value)
564 3ac93df9 2023-07-02 op if (value > UINT8_MAX) {
565 3ac93df9 2023-07-02 op errno = EINVAL;
569 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
573 3ac93df9 2023-07-02 op ibuf_set_n16(struct ibuf *buf, size_t pos, uint64_t value)
577 3ac93df9 2023-07-02 op if (value > UINT16_MAX) {
578 3ac93df9 2023-07-02 op errno = EINVAL;
581 3ac93df9 2023-07-02 op v = htobe16(value);
582 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
586 3ac93df9 2023-07-02 op ibuf_set_n32(struct ibuf *buf, size_t pos, uint64_t value)
590 3ac93df9 2023-07-02 op if (value > UINT32_MAX) {
591 3ac93df9 2023-07-02 op errno = EINVAL;
594 3ac93df9 2023-07-02 op v = htobe32(value);
595 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
599 3ac93df9 2023-07-02 op ibuf_set_n64(struct ibuf *buf, size_t pos, uint64_t value)
601 3ac93df9 2023-07-02 op value = htobe64(value);
602 3ac93df9 2023-07-02 op return (ibuf_set(buf, pos, &value, sizeof(value)));
606 3ac93df9 2023-07-02 op ibuf_data(struct ibuf *buf)
608 3ac93df9 2023-07-02 op return (buf->buf);
612 f36fd90a 2022-07-09 op ibuf_size(struct ibuf *buf)
614 f36fd90a 2022-07-09 op return (buf->wpos);
618 f36fd90a 2022-07-09 op ibuf_left(struct ibuf *buf)
620 f36fd90a 2022-07-09 op return (buf->max - buf->wpos);
624 f36fd90a 2022-07-09 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
626 f36fd90a 2022-07-09 op ibuf_enqueue(msgbuf, buf);
630 3ac93df9 2023-07-02 op ibuf_free(struct ibuf *buf)
632 3ac93df9 2023-07-02 op if (buf == NULL)
634 3ac93df9 2023-07-02 op #ifdef NOTYET
635 3ac93df9 2023-07-02 op if (buf->fd != -1)
636 3ac93df9 2023-07-02 op close(buf->fd);
638 3ac93df9 2023-07-02 op freezero(buf->buf, buf->size);
643 3ac93df9 2023-07-02 op ibuf_fd_avail(struct ibuf *buf)
645 3ac93df9 2023-07-02 op return (buf->fd != -1);
649 3ac93df9 2023-07-02 op ibuf_fd_get(struct ibuf *buf)
653 3ac93df9 2023-07-02 op fd = buf->fd;
654 3ac93df9 2023-07-02 op #ifdef NOTYET
655 3ac93df9 2023-07-02 op buf->fd = -1;
661 3ac93df9 2023-07-02 op ibuf_fd_set(struct ibuf *buf, int fd)
663 3ac93df9 2023-07-02 op if (buf->fd != -1)
664 3ac93df9 2023-07-02 op close(buf->fd);
665 3ac93df9 2023-07-02 op buf->fd = fd;
669 f36fd90a 2022-07-09 op ibuf_write(struct msgbuf *msgbuf)
671 f36fd90a 2022-07-09 op struct iovec iov[IOV_MAX];
672 f36fd90a 2022-07-09 op struct ibuf *buf;
673 f36fd90a 2022-07-09 op unsigned int i = 0;
676 f36fd90a 2022-07-09 op memset(&iov, 0, sizeof(iov));
677 f36fd90a 2022-07-09 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
678 f36fd90a 2022-07-09 op if (i >= IOV_MAX)
680 f36fd90a 2022-07-09 op iov[i].iov_base = buf->buf + buf->rpos;
681 f36fd90a 2022-07-09 op iov[i].iov_len = buf->wpos - buf->rpos;
686 f36fd90a 2022-07-09 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
687 f36fd90a 2022-07-09 op if (errno == EINTR)
689 f36fd90a 2022-07-09 op if (errno == ENOBUFS)
690 f36fd90a 2022-07-09 op errno = EAGAIN;
694 f36fd90a 2022-07-09 op if (n == 0) { /* connection closed */
699 f36fd90a 2022-07-09 op msgbuf_drain(msgbuf, n);
705 f36fd90a 2022-07-09 op msgbuf_init(struct msgbuf *msgbuf)
707 f36fd90a 2022-07-09 op msgbuf->queued = 0;
708 f36fd90a 2022-07-09 op msgbuf->fd = -1;
709 f36fd90a 2022-07-09 op TAILQ_INIT(&msgbuf->bufs);
713 f36fd90a 2022-07-09 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
715 f36fd90a 2022-07-09 op struct ibuf *buf, *next;
717 f36fd90a 2022-07-09 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
718 f36fd90a 2022-07-09 op buf = next) {
719 f36fd90a 2022-07-09 op next = TAILQ_NEXT(buf, entry);
720 3ac93df9 2023-07-02 op if (n >= buf->wpos - buf->rpos) {
721 f36fd90a 2022-07-09 op n -= buf->wpos - buf->rpos;
722 f36fd90a 2022-07-09 op ibuf_dequeue(msgbuf, buf);
724 f36fd90a 2022-07-09 op buf->rpos += n;
731 f36fd90a 2022-07-09 op msgbuf_clear(struct msgbuf *msgbuf)
733 f36fd90a 2022-07-09 op struct ibuf *buf;
735 f36fd90a 2022-07-09 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
736 f36fd90a 2022-07-09 op ibuf_dequeue(msgbuf, buf);
740 f36fd90a 2022-07-09 op msgbuf_write(struct msgbuf *msgbuf)
742 f36fd90a 2022-07-09 op struct iovec iov[IOV_MAX];
743 f36fd90a 2022-07-09 op struct ibuf *buf, *buf0 = NULL;
744 f36fd90a 2022-07-09 op unsigned int i = 0;
746 f36fd90a 2022-07-09 op struct msghdr msg;
747 f36fd90a 2022-07-09 op struct cmsghdr *cmsg;
749 f36fd90a 2022-07-09 op struct cmsghdr hdr;
750 f36fd90a 2022-07-09 op char buf[CMSG_SPACE(sizeof(int))];
753 f36fd90a 2022-07-09 op memset(&iov, 0, sizeof(iov));
754 f36fd90a 2022-07-09 op memset(&msg, 0, sizeof(msg));
755 f36fd90a 2022-07-09 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
756 f36fd90a 2022-07-09 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
757 f36fd90a 2022-07-09 op if (i >= IOV_MAX)
759 f36fd90a 2022-07-09 op if (i > 0 && buf->fd != -1)
761 f36fd90a 2022-07-09 op iov[i].iov_base = buf->buf + buf->rpos;
762 f36fd90a 2022-07-09 op iov[i].iov_len = buf->wpos - buf->rpos;
764 f36fd90a 2022-07-09 op if (buf->fd != -1)
768 f36fd90a 2022-07-09 op msg.msg_iov = iov;
769 f36fd90a 2022-07-09 op msg.msg_iovlen = i;
771 f36fd90a 2022-07-09 op if (buf0 != NULL) {
772 f36fd90a 2022-07-09 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
773 f36fd90a 2022-07-09 op msg.msg_controllen = sizeof(cmsgbuf.buf);
774 f36fd90a 2022-07-09 op cmsg = CMSG_FIRSTHDR(&msg);
775 f36fd90a 2022-07-09 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
776 f36fd90a 2022-07-09 op cmsg->cmsg_level = SOL_SOCKET;
777 f36fd90a 2022-07-09 op cmsg->cmsg_type = SCM_RIGHTS;
778 f36fd90a 2022-07-09 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
782 f36fd90a 2022-07-09 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
783 f36fd90a 2022-07-09 op if (errno == EINTR)
785 f36fd90a 2022-07-09 op if (errno == ENOBUFS)
786 f36fd90a 2022-07-09 op errno = EAGAIN;
790 f36fd90a 2022-07-09 op if (n == 0) { /* connection closed */
796 f36fd90a 2022-07-09 op * assumption: fd got sent if sendmsg sent anything
797 f36fd90a 2022-07-09 op * this works because fds are passed one at a time
799 f36fd90a 2022-07-09 op if (buf0 != NULL) {
800 f36fd90a 2022-07-09 op close(buf0->fd);
801 f36fd90a 2022-07-09 op buf0->fd = -1;
804 f36fd90a 2022-07-09 op msgbuf_drain(msgbuf, n);
810 f36fd90a 2022-07-09 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
812 f36fd90a 2022-07-09 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
813 f36fd90a 2022-07-09 op msgbuf->queued++;
817 f36fd90a 2022-07-09 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
819 f36fd90a 2022-07-09 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
821 3ac93df9 2023-07-02 op if (buf->fd != -1) {
822 f36fd90a 2022-07-09 op close(buf->fd);
823 3ac93df9 2023-07-02 op buf->fd = -1;
826 f36fd90a 2022-07-09 op msgbuf->queued--;
827 f36fd90a 2022-07-09 op ibuf_free(buf);
832 f36fd90a 2022-07-09 op int imsg_fd_overhead = 0;
834 f36fd90a 2022-07-09 op static int imsg_get_fd(struct imsgbuf *);
837 f36fd90a 2022-07-09 op imsg_init(struct imsgbuf *ibuf, int fd)
839 f36fd90a 2022-07-09 op msgbuf_init(&ibuf->w);
840 f36fd90a 2022-07-09 op memset(&ibuf->r, 0, sizeof(ibuf->r));
841 f36fd90a 2022-07-09 op ibuf->fd = fd;
842 f36fd90a 2022-07-09 op ibuf->w.fd = fd;
843 f36fd90a 2022-07-09 op ibuf->pid = getpid();
844 f36fd90a 2022-07-09 op TAILQ_INIT(&ibuf->fds);
848 f36fd90a 2022-07-09 op imsg_read(struct imsgbuf *ibuf)
850 f36fd90a 2022-07-09 op struct msghdr msg;
851 f36fd90a 2022-07-09 op struct cmsghdr *cmsg;
853 f36fd90a 2022-07-09 op struct cmsghdr hdr;
854 f36fd90a 2022-07-09 op char buf[CMSG_SPACE(sizeof(int) * 1)];
856 f36fd90a 2022-07-09 op struct iovec iov;
857 f36fd90a 2022-07-09 op ssize_t n = -1;
859 f36fd90a 2022-07-09 op struct imsg_fd *ifd;
861 f36fd90a 2022-07-09 op memset(&msg, 0, sizeof(msg));
862 f36fd90a 2022-07-09 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
864 f36fd90a 2022-07-09 op iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
865 f36fd90a 2022-07-09 op iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
866 f36fd90a 2022-07-09 op msg.msg_iov = &iov;
867 f36fd90a 2022-07-09 op msg.msg_iovlen = 1;
868 f36fd90a 2022-07-09 op msg.msg_control = &cmsgbuf.buf;
869 f36fd90a 2022-07-09 op msg.msg_controllen = sizeof(cmsgbuf.buf);
871 f36fd90a 2022-07-09 op if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
875 f36fd90a 2022-07-09 op if (getdtablecount() + imsg_fd_overhead +
876 f36fd90a 2022-07-09 op (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
877 f36fd90a 2022-07-09 op >= getdtablesize()) {
878 f36fd90a 2022-07-09 op errno = EAGAIN;
883 f36fd90a 2022-07-09 op if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
884 f36fd90a 2022-07-09 op if (errno == EINTR)
889 f36fd90a 2022-07-09 op ibuf->r.wpos += n;
891 f36fd90a 2022-07-09 op for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
892 f36fd90a 2022-07-09 op cmsg = CMSG_NXTHDR(&msg, cmsg)) {
893 f36fd90a 2022-07-09 op if (cmsg->cmsg_level == SOL_SOCKET &&
894 f36fd90a 2022-07-09 op cmsg->cmsg_type == SCM_RIGHTS) {
899 f36fd90a 2022-07-09 op * We only accept one file descriptor. Due to C
900 f36fd90a 2022-07-09 op * padding rules, our control buffer might contain
901 f36fd90a 2022-07-09 op * more than one fd, and we must close them.
903 f36fd90a 2022-07-09 op j = ((char *)cmsg + cmsg->cmsg_len -
904 f36fd90a 2022-07-09 op (char *)CMSG_DATA(cmsg)) / sizeof(int);
905 f36fd90a 2022-07-09 op for (i = 0; i < j; i++) {
906 f36fd90a 2022-07-09 op fd = ((int *)CMSG_DATA(cmsg))[i];
907 f36fd90a 2022-07-09 op if (ifd != NULL) {
908 f36fd90a 2022-07-09 op ifd->fd = fd;
909 f36fd90a 2022-07-09 op TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
916 f36fd90a 2022-07-09 op /* we do not handle other ctl data level */
925 f36fd90a 2022-07-09 op imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
927 f36fd90a 2022-07-09 op size_t av, left, datalen;
929 f36fd90a 2022-07-09 op av = ibuf->r.wpos;
931 f36fd90a 2022-07-09 op if (IMSG_HEADER_SIZE > av)
934 f36fd90a 2022-07-09 op memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
935 f36fd90a 2022-07-09 op if (imsg->hdr.len < IMSG_HEADER_SIZE ||
936 f36fd90a 2022-07-09 op imsg->hdr.len > MAX_IMSGSIZE) {
937 f36fd90a 2022-07-09 op errno = ERANGE;
940 f36fd90a 2022-07-09 op if (imsg->hdr.len > av)
942 f36fd90a 2022-07-09 op datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
943 f36fd90a 2022-07-09 op ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
944 f36fd90a 2022-07-09 op if (datalen == 0)
945 f36fd90a 2022-07-09 op imsg->data = NULL;
946 f36fd90a 2022-07-09 op else if ((imsg->data = malloc(datalen)) == NULL)
949 f36fd90a 2022-07-09 op if (imsg->hdr.flags & IMSGF_HASFD)
950 f36fd90a 2022-07-09 op imsg->fd = imsg_get_fd(ibuf);
952 f36fd90a 2022-07-09 op imsg->fd = -1;
954 f36fd90a 2022-07-09 op if (datalen != 0)
955 f36fd90a 2022-07-09 op memcpy(imsg->data, ibuf->r.rptr, datalen);
957 f36fd90a 2022-07-09 op if (imsg->hdr.len < av) {
958 f36fd90a 2022-07-09 op left = av - imsg->hdr.len;
959 f36fd90a 2022-07-09 op memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
960 f36fd90a 2022-07-09 op ibuf->r.wpos = left;
962 f36fd90a 2022-07-09 op ibuf->r.wpos = 0;
964 f36fd90a 2022-07-09 op return (datalen + IMSG_HEADER_SIZE);
968 f36fd90a 2022-07-09 op imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
969 f36fd90a 2022-07-09 op int fd, const void *data, uint16_t datalen)
971 f36fd90a 2022-07-09 op struct ibuf *wbuf;
973 f36fd90a 2022-07-09 op if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
976 f36fd90a 2022-07-09 op if (imsg_add(wbuf, data, datalen) == -1)
979 3ac93df9 2023-07-02 op ibuf_fd_set(wbuf, fd);
980 f36fd90a 2022-07-09 op imsg_close(ibuf, wbuf);
986 f36fd90a 2022-07-09 op imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
987 f36fd90a 2022-07-09 op int fd, const struct iovec *iov, int iovcnt)
989 f36fd90a 2022-07-09 op struct ibuf *wbuf;
990 f36fd90a 2022-07-09 op int i, datalen = 0;
992 f36fd90a 2022-07-09 op for (i = 0; i < iovcnt; i++)
993 f36fd90a 2022-07-09 op datalen += iov[i].iov_len;
995 f36fd90a 2022-07-09 op if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
998 f36fd90a 2022-07-09 op for (i = 0; i < iovcnt; i++)
999 f36fd90a 2022-07-09 op if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
1000 f36fd90a 2022-07-09 op return (-1);
1002 3ac93df9 2023-07-02 op ibuf_fd_set(wbuf, fd);
1003 f36fd90a 2022-07-09 op imsg_close(ibuf, wbuf);
1009 3ac93df9 2023-07-02 op imsg_compose_ibuf(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
1010 3ac93df9 2023-07-02 op pid_t pid, struct ibuf *buf)
1012 3ac93df9 2023-07-02 op struct ibuf *wbuf = NULL;
1013 3ac93df9 2023-07-02 op struct imsg_hdr hdr;
1014 3ac93df9 2023-07-02 op int save_errno;
1016 3ac93df9 2023-07-02 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
1017 3ac93df9 2023-07-02 op errno = ERANGE;
1021 3ac93df9 2023-07-02 op hdr.type = type;
1022 3ac93df9 2023-07-02 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
1023 3ac93df9 2023-07-02 op hdr.flags = 0;
1024 3ac93df9 2023-07-02 op hdr.peerid = peerid;
1025 3ac93df9 2023-07-02 op if ((hdr.pid = pid) == 0)
1026 3ac93df9 2023-07-02 op hdr.pid = ibuf->pid;
1028 3ac93df9 2023-07-02 op if ((wbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
1030 3ac93df9 2023-07-02 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
1033 3ac93df9 2023-07-02 op ibuf_close(&ibuf->w, wbuf);
1034 3ac93df9 2023-07-02 op ibuf_close(&ibuf->w, buf);
1038 3ac93df9 2023-07-02 op save_errno = errno;
1039 3ac93df9 2023-07-02 op ibuf_free(buf);
1040 3ac93df9 2023-07-02 op ibuf_free(wbuf);
1041 3ac93df9 2023-07-02 op errno = save_errno;
1042 3ac93df9 2023-07-02 op return (-1);
1045 f36fd90a 2022-07-09 op struct ibuf *
1046 f36fd90a 2022-07-09 op imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
1047 f36fd90a 2022-07-09 op uint16_t datalen)
1049 f36fd90a 2022-07-09 op struct ibuf *wbuf;
1050 f36fd90a 2022-07-09 op struct imsg_hdr hdr;
1052 f36fd90a 2022-07-09 op datalen += IMSG_HEADER_SIZE;
1053 f36fd90a 2022-07-09 op if (datalen > MAX_IMSGSIZE) {
1054 f36fd90a 2022-07-09 op errno = ERANGE;
1055 f36fd90a 2022-07-09 op return (NULL);
1058 f36fd90a 2022-07-09 op hdr.type = type;
1059 f36fd90a 2022-07-09 op hdr.flags = 0;
1060 f36fd90a 2022-07-09 op hdr.peerid = peerid;
1061 f36fd90a 2022-07-09 op if ((hdr.pid = pid) == 0)
1062 f36fd90a 2022-07-09 op hdr.pid = ibuf->pid;
1063 f36fd90a 2022-07-09 op if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
1064 f36fd90a 2022-07-09 op return (NULL);
1066 f36fd90a 2022-07-09 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
1067 f36fd90a 2022-07-09 op return (NULL);
1069 f36fd90a 2022-07-09 op return (wbuf);
1073 f36fd90a 2022-07-09 op imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
1075 f36fd90a 2022-07-09 op if (datalen)
1076 f36fd90a 2022-07-09 op if (ibuf_add(msg, data, datalen) == -1) {
1077 f36fd90a 2022-07-09 op ibuf_free(msg);
1078 f36fd90a 2022-07-09 op return (-1);
1080 f36fd90a 2022-07-09 op return (datalen);
1084 f36fd90a 2022-07-09 op imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
1086 f36fd90a 2022-07-09 op struct imsg_hdr *hdr;
1088 f36fd90a 2022-07-09 op hdr = (struct imsg_hdr *)msg->buf;
1090 f36fd90a 2022-07-09 op hdr->flags &= ~IMSGF_HASFD;
1091 3ac93df9 2023-07-02 op if (ibuf_fd_avail(msg))
1092 f36fd90a 2022-07-09 op hdr->flags |= IMSGF_HASFD;
1093 3ac93df9 2023-07-02 op hdr->len = ibuf_size(msg);
1095 f36fd90a 2022-07-09 op ibuf_close(&ibuf->w, msg);
1099 f36fd90a 2022-07-09 op imsg_free(struct imsg *imsg)
1101 f36fd90a 2022-07-09 op freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
1105 f36fd90a 2022-07-09 op imsg_get_fd(struct imsgbuf *ibuf)
1108 f36fd90a 2022-07-09 op struct imsg_fd *ifd;
1110 f36fd90a 2022-07-09 op if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
1111 f36fd90a 2022-07-09 op return (-1);
1113 f36fd90a 2022-07-09 op fd = ifd->fd;
1114 f36fd90a 2022-07-09 op TAILQ_REMOVE(&ibuf->fds, ifd, entry);
1117 f36fd90a 2022-07-09 op return (fd);
1121 f36fd90a 2022-07-09 op imsg_flush(struct imsgbuf *ibuf)
1123 f36fd90a 2022-07-09 op while (ibuf->w.queued)
1124 f36fd90a 2022-07-09 op if (msgbuf_write(&ibuf->w) <= 0)
1125 f36fd90a 2022-07-09 op return (-1);
1130 f36fd90a 2022-07-09 op imsg_clear(struct imsgbuf *ibuf)
1134 f36fd90a 2022-07-09 op msgbuf_clear(&ibuf->w);
1135 f36fd90a 2022-07-09 op while ((fd = imsg_get_fd(ibuf)) != -1)
1138 82556d5b 2023-09-07 op #endif /* HAVE_LIB_IMSG */
1139 f36fd90a 2022-07-09 op #if !HAVE_MEMMEM
1141 f36fd90a 2022-07-09 op * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
1143 f36fd90a 2022-07-09 op * Redistribution and use in source and binary forms, with or without
1144 f36fd90a 2022-07-09 op * modification, are permitted provided that the following conditions
1146 f36fd90a 2022-07-09 op * 1. Redistributions of source code must retain the above copyright
1147 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer.
1148 f36fd90a 2022-07-09 op * 2. Redistributions in binary form must reproduce the above copyright
1149 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer in
1150 f36fd90a 2022-07-09 op * the documentation and/or other materials provided with the
1151 f36fd90a 2022-07-09 op * distribution.
1152 f36fd90a 2022-07-09 op * 3. The name of the author may not be used to endorse or promote
1153 f36fd90a 2022-07-09 op * products derived from this software without specific prior written
1154 f36fd90a 2022-07-09 op * permission.
1156 f36fd90a 2022-07-09 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
1157 f36fd90a 2022-07-09 op * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1158 f36fd90a 2022-07-09 op * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1159 f36fd90a 2022-07-09 op * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
1160 f36fd90a 2022-07-09 op * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1161 f36fd90a 2022-07-09 op * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1162 f36fd90a 2022-07-09 op * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1163 f36fd90a 2022-07-09 op * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
1164 f36fd90a 2022-07-09 op * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1165 f36fd90a 2022-07-09 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1166 f36fd90a 2022-07-09 op * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1169 f36fd90a 2022-07-09 op * Find the first occurrence of the byte string s in byte string l.
1172 f36fd90a 2022-07-09 op memmem(const void *l, size_t l_len, const void *s, size_t s_len)
1174 f36fd90a 2022-07-09 op const char *cur, *last;
1175 f36fd90a 2022-07-09 op const char *cl = l;
1176 f36fd90a 2022-07-09 op const char *cs = s;
1178 f36fd90a 2022-07-09 op /* a zero length needle should just return the haystack */
1179 f36fd90a 2022-07-09 op if (l_len == 0)
1180 f36fd90a 2022-07-09 op return (void *)cl;
1182 f36fd90a 2022-07-09 op /* "s" must be smaller or equal to "l" */
1183 f36fd90a 2022-07-09 op if (l_len < s_len)
1184 f36fd90a 2022-07-09 op return NULL;
1186 f36fd90a 2022-07-09 op /* special case where s_len == 1 */
1187 f36fd90a 2022-07-09 op if (s_len == 1)
1188 f36fd90a 2022-07-09 op return memchr(l, *cs, l_len);
1190 f36fd90a 2022-07-09 op /* the last position where its possible to find "s" in "l" */
1191 f36fd90a 2022-07-09 op last = cl + l_len - s_len;
1193 f36fd90a 2022-07-09 op for (cur = cl; cur <= last; cur++)
1194 f36fd90a 2022-07-09 op if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
1195 f36fd90a 2022-07-09 op return (void *)cur;
1197 f36fd90a 2022-07-09 op return NULL;
1199 f36fd90a 2022-07-09 op #endif /* !HAVE_MEMMEM */
1200 f36fd90a 2022-07-09 op #if !HAVE_MEMRCHR
1202 f36fd90a 2022-07-09 op * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
1204 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1205 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1206 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1208 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1209 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1210 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1211 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1212 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1213 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1214 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1218 f36fd90a 2022-07-09 op #include <string.h>
1221 f36fd90a 2022-07-09 op * Reverse memchr()
1222 f36fd90a 2022-07-09 op * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
1225 f36fd90a 2022-07-09 op memrchr(const void *s, int c, size_t n)
1227 f36fd90a 2022-07-09 op const unsigned char *cp;
1229 f36fd90a 2022-07-09 op if (n != 0) {
1230 f36fd90a 2022-07-09 op cp = (unsigned char *)s + n;
1232 f36fd90a 2022-07-09 op if (*(--cp) == (unsigned char)c)
1233 f36fd90a 2022-07-09 op return((void *)cp);
1234 f36fd90a 2022-07-09 op } while (--n != 0);
1236 f36fd90a 2022-07-09 op return(NULL);
1238 f36fd90a 2022-07-09 op #endif /* !HAVE_MEMRCHR */
1239 f36fd90a 2022-07-09 op #if !HAVE_OPTRESET
1241 f36fd90a 2022-07-09 op * Copyright (c) 1987, 1993, 1994
1242 f36fd90a 2022-07-09 op * The Regents of the University of California. All rights reserved.
1244 f36fd90a 2022-07-09 op * Redistribution and use in source and binary forms, with or without
1245 f36fd90a 2022-07-09 op * modification, are permitted provided that the following conditions
1247 f36fd90a 2022-07-09 op * 1. Redistributions of source code must retain the above copyright
1248 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer.
1249 f36fd90a 2022-07-09 op * 2. Redistributions in binary form must reproduce the above copyright
1250 f36fd90a 2022-07-09 op * notice, this list of conditions and the following disclaimer in the
1251 f36fd90a 2022-07-09 op * documentation and/or other materials provided with the distribution.
1252 f36fd90a 2022-07-09 op * 3. Neither the name of the University nor the names of its contributors
1253 f36fd90a 2022-07-09 op * may be used to endorse or promote products derived from this software
1254 f36fd90a 2022-07-09 op * without specific prior written permission.
1256 f36fd90a 2022-07-09 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1257 f36fd90a 2022-07-09 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1258 f36fd90a 2022-07-09 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1259 f36fd90a 2022-07-09 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1260 f36fd90a 2022-07-09 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1261 f36fd90a 2022-07-09 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1262 f36fd90a 2022-07-09 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1263 f36fd90a 2022-07-09 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1264 f36fd90a 2022-07-09 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1265 f36fd90a 2022-07-09 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1266 f36fd90a 2022-07-09 op * SUCH DAMAGE.
1269 f36fd90a 2022-07-09 op /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
1271 f36fd90a 2022-07-09 op #include <stdio.h>
1272 f36fd90a 2022-07-09 op #include <stdlib.h>
1273 f36fd90a 2022-07-09 op #include <string.h>
1275 f36fd90a 2022-07-09 op int BSDopterr = 1, /* if error message should be printed */
1276 f36fd90a 2022-07-09 op BSDoptind = 1, /* index into parent argv vector */
1277 f36fd90a 2022-07-09 op BSDoptopt, /* character checked for validity */
1278 f36fd90a 2022-07-09 op BSDoptreset; /* reset getopt */
1279 f36fd90a 2022-07-09 op char *BSDoptarg; /* argument associated with option */
1281 f36fd90a 2022-07-09 op #define BADCH (int)'?'
1282 f36fd90a 2022-07-09 op #define BADARG (int)':'
1283 f36fd90a 2022-07-09 op #define EMSG ""
1287 f36fd90a 2022-07-09 op * Parse argc/argv argument vector.
1290 f36fd90a 2022-07-09 op BSDgetopt(int nargc, char *const *nargv, const char *ostr)
1292 f36fd90a 2022-07-09 op static const char *place = EMSG; /* option letter processing */
1293 f36fd90a 2022-07-09 op char *oli; /* option letter list index */
1295 f36fd90a 2022-07-09 op if (ostr == NULL)
1296 f36fd90a 2022-07-09 op return (-1);
1298 f36fd90a 2022-07-09 op if (BSDoptreset || !*place) { /* update scanning pointer */
1299 f36fd90a 2022-07-09 op BSDoptreset = 0;
1300 f36fd90a 2022-07-09 op if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
1301 f36fd90a 2022-07-09 op place = EMSG;
1302 f36fd90a 2022-07-09 op return (-1);
1304 f36fd90a 2022-07-09 op if (place[1] && *++place == '-') { /* found "--" */
1305 f36fd90a 2022-07-09 op if (place[1])
1306 f36fd90a 2022-07-09 op return (BADCH);
1307 f36fd90a 2022-07-09 op ++BSDoptind;
1308 f36fd90a 2022-07-09 op place = EMSG;
1309 f36fd90a 2022-07-09 op return (-1);
1311 f36fd90a 2022-07-09 op } /* option letter okay? */
1312 f36fd90a 2022-07-09 op if ((BSDoptopt = (int)*place++) == (int)':' ||
1313 f36fd90a 2022-07-09 op !(oli = strchr(ostr, BSDoptopt))) {
1315 f36fd90a 2022-07-09 op * if the user didn't specify '-' as an option,
1316 f36fd90a 2022-07-09 op * assume it means -1.
1318 f36fd90a 2022-07-09 op if (BSDoptopt == (int)'-')
1319 f36fd90a 2022-07-09 op return (-1);
1320 f36fd90a 2022-07-09 op if (!*place)
1321 f36fd90a 2022-07-09 op ++BSDoptind;
1322 f36fd90a 2022-07-09 op if (BSDopterr && *ostr != ':')
1323 f36fd90a 2022-07-09 op (void)fprintf(stderr,
1324 f36fd90a 2022-07-09 op "%s: unknown option -- %c\n", getprogname(),
1326 f36fd90a 2022-07-09 op return (BADCH);
1328 f36fd90a 2022-07-09 op if (*++oli != ':') { /* don't need argument */
1329 f36fd90a 2022-07-09 op BSDoptarg = NULL;
1330 f36fd90a 2022-07-09 op if (!*place)
1331 f36fd90a 2022-07-09 op ++BSDoptind;
1333 f36fd90a 2022-07-09 op else { /* need an argument */
1334 f36fd90a 2022-07-09 op if (*place) /* no white space */
1335 f36fd90a 2022-07-09 op BSDoptarg = (char *)place;
1336 f36fd90a 2022-07-09 op else if (nargc <= ++BSDoptind) { /* no arg */
1337 f36fd90a 2022-07-09 op place = EMSG;
1338 f36fd90a 2022-07-09 op if (*ostr == ':')
1339 f36fd90a 2022-07-09 op return (BADARG);
1340 f36fd90a 2022-07-09 op if (BSDopterr)
1341 f36fd90a 2022-07-09 op (void)fprintf(stderr,
1342 f36fd90a 2022-07-09 op "%s: option requires an argument -- %c\n",
1343 f36fd90a 2022-07-09 op getprogname(), BSDoptopt);
1344 f36fd90a 2022-07-09 op return (BADCH);
1346 f36fd90a 2022-07-09 op else /* white space */
1347 f36fd90a 2022-07-09 op BSDoptarg = nargv[BSDoptind];
1348 f36fd90a 2022-07-09 op place = EMSG;
1349 f36fd90a 2022-07-09 op ++BSDoptind;
1351 f36fd90a 2022-07-09 op return (BSDoptopt); /* dump back option letter */
1354 f36fd90a 2022-07-09 op #if !HAVE_REALLOCARRAY
1356 f36fd90a 2022-07-09 op * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
1358 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1359 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1360 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1362 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1363 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1364 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1365 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1366 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1367 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1368 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1371 f36fd90a 2022-07-09 op #include <sys/types.h>
1372 f36fd90a 2022-07-09 op #include <errno.h>
1373 f36fd90a 2022-07-09 op #include <stdint.h>
1374 f36fd90a 2022-07-09 op #include <stdlib.h>
1377 f36fd90a 2022-07-09 op * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1378 f36fd90a 2022-07-09 op * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1380 f36fd90a 2022-07-09 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1383 f36fd90a 2022-07-09 op reallocarray(void *optr, size_t nmemb, size_t size)
1385 f36fd90a 2022-07-09 op if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1386 f36fd90a 2022-07-09 op nmemb > 0 && SIZE_MAX / nmemb < size) {
1387 f36fd90a 2022-07-09 op errno = ENOMEM;
1388 f36fd90a 2022-07-09 op return NULL;
1390 f36fd90a 2022-07-09 op return realloc(optr, size * nmemb);
1392 f36fd90a 2022-07-09 op #endif /* !HAVE_REALLOCARRAY */
1393 f36fd90a 2022-07-09 op #if !HAVE_RECALLOCARRAY
1395 f36fd90a 2022-07-09 op * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
1397 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1398 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1399 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1401 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1402 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1403 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1404 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1405 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1406 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1407 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1410 f36fd90a 2022-07-09 op /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
1412 f36fd90a 2022-07-09 op #include <errno.h>
1413 f36fd90a 2022-07-09 op #include <stdlib.h>
1414 f36fd90a 2022-07-09 op #include <stdint.h>
1415 f36fd90a 2022-07-09 op #include <string.h>
1416 f36fd90a 2022-07-09 op #include <unistd.h>
1419 f36fd90a 2022-07-09 op * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1420 f36fd90a 2022-07-09 op * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1422 f36fd90a 2022-07-09 op #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
1425 f36fd90a 2022-07-09 op recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
1427 f36fd90a 2022-07-09 op size_t oldsize, newsize;
1428 f36fd90a 2022-07-09 op void *newptr;
1430 f36fd90a 2022-07-09 op if (ptr == NULL)
1431 f36fd90a 2022-07-09 op return calloc(newnmemb, size);
1433 f36fd90a 2022-07-09 op if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1434 f36fd90a 2022-07-09 op newnmemb > 0 && SIZE_MAX / newnmemb < size) {
1435 f36fd90a 2022-07-09 op errno = ENOMEM;
1436 f36fd90a 2022-07-09 op return NULL;
1438 f36fd90a 2022-07-09 op newsize = newnmemb * size;
1440 f36fd90a 2022-07-09 op if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1441 f36fd90a 2022-07-09 op oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
1442 f36fd90a 2022-07-09 op errno = EINVAL;
1443 f36fd90a 2022-07-09 op return NULL;
1445 f36fd90a 2022-07-09 op oldsize = oldnmemb * size;
1448 f36fd90a 2022-07-09 op * Don't bother too much if we're shrinking just a bit,
1449 f36fd90a 2022-07-09 op * we do not shrink for series of small steps, oh well.
1451 f36fd90a 2022-07-09 op if (newsize <= oldsize) {
1452 f36fd90a 2022-07-09 op size_t d = oldsize - newsize;
1454 f36fd90a 2022-07-09 op if (d < oldsize / 2 && d < (size_t)getpagesize()) {
1455 f36fd90a 2022-07-09 op memset((char *)ptr + newsize, 0, d);
1460 f36fd90a 2022-07-09 op newptr = malloc(newsize);
1461 f36fd90a 2022-07-09 op if (newptr == NULL)
1462 f36fd90a 2022-07-09 op return NULL;
1464 f36fd90a 2022-07-09 op if (newsize > oldsize) {
1465 f36fd90a 2022-07-09 op memcpy(newptr, ptr, oldsize);
1466 f36fd90a 2022-07-09 op memset((char *)newptr + oldsize, 0, newsize - oldsize);
1468 f36fd90a 2022-07-09 op memcpy(newptr, ptr, newsize);
1470 f36fd90a 2022-07-09 op explicit_bzero(ptr, oldsize);
1473 f36fd90a 2022-07-09 op return newptr;
1475 f36fd90a 2022-07-09 op #endif /* !HAVE_RECALLOCARRAY */
1476 f36fd90a 2022-07-09 op #if !HAVE_SETPROCTITLE
1478 f36fd90a 2022-07-09 op * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
1480 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1481 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1482 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1484 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1485 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1486 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1487 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1488 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1489 f36fd90a 2022-07-09 op * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1490 f36fd90a 2022-07-09 op * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1493 f36fd90a 2022-07-09 op # if HAVE_PR_SET_NAME
1494 f36fd90a 2022-07-09 op #include <sys/types.h>
1495 f36fd90a 2022-07-09 op #include <stdarg.h>
1496 f36fd90a 2022-07-09 op #include <stdio.h>
1497 f36fd90a 2022-07-09 op #include <string.h>
1498 f36fd90a 2022-07-09 op #include <sys/prctl.h>
1500 f36fd90a 2022-07-09 op setproctitle(const char *fmt, ...)
1502 f36fd90a 2022-07-09 op char title[16], name[16], *cp;
1506 f36fd90a 2022-07-09 op va_start(ap, fmt);
1507 f36fd90a 2022-07-09 op vsnprintf(title, sizeof(title), fmt, ap);
1510 f36fd90a 2022-07-09 op used = snprintf(name, sizeof(name), "%s: %s", getprogname(), title);
1511 f36fd90a 2022-07-09 op if (used >= (int)sizeof(name)) {
1512 f36fd90a 2022-07-09 op cp = strrchr(name, ' ');
1513 f36fd90a 2022-07-09 op if (cp != NULL)
1516 f36fd90a 2022-07-09 op prctl(PR_SET_NAME, name);
1520 f36fd90a 2022-07-09 op setproctitle(const char *fmt, ...)
1525 f36fd90a 2022-07-09 op #endif /* !HAVE_SETPROCTITLE */
1526 f36fd90a 2022-07-09 op #if !HAVE_STRLCAT
1528 f36fd90a 2022-07-09 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1530 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1531 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1532 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1534 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1535 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1536 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1537 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1538 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1539 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1540 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1543 f36fd90a 2022-07-09 op #include <sys/types.h>
1544 f36fd90a 2022-07-09 op #include <string.h>
1547 f36fd90a 2022-07-09 op * Appends src to string dst of size siz (unlike strncat, siz is the
1548 f36fd90a 2022-07-09 op * full size of dst, not space left). At most siz-1 characters
1549 f36fd90a 2022-07-09 op * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
1550 f36fd90a 2022-07-09 op * Returns strlen(src) + MIN(siz, strlen(initial dst)).
1551 f36fd90a 2022-07-09 op * If retval >= siz, truncation occurred.
1554 f36fd90a 2022-07-09 op strlcat(char *dst, const char *src, size_t siz)
1556 f36fd90a 2022-07-09 op char *d = dst;
1557 f36fd90a 2022-07-09 op const char *s = src;
1558 f36fd90a 2022-07-09 op size_t n = siz;
1559 f36fd90a 2022-07-09 op size_t dlen;
1561 f36fd90a 2022-07-09 op /* Find the end of dst and adjust bytes left but don't go past end */
1562 f36fd90a 2022-07-09 op while (n-- != 0 && *d != '\0')
1564 f36fd90a 2022-07-09 op dlen = d - dst;
1565 f36fd90a 2022-07-09 op n = siz - dlen;
1568 f36fd90a 2022-07-09 op return(dlen + strlen(s));
1569 f36fd90a 2022-07-09 op while (*s != '\0') {
1570 f36fd90a 2022-07-09 op if (n != 1) {
1578 f36fd90a 2022-07-09 op return(dlen + (s - src)); /* count does not include NUL */
1580 f36fd90a 2022-07-09 op #endif /* !HAVE_STRLCAT */
1581 f36fd90a 2022-07-09 op #if !HAVE_STRLCPY
1583 f36fd90a 2022-07-09 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
1585 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1586 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1587 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1589 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1590 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1591 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1592 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1593 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1594 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1595 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1598 f36fd90a 2022-07-09 op #include <sys/types.h>
1599 f36fd90a 2022-07-09 op #include <string.h>
1602 f36fd90a 2022-07-09 op * Copy src to string dst of size siz. At most siz-1 characters
1603 f36fd90a 2022-07-09 op * will be copied. Always NUL terminates (unless siz == 0).
1604 f36fd90a 2022-07-09 op * Returns strlen(src); if retval >= siz, truncation occurred.
1607 f36fd90a 2022-07-09 op strlcpy(char *dst, const char *src, size_t siz)
1609 f36fd90a 2022-07-09 op char *d = dst;
1610 f36fd90a 2022-07-09 op const char *s = src;
1611 f36fd90a 2022-07-09 op size_t n = siz;
1613 f36fd90a 2022-07-09 op /* Copy as many bytes as will fit */
1614 f36fd90a 2022-07-09 op if (n != 0) {
1615 f36fd90a 2022-07-09 op while (--n != 0) {
1616 f36fd90a 2022-07-09 op if ((*d++ = *s++) == '\0')
1621 f36fd90a 2022-07-09 op /* Not enough room in dst, add NUL and traverse rest of src */
1622 f36fd90a 2022-07-09 op if (n == 0) {
1623 f36fd90a 2022-07-09 op if (siz != 0)
1624 f36fd90a 2022-07-09 op *d = '\0'; /* NUL-terminate dst */
1625 f36fd90a 2022-07-09 op while (*s++)
1629 f36fd90a 2022-07-09 op return(s - src - 1); /* count does not include NUL */
1631 f36fd90a 2022-07-09 op #endif /* !HAVE_STRLCPY */
1632 f36fd90a 2022-07-09 op #if !HAVE_STRNDUP
1633 f36fd90a 2022-07-09 op /* $OpenBSD$ */
1635 f36fd90a 2022-07-09 op * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1637 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1638 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1639 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1641 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1642 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1643 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1644 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1645 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1646 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1647 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1650 f36fd90a 2022-07-09 op #include <sys/types.h>
1652 f36fd90a 2022-07-09 op #include <stddef.h>
1653 f36fd90a 2022-07-09 op #include <stdlib.h>
1654 f36fd90a 2022-07-09 op #include <string.h>
1657 f36fd90a 2022-07-09 op strndup(const char *str, size_t maxlen)
1662 f36fd90a 2022-07-09 op len = strnlen(str, maxlen);
1663 f36fd90a 2022-07-09 op copy = malloc(len + 1);
1664 f36fd90a 2022-07-09 op if (copy != NULL) {
1665 f36fd90a 2022-07-09 op (void)memcpy(copy, str, len);
1666 f36fd90a 2022-07-09 op copy[len] = '\0';
1669 f36fd90a 2022-07-09 op return copy;
1671 f36fd90a 2022-07-09 op #endif /* !HAVE_STRNDUP */
1672 f36fd90a 2022-07-09 op #if !HAVE_STRNLEN
1673 f36fd90a 2022-07-09 op /* $OpenBSD$ */
1676 f36fd90a 2022-07-09 op * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1678 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1679 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1680 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1682 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1683 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1684 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1685 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1686 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1687 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1688 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1691 f36fd90a 2022-07-09 op #include <sys/types.h>
1692 f36fd90a 2022-07-09 op #include <string.h>
1695 f36fd90a 2022-07-09 op strnlen(const char *str, size_t maxlen)
1697 f36fd90a 2022-07-09 op const char *cp;
1699 f36fd90a 2022-07-09 op for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
1702 f36fd90a 2022-07-09 op return (size_t)(cp - str);
1704 f36fd90a 2022-07-09 op #endif /* !HAVE_STRNLEN */
1705 f36fd90a 2022-07-09 op #if !HAVE_STRTONUM
1707 f36fd90a 2022-07-09 op * Copyright (c) 2004 Ted Unangst and Todd Miller
1708 f36fd90a 2022-07-09 op * All rights reserved.
1710 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
1711 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
1712 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
1714 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1715 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1716 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1717 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1718 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1719 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1720 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1723 f36fd90a 2022-07-09 op #include <errno.h>
1724 f36fd90a 2022-07-09 op #include <limits.h>
1725 f36fd90a 2022-07-09 op #include <stdlib.h>
1727 f36fd90a 2022-07-09 op #define INVALID 1
1728 f36fd90a 2022-07-09 op #define TOOSMALL 2
1729 f36fd90a 2022-07-09 op #define TOOLARGE 3
1732 f36fd90a 2022-07-09 op strtonum(const char *numstr, long long minval, long long maxval,
1733 f36fd90a 2022-07-09 op const char **errstrp)
1735 f36fd90a 2022-07-09 op long long ll = 0;
1736 f36fd90a 2022-07-09 op int error = 0;
1738 f36fd90a 2022-07-09 op struct errval {
1739 f36fd90a 2022-07-09 op const char *errstr;
1742 f36fd90a 2022-07-09 op { NULL, 0 },
1743 f36fd90a 2022-07-09 op { "invalid", EINVAL },
1744 f36fd90a 2022-07-09 op { "too small", ERANGE },
1745 f36fd90a 2022-07-09 op { "too large", ERANGE },
1748 f36fd90a 2022-07-09 op ev[0].err = errno;
1750 f36fd90a 2022-07-09 op if (minval > maxval) {
1751 f36fd90a 2022-07-09 op error = INVALID;
1753 f36fd90a 2022-07-09 op ll = strtoll(numstr, &ep, 10);
1754 f36fd90a 2022-07-09 op if (numstr == ep || *ep != '\0')
1755 f36fd90a 2022-07-09 op error = INVALID;
1756 f36fd90a 2022-07-09 op else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
1757 f36fd90a 2022-07-09 op error = TOOSMALL;
1758 f36fd90a 2022-07-09 op else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
1759 f36fd90a 2022-07-09 op error = TOOLARGE;
1761 f36fd90a 2022-07-09 op if (errstrp != NULL)
1762 f36fd90a 2022-07-09 op *errstrp = ev[error].errstr;
1763 f36fd90a 2022-07-09 op errno = ev[error].err;
1767 f36fd90a 2022-07-09 op return (ll);
1769 f36fd90a 2022-07-09 op #endif /* !HAVE_STRTONUM */
1770 31e12e67 2023-09-07 op #if !HAVE_TIMESPECSUB
1771 31e12e67 2023-09-07 op #include <sys/time.h>
1773 31e12e67 2023-09-07 op timespecsub(struct timespec *a, struct timespec *b, struct timespec *ret)
1775 31e12e67 2023-09-07 op ret->tv_sec = a->tv_sec - b->tv_sec;
1776 31e12e67 2023-09-07 op ret->tv_nsec = a->tv_nsec - b->tv_nsec;
1777 31e12e67 2023-09-07 op if (ret->tv_nsec < 0) {
1778 31e12e67 2023-09-07 op ret->tv_sec--;
1779 31e12e67 2023-09-07 op ret->tv_nsec += 1000000000L;
1782 31e12e67 2023-09-07 op #endif /* !HAVE_TIMESPECSUB */