"Fossies" - the Fresh Open Source Software Archive 
Member "vnstat-2.9/src/vnstat.c" (23 Jan 2022, 4286 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 "vnstat.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.8_vs_2.9.
1 /*
2 vnStat - Copyright (C) 2002-2022 Teemu Toivola <tst@iki.fi>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 dated June, 1991.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include "common.h"
19 #include "ifinfo.h"
20 #include "dbsql.h"
21 #include "misc.h"
22 #include "cfg.h"
23 #include "ibw.h"
24 #include "vnstat_func.h"
25
26 int main(int argc, char *argv[])
27 {
28 int currentarg;
29 DIR *dir = NULL;
30 PARAMS p;
31
32 initparams(&p);
33
34 /* early check for debug and config parameter */
35 if (argc > 1) {
36 for (currentarg = 1; currentarg < argc; currentarg++) {
37 if ((strcmp(argv[currentarg], "-D") == 0) || (strcmp(argv[currentarg], "--debug") == 0)) {
38 debug = 1;
39 printf("Debug enabled, vnstat %s\n", VERSION);
40 } else if (strcmp(argv[currentarg], "--config") == 0) {
41 if (currentarg + 1 < argc) {
42 strncpy_nt(p.cfgfile, argv[currentarg + 1], 512);
43 if (debug)
44 printf("Used config file: %s\n", p.cfgfile);
45 currentarg++;
46 } else {
47 printf("Error: File for --config missing.\n");
48 return 1;
49 }
50 }
51 }
52 }
53
54 /* load config if available */
55 if (!loadcfg(p.cfgfile, CT_CLI)) {
56 return 1;
57 }
58 if (!ibwloadcfg(p.cfgfile)) {
59 return 1;
60 }
61
62 configlocale();
63 strncpy_nt(p.interface, "default", 32);
64 strncpy_nt(p.definterface, cfg.iface, 32);
65 strncpy_nt(p.alias, "none", 32);
66
67 parseargs(&p, argc, argv);
68
69 /* open database and see if it contains any interfaces */
70 if (!p.traffic && !p.livetraffic) {
71 if ((dir = opendir(cfg.dbdir)) != NULL) {
72 if (debug)
73 printf("Dir OK\n");
74 closedir(dir);
75 if (!db_open_ro()) {
76 printf("Error: Failed to open database \"%s/%s\" in read-only mode.\n", cfg.dbdir, DATABASEFILE);
77 if (errno == ENOENT) {
78 printf("The vnStat daemon should have created the database when started.\n");
79 printf("Check that it is configured and running. See also \"man vnstatd\".\n");
80 }
81 return 1;
82 }
83 p.dbifcount = db_getinterfacecount();
84 if (debug)
85 printf("%" PRIu64 " interface(s) found\n", p.dbifcount);
86
87 if (p.dbifcount > 1) {
88 strncpy_nt(p.definterface, cfg.iface, 32);
89 }
90 } else {
91 printf("Error: Unable to open database directory \"%s\": %s\n", cfg.dbdir, strerror(errno));
92 if (errno == ENOENT) {
93 printf("The vnStat daemon should have created this directory when started.\n");
94 printf("Check that it is configured and running. See also \"man vnstatd\".\n");
95 } else {
96 printf("Make sure it is at least read enabled for current user.\n");
97 printf("Use --help for help.\n");
98 }
99 return 1;
100 }
101 }
102
103 /* set used interface if none specified */
104 handleifselection(&p);
105
106 /* parameter handlers */
107 handleshowalert(&p);
108 handleremoveinterface(&p);
109 handlerenameinterface(&p);
110 handleaddinterface(&p);
111 handlesetalias(&p);
112 handleshowdata(&p);
113 handletrafficmeters(&p);
114
115 /* show something if nothing was shown previously */
116 if (!p.query && !p.traffic && !p.livetraffic) {
117
118 /* give more help if there's no database */
119 if (p.dbifcount == 0) {
120 getifliststring(&p.ifacelist, 1);
121 printf("No interfaces found in the database, nothing to do. Use --help for help.\n\n");
122 printf("Interfaces can be added to the database with the following command:\n");
123 printf(" %s --add -i eth0\n\n", argv[0]);
124 printf("Replace 'eth0' with the interface that should be monitored.\n\n");
125 if (strlen(cfg.cfgfile)) {
126 printf("The default interface can be changed by updating the \"Interface\" keyword\n");
127 printf("value in the configuration file \"%s\".\n\n", cfg.cfgfile);
128 }
129 printf("The following interfaces are currently available:\n %s\n", p.ifacelist);
130 free(p.ifacelist);
131 } else {
132 printf("Nothing to do. Use --help for help.\n");
133 }
134 }
135
136 /* cleanup */
137 db_close();
138 ibwflush();
139
140 return 0;
141 }