[BACK]Return to rup.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rup

Annotation of src/usr.bin/rup/rup.c, Revision 1.26

1.26    ! okan        1: /*     $OpenBSD: rup.c,v 1.25 2013/04/03 04:12:23 deraadt Exp $        */
1.2       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1993, John Brezak
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.20      deraadt    15:  *  3. The name of the author may not be used to endorse or promote products
                     16:  *     derived from this software without specific prior written permission.
1.1       deraadt    17:  *
                     18:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     19:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     20:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     21:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     22:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     23:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     24:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     25:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     26:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     27:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     28:  * SUCH DAMAGE.
                     29:  */
                     30:
                     31: #include <stdio.h>
                     32: #include <stdlib.h>
                     33: #include <string.h>
                     34: #include <time.h>
                     35: #include <sys/param.h>
                     36: #include <sys/socket.h>
                     37: #include <netdb.h>
                     38: #include <rpc/rpc.h>
1.7       deraadt    39: #include <rpc/pmap_clnt.h>
1.1       deraadt    40: #include <arpa/inet.h>
                     41: #include <err.h>
1.26    ! okan       42: #include <unistd.h>
1.1       deraadt    43:
                     44: #undef FSHIFT                  /* Use protocol's shift and scale values */
                     45: #undef FSCALE
                     46: #include <rpcsvc/rstat.h>
                     47:
1.11      aaron      48: #define HOST_WIDTH 27
1.1       deraadt    49:
                     50: int printtime;                 /* print the remote host(s)'s time */
                     51:
                     52: struct host_list {
                     53:        struct host_list *next;
                     54:        struct in_addr addr;
                     55: } *hosts;
                     56:
1.15      millert    57: void usage(void);
                     58: int print_rup_data(char *, statstime *host_stat);
1.7       deraadt    59:
1.18      deraadt    60: static int
1.17      deraadt    61: search_host(struct in_addr addr)
1.1       deraadt    62: {
                     63:        struct host_list *hp;
1.22      moritz     64:
1.1       deraadt    65:        if (!hosts)
                     66:                return(0);
                     67:
                     68:        for (hp = hosts; hp != NULL; hp = hp->next) {
                     69:                if (hp->addr.s_addr == addr.s_addr)
                     70:                        return(1);
                     71:        }
                     72:        return(0);
                     73: }
                     74:
1.18      deraadt    75: static void
1.17      deraadt    76: remember_host(struct in_addr addr)
1.1       deraadt    77: {
                     78:        struct host_list *hp;
                     79:
                     80:        if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
                     81:                err(1, NULL);
                     82:                /* NOTREACHED */
                     83:        }
                     84:        hp->addr.s_addr = addr.s_addr;
                     85:        hp->next = hosts;
                     86:        hosts = hp;
                     87: }
                     88:
                     89:
                     90: struct rup_data {
                     91:        char *host;
                     92:        struct statstime statstime;
                     93: };
                     94: struct rup_data *rup_data;
                     95: int rup_data_idx = 0;
                     96: int rup_data_max = 0;
                     97:
1.17      deraadt    98: enum sort_type {
1.1       deraadt    99:        SORT_NONE,
                    100:        SORT_HOST,
                    101:        SORT_LDAV,
                    102:        SORT_UPTIME
                    103: };
                    104: enum sort_type sort_type;
                    105:
1.18      deraadt   106: static int
1.17      deraadt   107: compare(const void *v1, const void *v2)
1.1       deraadt   108: {
1.17      deraadt   109:        const struct rup_data *d1 = v1;
                    110:        const struct rup_data *d2 = v2;
                    111:
1.1       deraadt   112:        switch(sort_type) {
                    113:        case SORT_HOST:
                    114:                return strcmp(d1->host, d2->host);
                    115:        case SORT_LDAV:
1.3       deraadt   116:                return d1->statstime.avenrun[0]
1.1       deraadt   117:                        - d2->statstime.avenrun[0];
                    118:        case SORT_UPTIME:
1.17      deraadt   119:                return d1->statstime.boottime.tv_sec
1.1       deraadt   120:                        - d2->statstime.boottime.tv_sec;
                    121:        default:
                    122:                /* something's really wrong here */
                    123:                abort();
                    124:        }
                    125: }
                    126:
1.18      deraadt   127: static void
1.17      deraadt   128: remember_rup_data(char *host, struct statstime *st)
1.1       deraadt   129: {
1.14      deraadt   130:        if (rup_data_idx >= rup_data_max) {
1.19      tedu      131:                int newsize;
                    132:                struct rup_data *newrup;
                    133:
                    134:                newsize = rup_data_max + 16;
                    135:                newrup = realloc(rup_data, newsize * sizeof(struct rup_data));
                    136:                if (newrup == NULL) {
1.14      deraadt   137:                        err(1, NULL);
1.1       deraadt   138:                        /* NOTREACHED */
1.14      deraadt   139:                }
1.19      tedu      140:                rup_data = newrup;
                    141:                rup_data_max = newsize;
1.14      deraadt   142:        }
1.22      moritz    143:
1.19      tedu      144:        if ((rup_data[rup_data_idx].host = strdup(host)) == NULL)
                    145:                err(1, NULL);
1.1       deraadt   146:        rup_data[rup_data_idx].statstime = *st;
                    147:        rup_data_idx++;
                    148: }
                    149:
                    150:
1.18      deraadt   151: static int
1.17      deraadt   152: rstat_reply(char *replyp, struct sockaddr_in *raddrp)
1.1       deraadt   153: {
                    154:        struct hostent *hp;
                    155:        char *host;
                    156:        statstime *host_stat = (statstime *)replyp;
                    157:
                    158:        if (!search_host(raddrp->sin_addr)) {
                    159:                hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
1.3       deraadt   160:                    sizeof(struct in_addr), AF_INET);
1.1       deraadt   161:                if (hp)
                    162:                        host = hp->h_name;
                    163:                else
                    164:                        host = inet_ntoa(raddrp->sin_addr);
                    165:
                    166:                remember_host(raddrp->sin_addr);
                    167:
1.3       deraadt   168:                if (sort_type != SORT_NONE)
1.1       deraadt   169:                        remember_rup_data(host, host_stat);
1.3       deraadt   170:                else
1.1       deraadt   171:                        print_rup_data(host, host_stat);
                    172:        }
                    173:
                    174:        return (0);
                    175: }
                    176:
                    177:
                    178: int
1.17      deraadt   179: print_rup_data(char *host, statstime *host_stat)
1.1       deraadt   180: {
1.16      deraadt   181:        unsigned int ups = 0, upm = 0, uph = 0, upd = 0;
                    182:        struct tm *tmp_time, host_time;
                    183:        char days_buf[16], hours_buf[16];
1.25      deraadt   184:        time_t tim;
1.1       deraadt   185:
1.9       millert   186:        if (printtime)
1.11      aaron     187:                printf("%-*.*s", HOST_WIDTH-8, HOST_WIDTH-8, host);
1.9       millert   188:        else
                    189:                printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
1.1       deraadt   190:
1.25      deraadt   191:        tim = host_stat->curtime.tv_sec;
                    192:        tmp_time = localtime(&tim);
1.1       deraadt   193:        host_time = *tmp_time;
                    194:
                    195:        host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
                    196:
1.12      millert   197:        if (host_stat->curtime.tv_sec > 0)
1.22      moritz    198:                ups = host_stat->curtime.tv_sec;
                    199:        upd = ups / (3600 * 24);
                    200:        ups -= upd * 3600 * 24;
                    201:        uph = ups / 3600;
                    202:        ups -= uph * 3600;
                    203:        upm = ups / 60;
1.6       tholo     204:
                    205:        if (upd != 0)
1.16      deraadt   206:                snprintf(days_buf, sizeof days_buf, "%3u day%s, ", upd,
                    207:                    (upd > 1) ? "s" : "");
1.1       deraadt   208:        else
                    209:                days_buf[0] = '\0';
                    210:
1.6       tholo     211:        if (uph != 0)
1.16      deraadt   212:                snprintf(hours_buf, sizeof hours_buf, "%2u:%02u, ",
                    213:                    uph, upm);
1.1       deraadt   214:        else
1.6       tholo     215:                if (upm != 0)
1.16      deraadt   216:                        snprintf(hours_buf, sizeof hours_buf, "%2u min%s ",
                    217:                            upm, (upm == 1) ? ", " : "s,");
1.1       deraadt   218:                else
                    219:                        hours_buf[0] = '\0';
                    220:
                    221:        if (printtime)
1.4       deraadt   222:                printf(" %2d:%02d%cm",
                    223:                    (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
1.3       deraadt   224:                    host_time.tm_min,
                    225:                    (host_time.tm_hour >= 12) ? 'p' : 'a');
1.1       deraadt   226:
                    227:        printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
1.3       deraadt   228:            days_buf, hours_buf,
1.22      moritz    229:            (double)host_stat->avenrun[0] / FSCALE,
                    230:            (double)host_stat->avenrun[1] / FSCALE,
                    231:            (double)host_stat->avenrun[2] / FSCALE);
1.1       deraadt   232:
                    233:        return(0);
                    234: }
                    235:
                    236:
1.18      deraadt   237: static void
1.17      deraadt   238: onehost(char *host)
1.1       deraadt   239: {
                    240:        CLIENT *rstat_clnt;
                    241:        statstime host_stat;
                    242:        static struct timeval timeout = {25, 0};
1.5       deraadt   243:        extern char *__progname;
1.22      moritz    244:
1.1       deraadt   245:        rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
                    246:        if (rstat_clnt == NULL) {
1.5       deraadt   247:                fprintf(stderr, "%s: %s", __progname,
                    248:                    clnt_spcreateerror(host));
1.1       deraadt   249:                return;
                    250:        }
                    251:
                    252:        bzero((char *)&host_stat, sizeof(host_stat));
1.3       deraadt   253:        if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL,
                    254:            xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
1.5       deraadt   255:                fprintf(stderr, "%s: %s", __progname,
                    256:                    clnt_sperror(rstat_clnt, host));
1.13      deraadt   257:                clnt_destroy(rstat_clnt);
1.1       deraadt   258:                return;
                    259:        }
                    260:
1.21      moritz    261:        if (sort_type != SORT_NONE)
                    262:                remember_rup_data(host, &host_stat);
                    263:        else
                    264:                print_rup_data(host, &host_stat);
                    265:
1.1       deraadt   266:        clnt_destroy(rstat_clnt);
                    267: }
                    268:
1.18      deraadt   269: static void
1.17      deraadt   270: allhosts(void)
1.1       deraadt   271: {
                    272:        statstime host_stat;
                    273:        enum clnt_stat clnt_stat;
1.5       deraadt   274:        extern char *__progname;
1.1       deraadt   275:
                    276:        if (sort_type != SORT_NONE) {
1.21      moritz    277:                printf("collecting responses...\n");
1.1       deraadt   278:                fflush(stdout);
                    279:        }
                    280:
                    281:        clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1.7       deraadt   282:            xdr_void, NULL, xdr_statstime, (char *)&host_stat, rstat_reply);
1.1       deraadt   283:        if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
1.8       deraadt   284:                fprintf(stderr, "%s: %s\n", __progname, clnt_sperrno(clnt_stat));
1.1       deraadt   285:                exit(1);
                    286:        }
                    287: }
                    288:
1.7       deraadt   289: int
1.17      deraadt   290: main(int argc, char *argv[])
1.1       deraadt   291: {
                    292:        int ch;
1.21      moritz    293:        size_t i;
1.1       deraadt   294:        extern int optind;
                    295:
                    296:        sort_type = SORT_NONE;
                    297:        while ((ch = getopt(argc, argv, "dhlt")) != -1)
                    298:                switch (ch) {
                    299:                case 'd':
                    300:                        printtime = 1;
                    301:                        break;
                    302:                case 'h':
                    303:                        sort_type = SORT_HOST;
                    304:                        break;
                    305:                case 'l':
                    306:                        sort_type = SORT_LDAV;
                    307:                        break;
                    308:                case 't':
                    309:                        sort_type = SORT_UPTIME;
                    310:                        break;
                    311:                default:
                    312:                        usage();
                    313:                        /*NOTREACHED*/
                    314:                }
1.22      moritz    315:
1.1       deraadt   316:        setlinebuf(stdout);
                    317:
                    318:        if (argc == optind)
                    319:                allhosts();
                    320:        else {
                    321:                for (; optind < argc; optind++)
                    322:                        onehost(argv[optind]);
1.21      moritz    323:        }
                    324:
                    325:        if (sort_type != SORT_NONE) {
                    326:                qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
                    327:                    compare);
                    328:
                    329:                for (i = 0; i < rup_data_idx; i++) {
                    330:                        print_rup_data(rup_data[i].host,
                    331:                            &rup_data[i].statstime);
                    332:                }
1.1       deraadt   333:        }
                    334:
                    335:        exit(0);
                    336: }
                    337:
                    338:
1.7       deraadt   339: void
1.17      deraadt   340: usage(void)
1.1       deraadt   341: {
1.23      sobrado   342:        fprintf(stderr, "usage: rup [-dhlt] [host ...]\n");
1.1       deraadt   343:        exit(1);
                    344: }