Setting the node name

John F Haugh II jfh at rpp386.cactus.org
Fri Feb 8 02:52:27 AEST 1991


Someone asked for a program which did this.  I just found this while
cleaning out a directory.  Enjoy!
--
/*
 * setuname.c
 *
 *  This program is hereby placed in the public domain.  You may do
 * whatever you wish.  You may even lie and say you wrote it yourself.
 * Most importantly you may sell it with your operating system so people
 * won't have to reconfigure their systems to change the nodename.
 *
 *	John F. Haugh II	(jfh at rpp368.cactus.org)	9/29/88
 *
 *	Modification History
 *
 *	02/07/91	jfh at rpp386.cactus.org
 *		Changed my mail address.
 */

#include <sys/a.out.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <stdio.h>

int	memfd;
int	kmemfd;

struct	utsname	utsname;
struct	utsname	Kutsname;
char	nodename[SYS_NMLN];

#define	UTSNAME	0
#define	NODENAME 1

struct	xlist	namelist[] = {
	{ 0, 0, 0, "_utsname" },
	{ 0, 0, 0, "_node" },
	{ 0, 0, 0, (char *) 0 }
};

usage ()
{
	fprintf (stderr, "usage: setuname [ -n namelist ] [ -k kmem ] nodename\n");
	exit (1);
}

r_read (fd, buf, n)
int	fd;
char	*buf;
int	n;
{
	int	i;

	if ((i = read (fd, buf, n)) == -1) {
		perror ("error on read");
		return (-1);
	} else
		return (i);
}

w_write (fd, buf, n)
int	fd;
char	*buf;
int	n;
{
	int	i;

	if ((i = write (fd, buf, n)) == -1) {
		perror ("error on write");
		return (-1);
	} else
		return (i);
}

long	l_lseek (fd, offs, whence)
int	fd;
long	offs;
int	whence;
{
	long	i;
	long	lseek ();

	if ((i = lseek (fd, offs, whence)) == -1L) {
		perror ("error on lseek");
		return (-1);
	} else
		return (i);
}

main (argc, argv)
int	argc;
char	**argv;
{
	char	newname[10];
	char	*namefile = "/xenix";
	char	*kmemfile = "/dev/kmem";
	char	*name;
	int	c;
	extern	int	optind;
	extern	char	*optarg;

	while ((c = getopt (argc, argv, "n:k:")) != EOF) {
		switch (c) {
			case 'k':
				kmemfile = optarg;
				break;
			case 'n':
				namefile = optarg;
				break;
			default:
				usage ();
		}
	}
	if (optind == argc)
		usage ();

	name = argv[optind];

	if (xlist (namefile, namelist) != 0) {
		perror ("pstat: namelist");
		exit (1);
	}
	if (namelist[UTSNAME].xl_value == 0 ||
			namelist[NODENAME].xl_value == 0){
		fprintf (stderr, "pstat: bad namelist: %s\n", namefile);
		exit (1);
	}
	if ((kmemfd = open (kmemfile, O_RDWR)) < 0) {
		perror ("pstat: kmemfile");
		exit (1);
	}
	l_lseek (kmemfd, namelist[NODENAME].xl_value, 0);
	r_read (kmemfd, nodename, sizeof nodename);

	l_lseek (kmemfd, namelist[UTSNAME].xl_value, 0);
	r_read (kmemfd, &Kutsname, sizeof Kutsname);
	uname (&utsname);

	if (memcmp (&Kutsname, &utsname, sizeof utsname) != 0) {
		fprintf (stderr, "pstat: can't find utsname!\n");
		exit (1);
	}
	printf ("changing node %.9s to %.9s:  DEL if wrong!\n", nodename, name);
	sleep (10);

	strncpy (nodename, name, sizeof utsname.nodename);
	l_lseek (kmemfd, namelist[NODENAME].xl_value, 0);
	w_write (kmemfd, nodename, sizeof utsname.nodename);

	strncpy (utsname.nodename, name, sizeof utsname.nodename);
	l_lseek (kmemfd, namelist[UTSNAME].xl_value, 0);
	w_write (kmemfd, &utsname, sizeof utsname);

	exit (0);
}
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh at rpp386.cactus.org
"I've never written a device driver, but I have written a device driver manual"
                -- Robert Hartman, IDE Corp.



More information about the Alt.sources mailing list