Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.

Bug 6418

Summary: sethostid fails
Product: [Retired] Red Hat Linux Reporter: mcgahan
Component: glibcAssignee: Jakub Jelinek <jakub>
Status: CLOSED ERRATA QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 7.0   
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2000-10-06 17:27:54 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description mcgahan 1999-10-27 12:09:34 UTC
the call sethostid worked under 5.2 but fails under 6.0 and
6.1

Alan Cox at alan@lxorguk.ukuu.org.uk pointed out that the
source for this call expects a directory path of /var/adm
to exist.

strerror reports "No such file or directory"
see attached source code
compile with g++ or c++
command line options are listed in source code

you are welcome to the code if it is of any use.


//start
//**********************************************************
*******
//Name: hostid
//Date: 08/27/99
//Originator: Mike McGahan
//Compile: c++ hostid.cc -o hostid
//
//  hostid usage
//         - get current hostid
//      -g - get current hostid
//      -r - restore hostid from /etc/hostid file
//      -s <hexid> - set hostid
//             If the file /etc/hostid does not exist it is
created
//
//**********************************************************
********

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

long HostIdFromFile(void);
int HostIdToFile(void);
void Usage(void);

int main (int argc, char* argv[]) {
  bool invalid = false;
  long hostid = -1;
  if (argc == 1) {
    cout << "hostid: " << hex << gethostid() << endl;
  }
  else if (argc == 2) {
    if (argv[1][0] == '-') {

      if (argv[1][1] == 'g') {    // get current hostid
	cout << "hostid: " << hex << gethostid()  << endl;
      }

      else if (argv[1][1] == 'r') {    // restore hostid
	hostid = HostIdFromFile();
	if ( hostid != -1) {    // got hostid
	  int status = 0;
	  if ((status = sethostid (hostid)) == 0) {
	    cout << "Hostid \" " << hex << hostid << "\"
restored." << endl;
	  }
	  else {
	    cout << "Failed to set hostid, " << hex <<
hostid << dec << ". Status:" << status << endl;
	    cout << "Error: " << strerror(errno) << endl;
	    exit (-1);
	  }
	}
	else {
	  cout << "Failed to open /etc/hostid file." <<
endl;
	  exit (-1);
	}
      }
      else if (argv[1][1] == 'h') {    // help hostid
	Usage();
      }
      else {
	invalid = true;
      }
    }
    else {
      invalid = true;

    }
  }
  else if (argc == 3) {
    if (argv[1][0] == '-') {

      if (argv[1][1] == 's') {      // set hostid
	int status = 0;
	sscanf(argv[2], "%lX", &hostid);
	if (HostIdFromFile() == -1) {    // hostid file
does not exist
	  if (HostIdToFile() == -1) {
	    cout << "Could not create /etc/hostid file." <<
endl;
	    exit (-1);
	  }
	}
	if ((status = sethostid (hostid)) == 0) {
	  cout << "Hostid \"" << hex << hostid << "\" set."
<< endl;
	}
	else {
	  cout << "Failed to set hostid, " << hex << hostid
<< dec << ". Status:" << status << endl;
	  cout << "Error: " << strerror(errno) << endl;
	  exit (-1);
	}
      }
      else {
	invalid = true;
      }
    }
    else {
      invalid = true;
    }
  }
  else {
    invalid = true;
  }

  if (invalid) {
    cout << "Invalid option or number of parameters." <<
endl;
    Usage();
  }
}

long HostIdFromFile(void) {
  long hostid = -1;
  std::ifstream hostidfile ("/etc/hostid");
  if (!hostidfile.is_open()) {
    return -1;
  }
  hostidfile >> hex >> hostid;
  hostidfile.close();
  return hostid;
}

int HostIdToFile(void) {
  std::ofstream hostidfile ("/etc/hostid");
  if (!hostidfile) {
    cout << "Failed to open /etc/hostid." << endl;
    return -1;
  }
  hostidfile << hex << gethostid();
  hostidfile.close();
  return 1;
}

void Usage (void) {
  cout << "gethostid usage" << endl;
  cout << "    -g - get current hostid" << endl;
  cout << "    -r - restore hostid from /etc/hostid file"
<< endl;
  cout << "    -s \"numericid\" - set hostid" << endl;
  cout << "           If the file /etc/hostid does not
exist it is created" << endl << endl;
}

//end

Comment 1 Cristian Gafton 2000-05-22 14:53:59 UTC
assign to jakub

Comment 2 Jakub Jelinek 2000-10-09 14:48:30 UTC
Should be fixed in glibc-2.1.94-3, though your example will not work, because
glibc's internal storage for hostid is /etc/hostid.