"Fossies" - the Fresh Open Source Software archive

Member "open-fcoe/usr/ofc/tools/fcconf/src/fcc_sys.c" of archive open-fcoe.tar.gz:


/*
 * Copyright(c) 2007 Intel Corporation. All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Maintained at www.Open-FCoE.org
 */
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/utsname.h>
#include "fcc_menus.h"
#include "sa_log.h"

/*
 * Trim trailing whitespace from a string.
 */
static void
fcc_strtrim(char *s)
{
    char *ep;

    ep = s + strlen(s);
    while (--ep >= s && isspace(*ep)) 
        *ep = '\0';
}

/*
 * Get release information from an /etc/xxx-release file.
 */
static int
fcc_host_release_file_get(char *buf, size_t len, struct utsname *uts)
{
    DIR *dfp;
    FILE    *file;
    struct dirent *dp;
    char    *cp;
    int error = -1;
    char    *pattern = "-release";
    int pat_len = strlen(pattern);
    int found = 0;
    char    line[256];
    char    namebuf[256];
    char    temp_buf[256];

    dfp = opendir("/etc");
    if (dfp) {
        while ((dp = readdir(dfp)) != NULL) {
            cp = strstr(dp->d_name, pattern);
            if (!cp || cp[pat_len] != '\0')
                continue;
            snprintf(namebuf, sizeof (namebuf), "/etc/%s", dp->d_name);
            file = fopen(namebuf, "r");
            if (!file)
                continue;
            /*
             * Read through the file, ignoring lines with '='.
             * Trim trailing white space, including the newline.
             * Trim anything after a '('.
             */
            while (fgets(line, sizeof (line), file)) {
                if (strchr(line, '=') != NULL)
                    continue;
                cp = strchr(line, '(');
                if (cp)
                    *cp = '\0';
                fcc_strtrim(line);
                snprintf(temp_buf, sizeof (temp_buf), "%s - %s  %s",
                  uts->sysname, line, uts->machine);

                /*
                 * Don't count duplicates caused by symbolic links.
                 */
                if (found == 0 || strcmp(temp_buf, buf) != 0) {
                    strncpy(buf, temp_buf, len);
                    found++;
                }
            }
            fclose(file);
        }
        closedir(dfp);
    }
    if (found == 1)
        error = 0;
    return error;
}
    
static struct utsname fcc_uts;                  /* set as a side effect */

char *
fcc_host_release_get(char *buf, size_t len)
{
    int error;

    memset(&fcc_uts, 0, sizeof (fcc_uts));
    uname(&fcc_uts);

    error = fcc_host_release_file_get(buf, len, &fcc_uts);
    if (error) {
        snprintf(buf, len, "%s - %s   %s",
            fcc_uts.sysname, fcc_uts.release, fcc_uts.machine);
    }
    return (buf);
}

char *
fcc_hostname_get(char *buf, size_t len)
{
    char host[50];
    char domain[50];

    host[0] = '\0';
    gethostname(host, sizeof (host));
    domain[0] = '\0';
    getdomainname(domain, sizeof (domain));

    if (strchr(host, '.') == NULL && domain[0] != '\0') {
        snprintf(buf, len, "%s.%s", host, domain);
    } else {
        snprintf(buf, len, "%s", host);
    }
    return (buf);
}

void
fcc_host_put(void)
{
    char buf[150];

    cli_put_name_val("Host Name", fcc_hostname_get(buf, sizeof (buf)));
}


void
fcc_host_info(void *args)
{
    char buf[150];

    cli_put_start();
    cli_put_line("-");
    fcc_host_put();
    cli_put_name_val("OS Type", fcc_host_release_get(buf, sizeof(buf)));
    cli_put_name_val("OS Version", fcc_uts.release);
    cli_put_name_val("fcconf Version", fcc_version);
    cli_put_line("-");
    fcc_port_show_sum(args);
}