Operating System - HP-UX
1831406 Members
3672 Online
110025 Solutions
New Discussion

HP UX 11.23 uname issue with c programm

 
shirish11
Advisor

HP UX 11.23 uname issue with c programm

#include
#include
void main()
{
struct utsname uts;
uname(&uts);
printf("name =%s",uts.sysname);
printf("\n name =%s",uts.nodename);
}

hi my node name is more than 8 characters
pumass00001 130: uname -a
HP-UX pumass0001 B.11.31 U ia64 1883224899 unlimited-user license

so i executed this programm i am getting
pumass00001 128: ./a.out
sys name =HP-UX
nodename =pumass00pumass00001

why this type of output comming?
6 REPLIES 6
Matti_Kurkela
Honored Contributor

Re: HP UX 11.23 uname issue with c programm

Your oversized nodename causes the OS's nodename field to overflow, so the nodename string is not guaranteed to be null-terminated anymore. After the 8th character, you'll get whatever is located in memory next... which seems to be the hostname in your case.

In HP-UX, the hostname and nodename are the same by default. But the nodename cannot be longer than 8 characters unless the "Node and Host Name Expansion" optional package is installed:

https://h20392.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=NodeHostNameXpnd

Apparently you just discovered what happens if your nodename is longer than 8 characters and the NodeHostNameXpnd package is not installed.

If you want your hostname to be longer than 8 but less than 64 characters, that can be done without the expansion package; but then you must configure an extra variable NODENAME in your /etc/rc.config.d/netconf file, to explicitly set a nodename that is 8 characters or less.

The nodename is historically used by the UUCP utilities; the hostname is what is used in the context of Internet Domain Name Service (DNS).

Unless you're doing something related to UUCP, you should probably use the gethostname(2) function instead, and leave the legacy uts.nodename alone.

#include
#include

int main() {
char *hostname_buf = NULL;
size_t hostname_size;

/* get the max size of hostname: */
/* it's typically 64 or 256, but may be */
/* longer in future HP-UX versions */
hostname_size = sysconf(_SC_HOST_NAME_MAX);

/* create a buffer of required size*/
hostname_buf = malloc(hostname_size);
if (hostname_buf == NULL) {
/* critically out of memory */
exit(1);
}

gethostname(hostname_buf, hostname_size);

printf("name = %s\n", hostname_buf);
return 0;
}

MK
MK
Dennis Handly
Acclaimed Contributor

Re: HP UX 11.23 uname issue with c programm

(Please don't put the wrong HP-UX version in the subject.)

>my node name is more than 8 characters
HP-UX pumass0001 B.11.31 U ia64

You do realize you are in for a heap of trouble.

>I am getting: nodename =pumass00pumass00001
>why this type of output coming?

How are you compiling this program? Are you checking the return value? What is errno?

Have you looked at the documentation for Node and Host Name Sizes on HP-UX: Using the Expanded Capabilities

>MK: But the nodename cannot be longer than 8 characters unless the "Node and Host Name Expansion" optional package is installed:

For 11.31, it's just a kernel parm, expanded_node_host_names(5).
Dennis Handly
Acclaimed Contributor

Re: HP UX 11.23 uname issue with c programm

Node and Host Name Sizes on HP-UX
Using the Expanded Capabilities of HP-UX
http://bizsupport2.austin.hp.com/bc/docs/support/SupportManual/c01925495/c01925495.pdf

>void main()

This is required to be "int main".
shirish11
Advisor

Re: HP UX 11.23 uname issue with c programm

Hi i just check in my system there is an kernel parameter expanded_node_host_names=1
and uname_eoverflow=0 ,

then only we can set the node name more than 8 characters.

i am compiling this programm with aCC compiler.
so is uname function dose not obeying the kernel parameters.
Thanks
Andre-Marcel Hellmund
Frequent Advisor

Re: HP UX 11.23 uname issue with c programm

Hey,

the uname(2) system call DOES respect the mentioned kernel parameters. By the way, you should also set the uname_eoverflow parameter because this will help older applications to detect the overflow error.

Please note that the default utsname structure used by your program does only use a character array of 9 bytes for the 'nodename' value. Please read the whitepaper referenced by Dennis, it would have told you to use the extra define -D_HPUX_API_LEVEL=20040821 to enlarge the utsname structure. Though, it is worth reading ;-)

Andi
Dennis Handly
Acclaimed Contributor

Re: HP UX 11.23 uname issue with c programm

>I am compiling this program with aCC compiler.
>so is uname function does not obeying the kernel parameters.

You basically told the kernel you don't care if the name is too long and worse, you didn't care to detect an error.