~ubuntu-branches/ubuntu/vivid/xfce4-netload-plugin/vivid-proposed

« back to all changes in this revision

Viewing changes to .pc/01_fix-snprintf-format.patch/panel-plugin/net.c

  • Committer: Package Import Robot
  • Author(s): Yves-Alexis Perez
  • Date: 2012-01-14 13:38:21 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120114133821-wcootttf8teoc234
Tags: 1.1.0-1
* New upstream release.
  - makes it possible to select bits or bytes as unit.        closes: #652892
* debian/rules:
  - use debhelper 9 and dpkg-dev 1.16.1 hardening support
* debian/control:
  - drop hardening-includes build-dep.
  - update debhelper build-dep for hardening support.
  - add dpkg-dev build-dep for hardening support.
  - replace libxfcegui4 build-dep by libxfce4ui one.
* debian/compat bumped to 9.
* debian/patches
  - 01_fix-snprintf-format dropped, included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2003,2005,2006 Bernhard Walle <bernhard@bwalle.de>
3
 
 * Copyright 2010 Florian Rivoal <frivoal@gmail.com>
4
 
 * -------------------------------------------------------------------------------------------------
5
 
 * 
6
 
 *  This program is free software; you can redistribute it and/or modify it
7
 
 *  under the terms of the GNU General Public License as published by the Free
8
 
 *  Software Foundation; either version 2 of the License, or (at your option)
9
 
 *  any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14
 
 * more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License along with
17
 
 * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
18
 
 * Ave, Cambridge, MA 02139, USA.
19
 
 *
20
 
 * ------------------------------------------------------------------------------------------------- 
21
 
 */
22
 
 
23
 
/*
24
 
 * This is just a wrapper between the netload-plugin and the wormulon source.
25
 
 * Wormulon is a small command-line util which displays the netload. You can find it
26
 
 * at http://raisdorf.net/wormulon. Most sourcecode is taken from wormulon.
27
 
 *
28
 
 * Thanks to Hendrik Scholz. Only his work made it possible to support a large
29
 
 * number of operating systems quickly without a library! Without him only 
30
 
 * Linux and FreeBSD (with foreign code from IceWM) would be supported.
31
 
 */
32
 
#ifdef HAVE_CONFIG_H
33
 
#include <config.h>
34
 
#endif
35
 
 
36
 
/* From Wormulon */
37
 
#include "net.h"
38
 
#include "os.h"
39
 
#include "wormulon.h"
40
 
#include "slurm.h"      /* slurm structs */
41
 
 
42
 
#include <sys/types.h>
43
 
#include <errno.h>
44
 
 
45
 
#include "global.h"
46
 
 
47
 
#ifdef __HPUX__
48
 
# include "wormulon/hpux.h"
49
 
# include "wormulon/hpux.c"
50
 
#elif __APPLE__
51
 
# include "src/macos.h"
52
 
# include "src/macos.c"
53
 
#elif __FreeBSD__ || __DragonFly__ || __FreeBSD_kernel__
54
 
# include "wormulon/freebsd.h"
55
 
# include "wormulon/freebsd.c"
56
 
#elif __linux__
57
 
# include "wormulon/linux.h"
58
 
# include "wormulon/linux.c"
59
 
#elif __OpenBSD__ || __MicroBSD__
60
 
# include "wormulon/openbsd.h"
61
 
# include "wormulon/openbsd.c"
62
 
#elif __NetBSD__
63
 
# include "wormulon/netbsd.h"
64
 
# include "wormulon/netbsd.c"
65
 
#elif __Solaris__
66
 
# include "wormulon/solaris.h"
67
 
# include "wormulon/solaris.c"
68
 
#else
69
 
/* should not get here */
70
 
# error "OS not supported"
71
 
#endif
72
 
 
73
 
 
74
 
/* ---------------------------------------------------------------------------------------------- */
75
 
int init_netload(netdata* data, const char* device)
76
 
{
77
 
    memset( data, 0, sizeof(netdata) );
78
 
    
79
 
    if (device == NULL || strlen(device) == 0)
80
 
    {
81
 
        return TRUE;
82
 
    }
83
 
    
84
 
    strncpy( data->ifdata.if_name, device, INTERFACE_NAME_LENGTH);
85
 
    data->ifdata.if_name[INTERFACE_NAME_LENGTH] = '\0';
86
 
    
87
 
    init_osspecific( data );
88
 
    
89
 
    data->ip_address[0] = 0;
90
 
    data->ip_update_count = 0;
91
 
    data->up = FALSE;
92
 
    data->up_update_count = 0;
93
 
    
94
 
    if (checkinterface(data) != TRUE)
95
 
        {
96
 
        data->correct_interface = FALSE;
97
 
                return FALSE;
98
 
        }
99
 
    
100
 
    /* init in a sane state */
101
 
    get_stat(data);
102
 
    data->backup_in  = data->stats.rx_bytes;
103
 
    data->backup_out = data->stats.tx_bytes;
104
 
    
105
 
    data->correct_interface = TRUE;
106
 
    
107
 
    PRINT_DBG("The netload plugin was initialized for '%s'.", device);
108
 
    
109
 
    return TRUE;
110
 
}
111
 
 
112
 
 
113
 
/* ---------------------------------------------------------------------------------------------- */
114
 
void get_current_netload(netdata* data, unsigned long *in, unsigned long *out, unsigned long *tot) 
115
 
{
116
 
    struct timeval curr_time;
117
 
    double delta_t;
118
 
    
119
 
    if (! data->correct_interface)
120
 
    {
121
 
        if (in != NULL && out != NULL && tot != NULL)
122
 
        {
123
 
            *in = *out = *tot = 0;
124
 
        }
125
 
    }
126
 
    
127
 
    gettimeofday(&curr_time, NULL);
128
 
    
129
 
    delta_t = (double) ((curr_time.tv_sec  - data->prev_time.tv_sec) * 1000000L
130
 
                             + (curr_time.tv_usec - data->prev_time.tv_usec)) / 1000000.0;
131
 
    
132
 
    /* update */
133
 
    get_stat(data);
134
 
    if (data->backup_in > data->stats.rx_bytes)
135
 
    {
136
 
        data->cur_in = (int)( data->stats.rx_bytes / delta_t + 0.5);
137
 
    }
138
 
    else
139
 
    {
140
 
        data->cur_in = (int)( (data->stats.rx_bytes - data->backup_in) / delta_t + 0.5);
141
 
    }
142
 
        
143
 
    if (data->backup_out > data->stats.tx_bytes)
144
 
    {
145
 
        data->cur_out = (int)( data->stats.tx_bytes / delta_t + 0.5);
146
 
    }
147
 
    else
148
 
    {
149
 
        data->cur_out = (int)( (data->stats.tx_bytes - data->backup_out) / delta_t + 0.5);
150
 
    }
151
 
 
152
 
    if (in != NULL && out != NULL && tot != NULL)
153
 
    {
154
 
        *in = data->cur_in;
155
 
        *out = data->cur_out;
156
 
        *tot = *in + *out;
157
 
    }
158
 
    
159
 
    /* save 'new old' values */
160
 
    data->backup_in = data->stats.rx_bytes;
161
 
    data->backup_out = data->stats.tx_bytes;
162
 
    
163
 
    /* do the same with time */
164
 
    data->prev_time.tv_sec = curr_time.tv_sec;
165
 
    data->prev_time.tv_usec = curr_time.tv_usec;
166
 
}
167
 
 
168
 
 
169
 
/* ---------------------------------------------------------------------------------------------- */
170
 
char* get_name(netdata* data)
171
 
{
172
 
    return data->ifdata.if_name;
173
 
}
174
 
 
175
 
 
176
 
/* ---------------------------------------------------------------------------------------------- */
177
 
int get_interface_up(netdata* data)
178
 
{
179
 
    int sockfd;
180
 
    struct ifreq ifr;
181
 
    struct sockaddr_in *p_sa;
182
 
        
183
 
    /* if the update count is non-zero */ 
184
 
    if (data->up_update_count > 0)
185
 
    {
186
 
        data->up_update_count--;
187
 
        return data->up;
188
 
    }
189
 
 
190
 
    /* get the value from the operating system */
191
 
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
192
 
    {
193
 
        PRINT_DBG("Error in socket: %s", strerror(errno));
194
 
        return FALSE;
195
 
    }
196
 
    
197
 
    snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
198
 
    if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) != 0)
199
 
    {
200
 
        PRINT_DBG("Error in ioctl(sockfd): %s", strerror(errno));
201
 
        close(sockfd);
202
 
        return FALSE;
203
 
    }
204
 
    close(sockfd);
205
 
 
206
 
    data->up = ((ifr.ifr_flags & IFF_UP) == IFF_UP) ? TRUE : FALSE;
207
 
    data->up_update_count = UP_UPDATE_INTERVAL;
208
 
 
209
 
    return data->up;
210
 
}
211
 
 
212
 
 
213
 
/* ---------------------------------------------------------------------------------------------- */
214
 
char* get_ip_address(netdata* data)
215
 
{
216
 
    int sockfd;
217
 
    struct ifreq ifr;
218
 
    struct sockaddr_in *p_sa;
219
 
        
220
 
    /* use cached value if possible and if the update count is non-zero */ 
221
 
    if (data->ip_address && data->ip_update_count > 0)
222
 
    {
223
 
        data->ip_update_count--;
224
 
        return data->ip_address;
225
 
    }
226
 
    
227
 
    /* get the value from the operating system */
228
 
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
229
 
    {
230
 
        PRINT_DBG("Error in socket: %s", strerror(errno));
231
 
        return NULL;
232
 
    }
233
 
    
234
 
    snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
235
 
    if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0)
236
 
    {
237
 
            if (errno != EADDRNOTAVAIL)
238
 
        {
239
 
            PRINT_DBG("Error in ioctl(sockfd): %s", strerror(errno));
240
 
        }
241
 
        close(sockfd);
242
 
        return NULL;
243
 
    }
244
 
    close(sockfd);
245
 
    
246
 
    p_sa = (struct sockaddr_in*) &ifr.ifr_addr;
247
 
    
248
 
    if (!inet_ntop(AF_INET, &p_sa->sin_addr, data->ip_address, IP_ADDRESS_LENGTH))
249
 
    {
250
 
        PRINT_DBG("Error in inet_ntop: %s", strerror(errno));
251
 
        return NULL;
252
 
    }
253
 
    
254
 
    /* now updated */
255
 
    data->ip_update_count = IP_UPDATE_INTERVAL;
256
 
    
257
 
    return data->ip_address;
258
 
}
259
 
 
260
 
 
261
 
/* ---------------------------------------------------------------------------------------------- */
262
 
void close_netload(netdata* data)
263
 
{
264
 
    /* We need not code here */
265
 
}
266