123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- uname.c
- Abstract:
- This module implements support for getting the system name.
- Author:
- Evan Green 17-Jul-2013
- Environment:
- User Mode C Library
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "libcp.h"
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/utsname.h>
- #include <time.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define UNAME_SYSTEM_NAME "Minoca"
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- LIBC_API
- int
- uname (
- struct utsname *Name
- )
- /*++
- Routine Description:
- This routine returns the system name and version.
- Arguments:
- Name - Supplies a pointer to a name structure to fill out.
- Return Value:
- Returns a non-negative value on success.
- -1 on error, and errno will be set to indicate the error.
- --*/
- {
- CHAR EndTag[32];
- UINTN Size;
- KSTATUS Status;
- SYSTEM_VERSION_INFORMATION Version;
- Status = OsGetSystemVersion(&Version, TRUE);
- if (!KSUCCESS(Status)) {
- goto unameEnd;
- }
- //
- // Start by getting the hostname and domain name.
- //
- Size = sizeof(Name->nodename) - 1;
- Name->nodename[Size] = '\0';
- Status = OsGetSetSystemInformation(SystemInformationPs,
- PsInformationHostName,
- Name->nodename,
- &Size,
- FALSE);
- if ((!KSUCCESS(Status)) && (Status != STATUS_BUFFER_TOO_SMALL)) {
- goto unameEnd;
- }
- Size = sizeof(Name->domainname) - 1;
- Name->domainname[Size] = '\0';
- Status = OsGetSetSystemInformation(SystemInformationPs,
- PsInformationDomainName,
- Name->domainname,
- &Size,
- FALSE);
- if ((!KSUCCESS(Status)) && (Status != STATUS_BUFFER_TOO_SMALL)) {
- goto unameEnd;
- }
- strcpy(Name->sysname, UNAME_SYSTEM_NAME);
- if ((Version.ReleaseLevel == SystemReleaseFinal) &&
- (Version.DebugLevel == SystemBuildRelease)) {
- EndTag[0] = '\0';
- } else if (Version.ReleaseLevel == SystemReleaseFinal) {
- snprintf(EndTag,
- sizeof(EndTag),
- "-%s",
- RtlGetBuildDebugLevelString(Version.DebugLevel));
- } else if (Version.DebugLevel == SystemBuildRelease) {
- snprintf(EndTag,
- sizeof(EndTag),
- "-%s",
- RtlGetReleaseLevelString(Version.ReleaseLevel));
- } else {
- snprintf(EndTag,
- sizeof(EndTag),
- "-%s-%s",
- RtlGetReleaseLevelString(Version.ReleaseLevel),
- RtlGetBuildDebugLevelString(Version.DebugLevel));
- }
- snprintf(Name->release,
- sizeof(Name->release),
- "%d.%d.%d.%lld%s",
- Version.MajorVersion,
- Version.MinorVersion,
- Version.Revision,
- Version.SerialVersion,
- EndTag);
- if (Version.BuildString == NULL) {
- Version.BuildString = "";
- }
- strncpy(Name->version, Version.BuildString, sizeof(Name->version));
- Name->version[sizeof(Name->version) - 1] = '\0';
- #if defined(__i386)
- //
- // Determine whether this is a Pentium Pro machine (everything after 1995)
- // or a Pentium (including Intel Quark).
- //
- if (OsTestProcessorFeature(OsX86I686) != FALSE) {
- strcpy(Name->machine, "i686");
- } else {
- strcpy(Name->machine, "i586");
- }
- #elif defined(__amd64)
- strcpy(Name->machine, "x86_64");
- #elif defined(__arm__)
- if (OsTestProcessorFeature(OsArmArmv7) != FALSE) {
- strcpy(Name->machine, "armv7");
- } else {
- strcpy(Name->machine, "armv6");
- }
- #else
- #error Unknown Architecture
- #endif
- Status = STATUS_SUCCESS;
- unameEnd:
- if (!KSUCCESS(Status)) {
- errno = ClConvertKstatusToErrorNumber(Status);
- return -1;
- }
- return 0;
- }
- LIBC_API
- int
- gethostname (
- char *Name,
- size_t NameLength
- )
- /*++
- Routine Description:
- This routine returns the network host name for the current machine.
- Arguments:
- Name - Supplies a pointer where the null-terminated name will be returned
- on success.
- NameLength - Supplies the length of the name buffer in bytes.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to indicate the error.
- --*/
- {
- UINTN NameSize;
- KSTATUS Status;
- NameSize = NameLength;
- Status = OsGetSetSystemInformation(SystemInformationPs,
- PsInformationHostName,
- Name,
- &NameSize,
- FALSE);
- if (!KSUCCESS(Status)) {
- if (Status == STATUS_BUFFER_TOO_SMALL) {
- errno = ENAMETOOLONG;
- } else {
- errno = ClConvertKstatusToErrorNumber(Status);
- }
- return -1;
- }
- return 0;
- }
- LIBC_API
- int
- getdomainname (
- char *Name,
- size_t NameLength
- )
- /*++
- Routine Description:
- This routine returns the network domain name for the current machine.
- Arguments:
- Name - Supplies a pointer where the null-terminated name will be returned
- on success.
- NameLength - Supplies the length of the name buffer in bytes.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to indicate the error.
- --*/
- {
- UINTN NameSize;
- KSTATUS Status;
- NameSize = NameLength;
- Status = OsGetSetSystemInformation(SystemInformationPs,
- PsInformationDomainName,
- Name,
- &NameSize,
- FALSE);
- if (!KSUCCESS(Status)) {
- if (Status == STATUS_BUFFER_TOO_SMALL) {
- errno = ENAMETOOLONG;
- } else {
- errno = ClConvertKstatusToErrorNumber(Status);
- }
- return -1;
- }
- return 0;
- }
- LIBC_API
- int
- sethostname (
- const char *Name,
- size_t Size
- )
- /*++
- Routine Description:
- This routine sets the network host name for the current machine.
- Arguments:
- Name - Supplies a pointer to the new name to set.
- Size - Supplies the size of the name, not including a null terminator.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to indicate the error.
- --*/
- {
- UINTN NameSize;
- KSTATUS Status;
- NameSize = Size;
- Status = OsGetSetSystemInformation(SystemInformationPs,
- PsInformationHostName,
- (PVOID)Name,
- &NameSize,
- TRUE);
- if (!KSUCCESS(Status)) {
- errno = ClConvertKstatusToErrorNumber(Status);
- return -1;
- }
- return 0;
- }
- LIBC_API
- int
- setdomainname (
- const char *Name,
- size_t Size
- )
- /*++
- Routine Description:
- This routine sets the network domain name for the current machine.
- Arguments:
- Name - Supplies a pointer to the new name to set.
- Size - Supplies the size of the name, not including a null terminator.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to indicate the error.
- --*/
- {
- UINTN NameSize;
- KSTATUS Status;
- NameSize = Size;
- Status = OsGetSetSystemInformation(SystemInformationPs,
- PsInformationDomainName,
- (PVOID)Name,
- &NameSize,
- TRUE);
- if (!KSUCCESS(Status)) {
- errno = ClConvertKstatusToErrorNumber(Status);
- return -1;
- }
- return 0;
- }
- PSTR
- ClpGetFqdn (
- VOID
- )
- /*++
- Routine Description:
- This routine returns a null terminated string containing the fully
- qualified domain name of the machine.
- Arguments:
- None.
- Return Value:
- Returns a null terminated string containing nodename.domainname on success.
- The caller is responsible for freeing this string.
- NULL on allocation failure.
- --*/
- {
- PSTR FullName;
- ULONG HostNameLength;
- FullName = malloc((HOST_NAME_MAX * 2) + 3);
- if (FullName == NULL) {
- return NULL;
- }
- FullName[HOST_NAME_MAX] = '\0';
- if (gethostname(FullName, HOST_NAME_MAX) != 0) {
- FullName[0] = '\0';
- }
- HostNameLength = strlen(FullName);
- FullName[HostNameLength + HOST_NAME_MAX] = '\0';
- if (getdomainname(FullName + HostNameLength + 1, HOST_NAME_MAX) == 0) {
- FullName[HostNameLength] = '.';
- } else {
- FullName[HostNameLength] = '\0';
- }
- return FullName;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|