1 | /* $Id: ifplugstatus.c,v 1.1.1.1 2004-04-09 20:09:09 amb Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of ifplugd. |
---|
5 | * |
---|
6 | * ifplugd is free software; you can redistribute it and/or modify it |
---|
7 | * under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * ifplugd is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with ifplugd; if not, write to the Free Software Foundation, |
---|
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
19 | */ |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <string.h> |
---|
24 | #include <unistd.h> |
---|
25 | #include <getopt.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <sys/ioctl.h> |
---|
28 | #include <net/if.h> |
---|
29 | |
---|
30 | #include <libdaemon/dlog.h> |
---|
31 | |
---|
32 | #include "interface.h" |
---|
33 | #include "svn-revision.h" |
---|
34 | |
---|
35 | #ifdef HAVE_CONFIG_H |
---|
36 | #include <config.h> |
---|
37 | #endif |
---|
38 | |
---|
39 | int interface_auto_up = 0, interface_do_message = 0; |
---|
40 | |
---|
41 | int verbose = 0; |
---|
42 | char *interface = NULL; |
---|
43 | |
---|
44 | int handle(char *iface) { |
---|
45 | int fd, r = 0; |
---|
46 | interface_status_t s; |
---|
47 | |
---|
48 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
---|
49 | return -1; |
---|
50 | |
---|
51 | if (verbose > 0) { |
---|
52 | printf("%s:\n", iface); |
---|
53 | |
---|
54 | if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR) |
---|
55 | printf(" SIOCETHTOOL failed (%s)\n", strerror(errno)); |
---|
56 | else |
---|
57 | printf(" SIOCETHTOOL: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged"); |
---|
58 | |
---|
59 | if ((s = interface_detect_beat_mii(fd, iface)) == IFSTATUS_ERR) |
---|
60 | printf(" SIOCGMIIPHY failed (%s)\n", strerror(errno)); |
---|
61 | else |
---|
62 | printf(" SIOCGMIIPHY: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged"); |
---|
63 | |
---|
64 | if ((s = interface_detect_beat_priv(fd, iface)) == IFSTATUS_ERR) |
---|
65 | printf(" SIOCDEVPRIVATE failed (%s)\n", strerror(errno)); |
---|
66 | else |
---|
67 | printf(" SIOCDEVPRIVATE: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged"); |
---|
68 | |
---|
69 | if ((s = interface_detect_beat_wlan(fd, iface)) == IFSTATUS_ERR) |
---|
70 | printf(" Wireless failed.\n"); |
---|
71 | else |
---|
72 | printf(" Wireless: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged"); |
---|
73 | |
---|
74 | } else { |
---|
75 | |
---|
76 | if ((s = interface_detect_beat_mii(fd, iface)) == IFSTATUS_ERR) |
---|
77 | if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR) |
---|
78 | if ((s = interface_detect_beat_wlan(fd, iface)) == IFSTATUS_ERR) |
---|
79 | s = interface_detect_beat_priv(fd, iface); |
---|
80 | |
---|
81 | switch(s) { |
---|
82 | case IFSTATUS_UP: |
---|
83 | if (!verbose) |
---|
84 | printf("%s: link beat detected\n", iface); |
---|
85 | r = 1; |
---|
86 | break; |
---|
87 | |
---|
88 | case IFSTATUS_DOWN: |
---|
89 | if (!verbose) |
---|
90 | printf("%s: unplugged\n", iface); |
---|
91 | r = 2; |
---|
92 | break; |
---|
93 | |
---|
94 | default: |
---|
95 | if (!verbose) |
---|
96 | printf("%s: not supported%s\n", iface, getuid() != 0 ? " (Retry as root?)" : ""); |
---|
97 | |
---|
98 | r = -1; |
---|
99 | break; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | close(fd); |
---|
105 | return r; |
---|
106 | } |
---|
107 | |
---|
108 | void usage(char *p) { |
---|
109 | if (strrchr(p, '/')) |
---|
110 | p = strchr(p, '/')+1; |
---|
111 | |
---|
112 | printf("%s [options] [INTERFACE]\n" |
---|
113 | " -a --auto Enable interface automatically (%s)\n" |
---|
114 | " -q --quiet Quiet behaviour (%s)\n" |
---|
115 | " -v --verbose Enable verbosity (%s)\n" |
---|
116 | " -h --help Show this help\n" |
---|
117 | " -V --version Show version number\n", |
---|
118 | p, interface_auto_up ? "on" : "off", verbose < 0 ? "on" : "off", verbose > 0 ? "on" : "off"); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | void parse(int argc, char *argv[]) { |
---|
123 | static struct option long_options[] = { |
---|
124 | {"auto", no_argument, 0, 'a'}, |
---|
125 | {"quiet", no_argument, 0, 'q'}, |
---|
126 | {"verbose", no_argument, 0, 'v'}, |
---|
127 | {"help", no_argument, 0, 'h'}, |
---|
128 | {"version", no_argument, 0, 'V'}, |
---|
129 | {0, 0, 0, 0} |
---|
130 | }; |
---|
131 | int option_index = 0; |
---|
132 | int help = 0; |
---|
133 | |
---|
134 | for (;;) { |
---|
135 | int c; |
---|
136 | |
---|
137 | if ((c = getopt_long(argc, argv, "avhqV", long_options, &option_index)) < 0) |
---|
138 | break; |
---|
139 | |
---|
140 | switch (c) { |
---|
141 | case 'a' : |
---|
142 | interface_auto_up = !interface_auto_up; |
---|
143 | break; |
---|
144 | case 'v': |
---|
145 | verbose++; |
---|
146 | break; |
---|
147 | case 'q': |
---|
148 | verbose--; |
---|
149 | break; |
---|
150 | case 'h': |
---|
151 | help = 1; |
---|
152 | break; |
---|
153 | case 'V': |
---|
154 | #ifdef SVN_REVISION |
---|
155 | printf("ifplugstatus "VERSION" (SVN: "SVN_REVISION")\n"); |
---|
156 | #else |
---|
157 | printf("ifplugstatus "VERSION"\n"); |
---|
158 | #endif |
---|
159 | exit(0); |
---|
160 | default: |
---|
161 | fprintf(stderr, "Unknown parameter.\n"); |
---|
162 | exit(1); |
---|
163 | |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | if (help) { |
---|
168 | usage(argv[0]); |
---|
169 | exit(0); |
---|
170 | } |
---|
171 | |
---|
172 | if (optind < argc) |
---|
173 | interface = argv[optind]; |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | int main(int argc, char *argv[]) { |
---|
178 | parse(argc, argv); |
---|
179 | |
---|
180 | if (interface) { |
---|
181 | int r; |
---|
182 | |
---|
183 | if ((r = handle(interface)) < 0) { |
---|
184 | if (verbose == 0) |
---|
185 | fprintf(stderr, "Failure (%s)\n", strerror(errno)); |
---|
186 | return 1; |
---|
187 | } |
---|
188 | |
---|
189 | return r+1; |
---|
190 | |
---|
191 | } else { |
---|
192 | FILE *f; |
---|
193 | char ln[256]; |
---|
194 | |
---|
195 | if (!(f = fopen("/proc/net/dev", "r"))) { |
---|
196 | fprintf(stderr, "Failed to open /proc/net/dev: %s\n", strerror(errno)); |
---|
197 | return 1; |
---|
198 | } |
---|
199 | |
---|
200 | fgets(ln, sizeof(ln), f); |
---|
201 | fgets(ln, sizeof(ln), f); |
---|
202 | |
---|
203 | while (fgets(ln, sizeof(ln), f)) { |
---|
204 | char *p, *e; |
---|
205 | |
---|
206 | p = ln+strspn(ln, " \t"); |
---|
207 | if (!(e = strchr(p, ':'))) { |
---|
208 | fprintf(stderr, "Parse failure in /proc/net/dev.\n"); |
---|
209 | fclose(f); |
---|
210 | return 1; |
---|
211 | } |
---|
212 | |
---|
213 | *e = 0; |
---|
214 | handle(p); |
---|
215 | } |
---|
216 | |
---|
217 | fclose(f); |
---|
218 | } |
---|
219 | |
---|
220 | return 0; |
---|
221 | } |
---|