uname.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. uname.c
  9. Abstract:
  10. This module implements support for getting the system name.
  11. Author:
  12. Evan Green 17-Jul-2013
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/utsname.h>
  24. #include <time.h>
  25. //
  26. // ---------------------------------------------------------------- Definitions
  27. //
  28. #define UNAME_SYSTEM_NAME "Minoca"
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // ----------------------------------------------- Internal Function Prototypes
  34. //
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. //
  39. // ------------------------------------------------------------------ Functions
  40. //
  41. LIBC_API
  42. int
  43. uname (
  44. struct utsname *Name
  45. )
  46. /*++
  47. Routine Description:
  48. This routine returns the system name and version.
  49. Arguments:
  50. Name - Supplies a pointer to a name structure to fill out.
  51. Return Value:
  52. Returns a non-negative value on success.
  53. -1 on error, and errno will be set to indicate the error.
  54. --*/
  55. {
  56. CHAR EndTag[32];
  57. UINTN Size;
  58. KSTATUS Status;
  59. SYSTEM_VERSION_INFORMATION Version;
  60. Status = OsGetSystemVersion(&Version, TRUE);
  61. if (!KSUCCESS(Status)) {
  62. goto unameEnd;
  63. }
  64. //
  65. // Start by getting the hostname and domain name.
  66. //
  67. Size = sizeof(Name->nodename) - 1;
  68. Name->nodename[Size] = '\0';
  69. Status = OsGetSetSystemInformation(SystemInformationPs,
  70. PsInformationHostName,
  71. Name->nodename,
  72. &Size,
  73. FALSE);
  74. if ((!KSUCCESS(Status)) && (Status != STATUS_BUFFER_TOO_SMALL)) {
  75. goto unameEnd;
  76. }
  77. Size = sizeof(Name->domainname) - 1;
  78. Name->domainname[Size] = '\0';
  79. Status = OsGetSetSystemInformation(SystemInformationPs,
  80. PsInformationDomainName,
  81. Name->domainname,
  82. &Size,
  83. FALSE);
  84. if ((!KSUCCESS(Status)) && (Status != STATUS_BUFFER_TOO_SMALL)) {
  85. goto unameEnd;
  86. }
  87. strcpy(Name->sysname, UNAME_SYSTEM_NAME);
  88. if ((Version.ReleaseLevel == SystemReleaseFinal) &&
  89. (Version.DebugLevel == SystemBuildRelease)) {
  90. EndTag[0] = '\0';
  91. } else if (Version.ReleaseLevel == SystemReleaseFinal) {
  92. snprintf(EndTag,
  93. sizeof(EndTag),
  94. "-%s",
  95. RtlGetBuildDebugLevelString(Version.DebugLevel));
  96. } else if (Version.DebugLevel == SystemBuildRelease) {
  97. snprintf(EndTag,
  98. sizeof(EndTag),
  99. "-%s",
  100. RtlGetReleaseLevelString(Version.ReleaseLevel));
  101. } else {
  102. snprintf(EndTag,
  103. sizeof(EndTag),
  104. "-%s-%s",
  105. RtlGetReleaseLevelString(Version.ReleaseLevel),
  106. RtlGetBuildDebugLevelString(Version.DebugLevel));
  107. }
  108. snprintf(Name->release,
  109. sizeof(Name->release),
  110. "%d.%d.%d.%lld%s",
  111. Version.MajorVersion,
  112. Version.MinorVersion,
  113. Version.Revision,
  114. Version.SerialVersion,
  115. EndTag);
  116. if (Version.BuildString == NULL) {
  117. Version.BuildString = "";
  118. }
  119. strncpy(Name->version, Version.BuildString, sizeof(Name->version));
  120. Name->version[sizeof(Name->version) - 1] = '\0';
  121. #if defined(__i386)
  122. //
  123. // Determine whether this is a Pentium Pro machine (everything after 1995)
  124. // or a Pentium (including Intel Quark).
  125. //
  126. if (OsTestProcessorFeature(OsX86I686) != FALSE) {
  127. strcpy(Name->machine, "i686");
  128. } else {
  129. strcpy(Name->machine, "i586");
  130. }
  131. #elif defined(__amd64)
  132. strcpy(Name->machine, "x86_64");
  133. #elif defined(__arm__)
  134. if (OsTestProcessorFeature(OsArmArmv7) != FALSE) {
  135. strcpy(Name->machine, "armv7");
  136. } else {
  137. strcpy(Name->machine, "armv6");
  138. }
  139. #else
  140. #error Unknown Architecture
  141. #endif
  142. Status = STATUS_SUCCESS;
  143. unameEnd:
  144. if (!KSUCCESS(Status)) {
  145. errno = ClConvertKstatusToErrorNumber(Status);
  146. return -1;
  147. }
  148. return 0;
  149. }
  150. LIBC_API
  151. int
  152. gethostname (
  153. char *Name,
  154. size_t NameLength
  155. )
  156. /*++
  157. Routine Description:
  158. This routine returns the network host name for the current machine.
  159. Arguments:
  160. Name - Supplies a pointer where the null-terminated name will be returned
  161. on success.
  162. NameLength - Supplies the length of the name buffer in bytes.
  163. Return Value:
  164. 0 on success.
  165. -1 on failure, and errno will be set to indicate the error.
  166. --*/
  167. {
  168. UINTN NameSize;
  169. KSTATUS Status;
  170. NameSize = NameLength;
  171. Status = OsGetSetSystemInformation(SystemInformationPs,
  172. PsInformationHostName,
  173. Name,
  174. &NameSize,
  175. FALSE);
  176. if (!KSUCCESS(Status)) {
  177. if (Status == STATUS_BUFFER_TOO_SMALL) {
  178. errno = ENAMETOOLONG;
  179. } else {
  180. errno = ClConvertKstatusToErrorNumber(Status);
  181. }
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. LIBC_API
  187. int
  188. getdomainname (
  189. char *Name,
  190. size_t NameLength
  191. )
  192. /*++
  193. Routine Description:
  194. This routine returns the network domain name for the current machine.
  195. Arguments:
  196. Name - Supplies a pointer where the null-terminated name will be returned
  197. on success.
  198. NameLength - Supplies the length of the name buffer in bytes.
  199. Return Value:
  200. 0 on success.
  201. -1 on failure, and errno will be set to indicate the error.
  202. --*/
  203. {
  204. UINTN NameSize;
  205. KSTATUS Status;
  206. NameSize = NameLength;
  207. Status = OsGetSetSystemInformation(SystemInformationPs,
  208. PsInformationDomainName,
  209. Name,
  210. &NameSize,
  211. FALSE);
  212. if (!KSUCCESS(Status)) {
  213. if (Status == STATUS_BUFFER_TOO_SMALL) {
  214. errno = ENAMETOOLONG;
  215. } else {
  216. errno = ClConvertKstatusToErrorNumber(Status);
  217. }
  218. return -1;
  219. }
  220. return 0;
  221. }
  222. LIBC_API
  223. int
  224. sethostname (
  225. const char *Name,
  226. size_t Size
  227. )
  228. /*++
  229. Routine Description:
  230. This routine sets the network host name for the current machine.
  231. Arguments:
  232. Name - Supplies a pointer to the new name to set.
  233. Size - Supplies the size of the name, not including a null terminator.
  234. Return Value:
  235. 0 on success.
  236. -1 on failure, and errno will be set to indicate the error.
  237. --*/
  238. {
  239. UINTN NameSize;
  240. KSTATUS Status;
  241. NameSize = Size;
  242. Status = OsGetSetSystemInformation(SystemInformationPs,
  243. PsInformationHostName,
  244. (PVOID)Name,
  245. &NameSize,
  246. TRUE);
  247. if (!KSUCCESS(Status)) {
  248. errno = ClConvertKstatusToErrorNumber(Status);
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. LIBC_API
  254. int
  255. setdomainname (
  256. const char *Name,
  257. size_t Size
  258. )
  259. /*++
  260. Routine Description:
  261. This routine sets the network domain name for the current machine.
  262. Arguments:
  263. Name - Supplies a pointer to the new name to set.
  264. Size - Supplies the size of the name, not including a null terminator.
  265. Return Value:
  266. 0 on success.
  267. -1 on failure, and errno will be set to indicate the error.
  268. --*/
  269. {
  270. UINTN NameSize;
  271. KSTATUS Status;
  272. NameSize = Size;
  273. Status = OsGetSetSystemInformation(SystemInformationPs,
  274. PsInformationDomainName,
  275. (PVOID)Name,
  276. &NameSize,
  277. TRUE);
  278. if (!KSUCCESS(Status)) {
  279. errno = ClConvertKstatusToErrorNumber(Status);
  280. return -1;
  281. }
  282. return 0;
  283. }
  284. PSTR
  285. ClpGetFqdn (
  286. VOID
  287. )
  288. /*++
  289. Routine Description:
  290. This routine returns a null terminated string containing the fully
  291. qualified domain name of the machine.
  292. Arguments:
  293. None.
  294. Return Value:
  295. Returns a null terminated string containing nodename.domainname on success.
  296. The caller is responsible for freeing this string.
  297. NULL on allocation failure.
  298. --*/
  299. {
  300. PSTR FullName;
  301. ULONG HostNameLength;
  302. FullName = malloc((HOST_NAME_MAX * 2) + 3);
  303. if (FullName == NULL) {
  304. return NULL;
  305. }
  306. FullName[HOST_NAME_MAX] = '\0';
  307. if (gethostname(FullName, HOST_NAME_MAX) != 0) {
  308. FullName[0] = '\0';
  309. }
  310. HostNameLength = strlen(FullName);
  311. FullName[HostNameLength + HOST_NAME_MAX] = '\0';
  312. if (getdomainname(FullName + HostNameLength + 1, HOST_NAME_MAX) == 0) {
  313. FullName[HostNameLength] = '.';
  314. } else {
  315. FullName[HostNameLength] = '\0';
  316. }
  317. return FullName;
  318. }
  319. //
  320. // --------------------------------------------------------- Internal Functions
  321. //