From owner-freebsd-hackers@freebsd.org Sat Dec 22 19:22:39 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 448491338B74 for ; Sat, 22 Dec 2018 19:22:39 +0000 (UTC) (envelope-from alfix86@gmail.com) Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 253616DDE2 for ; Sat, 22 Dec 2018 19:22:38 +0000 (UTC) (envelope-from alfix86@gmail.com) Received: by mail-wr1-x42c.google.com with SMTP id r10so8335780wrs.10 for ; Sat, 22 Dec 2018 11:22:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version :content-transfer-encoding; bh=exb5ONv+5GASw/5AYlJY3jzClA4RQuAdvPW3C+p7ZXg=; b=S0w6foFjp5ZdNKGkK/AkEej10NI9UddgZuSSZlB+Jaa2uIK1za8JuUELDIii2EB7aq PSmLQ3FA97hQ4RPREfrdOrE+I6VG2YWnfvxv0NxL/BDcbkBBV1Dc34TkSKxO0lv8iM+N qacaRiUZp87hZL3WJmKH97tX6nj3l7qItYNgKzMGFqZIMFdoPeKEpHQBIKjdD2sHNghU UZBuvdKZv+7xLYgoRDlgveSDz7K9jpB9SDgkkcwiDeEpEOGWimyCFTGnwY6pBy8RR6TB /bNOLp100XbJ/Q/ii+etaJapZ9YR6Q8ad6l3C/7HIyZy8oCiUP122VVrnOKzp1RRSdEk etNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-transfer-encoding; bh=exb5ONv+5GASw/5AYlJY3jzClA4RQuAdvPW3C+p7ZXg=; b=RCwk/bYbU0I9j751tdAPU70K1zmNO70aNrF5OnkC1mdoVXuchn/rC4uevblCiobM/t gqYGp72ZEFs+4Y5kmYMDeJz3ZsBRDZ4UUCfepK954AdPdR9SmA1Z/bbR7zF/fbOeYRPm iN4ld3U229S/lkn5qEU5Asq8oJJx+23kteURR7qShPS1t3bT5YYxPiN+nEDvcbZ7kjm5 VZioOA+z9peK+MGaPwX23kg+UDZspntLQ5ICU3XrK4mqxiaW4iB2s0PwSMBau/2yC9jT 3Lrx2BzSiO/dkc4P7m0onGK+A9t/5zUsBjK4zjVt2D+o1kM6gzNF/UYMWO2sH+6Vab+m o5EA== X-Gm-Message-State: AJcUukeMnwemRksMxR0b/XACx5YImqe6biFo0Or79qFP4FXT8VA9IKuF rVJA4CKKLFhDiccSfEZUKkB8IiKU X-Google-Smtp-Source: ALg8bN7TH/QEr8FYJHmD2XOxtgIVnJO/8Ufoc2MqCcYA4fWUc+O7pQBzU+ixQUeTLZtRyJOkJjAKjA== X-Received: by 2002:a5d:538a:: with SMTP id d10mr6711196wrv.202.1545506555412; Sat, 22 Dec 2018 11:22:35 -0800 (PST) Received: from alffbsd ([95.238.231.159]) by smtp.gmail.com with ESMTPSA id k23sm10099548wmj.32.2018.12.22.11.22.34 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 22 Dec 2018 11:22:34 -0800 (PST) Date: Sat, 22 Dec 2018 20:22:28 +0100 From: Alfonso Siciliano To: freebsd-hackers@freebsd.org Subject: libsysctl: a C API for sysctl-mib-tree Message-Id: <20181222202228.95f48408c8e2ebe9c40d9d75@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; amd64-portbld-freebsd13.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 253616DDE2 X-Spamd-Bar: ----- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=S0w6foFj; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of alfix86@gmail.com designates 2a00:1450:4864:20::42c as permitted sender) smtp.mailfrom=alfix86@gmail.com X-Spamd-Result: default: False [-5.96 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; MV_CASE(0.50)[]; TO_DN_NONE(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; MX_GOOD(-0.01)[cached: alt3.gmail-smtp-in.l.google.com]; NEURAL_HAM_SHORT(-0.98)[-0.980,0]; RECEIVED_SPAMHAUS_PBL(0.00)[159.231.238.95.zen.spamhaus.org : 127.0.0.10]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; MID_RHS_MATCH_FROM(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com.dwl.dnswl.org : 127.0.5.0]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; RCPT_COUNT_ONE(0.00)[1]; RCVD_TLS_LAST(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[c.2.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.5.4.1.0.0.a.2.list.dnswl.org : 127.0.5.0]; IP_SCORE(-2.47)[ip: (-9.14), ipnet: 2a00:1450::/32(-1.67), asn: 15169(-1.46), country: US(-0.08)] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Dec 2018 19:22:39 -0000 Hey! I' m currently working on a library called "libsysctl" which will provide a C API to wrap kern_sysctl.c undocumented interface, build mib-entry, entries-list and mib-tree in userspace and then to do the work that /sbin/sysctl currently does. This project started from a wish to write a "sysctl-table" tool (like "top" and "htop" for processes), examples below, so I need to have info about kernel sysctl-mib-tree. The advantages to have libsysctl are: an easy userspace API to the kernel sysctl-mib-tree, building quickly a custom sysctl(8) tool and changes to kern_sysctl.c interface won't upset userspace tools. I am coding on my pc: FreeBSD 13.0-CURRENT amd64 and src are in http://gitlab.com/alfix/libsysctl [0] I could submit a patch to reviews.freebsd.org to solicit feedback and write a wiki page. The README.md [2] shows every function and links to examples [1], anyway the following API overview has extras notes. -- API Overview -- Implementation note: the following functions call sysctl(2) for wrapping 0.[1-6] entries/'kernel state'. Kernel returns only next leaf, nextnode() needs extra computation /* 'undocumented kern_sysctl.c API' wrap functions * return: 0 for success, negative value for failure. */ libsysctl_nametoid(), libsysctl_name(), libsysctl_desc(), libsysctl_label(), libsysctl_info(), libsysctl_nextnode(), libsysctl_next() and libsysctl_nextleaf(); ... EXTRA MACROS ... Note: I prefer to use the following functions: /* "struct libsysctl_object" functions * return: NULL for failure, pointer to heap for success. */ Note: - The following functions use previous functions plus SLIST macros. - "struct libsysctl_object" is a mib-entry in userspace, it is handier than kernel sysctl_oid . - libsysctl_object() and libsysctl_tree() are "conceptually similar" to NetBSD sysctlgetmibinfo(). - libsysctl_grouplist() returns a "linear" tree. - libsysctl_tree() sets .childs so it builds an userspace mib-tree, it is useful to make a custom sysctl tool; I like it :-) . - libsysctl_free*() functions free the heap. SLIST_HEAD(libsysctl_object_list, libsysctl_object); struct libsysctl_object { ... int *id; size_t idlen; char *name; char *desc; char *label; uint8_t type; uint32_t flags; char *fmt; struct libsysctl_object_list *childs; ... }; Note: OR_FLAGS are just the object' s fields to set. struct libsysctl_object * libsysctl_object(id, idlen, OR_FLAGS); struct libsysctl_object_list * libsysctl_filterlist(libsysctl_filterfunc_t *, OR_FLAGS); struct libsysctl_object_list * libsysctl_grouplist(id, idlen, OR_FLAGS, max_depth); struct libsysctl_object * libsysctl_tree(id, idlen, OR_FLAGS, max_edges); libsysctl_freeobject(), libsysctl_freelist() and libsysctl_freetree(). --- Docs --- Files *.c in libsysctl/examples/ show every function. [1] Overview http://gitlab.com/alfix/libsysctl/blob/master/README.md [2] manuals *.3 in libsysctl/doc/ are 'work in progress'. --- Tools using libsysctl --- - "sysctlview 0.1", a sysctl gtk gui tool, http://gitlab.com/alfix/sysctlview [3] - "nsysctl 0.1", sysctl(8) + [-FIlMmSy] + [--libxo], http://gitlab.com/alfix/nsysctl [4] I appreciate any feedback that you can give me. Regards, Alfonso S. Siciliano wiki.freebsd.org/AlfonsoSiciliano LINKS [0] git repo: http://gitlab.com/alfix/libsysctl [1] examples: http://gitlab.com/alfix/libsysctl/examples [2] README.md: http://gitlab.com/alfix/libsysctl/blob/master/README.md [3] sysctlview http://gitlab.com/alfix/sysctlview [4] nsysctl: http://gitlab.com/alfix/nsysctl