Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
3.7 KiB

20 years ago
  1. /* $OpenBSD: imsg.c,v 1.7 2004/09/16 01:13:42 henning Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include "ntpd.h"
  23. void
  24. imsg_init(struct imsgbuf *ibuf, int fd)
  25. {
  26. msgbuf_init(&ibuf->w);
  27. bzero(&ibuf->r, sizeof(ibuf->r));
  28. ibuf->fd = fd;
  29. ibuf->w.fd = fd;
  30. ibuf->pid = getpid();
  31. }
  32. int
  33. imsg_read(struct imsgbuf *ibuf)
  34. {
  35. ssize_t n;
  36. if ((n = read(ibuf->fd, ibuf->r.buf + ibuf->r.wpos,
  37. sizeof(ibuf->r.buf) - ibuf->r.wpos)) == -1) {
  38. if (errno != EINTR && errno != EAGAIN) {
  39. log_warn("imsg_read: pipe read error");
  40. return (-1);
  41. }
  42. return (0);
  43. }
  44. ibuf->r.wpos += n;
  45. return (n);
  46. }
  47. int
  48. imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
  49. {
  50. ssize_t datalen = 0;
  51. size_t av, left;
  52. av = ibuf->r.wpos;
  53. if (IMSG_HEADER_SIZE > av)
  54. return (0);
  55. memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
  56. if (imsg->hdr.len < IMSG_HEADER_SIZE ||
  57. imsg->hdr.len > MAX_IMSGSIZE) {
  58. log_warnx("imsg_get: imsg hdr len out of bounds");
  59. return (-1);
  60. }
  61. if (imsg->hdr.len > av)
  62. return (0);
  63. datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
  64. ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
  65. if ((imsg->data = malloc(datalen)) == NULL) {
  66. log_warn("imsg_get");
  67. return (-1);
  68. }
  69. memcpy(imsg->data, ibuf->r.rptr, datalen);
  70. if (imsg->hdr.len < av) {
  71. left = av - imsg->hdr.len;
  72. memcpy(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
  73. ibuf->r.wpos = left;
  74. } else
  75. ibuf->r.wpos = 0;
  76. return (datalen + IMSG_HEADER_SIZE);
  77. }
  78. int
  79. imsg_compose(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
  80. pid_t pid, void *data, u_int16_t datalen)
  81. {
  82. struct buf *wbuf;
  83. int n;
  84. if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
  85. return (-1);
  86. if (imsg_add(wbuf, data, datalen) == -1)
  87. return (-1);
  88. if ((n = imsg_close(ibuf, wbuf)) < 0)
  89. return (-1);
  90. return (n);
  91. }
  92. struct buf *
  93. imsg_create(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
  94. pid_t pid, u_int16_t datalen)
  95. {
  96. struct buf *wbuf;
  97. struct imsg_hdr hdr;
  98. if (datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
  99. log_warnx("imsg_create: len %u > MAX_IMSGSIZE; "
  100. "type %u peerid %lu", datalen + IMSG_HEADER_SIZE,
  101. type, peerid);
  102. return (NULL);
  103. }
  104. hdr.len = datalen + IMSG_HEADER_SIZE;
  105. hdr.type = type;
  106. hdr.peerid = peerid;
  107. hdr.pid = pid;
  108. if ((wbuf = buf_open(hdr.len)) == NULL) {
  109. log_warn("imsg_create: buf_open");
  110. return (NULL);
  111. }
  112. if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
  113. return (NULL);
  114. return (wbuf);
  115. }
  116. int
  117. imsg_add(struct buf *msg, void *data, u_int16_t datalen)
  118. {
  119. if (datalen)
  120. if (buf_add(msg, data, datalen) == -1) {
  121. log_warnx("imsg_add: buf_add error");
  122. buf_free(msg);
  123. return (-1);
  124. }
  125. return (datalen);
  126. }
  127. int
  128. imsg_close(struct imsgbuf *ibuf, struct buf *msg)
  129. {
  130. int n;
  131. if ((n = buf_close(&ibuf->w, msg)) < 0) {
  132. log_warnx("imsg_close: buf_add error");
  133. buf_free(msg);
  134. return (-1);
  135. }
  136. return (n);
  137. }
  138. void
  139. imsg_free(struct imsg *imsg)
  140. {
  141. free(imsg->data);
  142. }