"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/src/common.h" (7 Jan 2022, 9894 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8_vs_2.9.

    1 #ifndef COMMON_H
    2 #define COMMON_H
    3 
    4 #include <config.h>
    5 #include <stdio.h>
    6 #include <stdlib.h>
    7 #include <stdint.h>
    8 #include <unistd.h>
    9 #include <locale.h>
   10 #include <time.h>
   11 #include <string.h>
   12 #include <sys/types.h>
   13 #include <sys/stat.h>
   14 #include <dirent.h>
   15 #include <ctype.h>
   16 #include <signal.h>
   17 #include <math.h>
   18 #include <errno.h>
   19 #include <sys/file.h>
   20 #include <inttypes.h>
   21 #include <syslog.h>
   22 #include <sys/statvfs.h>
   23 #include <pwd.h>
   24 #include <grp.h>
   25 #include <libgen.h>
   26 #include <fcntl.h>
   27 #include <sys/time.h>
   28 
   29 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__FreeBSD_kernel__)
   30 #include <sys/param.h>
   31 #include <sys/mount.h>
   32 #include <sys/socket.h>
   33 #include <sys/sysctl.h>
   34 #include <net/if.h>
   35 #include <ifaddrs.h>
   36 #define BSD_VNSTAT
   37 #endif
   38 
   39 /* OpenBSD and NetBSD don't support the ' character (decimal conversion) in printf formatting */
   40 #if !defined(__OpenBSD__) && !defined(__NetBSD__)
   41 #define DECCONV "'"
   42 #else
   43 #define DECCONV
   44 #endif
   45 
   46 /* used in debug to get function name */
   47 #if __STDC_VERSION__ < 199901L
   48 #if __GNUC__ >= 2
   49 #define __func__ __FUNCTION__
   50 #else
   51 #define __func__ "function"
   52 #endif
   53 #endif
   54 
   55 /*
   56 
   57 Note! These are only the default values for settings
   58 and most can be changed later from the config file.
   59 
   60 */
   61 
   62 /* location of the database directory */
   63 #ifndef DATABASEDIR
   64 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
   65 #define DATABASEDIR "/var/db/vnstat"
   66 #else
   67 #define DATABASEDIR "/var/lib/vnstat"
   68 #endif
   69 #endif
   70 
   71 /* database file name */
   72 #define DATABASEFILE "vnstat.db"
   73 
   74 /* modifier used for sqlite date and time functions */
   75 #define DATABASELOCALTIMEMODIFIER ", 'localtime'"
   76 
   77 /* on which day should months change */
   78 #define MONTHROTATE 1
   79 #define MONTHROTATEYEARS 0
   80 
   81 /* date output formats for -d, -m, -t and image header*/
   82 /* see 'man date' for control codes      1.x values     <1.8 values */
   83 #define DFORMAT "%Y-%m-%d"       /* "%x"         "%d.%m." */
   84 #define MFORMAT "%Y-%m"          /* "%b '%y"     "%b '%y" */
   85 #define TFORMAT "%Y-%m-%d"       /* "%x"         "%d.%m.%y" */
   86 #define HFORMAT "%Y-%m-%d %H:%M" /* "%x %H:%M"   "%d.%m.%Y %H:%M" */
   87 
   88 #ifndef DATETIMEFORMAT
   89 #define DATETIMEFORMAT "%Y-%m-%d %H:%M:%S"
   90 #endif
   91 
   92 /* characters used for visuals */
   93 #define RXCHAR "%"
   94 #define TXCHAR ":"
   95 #define RXHOURCHAR "r"
   96 #define TXHOURCHAR "t"
   97 
   98 /* unit mode */
   99 /* 0 = KiB/MiB/GiB/TiB, 1 = KB/MB/GB/TB */
  100 #define UNITMODE 0
  101 
  102 /* rate unit mode */
  103 /* 0 = Kibit/s..., 1 = kbit/s... */
  104 #define RATEUNITMODE 1
  105 
  106 /* output style */
  107 /* 0 = minimal/narrow, 1 = bars everywhere */
  108 /* 2 = same as 1 + rate in summary */
  109 /* 3 = rate everywhere */
  110 #define OSTYLE 3
  111 #define ESTIMATEBARVISIBLE 1
  112 
  113 /* rate in vnstati summary output */
  114 #define SUMMARYRATE 1
  115 
  116 /* rate in vnstati hourly output */
  117 #define HOURLYRATE 1
  118 
  119 /* rate unit */
  120 /* 0 = bytes, 1 = bits */
  121 #define RATEUNIT 1
  122 
  123 /* number of decimals */
  124 #define DEFAULTDECIMALS 2
  125 #define HOURLYDECIMALS 1
  126 
  127 /* hourly section style */
  128 #define HOURLYSTYLE 2
  129 
  130 /* default interface */
  131 #ifndef DEFIFACE
  132 #define DEFIFACE ""
  133 #endif
  134 
  135 /* default locale */
  136 #define LOCALE "-"
  137 
  138 /* bandwidth detection, 0 = feature disabled */
  139 #define BWDETECT 1
  140 #define BWDETECTINTERVAL 5
  141 
  142 /* default maximum bandwidth (Mbit) for all interfaces */
  143 /* 0 = feature disabled */
  144 #define DEFMAXBW 1000
  145 
  146 /* maximum allowed config value for bandwidth */
  147 #define BWMAX 50000
  148 
  149 /* how many seconds should sampling take by default */
  150 #define DEFSAMPTIME 5
  151 
  152 /* default query mode */
  153 /* 0 = normal, 1 = days, 2 = months, 3 = top, 5 = short */
  154 /* 7 = hours, 8 = xml, 9 = one line, 10 = json */
  155 #define DEFQMODE 0
  156 
  157 /* interface match method */
  158 /* 0 = interface name exact case sensitive, 1 = 0 + case sensitive exact alias */
  159 /* 2 = 1 + case insensitive exact alias, 3 = 2 + case insensitive beginning of alias */
  160 #define IFACEMATCHMETHOD 3
  161 
  162 /* how much the boot time can variate between updates (seconds) */
  163 #define BVAR 15
  164 
  165 /* check disk space by default */
  166 #define USESPACECHECK 1
  167 
  168 /* create trafficless entries by default */
  169 #define TRAFFICLESSENTRIES 1
  170 
  171 /* list outputs */
  172 #define LISTFIVEMINS 24
  173 #define LISTHOURS 24
  174 #define LISTDAYS 30
  175 #define LISTMONTHS 12
  176 #define LISTYEARS 0
  177 #define LISTTOP 10
  178 #define LISTJSONXML 0
  179 
  180 /* data retention defaults */
  181 #define FIVEMINUTEHOURS 48
  182 #define HOURLYDAYS 4
  183 #define DAILYDAYS 62
  184 #define MONTHLYMONTHS 25
  185 #define YEARLYYEARS -1
  186 #define TOPDAYENTRIES 20
  187 
  188 /* assume that locale can be UTF-n when enabled */
  189 #define UTFLOCALE 1
  190 
  191 /* 1 = 2.0 */
  192 #define SQLDBVERSION "1"
  193 
  194 /* xml format version */
  195 /* 1 = 1.7-1.16, 2 = 2.0 */
  196 #define XMLVERSION 2
  197 
  198 /* json format version */
  199 /* 1 = 1.13-1.16, 2 = 2.0 */
  200 #define JSONVERSION 2
  201 
  202 /* json format version, -tr */
  203 /* 1 = 1.18- */
  204 #define JSONVERSION_TR 1
  205 
  206 /* json format version, --live */
  207 /* 1 = 1.18- */
  208 #define JSONVERSION_LIVE 1
  209 
  210 /* --oneline format version */
  211 #define ONELINEVERSION 1
  212 
  213 /* integer limits */
  214 #define MAX32 4294967295ULL
  215 #define MAX64 18446744073709551615ULL
  216 
  217 /* sampletime in seconds for live traffic */
  218 /* don't use values below 2 */
  219 #define LIVETIME 2
  220 
  221 /* /proc/net/dev */
  222 #ifndef PROCNETDEV
  223 #define PROCNETDEV "/proc/net/dev"
  224 #endif
  225 
  226 /* /sys/class/net */
  227 #ifndef SYSCLASSNET
  228 #define SYSCLASSNET "/sys/class/net"
  229 #endif
  230 
  231 /* daemon defaults */
  232 #define UPDATEINTERVAL 20
  233 #define TIMESYNCWAIT 5
  234 #define POLLINTERVAL 5
  235 #define SAVEINTERVAL 5
  236 #define OFFSAVEINTERVAL 30
  237 #define RESCANONSAVE 1
  238 #define ALWAYSADD 0
  239 #define SAVESTATUS 1
  240 #define USELOGGING 2
  241 #define CREATEDIRS 1
  242 #define UPDATEFILEOWNER 1
  243 #define LOGFILE "/var/log/vnstat/vnstat.log"
  244 #define PIDFILE "/var/run/vnstat/vnstat.pid"
  245 #define IS64BIT -2
  246 #define WALDB 0
  247 #define WALDBCHECKPOINTINTERVALMINS 240
  248 #define SLOWDBWARNLIMIT 4.0 // needs to be less than DBREADTIMEOUTSECS
  249 #define DBSYNCHRONOUS -1
  250 #define USEUTC 0
  251 
  252 /* database read timeout */
  253 #define DBREADTIMEOUTSECS 5
  254 
  255 /* no transparency by default */
  256 #define TRANSBG 0
  257 
  258 /* small fonts by default */
  259 #define LARGEFONTS 0
  260 
  261 /* no extra space between lines by default */
  262 #define LINESPACEADJUST 0
  263 
  264 /* no image scaling by default */
  265 #define IMAGESCALE 100
  266 
  267 /* image output estimate bar style */
  268 /* 0 = not shown, 1 = continuation of existing bar, 2 = separate bar */
  269 #define ESTIMATESTYLE 1
  270 
  271 /* bar column in list outputs shows rate when rate column is visible */
  272 #define BARSHOWSRATE 0
  273 
  274 /* 5 minute graph size */
  275 #define FIVEGRESULTCOUNT 576
  276 #define FIVEGHEIGHT 300
  277 #define FIVEGMINRESULTCOUNT 288
  278 #define FIVEGMINHEIGHT 150
  279 
  280 /* hourly graph mode (0 = 24 hour sliding window, 1 = begins from midnight) */
  281 #define HOURLYGMODE 0
  282 
  283 /* graph used in extended summary, 0 = hours, 1 = 5 minutes*/
  284 #define SUMMARYGRAPH 0
  285 
  286 /* default colors */
  287 #define CBACKGROUND "FFFFFF"
  288 #define CEDGE "AEAEAE"
  289 #define CHEADER "606060"
  290 #define CHEADERTITLE "FFFFFF"
  291 #define CHEADERDATE "FFFFFF"
  292 #define CTEXT "000000"
  293 #define CLINE "B0B0B0"
  294 #define CLINEL "-"
  295 #define CRX "92CF00"
  296 #define CRXD "-"
  297 #define CTX "606060"
  298 #define CTXD "-"
  299 
  300 /* number of retries after non-fatal database errors, */
  301 /* will result in given number + 1 tries in total before exit, */
  302 /* a full disk (as reported by sqlite) will no cause retries or exit */
  303 #define DBRETRYLIMIT 5
  304 
  305 /* internal config structure */
  306 typedef struct {
  307     char dformat[64], mformat[64], tformat[64], hformat[64];
  308     char iface[32];
  309     char locale[32];
  310     char dbdir[512], dbtzmodifier[14];
  311     char rxchar[2], txchar[2], rxhourchar[2], txhourchar[2];
  312     char cbg[8], cedge[8], cheader[8], cheadertitle[8], cheaderdate[8], ctext[8];
  313     char cline[8], clinel[8], cvnstat[8], crx[8], crxd[8], ctx[8], ctxd[8];
  314     int32_t unitmode, rateunitmode, rateunit, bvar, qmode, ifacematchmethod, sampletime, hourlyrate, summaryrate;
  315     int32_t monthrotate, monthrotateyears, maxbw, spacecheck, trafficlessentries, transbg, ostyle;
  316     int32_t defaultdecimals, hourlydecimals, hourlystyle, is64bit, waldb, dbsynchronous, useutc, imagescale;
  317     int32_t largefonts, linespaceadjust, estimatebarvisible, estimatestyle, barshowsrate, fivegresultcount;
  318     int32_t fivegheight, summarygraph, hourlygmode, alwaysadd;
  319     char cfgfile[512], logfile[512], pidfile[512];
  320     char daemonuser[33], daemongroup[33];
  321     int32_t timesyncwait, updateinterval, pollinterval, saveinterval, offsaveinterval, rescanonsave, savestatus;
  322     int32_t uselogging, createdirs, updatefileowner, bwdetection, bwdetectioninterval, utflocale;
  323     int32_t fiveminutehours, hourlydays, dailydays, monthlymonths, yearlyyears, topdayentries;
  324     int32_t listfivemins, listhours, listdays, listmonths, listyears, listtop, listjsonxml;
  325     int32_t timestampprints, experimental;
  326 } CFG;
  327 
  328 /* internal interface information structure */
  329 typedef struct {
  330     char name[32];
  331     short filled;
  332     short is64bit;
  333     uint64_t rx;
  334     uint64_t tx;
  335     uint64_t rxp;
  336     uint64_t txp;
  337     time_t timestamp;
  338 } IFINFO;
  339 
  340 typedef struct ibwnode {
  341     char interface[32];
  342     uint32_t limit;
  343     uint32_t fallback;
  344     short retries;
  345     time_t detected;
  346     struct ibwnode *next;
  347 } ibwnode;
  348 
  349 typedef enum PrintType {
  350     PT_Info = 0,
  351     PT_Infoless,
  352     PT_Warning,
  353     PT_Error,
  354     PT_Config,
  355     PT_Multiline,
  356     PT_ShortMultiline
  357 } PrintType;
  358 
  359 /* common functions */
  360 int printe(const PrintType type);
  361 int logprint(const PrintType type);
  362 int verifylogaccess(void);
  363 int dmonth(const int month);
  364 int isleapyear(const int year);
  365 time_t mosecs(time_t month, time_t updated);
  366 uint64_t countercalc(const uint64_t *a, const uint64_t *b, const short is64bit);
  367 char *strncpy_nt(char *dest, const char *src, size_t n);
  368 int isnumeric(const char *s);
  369 void panicexit(const char *sourcefile, const int sourceline) __attribute__((noreturn));
  370 char *getversion(void);
  371 double timeused(const char *func, const int reset);
  372 void timeused_debug(const char *func, const int reset);
  373 
  374 /* global variables */
  375 extern CFG cfg;
  376 extern IFINFO ifinfo;
  377 extern char errorstring[1024];
  378 extern ibwnode *ifacebw;
  379 extern int debug;
  380 extern int noexit; /* = running as daemon if 2 */
  381 extern int intsignal;
  382 extern int pidfile;
  383 extern int disableprints;
  384 
  385 #endif