~ubuntu-branches/ubuntu/trusty/rstatd/trusty

« back to all changes in this revision

Viewing changes to .pc/04-fix-no-format.patch/rsysinfo.c

  • Committer: Package Import Robot
  • Author(s): Anibal Monsalve Salazar
  • Date: 2012-06-13 19:45:28 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120613194528-jie61l066k3qdq0l
Tags: 4.0.1-7
Enable hardened build flags
Add 04-fix-no-format.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: rsysinfo.c,v 1.4 2005/09/07 08:07:28 afm Exp $
 
3
 *  rsysinfo version 3.03 rpc ruptime client for use with rpc.rstatd 3.03
 
4
 *  Copyright (C) 1995  Adam Migus, Memorial University of Newfoundland
 
5
 *      (MUN)
 
6
 *  Copyright (C) 2001 Andreas Klingler, University of Erlangen-Nuernberg
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 1, or (at your option)
 
11
 *  any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
21
 *
 
22
 *  If you make modifications to the source, I would be happy to have
 
23
 *  them to include in future releases.  Feel free to send them to:
 
24
 *      Adam Migus                                      
 
25
 *              amigus@cs.mun.ca 
 
26
 *      amigus@ucs.mun.ca   
 
27
 *
 
28
 **************************************************************************/
 
29
 
 
30
#include <stdio.h>
 
31
#include <rpc/rpc.h>
 
32
#include <sys/socket.h>
 
33
#include <sys/time.h>
 
34
#include <time.h>
 
35
#include <netdb.h>
 
36
#include <netinet/in.h>
 
37
#include <arpa/inet.h>
 
38
#include <unistd.h>
 
39
#include "rstat.h"
 
40
 
 
41
void bzero();
 
42
void bcopy();
 
43
char *rindex();
 
44
 
 
45
static char *id = "@(#) $Id: rsysinfo.c,v 1.4 2005/09/07 08:07:28 afm Exp $";
 
46
static char *version0 = "@(#)rsysinfo version 3.06 (c) Andreas Klingler 3/5/2001 based on\n";
 
47
char *version = "@(#)rsysinfo version 3.03 Copyright (Adam Migus) 1/9/96\n";
 
48
char *program_name;
 
49
 
 
50
int     rsysinfo_answer_v3(struct statstime *replyp,
 
51
        struct sockaddr_in *raddrp)
 
52
{
 
53
        struct hostent *hp;
 
54
        char *host;
 
55
        struct statstime *host_stat;
 
56
        struct tm *tmp_time;
 
57
        struct tm host_time;
 
58
        struct tm host_uptime;
 
59
        char days_buf[16];
 
60
        char hours_buf[16];
 
61
 
 
62
        host_stat = (statstime *)replyp;
 
63
 
 
64
        hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
 
65
                           sizeof(struct in_addr), AF_INET);
 
66
        if (hp)
 
67
                host = (char *)hp->h_name;
 
68
        else
 
69
                host = (char *)inet_ntoa(raddrp->sin_addr);
 
70
 
 
71
 
 
72
        tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
 
73
        host_time = *tmp_time;
 
74
 
 
75
        host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
 
76
 
 
77
        tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
 
78
        host_uptime = *tmp_time;
 
79
 
 
80
 
 
81
        if (host_uptime.tm_yday != 0)
 
82
                sprintf(days_buf, "%3d day%s, ", host_uptime.tm_yday,
 
83
                        (host_uptime.tm_yday > 1) ? "s" : "");
 
84
        else
 
85
                days_buf[0] = '\0';
 
86
 
 
87
        if (host_uptime.tm_hour != 0)
 
88
                sprintf(hours_buf, "%2d:%02d, ",
 
89
                        host_uptime.tm_hour, host_uptime.tm_min);
 
90
        else
 
91
                if (host_uptime.tm_min != 0)
 
92
                        sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
 
93
                else
 
94
                        hours_buf[0] = '\0';
 
95
 
 
96
 
 
97
        printf("System Information for: %s\n",host);
 
98
        printf("uptime: %s%sload average: %.2f %.2f %.2f\n",
 
99
                days_buf, hours_buf, host_stat->avenrun[0]/256.0,
 
100
                host_stat->avenrun[1]/256.0, host_stat->avenrun[2]/256.0);
 
101
 
 
102
        printf("cpu usage (jiffies): user %d  nice %d  system %d  idle %d\n",
 
103
                host_stat->cp_time[0], host_stat->cp_time[1],
 
104
                host_stat->cp_time[2], host_stat->cp_time[3]);
 
105
 
 
106
        printf("page in: %d  page out: %d   swap in: %d  swap out: %d\n",
 
107
                host_stat->v_pgpgin, host_stat->v_pgpgout,
 
108
                host_stat->v_pswpin, host_stat->v_pswpout);
 
109
        printf("intr: %d     context switches: %d\n",
 
110
                host_stat->v_intr, host_stat->v_swtch);
 
111
        printf ("disks: %d %d %d %d\n",
 
112
                host_stat->dk_xfer[0], host_stat->dk_xfer[1],
 
113
                host_stat->dk_xfer[2], host_stat->dk_xfer[3]);
 
114
        printf("ethernet:  rx: %u   rx-err: %u\n"
 
115
                        "           tx: %u   tx-err: %u    collisions: %u\n",
 
116
                host_stat->if_ipackets, host_stat->if_ierrors,
 
117
                host_stat->if_opackets, host_stat->if_oerrors,
 
118
                host_stat->if_collisions);
 
119
 
 
120
        return(0);
 
121
}
 
122
 
 
123
int pointopoint_v3(server_addr)
 
124
struct sockaddr_in *server_addr;
 
125
{
 
126
        struct statstime host_stat;
 
127
        struct timeval pertry_timeout, total_timeout;
 
128
        enum clnt_stat clnt_stat;
 
129
        int sock=RPC_ANYSOCK;
 
130
        CLIENT *client;
 
131
 
 
132
        pertry_timeout.tv_sec = 3;
 
133
        pertry_timeout.tv_usec = 0;
 
134
        total_timeout.tv_sec = 10;
 
135
        total_timeout.tv_usec = 0;
 
136
 
 
137
        if((client = clntudp_create(server_addr, RSTATPROG, RSTATVERS_TIME,
 
138
        pertry_timeout, &sock)) == NULL) {
 
139
                close(sock);
 
140
                return(0);
 
141
        }
 
142
        clnt_stat = clnt_call(client, RSTATPROC_STATS, (xdrproc_t) xdr_void, 0,
 
143
                (xdrproc_t)xdr_statstime, (void *)&host_stat,
 
144
                total_timeout);
 
145
 
 
146
        if(clnt_stat == RPC_SUCCESS) {
 
147
                rsysinfo_answer_v3(&host_stat, server_addr);
 
148
                clnt_destroy(client);
 
149
                close(sock);
 
150
                return(1);
 
151
        }
 
152
        else {
 
153
                clnt_destroy(client);
 
154
                close(sock);
 
155
                return(0);
 
156
        }
 
157
        return(0);
 
158
}
 
159
void usage()
 
160
{
 
161
        printf("usage: %s \"host\" | -v\n"\
 
162
                        "\t-v\tprint version and exit\n", program_name);
 
163
}
 
164
 
 
165
int main(int argc, char **argv)
 
166
{
 
167
        struct hostent *hp;
 
168
        struct sockaddr_in server_addr;
 
169
        char c;
 
170
 
 
171
        program_name = rindex(argv[0],'/');
 
172
        if(!(program_name)) 
 
173
                program_name = argv[0];
 
174
        else 
 
175
                program_name++;
 
176
        
 
177
        if(argc == 1) {
 
178
                usage();
 
179
                exit(0);
 
180
        }
 
181
 
 
182
        while((c = getopt(argc,argv,"h?v")) != EOF)
 
183
                switch(c) {
 
184
                        case 'v':
 
185
                                                printf(version0);
 
186
                                                printf(version);
 
187
 
 
188
                                                exit(0);
 
189
                        case '?':
 
190
                        case 'h':
 
191
                        default:
 
192
                                                usage();
 
193
                                                exit(0);
 
194
                }
 
195
 
 
196
        if((hp = gethostbyname(argv[1])) == NULL) {
 
197
                fprintf(stderr,"%s: cannot get hostname %s\n",program_name,argv[1]);
 
198
                exit(-1);
 
199
        }
 
200
 
 
201
        bcopy(hp->h_addr,(caddr_t)&server_addr.sin_addr, hp->h_length);
 
202
        server_addr.sin_family = AF_INET;
 
203
        server_addr.sin_port = 0;
 
204
 
 
205
        if(pointopoint_v3(&server_addr) == 1)
 
206
                exit(0);
 
207
        clnt_pcreateerror(program_name);
 
208
        exit(EXIT_FAILURE);
 
209
}