summaryrefslogtreecommitdiffstats
path: root/usr.bin/systat/fetch.c
diff options
context:
space:
mode:
authortmm <tmm@FreeBSD.org>2001-03-23 03:58:25 +0000
committertmm <tmm@FreeBSD.org>2001-03-23 03:58:25 +0000
commitd6fecac748fdca2de8bbfd045bceab19277abab1 (patch)
tree62457bf2e3ef9a2f6fe6f055b531080c23af45c2 /usr.bin/systat/fetch.c
parent76ba4861cd1cb43f58cdf0c32eafb5669114b1f0 (diff)
downloadFreeBSD-src-d6fecac748fdca2de8bbfd045bceab19277abab1.zip
FreeBSD-src-d6fecac748fdca2de8bbfd045bceab19277abab1.tar.gz
Get rid of setgid kmem for systat, and while being there, fix some bugs
and compiler warnings. The data for network statistics are still obtained via the kvm interface if systat was started with the needed privileges, otherwise sysctls are used. The reason for this is that with really many open sockets, the sysctl method is probably slower, but it systat -netstat is probably not really usable in either mode under these conditions. Approved by: rwatson
Diffstat (limited to 'usr.bin/systat/fetch.c')
-rw-r--r--usr.bin/systat/fetch.c96
1 files changed, 95 insertions, 1 deletions
diff --git a/usr.bin/systat/fetch.c b/usr.bin/systat/fetch.c
index ff69ac5..94ef619 100644
--- a/usr.bin/systat/fetch.c
+++ b/usr.bin/systat/fetch.c
@@ -29,6 +29,8 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
+ *
+ * $FreeBSD$
*/
#ifndef lint
@@ -36,6 +38,13 @@ static char sccsid[] = "@(#)fetch.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#include <sys/types.h>
+#include <sys/sysctl.h>
+
+#include <errno.h>
+#include <err.h>
+#include <string.h>
+#include <stdlib.h>
+
#include "systat.h"
#include "extern.h"
@@ -46,9 +55,94 @@ kvm_ckread(a, b, l)
{
if (kvm_read(kd, (u_long)a, b, l) != l) {
if (verbose)
- error("error reading kmem at %x\n", a);
+ error("error reading kmem at %x", a);
return (0);
}
else
return (1);
}
+
+void getsysctl(name, ptr, len)
+ char *name;
+ void *ptr;
+ size_t len;
+{
+ int err;
+ size_t nlen = len;
+ if ((err = sysctlbyname(name, ptr, &nlen, NULL, 0)) != 0) {
+ error("sysctl(%s...) failed: %s", name,
+ strerror(errno));
+ }
+ if (nlen != len) {
+ error("sysctl(%s...) expected %d, got %d", name,
+ len, nlen);
+ }
+}
+
+/*
+ * Read sysctl data with variable size. Try some times (with increasing
+ * buffers), fail if still too small.
+ * This is needed sysctls with possibly raplidly increasing data sizes,
+ * but imposes little overhead in the case of constant sizes.
+ * Returns NULL on error, or a pointer to freshly malloc()'ed memory that holds
+ * the requested data.
+ * If szp is not NULL, the size of the returned data will be written into *szp.
+ */
+
+/* Some defines: Number of tries. */
+#define SD_NTRIES 10
+/* Percent of over-allocation (initial) */
+#define SD_MARGIN 10
+/*
+ * Factor for over-allocation in percent (the margin is increased by this on
+ * any failed try).
+ */
+#define SD_FACTOR 50
+/* Maximum supported MIB depth */
+#define SD_MAXMIB 16
+
+char *
+sysctl_dynread(n, szp)
+ char *n;
+ size_t *szp;
+{
+ char *rv = NULL;
+ int mib[SD_MAXMIB];
+ size_t mibsz = SD_MAXMIB;
+ size_t mrg = SD_MARGIN;
+ size_t sz;
+ int i;
+
+ /* cache the MIB */
+ if (sysctlnametomib(n, mib, &mibsz) == -1) {
+ if (errno == ENOMEM) {
+ error("XXX: SD_MAXMIB too small, please bump!");
+ }
+ return NULL;
+ }
+ for (i = 0; i < SD_NTRIES; i++) {
+ /* get needed buffer size */
+ if (sysctl(mib, mibsz, NULL, &sz, NULL, 0) == -1)
+ break;
+ sz += sz * mrg / 100;
+ if ((rv = (char *)malloc(sz)) == NULL) {
+ error("Out of memory!");
+ return NULL;
+ }
+ if (sysctl(mib, mibsz, rv, &sz, NULL, 0) == -1) {
+ free(rv);
+ rv = NULL;
+ if (errno == ENOMEM) {
+ mrg += mrg * SD_FACTOR / 100;
+ } else
+ break;
+ } else {
+ /* success */
+ if (szp != NULL)
+ *szp = sz;
+ break;
+ }
+ }
+
+ return rv;
+}
OpenPOWER on IntegriCloud