From 4e1e632e33c4626b2ee0b45f0ebe48d77f082e4d Mon Sep 17 00:00:00 2001 From: wifiextender Date: Fri, 19 Aug 2016 05:28:47 +0200 Subject: [PATCH] Make get_statio available in FreeBSD --- bootstrap | 6 ++-- m4/the_rest_funcs.m4 | 22 ++++++++++++++ m4/typez.m4 | 1 + src/freebsd_functions.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ src/options.c | 4 +-- src/prototypes/functions.h | 2 +- 6 files changed, 101 insertions(+), 6 deletions(-) diff --git a/bootstrap b/bootstrap index 245bc9c..39f2106 100644 --- a/bootstrap +++ b/bootstrap @@ -38,13 +38,13 @@ _printf_err() { _gen_files() { progName='dwmbar' progVer='1.0.0' - argpLib='' + addLibs='' defTits='' osEntered="${1,,}" declare -a srcToAppend [[ "${osEntered}" == 'freebsd' ]] && { - argpLib='-largp' + addLibs='-largp -ldevstat' amCF='-I/usr/local/include -L/usr/local/lib' # FreeBSD specific m4 tits defTits='m4_define([ITS_BSD], [tits])' @@ -163,7 +163,7 @@ _gen_files() { $(X_LIBS) \ $(ALSA_LIBS) \ $(MPD_LIBS) \ - $(PCI_LIBS) '${argpLib}' + $(PCI_LIBS) '${addLibs}' '${progName}'_SOURCES = '${src_files[@]}' diff --git a/m4/the_rest_funcs.m4 b/m4/the_rest_funcs.m4 index 8c25ef6..62c8141 100644 --- a/m4/the_rest_funcs.m4 +++ b/m4/the_rest_funcs.m4 @@ -317,7 +317,29 @@ AC_DEFUN([TEST_SOME_FUNCS],[ ] ) + + NOTIFY([malloc]) + AC_COMPILE_IFELSE([ + AC_LANG_SOURCE([[ + #include + #include + int main(void) { + char *a = (char *)malloc(10); + if (NULL == a) { + return 0; + } + free(a); + return 0; + } + ]]) + ],[],[ + COMPILE_FAILED([malloc]) + ] + ) + ], []) + + ]) diff --git a/m4/typez.m4 b/m4/typez.m4 index 684d459..aa84258 100644 --- a/m4/typez.m4 +++ b/m4/typez.m4 @@ -136,6 +136,7 @@ AC_DEFUN([TEST_TYPEZ],[ sysctl \ sysctlbyname \ getloadavg \ + malloc \ ],[],[ ERR([Missing core library functions.]) ]) diff --git a/src/freebsd_functions.c b/src/freebsd_functions.c index 05b1635..a26c2ff 100644 --- a/src/freebsd_functions.c +++ b/src/freebsd_functions.c @@ -34,6 +34,7 @@ #include #include +#include #include "include/headers.h" #include "include/freebzd.h" @@ -161,3 +162,74 @@ get_mobo_temp(char *str1) { temp2 / 100 : temp2 / 10)); /* > 9C || < 9C */ } } + + +/* + Couldn't add this option on my own. + Used the following resources to make this function happen: + http://ftp.stu.edu.tw/FreeBSD/branches/3.0-stable/src/libexec/rpc.rstatd/rstat_proc.c + https://github.com/giampaolo/psutil/blob/master/psutil/arch/bsd/freebsd.c + http://opensource.apple.com/source/net_snmp/net_snmp-16/net-snmp/agent/mibgroup/ucd-snmp/diskio.c + https://searchcode.com/codesearch/view/29835031/ + http://fossies.org/linux/pcp/src/pmdas/freebsd/disk.c + Had to use Valgrind since we allocate memory with malloc. +*/ +void +get_statio(char *str1, char *str2) { + struct statinfo stats; + struct device_selection *dev_select = NULL; + struct devstat *d = NULL; + long select_generation = 0; + int x = 0, num_devices = 0, num_selected = 0, num_selections = 0; + + FILL_STR_ARR(1, str1, "Null"); + if(0 != devstat_checkversion(NULL)) { + return; + } + + memset(&stats, 0, sizeof(stats)); + stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); + if (NULL == stats.dinfo) { + goto error; + } + + if(-1 == devstat_getdevs(NULL, &stats)) { + goto error; + } + + num_devices = stats.dinfo->numdevs; + if (-1 == devstat_selectdevs(&dev_select, &num_selected, &num_selections, + &select_generation, stats.dinfo->generation, stats.dinfo->devices, num_devices, + NULL, 0, NULL, 0, DS_SELECT_ADD, 16, 0)) { + goto error; + } + + for (x = 0; x < 16; x++) { + d = &stats.dinfo->devices[x]; + if (!(strcmp(str2, d->device_name))) { + + if (d->device_type != DEVSTAT_TYPE_DIRECT && + d->device_type != DEVSTAT_TYPE_IF_SCSI && + d->device_type != DEVSTAT_TYPE_IF_IDE) { + break; + } + + FILL_ARR(str1, "Read " FMT_UINT " MB, Written " FMT_UINT " MB", + (uintmax_t)d->bytes[DEVSTAT_READ] / MB, + (uintmax_t)d->bytes[DEVSTAT_WRITE] / MB); + break; + } + } + +error: + if (NULL != dev_select) { + free(dev_select); + } + if (stats.dinfo->mem_ptr) { + free(stats.dinfo->mem_ptr); + } + if (NULL != stats.dinfo) { + free(stats.dinfo); + } + return; +} diff --git a/src/options.c b/src/options.c index b0c8a00..eccccd5 100644 --- a/src/options.c +++ b/src/options.c @@ -177,6 +177,8 @@ parse_opt(int key, char *arg, struct argp_state *state) { NEW_ARG_LABEL('E', char ip_lookup[VLA], ip_lookup, FMT_KERN); + NEW_ARG_LABEL('S', char statio[VLA], statio, FMT_STATIO, STATIO_STR); + #if defined(__linux__) NEW_LABEL('g', char battery[VLA], battery, FMT_BATT, BATT_STR); @@ -192,8 +194,6 @@ parse_opt(int key, char *arg, struct argp_state *state) { NEW_ARG_LABEL('G', char nic_info[VLA], nic_info, FMT_KERN); NEW_NET_LABEL('e', char link_speed[VLA], link_speed, 7, FMT_KERN); - - NEW_ARG_LABEL('S', char statio[VLA], statio, FMT_STATIO, STATIO_STR); #endif case 'V': diff --git a/src/prototypes/functions.h b/src/prototypes/functions.h index 25151b9..b3a0b13 100644 --- a/src/prototypes/functions.h +++ b/src/prototypes/functions.h @@ -26,10 +26,10 @@ void get_loadavg(char *); void get_voltage(char *); void get_mobo(char *); void get_mobo_temp(char *); +void get_statio(char *, char *); #if defined(__linux__) void get_ssd_model(char *, char *); -void get_statio(char *, char *); void get_battery(char *); #endif -- 2.11.0