10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1952Selowe * Common Development and Distribution License (the "License").
6*1952Selowe * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
220Sstevel@tonic-gate /* All Rights Reserved */
230Sstevel@tonic-gate
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
26*1952Selowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27*1952Selowe * Use is subject to license terms.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3 */
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * setuname [-t] [-s name] [-n node]
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate * Header files referenced:
380Sstevel@tonic-gate * <stdio.h> Standard I/O
390Sstevel@tonic-gate * <unistd.h> Standard UNIX definitions
400Sstevel@tonic-gate * <string.h> String handling
410Sstevel@tonic-gate * <fmtmsg.h> Standard message generation
420Sstevel@tonic-gate * <ctype.h> Character types
430Sstevel@tonic-gate * <errno.h> Error handling
440Sstevel@tonic-gate * <signal.h> Signal handling
450Sstevel@tonic-gate * <sys/types.h> Data types
460Sstevel@tonic-gate * <sys/fcntl.h> File control
470Sstevel@tonic-gate * <sys/utsname.h> System Name
480Sstevel@tonic-gate * <sys/sys3b.h> sys3b() definitions
490Sstevel@tonic-gate * <nlist.h> Definitions for Sun symbol table entries
500Sstevel@tonic-gate */
510Sstevel@tonic-gate
520Sstevel@tonic-gate #include <stdio.h>
530Sstevel@tonic-gate #include <unistd.h>
540Sstevel@tonic-gate #include <string.h>
550Sstevel@tonic-gate #include <fmtmsg.h>
560Sstevel@tonic-gate #include <ctype.h>
570Sstevel@tonic-gate #include <errno.h>
580Sstevel@tonic-gate #include <signal.h>
590Sstevel@tonic-gate #include <sys/types.h>
600Sstevel@tonic-gate #include <sys/uio.h>
610Sstevel@tonic-gate #include <sys/fcntl.h>
620Sstevel@tonic-gate #include <sys/psw.h>
630Sstevel@tonic-gate #include <sys/utsname.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate #if u3b || u3b15 || u3b2
660Sstevel@tonic-gate #include <sys/sys3b.h>
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate
690Sstevel@tonic-gate #if sun
700Sstevel@tonic-gate #include <nlist.h>
710Sstevel@tonic-gate #include <kvm.h>
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * Externals referenced (and not defined in a header)
760Sstevel@tonic-gate * optind index to the next arg for getopt()
770Sstevel@tonic-gate * opterr FLAG, TRUE tells getopt() to write messages
780Sstevel@tonic-gate * optarg Ptr to an option's argument
790Sstevel@tonic-gate * getopt() Gets an option from the command line
800Sstevel@tonic-gate * putenv() Writes values into the environment
810Sstevel@tonic-gate * exit() Exit the process
820Sstevel@tonic-gate * access() Check accessibility of a file
830Sstevel@tonic-gate * malloc() Allocate a block of main memory
840Sstevel@tonic-gate * free() Free allocated space
850Sstevel@tonic-gate * lseek() Seek within a file
860Sstevel@tonic-gate * open() Open a file
870Sstevel@tonic-gate * close() Close an open file
880Sstevel@tonic-gate */
890Sstevel@tonic-gate
900Sstevel@tonic-gate extern int optind; /* argv[] index of next arg */
910Sstevel@tonic-gate extern int opterr; /* TRUE if getopt() is to print msgs */
920Sstevel@tonic-gate extern char *optarg; /* Argument to parsed option */
930Sstevel@tonic-gate extern int getopt(); /* Get an option from the command line */
940Sstevel@tonic-gate extern int putenv(); /* Put a value into the environment */
950Sstevel@tonic-gate extern void exit(); /* Exit the process */
960Sstevel@tonic-gate extern int access(); /* Check the accessibility of a file */
970Sstevel@tonic-gate extern void *malloc(); /* Get a chunk of main memory */
980Sstevel@tonic-gate extern void free(); /* Free alloc'd space */
990Sstevel@tonic-gate extern long lseek(); /* Seek within a file */
1000Sstevel@tonic-gate extern int open(); /* Open a file */
1010Sstevel@tonic-gate extern int close(); /* Close an open a file */
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * L O C A L D E F I N I T I O N S
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * Constants
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate #ifndef TRUE
1120Sstevel@tonic-gate #define TRUE (1)
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate #ifndef FALSE
1160Sstevel@tonic-gate #define FALSE (0)
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate #ifndef NULL
1200Sstevel@tonic-gate #define NULL (0)
1210Sstevel@tonic-gate #endif
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate #define OPTSTRING "tn:s:"
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #define EX_OK 0
1260Sstevel@tonic-gate #define EX_ERROR 1
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate #define RC_FILENAME "/etc/rc2.d/S18setuname"
1290Sstevel@tonic-gate #define RC_DIRNAME "/etc/rc2.d"
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * Messages
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate #define E_USAGE "usage: setuname [-t] [-s name] [-n node]"
1370Sstevel@tonic-gate #define E_MISSING "Either -s name or -n node must be specified"
1380Sstevel@tonic-gate #define E_UNAME "Unable to get existing uname values"
1390Sstevel@tonic-gate #define E_INVNAME "System-name invalid: %s"
1400Sstevel@tonic-gate #define E_LONGNAME "System-name too long: %s"
1410Sstevel@tonic-gate #define E_INVNODE "Network node-name invalid: %s"
1420Sstevel@tonic-gate #define E_LONGNODE "Network node-name too long: %s"
1430Sstevel@tonic-gate #define E_NOPERMS "No permissions, request denied"
1440Sstevel@tonic-gate #define E_NOSUCHDIR "Directory doesn't exist: %s"
1450Sstevel@tonic-gate #define E_INTERNAL "Internal error: %d"
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * Macros:
1490Sstevel@tonic-gate * stdmsg(r,l,s,t) Write a standard message.
1500Sstevel@tonic-gate * 'r' is the recoverability flag
1510Sstevel@tonic-gate * 'l' is the label
1520Sstevel@tonic-gate * 's' is the severity
1530Sstevel@tonic-gate * 't' is the text.
1540Sstevel@tonic-gate * strend(p) Return the address of the end of a string
1550Sstevel@tonic-gate * (This is supposed to be defined in <sys/inline.h>
1560Sstevel@tonic-gate * but that file has string-handing def'ns that
1570Sstevel@tonic-gate * conflict with <string.h>, so we can't use it!
1580Sstevel@tonic-gate * MR dn89-04701 requests this fix.
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1620Sstevel@tonic-gate #define strend(p) strrchr(p,'\0')
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * Local functions:
1660Sstevel@tonic-gate * setuname Changes the system name and the network node name
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate static int setuname(); /* This does the "real" work */
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * Local data
1740Sstevel@tonic-gate * lbl Buffer for the standard message label
1750Sstevel@tonic-gate * txt Buffer for the standard message text
1760Sstevel@tonic-gate */
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; /* Space for std msg label */
1790Sstevel@tonic-gate static char msg[MM_MXTXTLN+1]; /* Space for std msg text */
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * int main(argc, argv)
1830Sstevel@tonic-gate * int argc
1840Sstevel@tonic-gate * char *argv;
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate int
main(argc,argv)1880Sstevel@tonic-gate main(argc, argv)
1890Sstevel@tonic-gate int argc; /* Argument count */
1900Sstevel@tonic-gate char *argv[]; /* Argument vector */
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate /* Automatic data */
1930Sstevel@tonic-gate char *n_arg; /* Ptr to arg for -n */
1940Sstevel@tonic-gate char *s_arg; /* Ptr to arg for -s */
1950Sstevel@tonic-gate int t_seen; /* FLAG, -t option seen */
1960Sstevel@tonic-gate char *cmdname; /* Ptr to the command's name */
1970Sstevel@tonic-gate char *p; /* Temp pointer */
1980Sstevel@tonic-gate int usageerr; /* FLAG, TRUE if usage error */
1990Sstevel@tonic-gate int exitcode; /* Value to exit with */
2000Sstevel@tonic-gate int c; /* Temp character */
2010Sstevel@tonic-gate int ok; /* Flag, everything okay? */
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /* Build the standard-message label */
2040Sstevel@tonic-gate if (p = strrchr(argv[0], '/')) cmdname = p+1;
2050Sstevel@tonic-gate else cmdname = argv[0];
2060Sstevel@tonic-gate (void) strcat(strcpy(lbl, "UX:"), cmdname);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /* Make only the text in standard messages appear (SVR4.0 only) */
2090Sstevel@tonic-gate (void) putenv("MSGVERB=text");
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /* Initializations */
2130Sstevel@tonic-gate n_arg = s_arg = (char *) NULL;
2140Sstevel@tonic-gate t_seen = FALSE;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * Parse command
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate usageerr = FALSE;
2220Sstevel@tonic-gate opterr = FALSE;
2230Sstevel@tonic-gate while (!usageerr && (c = getopt(argc, argv, OPTSTRING)) != EOF) switch(c) {
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate case 'n': /* -n node */
2260Sstevel@tonic-gate if (n_arg) usageerr = TRUE;
2270Sstevel@tonic-gate else n_arg = optarg;
2280Sstevel@tonic-gate break;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate case 's': /* -s name */
2310Sstevel@tonic-gate if (s_arg) usageerr = TRUE;
2320Sstevel@tonic-gate else s_arg = optarg;
2330Sstevel@tonic-gate break;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate case 't': /* -t */
2360Sstevel@tonic-gate if (t_seen) usageerr = TRUE;
2370Sstevel@tonic-gate else t_seen = TRUE;
2380Sstevel@tonic-gate break;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate default: /* Something that doesn't exist */
2410Sstevel@tonic-gate usageerr = TRUE;
2420Sstevel@tonic-gate } /* switch() */
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /* If there was a usage error, report the error and exit */
2450Sstevel@tonic-gate if ((argc >= (optind+1)) || usageerr) {
2460Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
2470Sstevel@tonic-gate exit(EX_ERROR);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /* Either -n <node> or -s <name> has to be specified */
2510Sstevel@tonic-gate if (!(n_arg || s_arg)) {
2520Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_MISSING);
2530Sstevel@tonic-gate exit(EX_ERROR);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate * Validate arguments:
2590Sstevel@tonic-gate * - The length of the system name must be less than SYS_NMLN-1
2600Sstevel@tonic-gate * characters,
2610Sstevel@tonic-gate * - The length of the network node-name must be less than
2620Sstevel@tonic-gate * SYS_NMLN-1 characters,
2630Sstevel@tonic-gate * - The system name must equal [a-zA-Z0-9-_]+,
2640Sstevel@tonic-gate * - The network node-name must equal [a-zA-Z0-9-_]+.
2650Sstevel@tonic-gate */
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /* Check the length and the character-set of the system name */
2680Sstevel@tonic-gate if (s_arg) {
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate /* Check length of the system name */
2710Sstevel@tonic-gate if (strlen(s_arg) > (size_t)(SYS_NMLN-1)) {
2720Sstevel@tonic-gate (void) sprintf(msg, E_LONGNAME, s_arg);
2730Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2740Sstevel@tonic-gate exit(EX_ERROR);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /* Check the character-set */
2780Sstevel@tonic-gate ok = TRUE;
2790Sstevel@tonic-gate for (p = s_arg ; ok && *p ; p++) {
2800Sstevel@tonic-gate if (!isalnum(*p) && (*p != '-') && (*p != '_')) ok = FALSE;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate if (!ok || (p == s_arg)) {
2830Sstevel@tonic-gate (void) sprintf(msg, E_INVNAME, s_arg);
2840Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2850Sstevel@tonic-gate exit(EX_ERROR);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /* Check the length and the character-set of the network node-name */
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (n_arg) {
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /* Check length of the network node-name */
2940Sstevel@tonic-gate if (strlen(n_arg) > (size_t)(SYS_NMLN-1)) {
2950Sstevel@tonic-gate (void) sprintf(msg, E_LONGNODE, n_arg);
2960Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2970Sstevel@tonic-gate exit(EX_ERROR);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /* Check the character-set */
3010Sstevel@tonic-gate ok = TRUE;
3020Sstevel@tonic-gate for (p = n_arg ; ok && *p ; p++) {
3030Sstevel@tonic-gate if (!isalnum(*p) && (*p != '-') && (*p != '_')) ok = FALSE;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate if (!ok || (p == n_arg)) {
3060Sstevel@tonic-gate (void) sprintf(msg, E_INVNODE, n_arg);
3070Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3080Sstevel@tonic-gate exit(EX_ERROR);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * Make sure we have access to needed resources:
3150Sstevel@tonic-gate * - Read/write access to kernel memory (/dev/kmem)
3160Sstevel@tonic-gate * - If -t is not specified, read/write access to /etc/rc2.d
3170Sstevel@tonic-gate * - If -t is not specified, read access to /etc/rc2.d/S18setuname
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate if (access("/dev/kmem", R_OK|W_OK) == 0) {
3210Sstevel@tonic-gate if (access(RC_DIRNAME, R_OK|W_OK) == 0) {
3220Sstevel@tonic-gate if ((access(RC_FILENAME, R_OK) != 0) &&
3230Sstevel@tonic-gate (access(RC_FILENAME, F_OK) == 0)) {
3240Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
3250Sstevel@tonic-gate exit(EX_ERROR);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate else {
3290Sstevel@tonic-gate if (access(RC_DIRNAME, F_OK) == 0) {
3300Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
3310Sstevel@tonic-gate exit(EX_ERROR);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate else {
3340Sstevel@tonic-gate (void) sprintf(msg, E_NOSUCHDIR, RC_DIRNAME);
3350Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3360Sstevel@tonic-gate exit(EX_ERROR);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate else {
3410Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
3420Sstevel@tonic-gate exit(EX_ERROR);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /* Attempt the setuname */
3470Sstevel@tonic-gate if (setuname(t_seen, s_arg, n_arg) == 0) exitcode = EX_OK;
3480Sstevel@tonic-gate else {
3490Sstevel@tonic-gate (void) sprintf(msg, E_INTERNAL, errno);
3500Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3510Sstevel@tonic-gate exitcode = EX_ERROR;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /* Finished */
3550Sstevel@tonic-gate return (exitcode);
3560Sstevel@tonic-gate } /* main() */
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * int setuname(temp, name, node)
3600Sstevel@tonic-gate * int temp
3610Sstevel@tonic-gate * char *name
3620Sstevel@tonic-gate * char *node
3630Sstevel@tonic-gate *
3640Sstevel@tonic-gate * Set any or all of the following machine parameters, either
3650Sstevel@tonic-gate * temporarily or permanently, depending on <temp>.
3660Sstevel@tonic-gate * - System name
3670Sstevel@tonic-gate * - Network Node-name
3680Sstevel@tonic-gate */
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate static int
setuname(temp,sysname,nodename)3710Sstevel@tonic-gate setuname(temp, sysname, nodename)
3720Sstevel@tonic-gate int temp; /* Set in kernel only flag */
3730Sstevel@tonic-gate char *sysname; /* System name */
3740Sstevel@tonic-gate char *nodename; /* Network node-name */
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate /* Automatic Data */
3770Sstevel@tonic-gate struct utsname utsname; /* Space for the kernel's utsname information */
3780Sstevel@tonic-gate #if u3b || u3b15 || u3b2
3790Sstevel@tonic-gate struct s3bsym *symbtbl; /* The kernel's symbol table */
3800Sstevel@tonic-gate #endif
3810Sstevel@tonic-gate #if sun
3820Sstevel@tonic-gate struct nlist nl[] = {
3830Sstevel@tonic-gate {"utsname", 0, 0, 0, 0, 0},
3840Sstevel@tonic-gate {NULL}
3850Sstevel@tonic-gate };
3860Sstevel@tonic-gate kvm_t *kd;
3870Sstevel@tonic-gate #endif
3880Sstevel@tonic-gate uintptr_t utsname_addr; /* Addr of "utsname" in the kernel */
3890Sstevel@tonic-gate char *sysnm = (char *)NULL; /* System name to set (from file or arg) */
3900Sstevel@tonic-gate char *nodenm = (char *)NULL; /* Network node-name to set (from file or arg) */
3910Sstevel@tonic-gate FILE *fd; /* Std I/O File Descriptor for /etc/rc2.d/S18setuname */
3920Sstevel@tonic-gate char *p; /* Temp pointer */
3930Sstevel@tonic-gate void (*oldsighup)(); /* Function to call for SIGHUP */
3940Sstevel@tonic-gate void (*oldsigint)(); /* Function to call for SIGINT */
3950Sstevel@tonic-gate int rtncd; /* Value to return to the caller */
3960Sstevel@tonic-gate unsigned long symbtblsz; /* The size of the kernel's symbol table, in bytes */
3970Sstevel@tonic-gate int memfd; /* File descriptor: open kernel memory */
3980Sstevel@tonic-gate int i; /* Temp counter */
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate /* Nothing's gone wrong yet (but we've only just begun!) */
4020Sstevel@tonic-gate rtncd = 0;
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate * Get the virtual address of the symbol "utsname" in the kernel
4070Sstevel@tonic-gate * so we can get set the system name and/or the network node-name
4080Sstevel@tonic-gate * directly in the kernel's memory space.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate #if u3b || u3b15 || u3b2
4120Sstevel@tonic-gate if ((sys3b(S3BSYM, (struct s3bsym *) &symbtblsz, sizeof(symbtblsz)) == 0) &&
4130Sstevel@tonic-gate (symbtbl = (struct s3bsym *) malloc(symbtblsz))) {
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate (void) sys3b(S3BSYM, symbtbl, symbtblsz);
4160Sstevel@tonic-gate p = (char *) symbtbl;
4170Sstevel@tonic-gate for (i = symbtbl->count; i-- && (strcmp(p, "utsname") != 0) ; p = S3BNXTSYM(p)) ;
4180Sstevel@tonic-gate if (i >= 0) utsname_addr = S3BSVAL(p);
4190Sstevel@tonic-gate else rtncd = -1;
4200Sstevel@tonic-gate free((void *) symbtbl);
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate } else rtncd = -1;
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate #elif sun
4250Sstevel@tonic-gate /* Check out namelist and memory files. */
4260Sstevel@tonic-gate if ((kd = kvm_open(NULL, NULL, NULL, O_RDWR, NULL)) == NULL)
4270Sstevel@tonic-gate rtncd = -1;
428*1952Selowe else if (kvm_nlist(kd, nl) != 0)
4290Sstevel@tonic-gate rtncd = -1;
4300Sstevel@tonic-gate else if (nl[0].n_value == 0)
4310Sstevel@tonic-gate rtncd = -1;
4320Sstevel@tonic-gate else
4330Sstevel@tonic-gate utsname_addr = (uintptr_t)nl[0].n_value;
4340Sstevel@tonic-gate #else
4350Sstevel@tonic-gate if (nlist("/unix", nl) != 0)
4360Sstevel@tonic-gate rtncd = -1;
4370Sstevel@tonic-gate #endif
4380Sstevel@tonic-gate if (rtncd != 0) return(rtncd);
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate * Open the kernel's memory, get the existing "utsname" structure,
4420Sstevel@tonic-gate * change the system name and/or the network node-name in that struct,
4430Sstevel@tonic-gate * write it back out to kernel memory, then close kernel memory.
4440Sstevel@tonic-gate */
4450Sstevel@tonic-gate #ifdef sun
4460Sstevel@tonic-gate if (kvm_kread(kd, utsname_addr, &utsname, sizeof (utsname)) ==
4470Sstevel@tonic-gate sizeof (utsname)) {
4480Sstevel@tonic-gate if (sysname)
4490Sstevel@tonic-gate (void) strncpy(utsname.sysname, sysname,
4500Sstevel@tonic-gate sizeof (utsname.sysname));
4510Sstevel@tonic-gate if (nodename)
4520Sstevel@tonic-gate (void) strncpy(utsname.nodename, nodename,
4530Sstevel@tonic-gate sizeof (utsname.nodename));
4540Sstevel@tonic-gate (void) kvm_kwrite(kd, utsname_addr, &utsname, sizeof (utsname));
4550Sstevel@tonic-gate kvm_close(kd);
4560Sstevel@tonic-gate } else
4570Sstevel@tonic-gate return (-1);
4580Sstevel@tonic-gate #else /* sun */
4590Sstevel@tonic-gate if ((memfd = open("/dev/kmem", O_RDWR, 0)) > 0) {
4600Sstevel@tonic-gate if ((lseek(memfd, (long) utsname_addr, SEEK_SET) != -1) &&
4610Sstevel@tonic-gate (read(memfd, &utsname, sizeof(utsname)) == sizeof(utsname))) {
4620Sstevel@tonic-gate if (sysname) (void) strncpy(utsname.sysname, sysname, sizeof(utsname.sysname));
4630Sstevel@tonic-gate if (nodename) (void) strncpy(utsname.nodename, nodename, sizeof(utsname.nodename));
4640Sstevel@tonic-gate (void) lseek(memfd, (long) utsname_addr, SEEK_SET);
4650Sstevel@tonic-gate (void) write(memfd, &utsname, sizeof(utsname));
4660Sstevel@tonic-gate (void) close(memfd);
4670Sstevel@tonic-gate } else rtncd = -1;
4680Sstevel@tonic-gate } else rtncd = -1;
4690Sstevel@tonic-gate if (rtncd != 0) return(rtncd);
4700Sstevel@tonic-gate #endif /* sun */
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * If the "temp" flag is FALSE, we need to permanently set the
4750Sstevel@tonic-gate * system name in the file /etc/rc2.d/S18setuname
4760Sstevel@tonic-gate */
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate if (!temp) {
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate * If a name was specified by the caller, use that, otherwise, use
4810Sstevel@tonic-gate * whatever was in the "rc" file.
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate if (sysname) sysnm = sysname;
4850Sstevel@tonic-gate if (nodename) nodenm = nodename;
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate * Write the file /etc/rc2.d/S18setuname so that the system name is
4900Sstevel@tonic-gate * set on boots and state changes.
4910Sstevel@tonic-gate *
4920Sstevel@tonic-gate * DISABLED SIGNALS: SIGHUP, SIGINT
4930Sstevel@tonic-gate */
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate /* Give us a reasonable chance to complete without interruptions */
4960Sstevel@tonic-gate oldsighup = signal(SIGHUP, SIG_IGN);
4970Sstevel@tonic-gate oldsigint = signal(SIGINT, SIG_IGN);
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate /* Write the new setuname "rc" file */
5000Sstevel@tonic-gate if (sysname != NULL) {
5010Sstevel@tonic-gate if ((fd = fopen(RC_FILENAME, "w")) != (FILE *) NULL) {
5020Sstevel@tonic-gate (void) fprintf(fd, "# %s\n", sysnm);
5030Sstevel@tonic-gate (void) fprintf(fd, "#\n");
5040Sstevel@tonic-gate (void) fprintf(fd, "# This script, generated by the setuname command,\n");
5050Sstevel@tonic-gate (void) fprintf(fd, "# sets the system's system-name\n");
5060Sstevel@tonic-gate (void) fprintf(fd, "#\n");
5070Sstevel@tonic-gate if (sysnm && *sysnm)
5080Sstevel@tonic-gate (void) fprintf(fd, "setuname -t -s %s\n", sysnm);
5090Sstevel@tonic-gate (void) fclose(fd);
5100Sstevel@tonic-gate } else return(rtncd = -1);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate if(nodename != NULL) {
5140Sstevel@tonic-gate char curname[SYS_NMLN];
5150Sstevel@tonic-gate int curlen;
5160Sstevel@tonic-gate FILE *file;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate if ((file = fopen("/etc/nodename", "r")) != NULL) {
5190Sstevel@tonic-gate curlen = fread(curname, sizeof(char), SYS_NMLN, file);
5200Sstevel@tonic-gate for (i = 0; i < curlen; i++) {
5210Sstevel@tonic-gate if (curname[i] == '\n') {
5220Sstevel@tonic-gate curname[i] = '\0';
5230Sstevel@tonic-gate break;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate if (i == curlen) {
5270Sstevel@tonic-gate curname[curlen] = '\0';
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate (void)fclose(file);
5300Sstevel@tonic-gate } else {
5310Sstevel@tonic-gate curname[0] = '\0';
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate if (strcmp(curname, nodenm) != 0) {
5340Sstevel@tonic-gate if ((file = fopen("/etc/nodename", "w")) == NULL) {
5350Sstevel@tonic-gate (void) fprintf(stderr, "setuname: error in writing name\n");
5360Sstevel@tonic-gate exit(1);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate if (fprintf(file, "%s\n", nodenm) < 0) {
5390Sstevel@tonic-gate (void) fprintf(stderr, "setuname: error in writing name\n");
5400Sstevel@tonic-gate exit(1);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate (void)fclose(file);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate /* Restore signal handling */
5460Sstevel@tonic-gate (void) signal(SIGHUP, oldsighup);
5470Sstevel@tonic-gate (void) signal(SIGINT, oldsigint);
5480Sstevel@tonic-gate } /* if (!temp) */
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate /* Fini */
5510Sstevel@tonic-gate return(rtncd);
5520Sstevel@tonic-gate }
553