module for bsnmp to interface to sysctl
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.
 
 

109 lines
2.8 KiB

  1. /*
  2. * Copyright (c) 1993
  3. * The Regents of the University of California. All rights reserved.
  4. * Copyright 2011 John-Mark Gurney
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 4. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/types.h>
  31. #include <sys/sysctl.h>
  32. #include <errno.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "sysctl_sup.h"
  36. int
  37. sysctl_alloc(int *name, u_int namelen, void **oldp, size_t *oldlenp,
  38. void *newp, size_t newlen)
  39. {
  40. void *buf;
  41. size_t curr, prev;
  42. int r;
  43. buf = NULL;
  44. if (sysctl(name, namelen, NULL, &curr, NULL, 0) == -1 &&
  45. errno != ENOMEM)
  46. return -1;
  47. for (;;) {
  48. buf = realloc(buf, curr);
  49. prev = curr;
  50. if (((r = sysctl(name, namelen, buf, &curr, newp, newlen)) ==
  51. -1 && errno != ENOMEM) || r == 0) {
  52. if (r == 0) {
  53. *oldlenp = curr;
  54. *oldp = realloc(buf, *oldlenp);
  55. }
  56. return r;
  57. }
  58. /* previous estimate was short, try again. */
  59. if (curr <= prev)
  60. curr = prev + 16;
  61. }
  62. }
  63. int
  64. sysctl_oidfmt(int *oid, int len, char **fmt, u_int *kind)
  65. {
  66. int qoid[CTL_MAXNAME+2];
  67. void *buf;
  68. u_char *fmtstart;
  69. int i;
  70. size_t j;
  71. qoid[0] = 0;
  72. qoid[1] = 4;
  73. memcpy(qoid + 2, oid, len * sizeof(int));
  74. i = sysctl_alloc(qoid, len + 2, &buf, &j, NULL, 0);
  75. if (i)
  76. return i;
  77. if (kind)
  78. *kind = *(u_int *)buf;
  79. if (fmt) {
  80. fmtstart = (char *)buf + sizeof(u_int);
  81. *fmt = malloc(strlen(fmtstart) + 1);
  82. if (*fmt == NULL) {
  83. free(buf);
  84. errno = ENOMEM;
  85. return -1;
  86. }
  87. strcpy(*fmt, fmtstart);
  88. }
  89. free(buf);
  90. return (0);
  91. }