Bug 6418 - sethostid fails
sethostid fails
Status: CLOSED ERRATA
Product: Red Hat Linux
Classification: Retired
Component: glibc (Show other bugs)
7.0
All Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 1999-10-27 08:09 EDT by mcgahan
Modified: 2008-05-01 11:37 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2000-10-06 13:27:54 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description mcgahan 1999-10-27 08:09:34 EDT
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 10:53:59 EDT
assign to jakub
Comment 2 Jakub Jelinek 2000-10-09 10:48:30 EDT
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.

Note You need to log in before you can comment on or make changes to this bug.